diff options
Diffstat (limited to 'service/src/stlfile.c')
| -rw-r--r-- | service/src/stlfile.c | 82 |
1 files changed, 59 insertions, 23 deletions
diff --git a/service/src/stlfile.c b/service/src/stlfile.c index 2bdf68f..f15cc95 100644 --- a/service/src/stlfile.c +++ b/service/src/stlfile.c @@ -1,5 +1,4 @@ #include "stlfile.h" -#include "util.h" static const char wsset[] = " \r\n\t"; static const struct { @@ -61,12 +60,11 @@ stack_ind(struct stack *stack, int search) return -1; } -long -skip_set(char *start, const char *set) +char* +skip_set(char *p, const char *set) { - char *p; - for (p = start; *p && strchr(set, *p); p++); - return p - start; + for (; *p && strchr(set, *p); p++); + return p; } char* @@ -74,8 +72,9 @@ consume_arg(char **start) { char *p, *tmp; - p = *start + skip_set(*start, wsset); + p = skip_set(*start, wsset); for (; !strchr(wsset, *p); p++); + if (!*p) return NULL; *p = '\0'; tmp = *start; *start = p + 1; @@ -88,7 +87,7 @@ consume_keyword(char **start) char *bp; int i, len; - bp = *start + skip_set(*start, wsset); + bp = skip_set(*start, wsset); for (i = 0; i < ARRSIZE(kwmap); i++) { len = strlen(kwmap[i].str); @@ -124,14 +123,13 @@ parse_file_ascii(struct parseinfo *info, char *buf, size_t len) case KW_SOLID_BEGIN: stack_push(&states, STATE_SOLID); if (stack_ind(&states, KW_SOLID_BEGIN) != -1) - PARSE_FAIL("multiple nested solids!\n"); + PARSE_FAIL("Multiple nested solids!\n"); tmp = bp; if (!consume_keyword(&bp) && (arg = consume_arg(&bp))) { - memcpy(info->extra, arg, MIN(strlen(arg), 80)); + strncpy(info->extra, arg, sizeof(info->extra)); } else { bp = tmp; } - info->namehash = checkp(strdup(mhash(info->extra, 80))); break; case KW_SOLID_END: if ((kw = stack_pop(&states)) != STATE_SOLID) @@ -139,8 +137,8 @@ parse_file_ascii(struct parseinfo *info, char *buf, size_t len) tmp = bp; if (*info->extra && !consume_keyword(&bp) && (arg = consume_arg(&bp))) { - if (strncmp(info->extra, arg, 80)) - PARSE_FAIL("solid end/begin names do not match!\n"); + if (strncmp(info->extra, arg, sizeof(info->extra))) + PARSE_FAIL("Solid end/begin names do not match!\n"); } else { bp = tmp; } @@ -148,7 +146,7 @@ parse_file_ascii(struct parseinfo *info, char *buf, size_t len) case KW_LOOP_BEGIN: stack_push(&states, STATE_LOOP); if (stack_ind(&states, KW_LOOP_BEGIN) != -1) - PARSE_FAIL("multiple nested loops!\n"); + PARSE_FAIL("Multiple nested loops!\n"); break; case KW_LOOP_END: if ((kw = stack_pop(&states)) != STATE_LOOP) @@ -158,7 +156,7 @@ parse_file_ascii(struct parseinfo *info, char *buf, size_t len) case KW_FACET_BEGIN: stack_push(&states, STATE_FACET); if (stack_ind(&states, KW_LOOP_BEGIN) != -1) - PARSE_FAIL("multiple nested facets!\n"); + PARSE_FAIL("Multiple nested facets!\n"); for (i = 0; i < 3; i++) { if (!(arg = consume_arg(&bp))) PARSE_FAIL("Facet with less than 3 args!\n"); @@ -182,7 +180,7 @@ parse_file_ascii(struct parseinfo *info, char *buf, size_t len) } break; case KW_UNKNOWN: - prev += skip_set(prev, wsset); + prev = skip_set(prev, wsset); PARSE_FAIL("Expected keyword, got:\n%.*s...\n", 30, prev); } prev = bp; @@ -210,8 +208,7 @@ parse_file_bin(struct parseinfo *info, char *buf, size_t len) if (len < 84) PARSE_FAIL("Truncated data! (header missing)\n"); - /* treat header as model name */ - info->namehash = strdup(mhash(buf, 80)); + memcpy(info->extra, buf, 80); bp = buf + 80; info->loopcount = le32toh(*(uint32_t*)bp); @@ -235,8 +232,7 @@ parse_file(struct parseinfo *info, char *buf, size_t len) int status; char *bp; - if (info->valid) - free_info(info); + if (info->valid) free_info(info); /* check bin vs ascii */ for (bp = buf; strchr(wsset, *bp); bp++); @@ -246,16 +242,56 @@ parse_file(struct parseinfo *info, char *buf, size_t len) : parse_file_bin(info, buf, len); if (status == FAIL) return FAIL; - printf("LOOPS: %i\n", info->loopcount); - /* TODO: create new dir and write parseinfo to files */ + /* create model hash */ + strncpy(info->hash, mhash(info->extra, sizeof(info->extra)), MHASHLEN); return OK; } +int +save_info(struct parseinfo *info, const char *resultdir) +{ + /* <HASH>: + * - info : binary file info + * - model : original stl file + */ + + return FAIL; +} + +void +print_info(struct parseinfo *info) +{ + int i, k; + +#define FILTERCHAR(c) ((c) >= 32 ? (c) : '?') + + if (info->type == TYPE_ASCII) { + printf("Modelname: %s\n", info->extra); + } else { + printf("Model Header:\n"); + for (i = 0; i < 80; i += k) { + for (k = i; k < MIN(80, i + 20); k++) + printf(" %02x", info->extra[i+k]); + printf(" | "); + for (k = i; k < MIN(80, i + 20); k++) + printf("%c", FILTERCHAR(info->extra[i+k])); + printf("\n"); + } + } + + printf("Hash: %s\n", info->hash); + printf("Triangles: %i\n", info->loopcount); + printf("Bounding Box: %.2f x %.2f x %.2f\n", + info->bbmax[0] - info->bbmin[0], + info->bbmax[1] - info->bbmin[1], + info->bbmax[2] - info->bbmin[2]); +} + void free_info(struct parseinfo *info) { - NULLFREE(info->namehash); NULLFREE(info->stlpath); NULLFREE(info->infopath); + info->valid = 0; } |
