diff options
Diffstat (limited to 'service/src/stlfile.c')
| -rw-r--r-- | service/src/stlfile.c | 109 |
1 files changed, 105 insertions, 4 deletions
diff --git a/service/src/stlfile.c b/service/src/stlfile.c index 0fbe6b0..3ae1441 100644 --- a/service/src/stlfile.c +++ b/service/src/stlfile.c @@ -1,16 +1,117 @@ #include "stlfile.h" +#include "util.h" +void +stack_init(struct stack *stack) +{ + stack->cap = 10; + stack->data = checkp(malloc(sizeof(int) * stack->cap)); + stack->count = 0; +} -struct parseinfo* -parse_file(char *buf, size_t len) +void +stack_push(struct stack *stack, int v) { + if (stack->count == stack->cap) { + stack->cap *= 2; + stack->data = checkp(realloc(stack->data, sizeof(int) * stack->cap)); + } - return NULL; + stack->data[stack->count] = v; + stack->count++; } +int +stack_pop(struct stack *stack) +{ + if (stack->count == 0) + die("popping empty stack!\n"); + + stack->count--; + return stack->data[stack->count]; +} void -free_parseinfo(struct parseinfo *info) +stack_free(struct stack *stack) +{ + free(stack->data); +} + +int +consume_keyword(char **bp, char *end) +{ + /* TODO */ + return KW_SOLID_BEGIN; +} + +int +parse_file_ascii(struct parseinfo *info, char *buf, size_t len) { + char *bp, *pbp, *end = buf + len; + struct stack states; + int type, kw; + + stack_init(&states); + + info = checkp(malloc(sizeof(struct parseinfo))); + info->type = TYPE_ASCII; + + while ((kw = consume_keyword(&bp, end))) { + /* TODO */ + } + + stack_free(&states); + return OK; + +fail: + stack_free(&states); free(info); + return FAIL; +} + +int +parse_file_bin(struct parseinfo *info, char *buf, size_t len) +{ + char *bp, *end = buf + len; + unsigned int i; + + if (len < 84) { + fprintf(stderr, "Truncated data! (header missing)\n"); + return FAIL; + } + + /* treat header as model name */ + info->namehash = strdup(mhash(buf, 80)); + + bp = buf + 80; + info->loopcount = le32toh(*(uint32_t*)bp); + + for (i = 0; i < info->loopcount; i++) { + if (bp + 50 > end) { + fprintf(stderr, "Truncated data! (loops missing)\n"); + return FAIL; + } + bp += 50; + } + + return OK; +} + +int +parse_file(struct parseinfo *info, char *buf, size_t len) +{ + char *bp; + + if (info->valid) { + NULLFREE(info->infopath); + NULLFREE(info->namehash); + NULLFREE(info->stlpath); + } + + /* check bin vs ascii */ + for (bp = buf; strchr(" \n\r\t", *bp); bp++); + + return !strncmp("solid ", bp, 6) + ? parse_file_ascii(info, buf, len) + : parse_file_bin(info, buf, len); } |
