commit 3b660c467f938df6898229e378074cfa5662ce9f
parent a0d6bf48a185026589288fd9aa94506b321301d8
Author: Louis Burda <quent.burda@gmail.com>
Date: Sun, 9 May 2021 12:37:16 +0200
save hash statically, added file info output, improved error strings and minor fixes
Diffstat:
7 files changed, 90 insertions(+), 40 deletions(-)
diff --git a/service/src/printdoc.c b/service/src/printdoc.c
@@ -73,7 +73,7 @@ search_cmd(char *arg)
DIR *d = NULL;
if (lastrun.valid)
- hash = lastrun.namehash;
+ hash = lastrun.hash;
else
hash = mhash(ask("What is the model name?\n"), -1);
@@ -126,7 +126,7 @@ submit_cmd(char *arg)
bufp = ask("How large is your file?\n");
len = strtoul(bufp, &end, 10);
- if (len <= 0 || len >= 7000 || *end) {
+ if (len <= 0 || len >= 10000 || *end) {
fprintf(stderr, "Invalid file length!\n");
return;
}
@@ -138,7 +138,12 @@ submit_cmd(char *arg)
lastrun.valid = parse_file(&lastrun, contents, len);
- if (lastrun.valid && lastrun.infopath) dump(lastrun.infopath);
+ if (lastrun.valid) {
+ print_info(&lastrun);
+ save_info(&lastrun, resultdir);
+ printf("Your file was saved with ID %s!\n", lastrun.hash);
+ printf("You may retrieve your results via the 'search' function\n");
+ }
free(contents);
}
@@ -169,8 +174,6 @@ main()
char linebuf[256], *cp, *arg;
int exit, i;
- setvbuf(stdin, NULL, _IONBF, 0);
-
if (!(resultdir = getenv("RESULTDIR")))
resultdir = "/data";
@@ -178,7 +181,7 @@ main()
exit = 0;
while (!exit) {
- memset(linebuf, ' ', sizeof(linebuf));
+ memset(linebuf, '\0', sizeof(linebuf));
printf("$ ");
exit = !fgets(linebuf, sizeof(linebuf), stdin);
diff --git 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;
}
diff --git a/service/src/stlfile.h b/service/src/stlfile.h
@@ -6,6 +6,8 @@
#include <stdint.h>
#include <endian.h>
+#include "util.h"
+
enum {
KW_INVALID = -1,
KW_UNKNOWN,
@@ -35,14 +37,16 @@ struct stack {
};
struct parseinfo {
- char extra[80];
+ char extra[80], hash[MHASHLEN+1];
unsigned int loopcount;
float bbmin[3], bbmax[3];
int type, valid;
- char *namehash, *infopath, *stlpath;
+ char *infopath, *stlpath;
};
int parse_file(struct parseinfo *info, char *buf, size_t len);
+int save_info(struct parseinfo *info, const char *resultdir);
+void print_info(struct parseinfo *info);
void free_info(struct parseinfo *info);
#endif /* STLFILE_H */
diff --git a/service/src/test.sh b/service/src/test.sh
@@ -26,7 +26,6 @@ checkleaks() {
announce "Trying ASCII STL"
(
- echo "help"
echo "submit"
cat tests/sample-ascii.stl | wc -c
cat tests/sample-ascii.stl
@@ -34,7 +33,6 @@ announce "Trying ASCII STL"
announce "Trying BIN STL"
(
- echo "help"
echo "submit"
cat tests/sample-binary.stl | wc -c
cat tests/sample-binary.stl
diff --git a/service/src/tests/sample-ascii.stl b/service/src/tests/sample-ascii.stl
@@ -1,4 +1,11 @@
solid test
+ facet normal 1.0 0 0
+ outer loop
+ vertex 0 1 0
+ vertex 0 1 1
+ vertex 0 0 1
+ endloop
+ endfacet
facet normal 0 0 1.0
outer loop
vertex 1 0 0
diff --git a/service/src/util.c b/service/src/util.c
@@ -46,27 +46,27 @@ const char*
mhash(const char *filename, int len)
{
static const char *hexalph = "0123456789ABCDEF";
- static char buf[33];
+ static char buf[MHASHLEN + 1];
int i, k;
if (len == -1) len = strlen(filename);
- for (i = 0; i < MIN(32, len); i++) {
+ for (i = 0; i < MIN(MHASHLEN, len); i++) {
unsigned char v = 0;
- for (k = i; k < len; k += 32)
+ for (k = i; k < len; k += MHASHLEN)
v ^= filename[k];
buf[i*2+0] = hexalph[(v >> 0) & 0x0f];
buf[i*2+1] = hexalph[(v >> 4) & 0x0f];
}
if (i == 0) {
- memset(buf, '0', 32);
- } else if (i < 32) {
- for (k = 0; k < 32; k++)
+ memset(buf, '0', MHASHLEN);
+ } else if (i < MHASHLEN) {
+ for (k = 0; k < MHASHLEN; k++)
buf[k] = buf[k % i];
}
- buf[32] = '\0';
+ buf[MHASHLEN] = '\0';
return buf;
}
diff --git a/service/src/util.h b/service/src/util.h
@@ -10,6 +10,8 @@
#define MIN(x,y) ((x) > (y) ? (y) : (x))
#define NULLFREE(p) do { free(p); p = NULL; } while (0)
+#define MHASHLEN 32
+
enum { FAIL = 0, OK = 1 };
void* checkp(void *p);