From 14bc8a3883663656cd4254567a978002c062992d Mon Sep 17 00:00:00 2001 From: Louis Burda Date: Thu, 24 Jun 2021 21:23:02 +0200 Subject: refactored code for readability and keeping within 80ch limit, updated service source --- src/stlfile.c | 131 +++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 85 insertions(+), 46 deletions(-) (limited to 'src/stlfile.c') diff --git a/src/stlfile.c b/src/stlfile.c index 10d9006..5180ffc 100644 --- a/src/stlfile.c +++ b/src/stlfile.c @@ -27,7 +27,8 @@ 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)); + stack->data = realloc(stack->data, sizeof(int) * stack->cap); + checkp(stack->data); } stack->data[stack->count] = v; @@ -98,7 +99,6 @@ consume_keyword(char **start) for (i = 0; i < ARRSIZE(kwmap); i++) { len = strlen(kwmap[i].str); if (!strncmp(kwmap[i].str, bp, len) && (!bp[len] || isws(bp[len]))) { - // printf("GOT: %s\n", kwmap[i].str); *start = bp + len + (bp[len] ? 1 : 0); return kwmap[i].code; } @@ -107,9 +107,6 @@ consume_keyword(char **start) return KW_UNKNOWN; } -#define PARSE_FAIL(...) \ - do { fprintf(stderr, "FORMAT ERR: " __VA_ARGS__); goto fail; } while (0) - int parse_file_ascii(struct parseinfo *info, char *buf, size_t len) { @@ -135,81 +132,113 @@ parse_file_ascii(struct parseinfo *info, char *buf, size_t len) switch (kw) { case KW_SOLID_BEGIN: stack_push(&states, STATE_SOLID); - if (stack_ind(&states, KW_SOLID_BEGIN) != -1) - PARSE_FAIL("Multiple nested solids!\n"); + if (stack_ind(&states, KW_SOLID_BEGIN) != -1) { + FMT_ERR("Multiple nested solids!\n"); + goto format_error; + } tmp = bp; - if (!consume_keyword(&bp) && (arg = consume_arg(&bp, &end))) { + if (!consume_keyword(&bp) + && (arg = consume_arg(&bp, &end))) { info->solidname = strndup(arg, end - arg); } else { bp = tmp; } break; case KW_SOLID_END: - if ((kw = stack_pop(&states)) != STATE_SOLID) - PARSE_FAIL("Improper nesting, parent: %s\n", kwmap[kw].str); + if ((kw = stack_pop(&states)) != STATE_SOLID) { + FMT_ERR("Improper nesting, parent: %s\n", + kwmap[kw].str); + goto format_error; + } tmp = bp; if (info->solidname && !consume_keyword(&bp) && (arg = consume_arg(&bp, &end))) { - if (strncmp(info->solidname, arg, end - arg)) - PARSE_FAIL("Solid end/begin names do not match!\n"); + if (strncmp(info->solidname, arg, end - arg)) { + FMT_ERR("Solid names do not match!\n"); + goto format_error; + } } else { bp = tmp; } break; case KW_LOOP_BEGIN: stack_push(&states, STATE_LOOP); - if (stack_ind(&states, KW_LOOP_BEGIN) != -1) - PARSE_FAIL("Multiple nested loops!\n"); + if (stack_ind(&states, KW_LOOP_BEGIN) != -1) { + FMT_ERR("Multiple nested loops!\n"); + goto format_error; + } break; case KW_LOOP_END: - if ((kw = stack_pop(&states)) != STATE_LOOP) - PARSE_FAIL("Improper nesting, parent: %s\n", kwmap[kw].str); + if ((kw = stack_pop(&states)) != STATE_LOOP) { + FMT_ERR("Improper nesting, parent: %s\n", + kwmap[kw].str); + goto format_error; + } info->loopcount++; break; case KW_FACET_BEGIN: stack_push(&states, STATE_FACET); - if (stack_ind(&states, KW_LOOP_BEGIN) != -1) - PARSE_FAIL("Multiple nested facets!\n"); + if (stack_ind(&states, KW_LOOP_BEGIN) != -1) { + FMT_ERR("Multiple nested facets!\n"); + goto format_error; + } for (i = 0; i < 3; i++) { - if (!(arg = consume_arg(&bp, &end))) - PARSE_FAIL("Facet with less than 3 args!\n"); + if (!(arg = consume_arg(&bp, &end))) { + FMT_ERR("Facet with < 3 args!\n"); + goto format_error; + } farg = strtof(arg, &tmp); - if (!isws(*tmp)) - PARSE_FAIL("Facet with invalid arg '%s'!\n", arg); + if (!isws(*tmp)) { + FMT_ERR("Facet arg '%s'\n", arg); + goto format_error; + } } break; case KW_FACET_END: - if ((kw = stack_pop(&states)) != STATE_FACET) - PARSE_FAIL("Improper nesting, parent: %s\n", kwmap[kw].str); + if ((kw = stack_pop(&states)) != STATE_FACET) { + FMT_ERR("Improper nesting, parent: %s\n", + kwmap[kw].str); + goto format_error; + } break; case KW_VERTEX: for (i = 0; i < 3; i++) { - if (!(arg = consume_arg(&bp, &end))) - PARSE_FAIL("Vertex with less than 3 args!\n"); + if (!(arg = consume_arg(&bp, &end))) { + FMT_ERR("Vertex with < 3 args\n"); + goto format_error; + } farg = strtof(arg, &tmp); - if (!isws(*tmp)) - PARSE_FAIL("Vertex with invalid arg '%s'!\n", arg); + if (!isws(*tmp)) { + FMT_ERR("Vertex arg '%s'\n", arg); + goto format_error; + } info->bbmin[i] = MIN(info->bbmin[i], farg); info->bbmax[i] = MAX(info->bbmax[i], farg); } break; case KW_UNKNOWN: prev = skipws(prev); - PARSE_FAIL("Expected keyword, got:\n%.*s...\n", 30, prev); + FMT_ERR("Expected keyword, got:\n%.*s...\n", 30, prev); + goto format_error; } prev = bp; } - if (states.count) - PARSE_FAIL("Expected keyword, got:\n%.*s...\n", 30, bp); + if (states.count) { + FMT_ERR("Expected keyword, got:\n%.*s...\n", 30, bp); + goto format_error; + } bp = skipws(bp); - if (*bp) PARSE_FAIL("Extraneous data at end of file\n"); + if (*bp) { + FMT_ERR("Extraneous data at end of file\n"); + goto format_error; + } stack_free(&states); return OK; -fail: +format_error: stack_free(&states); return FAIL; } @@ -223,8 +252,10 @@ parse_file_bin(struct parseinfo *info, char *buf, size_t len) info->type = TYPE_BIN; - if (len < 84) - PARSE_FAIL("Truncated data! (header missing)\n"); + if (len < 84) { + FMT_ERR("Truncated data! (header missing)\n"); + goto fail; + } memcpy(info->header, buf, 80); @@ -248,12 +279,16 @@ parse_file_bin(struct parseinfo *info, char *buf, size_t len) } for (i = 0; i < info->loopcount; i++) { - if (bp + 50 > end) - PARSE_FAIL("Truncated data! (loops missing)\n"); + if (bp + 50 > end) { + FMT_ERR("Truncated data! (loops missing)\n"); + goto fail; + } for (k = 0; k < 12; k++, bp += 4) { v = fle32toh(*(float*)bp); - if (v == INFINITY || v == NAN) - PARSE_FAIL("Encountered invalid float\n"); + if (v == INFINITY || v == NAN) { + FMT_ERR("Encountered invalid float\n"); + goto fail; + } if (k >= 3) { info->bbmin[k % 3] = MIN(info->bbmin[k % 3], v); info->bbmax[k % 3] = MAX(info->bbmax[k % 3], v); @@ -262,11 +297,15 @@ parse_file_bin(struct parseinfo *info, char *buf, size_t len) bp += 2; } - if (bp != end) PARSE_FAIL("Extraneous data at end of file\n"); + if (bp != end) { + FMT_ERR("Extraneous data at end of file\n"); + goto fail; + } return OK; fail: + NFREE(info->solidname); return FAIL; } @@ -277,10 +316,10 @@ parse_file(struct parseinfo *info, char *buf, size_t len) const char *resp; char *bp; - if (info->valid) free_info(info); + free_info(info); if (len < 10) { - fprintf(stderr, "ERR: File too small!\n"); + ERR("File too small!\n"); return FAIL; } @@ -298,7 +337,7 @@ parse_file(struct parseinfo *info, char *buf, size_t len) if (!info->modelname) { resp = ask("Please enter your model name: "); if (strlen(resp) < 4) { - fprintf(stderr, "ERR: Model name is too short!\n"); + ERR("Model name is too short!\n"); return FAIL; } info->modelname = checkp(strdup(resp)); @@ -404,9 +443,9 @@ print_info(struct parseinfo *info) void free_info(struct parseinfo *info) { - NULLFREE(info->hash); - NULLFREE(info->modelname); - NULLFREE(info->solidname); + NFREE(info->hash); + NFREE(info->modelname); + NFREE(info->solidname); info->valid = 0; } -- cgit v1.2.3-71-gd317