commit eb830b2cc4d059c59fc6bf4cb9aad7efc00ed02c
parent 2e7ff039d493bca515f48fa09805b9c736380249
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Thu, 7 Jan 2016 10:44:04 +0100
Handle solid name with spaces
Test the loading of submitted stl files.
Diffstat:
2 files changed, 78 insertions(+), 27 deletions(-)
diff --git a/src/sstl.c b/src/sstl.c
@@ -202,6 +202,47 @@ parse_float3
}
static INLINE res_T
+parse_name_string
+ (struct sstl* sstl,
+ char* str,
+ char** out_name,
+ const char* filename,
+ const size_t iline)
+{
+ char* name = NULL;
+ char* tk;
+ res_T res = RES_OK;
+ ASSERT(sstl && out_name);
+
+ if(!str) goto exit;
+
+ /* Handle name with spaces */
+ for(tk = strtok(str, " \t"); tk; tk = strtok(NULL, " \t")) {
+ char* remain = NULL;
+ if(name) name[strlen(name)] = ' '; /* Replace '\0' by ' ' */
+ remain = sa_add(name, strlen(tk) + 1/*NULL char*/);
+ if(!remain) {
+ print_log(sstl, LOG_ERROR,
+ "%s:%lu: not enough memory: couldn't allocate the name of the solid.\n",
+ filename, (unsigned long)iline);
+ res = RES_MEM_ERR;
+ goto error;
+ }
+ strcpy(name, tk);
+ }
+
+exit:
+ *out_name = name;
+ return res;
+error:
+ if(name) {
+ sa_release(name);
+ name = NULL;
+ }
+ goto exit;
+}
+
+static INLINE res_T
parse_solid_name
(struct sstl* sstl,
struct solid* solid,
@@ -209,7 +250,6 @@ parse_solid_name
const char* filename,
const size_t iline)
{
- char* tk;
res_T res = RES_OK;
ASSERT(sstl && solid && !solid->name);
@@ -221,23 +261,9 @@ parse_solid_name
goto error;
}
- tk = strtok(NULL, " \t");
- if(tk) {
- solid->name = sa_add(solid->name, strlen(tk)+1/*NULL char*/);
- if(!solid->name) {
- print_log(sstl, LOG_ERROR,
- "%s:%lu: not enough memory: couldn't allocate the name of the solid.\n",
- filename, (unsigned long)iline);
- res = RES_MEM_ERR;
- goto error;
- }
- strcpy(solid->name, tk);
-
- if((tk = strtok(NULL, "\0")) && strspn(tk, " \t\r\n") != strlen(tk)) {
- print_log(sstl, LOG_WARNING,
- "%s:%lu: malformed \"solid [NAME]\" directive.\n");
- }
- }
+ res = parse_name_string
+ (sstl, strtok(NULL, "\0"), &solid->name, filename, iline);
+ if(res != RES_OK) goto error;
exit:
return res;
@@ -430,19 +456,20 @@ load_stream(struct sstl* sstl, FILE* stream, const char* stream_name)
if(res != RES_OK) goto error;
}
- if(solid.name) { /* Parse "name" of the endsolid directive */
- tk = strtok(NULL, " \t");
- if(tk && strcmp(tk, solid.name)) {
+ /* Check the solid/endsolid name consistency */
+ if(sstl->verbose && solid.name) {
+ char* name = NULL;
+ res = parse_name_string
+ (sstl, strtok(NULL, "\0"), &name, streamer.name, streamer.iline);
+ if(res != RES_OK) goto error;
+
+ /* Compare the "endsolid" name with the one of the "solid" directive */
+ if(name && strcmp(name, solid.name)) {
print_log(sstl, LOG_WARNING,
"%s:%lu: inconsistent \"endsolid\" name.\n",
streamer.name, (unsigned long)streamer.iline);
}
- }
-
- if((tk = strtok(NULL, " \0")) && strspn(tk, " \t\r\n") != strlen(tk)) {
- print_log(sstl, LOG_WARNING,
- "%s:%lu: malformed \"endsolid\" directive\n",
- streamer.name, streamer.iline);
+ sa_release(name);
}
/* Register the solid */
diff --git a/src/test_sstl_load.c b/src/test_sstl_load.c
@@ -29,6 +29,7 @@
#include "sstl.h"
#include "test_sstl_utils.h"
+#include <rsys/clock_time.h>
#include <rsys/float3.h>
#include <rsys/logger.h>
@@ -304,6 +305,7 @@ main(int argc, char** argv)
{
struct mem_allocator allocator;
struct sstl* sstl;
+ int i;
(void)argc, (void)argv;
mem_init_proxy_allocator(&allocator, &mem_default_allocator);
@@ -313,6 +315,28 @@ main(int argc, char** argv)
test_basic(sstl);
test_tetrahedron(sstl);
+ FOR_EACH(i, 1, argc) {
+ struct sstl_desc desc;
+ char buf[512];
+ struct time t0, t1;
+
+ printf("loading %s", argv[i]);
+ fflush(stdout);
+
+ time_current(&t0);
+ CHECK(sstl_load(sstl, argv[i]), RES_OK);
+ time_current(&t1);
+ time_sub(&t0, &t1, &t0);
+ time_dump(&t0, TIME_MIN|TIME_SEC|TIME_MSEC, NULL, buf, sizeof(buf));
+
+ CHECK(sstl_get_desc(sstl, &desc), RES_OK);
+
+ printf(" - #vertices = %lu; #triangles = %lu - %s\n",
+ (unsigned long)desc.vertices_count,
+ (unsigned long)desc.triangles_count,
+ buf);
+ }
+
CHECK(sstl_ref_put(sstl), RES_OK);
check_memory_allocator(&allocator);