From 1a2c9767ab0565d85e627835dbf6d894245e102c Mon Sep 17 00:00:00 2001 From: hasufell Date: Wed, 7 May 2014 21:51:47 +0200 Subject: [PATCH] Implement parsing of the faces (only plain for now) This is not yet the HE_face structure! --- parser.c | 26 ++++++++++++++++++++++++-- types.h | 1 + 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/parser.c b/parser.c index 3190706..31f3e55 100644 --- a/parser.c +++ b/parser.c @@ -50,7 +50,9 @@ HE_face *parse_obj(char const * const filename) *str_ptr_newline = NULL, *str_tmp_ptr = NULL; const size_t vert_size = sizeof(HE_vert); - HE_vert *vertices = malloc(vert_size); + HE_vert *vertices = malloc(vert_size), + *vert_tmp; + FACE face_v = NULL; /* read the whole file into string */ string = read_file(filename); @@ -60,6 +62,7 @@ HE_face *parse_obj(char const * const filename) printf("file content\n%s\n\n", string); vc = 1; + fc = 1; str_tmp_ptr = strtok_r(string, "\n", &str_ptr_newline); while (str_tmp_ptr && *str_tmp_ptr) { @@ -93,8 +96,27 @@ HE_face *parse_obj(char const * const filename) /* exceeds 3 dimensions, malformed vertice */ if (strtok_r(NULL, " ", &str_ptr_space)) return NULL; + } else if (!strcmp(str_tmp_ptr, "f")) { /* parse faces */ - /* TODO */ + char *myint = NULL; + unsigned int i = 0; + FACE tmp_ptr = NULL; + + /* fill both HE_edge and HE_face */ + tmp_ptr = realloc(face_v, sizeof(FACE*) * fc); + CHECK_PTR_VAL(tmp_ptr); + face_v = tmp_ptr; + face_v[fc - 1] = NULL; + while ((myint = strtok_r(NULL, " ", &str_ptr_space))) { + unsigned int *tmp_ptr = NULL; + i++; + tmp_ptr = realloc(face_v[fc - 1], sizeof(FACE**) * (i + 1)); + CHECK_PTR_VAL(tmp_ptr); + tmp_ptr[i - 1] = (unsigned int) atoi(myint); + tmp_ptr[i] = 0; /* so we can iterate over it */ + face_v[fc - 1] = tmp_ptr; + } + fc++; } str_tmp_ptr = strtok_r(NULL, "\n", &str_ptr_newline); diff --git a/types.h b/types.h index d0af07d..29415de 100644 --- a/types.h +++ b/types.h @@ -25,6 +25,7 @@ */ #define STD_FILE_BUF 4096 +typedef unsigned int** FACE; typedef struct HE_edge HE_edge; typedef struct HE_vert HE_vert;