From bcd64245d23efced00f766662b4f7d80e01f6d75 Mon Sep 17 00:00:00 2001 From: tth Date: Tue, 18 May 2021 17:14:00 +0200 Subject: [PATCH] working on the .OBJ parser --- bubulles.h | 2 +- importobj.c | 95 +++++++++++++++++++++++++++++++++++++++++------- tools/read_obj.c | 2 +- 3 files changed, 83 insertions(+), 16 deletions(-) diff --git a/bubulles.h b/bubulles.h index 95c5a4d..7a1ba48 100644 --- a/bubulles.h +++ b/bubulles.h @@ -4,7 +4,7 @@ /* --------------------------------------------------------------------- */ -#define LIBBB_VERSION 55 +#define LIBBB_VERSION 56 #define SZ_BUBULLE_TEXT 51 /* arbitrary value */ diff --git a/importobj.c b/importobj.c index 8ff5dcc..8912b7b 100644 --- a/importobj.c +++ b/importobj.c @@ -15,13 +15,66 @@ extern int verbosity; /* --------------------------------------------------------------------- */ #define LINE_SZ 666 +/* --------------------------------------------------------------------- */ +typedef struct { + char *token; + int id; + } Tokens; + +enum token_id { T_comment=1, T_vertice }; + +Tokens TokenList[] = { + { "#", T_comment }, + { "v", T_vertice }, + { NULL, 0 } + }; + +static int type_of_the_line(char *text) +{ +Tokens *token; +int id; + +#if DEBUG_LEVEL +fprintf(stderr, "%s is searching '%s'\n", __func__, text); +#endif + +for (token=TokenList; token->token; token++) { + // fprintf(stderr, " %5s -> %d\n", token->token, token->id); + if (!strcmp(token->token, text)) { + return token->id; + } + } + +return -1; +} +/* --------------------------------------------------------------------- */ +static int parse_vertice(char *cptr, float *px, float *py, float *pz) +{ +float x, y, z; +int foo; + +foo = 0; +cptr = strtok(NULL, " "); +foo += sscanf(cptr, "%f", &x); +cptr = strtok(NULL, " "); +foo += sscanf(cptr, "%f", &y); +cptr = strtok(NULL, " "); +foo += sscanf(cptr, "%f", &z); + +if (3 == foo) { + *px = x; *py = y; *pz = z; + } + +return foo; +} +/* --------------------------------------------------------------------- */ int try_to_read_an_OBJ_file(char *fname, int notused) { FILE *fpin; char line[LINE_SZ+1], *cptr; float x, y, z; -int foo, nbre; +int foo, nbre, tokenid; BBList *bublist; Bubulle bubulle; @@ -44,24 +97,38 @@ print_bublist_desc(bublist, 0); nbre = 0; while(NULL!=(cptr=fgets(line, LINE_SZ, fpin))) { - if (verbosity>1) fputs(line, stderr); + if ('\n' != line[strlen(line)-1]) { + fprintf(stderr, "%s: short read, exiting...\n", __func__); + return -2; + } + line[strlen(line)-1] = '\0'; /* kill the newline */ + if (verbosity>1) fprintf(stderr, "line read ===%s===\n", line); cptr = strtok(line, " "); - if (strcmp(cptr, "v")) continue; - - cptr = strtok(NULL, " "); - foo = sscanf(cptr, "%f", &x); - cptr = strtok(NULL, " "); - foo = sscanf(cptr, "%f", &y); - cptr = strtok(NULL, " "); - foo = sscanf(cptr, "%f", &z); + if (NULL == cptr) { + fprintf(stderr, "no token ?\n"); + continue; + } + tokenid = type_of_the_line(cptr); + fprintf(stderr, "tok '%s' --> %d\n", cptr, tokenid); memset(&bubulle, 0, sizeof(Bubulle)); - bubulle.p.x = x; - bubulle.p.y = y; - bubulle.p.z = z; - if (verbosity > 1) niceprint_bubulle(&bubulle, 0); + switch (tokenid) { + case T_comment: + /* do nothing */ + break; + case T_vertice: + foo = parse_vertice(cptr, &x, &y, &z); + bubulle.p.x = x; + bubulle.p.y = y; + bubulle.p.z = z; + if (verbosity > 1) niceprint_bubulle(&bubulle, 0); + break; + default: + + continue; /* wtf ? */ + } foo = push_bubulle(bublist, &bubulle); if (foo) { diff --git a/tools/read_obj.c b/tools/read_obj.c index a7744fa..cd645af 100644 --- a/tools/read_obj.c +++ b/tools/read_obj.c @@ -18,7 +18,7 @@ if (2 != argc) { exit(0); } -verbosity = 1; +verbosity = 2; foo = try_to_read_an_OBJ_file(argv[1], 0); fprintf(stderr, "try to read -> %d\n", foo);