aboutsummaryrefslogtreecommitdiff
path: root/src/libsok/sok-level-parser.c
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2019-01-15 09:16:56 -0500
committerPaul Duncan <pabs@pablotron.org>2019-01-15 09:16:56 -0500
commit3e658ed87b5795b2be8f50d683dc19241aba0111 (patch)
tree0bc3b6e61e476e7ecdcb3c20429b159ea5465ae2 /src/libsok/sok-level-parser.c
parent914ca426630ccadcbb6f1ff02a599bdaf10b6cb2 (diff)
downloadsok-3e658ed87b5795b2be8f50d683dc19241aba0111.tar.bz2
sok-3e658ed87b5795b2be8f50d683dc19241aba0111.zip
s/libsok/core/g
Diffstat (limited to 'src/libsok/sok-level-parser.c')
-rw-r--r--src/libsok/sok-level-parser.c193
1 files changed, 0 insertions, 193 deletions
diff --git a/src/libsok/sok-level-parser.c b/src/libsok/sok-level-parser.c
deleted file mode 100644
index 276ebcb..0000000
--- a/src/libsok/sok-level-parser.c
+++ /dev/null
@@ -1,193 +0,0 @@
-#include <stdbool.h>
-#include <stddef.h> // size_t
-#include "sok.h"
-
-#define UNUSED(a) ((void) (a))
-
-#define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
-#define CASE_DIGIT \
- case '0': \
- case '1': \
- case '2': \
- case '3': \
- case '4': \
- case '5': \
- case '6': \
- case '7': \
- case '8': \
- case '9':
-
-void
-sok_level_parser_init(
- sok_level_parser_t * const parser,
- const sok_level_parser_cbs_t * const cbs,
- void * const user_data
-) {
- parser->cbs = cbs;
- parser->user_data = user_data;
-}
-
-bool
-sok_level_parser_parse(
- sok_level_parser_t * const parser,
- const char * const buf,
- const size_t buf_len
-) {
- sok_pos_t size = { .x = 0, .y = 0 };
-
- for (size_t i = 0, ofs = 0, w = 0; i < buf_len; i++) {
- if (buf[i] == '|') {
- if (w > size.x) {
- size.x = w;
- }
-
- // reset column position, increment row count
- w = 0;
- size.y++;
- } else if (IS_DIGIT(buf[i])) {
- ofs = 10 * ofs + (buf[i] - '0');
- } else if (ofs > 0) {
- w += ofs;
- ofs = 0;
- } else {
- w++;
- }
- }
-
- // increment row count
- size.y++;
-
- // emit level size
- if (
- parser->cbs->on_size &&
- !parser->cbs->on_size(parser, size)
- ) {
- // return failure
- return false;
- }
-
- sok_pos_t pos = { 0, 0 };
- for (size_t i = 0, ofs = 0; i < buf_len; i++) {
- switch (buf[i]) {
- case '|':
- // new line
- pos.x = 0;
- pos.y++;
-
- break;
- CASE_DIGIT
- ofs = 10 * ofs + (buf[i] - '0');
- break;
- case '-':
- case '_':
- case ' ':
- // advance
- pos.x += (ofs > 0) ? ofs : 1;
- ofs = 0;
-
- break;
- case '+':
- // emit goal
- if (parser->cbs->on_goal && !parser->cbs->on_goal(parser, pos)) {
- // return failure
- return false;
- }
-
- // emit home
- if (parser->cbs->on_home && !parser->cbs->on_home(parser, pos)) {
- // return failure
- return false;
- }
-
- // advance
- pos.x++;
-
- break;
- case '@':
- // emit home
- if (parser->cbs->on_home && !parser->cbs->on_home(parser, pos)) {
- // return failure
- return false;
- }
-
- // advance
- pos.x++;
-
- break;
- case '#':
- for (size_t j = (ofs > 0) ? ofs : 1; j; j--) {
- // emit wall
- if (parser->cbs->on_wall && !parser->cbs->on_wall(parser, pos)) {
- // return failure
- return false;
- }
-
- // advance
- pos.x++;
- }
- ofs = 0;
-
- break;
- case '*':
- for (size_t j = (ofs > 0) ? ofs : 1; j--;) {
- // emit goal
- if (parser->cbs->on_goal && !parser->cbs->on_goal(parser, pos)) {
- // return failure
- return false;
- }
-
- // emit box
- if (parser->cbs->on_box && !parser->cbs->on_box(parser, pos)) {
- // return failure
- return false;
- }
-
- // advance
- pos.x++;
- }
- ofs = 0;
-
- break;
- case '.':
- for (size_t j = (ofs > 0) ? ofs : 1; j--;) {
- // emit goal
- if (parser->cbs->on_goal && !parser->cbs->on_goal(parser, pos)) {
- // return failure
- return false;
- }
-
- // advance
- pos.x++;
- }
- ofs = 0;
-
- break;
- case '$':
- for (size_t j = (ofs > 0) ? ofs : 1; j--;) {
- // emit box
- if (parser->cbs->on_box && !parser->cbs->on_box(parser, pos)) {
- // return failure
- return false;
- }
-
- // advance
- pos.x++;
- }
- ofs = 0;
-
- break;
- default:
- // emit junk
- if (parser->cbs->on_junk && !parser->cbs->on_junk(parser, i, buf[i])) {
- // return failure
- return false;
- }
-
- // return failure
- return false;
- }
- }
-
- // return success
- return true;
-}