commit 80b190c66dab551b75db790df86efaffe0f86671
parent a50aacd30adb87698eb775ef5c7615c65e57a11d
Author: Louis Burda <quent.burda@gmail.com>
Date: Thu, 29 Apr 2021 18:37:03 +0200
fixed Makefile and extended ascii parsing structure
Diffstat:
3 files changed, 69 insertions(+), 7 deletions(-)
diff --git a/service/src/Makefile b/service/src/Makefile
@@ -9,5 +9,5 @@ clean:
%.o: %.c %.h
$(CC) -c -o $@ $< -I .
-printdoc: printdoc.c stlfile.o
+printdoc: printdoc.c stlfile.o util.o
$(CC) -o $@ $^ $(CFLAGS) $(LDLIBS)
diff --git 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);
diff --git a/service/src/stlfile.h b/service/src/stlfile.h
@@ -7,14 +7,23 @@
#include <endian.h>
enum {
+ KW_UNKNOWN,
KW_SOLID_BEGIN,
KW_SOLID_END,
- KW_FACET,
- KW_OUTER,
+ KW_FACET_BEGIN,
+ KW_FACET_END,
+ KW_LOOP_BEGIN,
+ KW_LOOP_END,
KW_VERTEX
};
enum {
+ STATE_SOLID,
+ STATE_FACET,
+ STATE_LOOP
+};
+
+enum {
TYPE_ASCII,
TYPE_BIN
};