aboutsummaryrefslogtreecommitdiffstats
path: root/service/src/stlfile.c
diff options
context:
space:
mode:
Diffstat (limited to 'service/src/stlfile.c')
-rw-r--r--service/src/stlfile.c61
1 files changed, 57 insertions, 4 deletions
diff --git a/service/src/stlfile.c b/service/src/stlfile.c
index 3ae1441..d57964b 100644
--- a/service/src/stlfile.c
+++ b/service/src/stlfile.c
@@ -38,10 +38,30 @@ stack_free(struct stack *stack)
}
int
-consume_keyword(char **bp, char *end)
+consume_keyword(char **start, char *end)
{
- /* TODO */
- return KW_SOLID_BEGIN;
+ static const struct {
+ int code;
+ const char *str;
+ } mapping[] = {
+ { KW_SOLID_BEGIN, "solid" },
+ { KW_SOLID_END, "endsolid" },
+ { KW_LOOP_BEGIN, "outer loop" },
+ { KW_LOOP_END, "endloop" },
+ { KW_FACET_BEGIN, "facet normal" },
+ { KW_FACET_END, "endfacet" },
+ { KW_VERTEX, "vertex" },
+ };
+ char *bp;
+ int i;
+
+ for (bp = *start; strchr(" \r\n\t", *bp); bp++);
+
+ for (i = 0; i < ARRSIZE(mapping); i++)
+ if (!strcmp(mapping[i].str, bp))
+ return mapping[i].code;
+
+ return KW_UNKNOWN;
}
int
@@ -53,11 +73,44 @@ parse_file_ascii(struct parseinfo *info, char *buf, size_t len)
stack_init(&states);
+#define PARSER_ASSERT_NESTING(x, y) \
+ do { if ((x) != (y)) { \
+ fprintf(stderr, "STL Format error: invalid nesting!"); \
+ return FAIL; \
+ } } while (0)
+
info = checkp(malloc(sizeof(struct parseinfo)));
info->type = TYPE_ASCII;
while ((kw = consume_keyword(&bp, end))) {
- /* TODO */
+ switch (kw) {
+ case KW_SOLID_BEGIN:
+ stack_push(&states, STATE_SOLID);
+ break;
+ case KW_SOLID_END:
+ if (stack_pop(&states) == STATE_SOLID)
+ break;
+ goto unknown;
+ case KW_LOOP_BEGIN:
+ stack_push(&states, STATE_LOOP);
+ break;
+ case KW_LOOP_END:
+ if (stack_pop(&states) == STATE_LOOP)
+ break;
+ goto unknown;
+ case KW_FACET_BEGIN:
+ stack_push(&states, STATE_FACET);
+ break;
+ case KW_FACET_END:
+ if (stack_pop(&states) == STATE_FACET)
+ break;
+ goto unknown;
+
+unknown:
+ case KW_UNKNOWN:
+ fprintf(stderr, "Encountered unexpected keyword: '%.*s..'", 30, bp);
+ return FAIL;
+ }
}
stack_free(&states);