loader_aw

Load OBJ/MTL file formats
git clone git://git.meso-star.fr/loader_aw.git
Log | Files | Refs | README | LICENSE

commit bbcb90efbee800e2a97233252642a2f88585db06
parent 66900a7d91cd84b0b6c8737e9c0417d7892ea1c3
Author: vaplv <vaplv@free.fr>
Date:   Sun, 20 Jul 2014 13:49:30 +0200

Use raw char* to manage material/map name

Diffstat:
Msrc/aw_mtl.c | 54+++++++++++++++++++++++++++++++++---------------------
1 file changed, 33 insertions(+), 21 deletions(-)

diff --git a/src/aw_mtl.c b/src/aw_mtl.c @@ -6,7 +6,6 @@ #include <rsys/float3.h> #include <rsys/mem_allocator.h> #include <rsys/ref_count.h> -#include <rsys/str.h> #include <float.h> @@ -173,32 +172,43 @@ struct aw_mtl { static enum aw_result parse_name(char** name, char* tk, char** tk_ctxt) { - struct str str; + char* buf = NULL; + size_t buf_len = 0; + size_t buf_size_max = 0; enum aw_result res = AW_OK; - ASSERT(tk && tk_ctxt && name); + ASSERT(tk_ctxt && name); + + if(!tk) { + res = AW_BAD_ARGUMENT; + goto error; + } - str_init(NULL, &str); do { - if(str_len(&str) && str_append_char(&str, ' ')) { - res = AW_MEMORY_ERROR; - goto error; - } - if(str_append(&str, tk)) { - res = AW_MEMORY_ERROR; - goto error; + const size_t capacity = + buf_len + strlen(tk) + 1 /* null char */ + (buf_len > 0) /* space */; + + if( capacity >= buf_size_max) { + buf_size_max = round_up_pow2(capacity); + buf = mem_realloc(buf, buf_size_max); + if(!buf) { + res = AW_MEMORY_ERROR; + goto error; + } } + if(buf_len > 0) + buf[buf_len++] = ' '; + buf[buf_len] = '\0'; + strcat(buf, tk); + buf_len = capacity - 1 /* don't count null char */; } while((tk = strtok_r(NULL, " \t", tk_ctxt))); - *name = mem_alloc(str_len(&str) + 1); - if(!*name) { - res = AW_MEMORY_ERROR; - goto error; - } - strcpy(*name, str_cget(&str)); + *name = buf; + exit: return res; error: - str_release(&str); + if(buf) + mem_free(buf); goto exit; } @@ -346,7 +356,8 @@ parse_map } else if(!strcmp(word, "-blendv")) { res = parse_bool_option(&map->options_mask, AW_MAP_BLEND_V, tk_ctxt); } else if(!strcmp(word, "-cc")) { - res = parse_bool_option(&map->options_mask, AW_MAP_COLOR_CORRECTION, tk_ctxt); + res = parse_bool_option + (&map->options_mask, AW_MAP_COLOR_CORRECTION, tk_ctxt); } else if(!strcmp(word, "-clamp")) { res = parse_bool_option(&map->options_mask, AW_MAP_CLAMP, tk_ctxt); } else if(!strcmp(word, "-imfchan") && (type & MAP_SCALAR)) { @@ -415,7 +426,7 @@ parse_mtl_file(struct aw_mtl* mtl, const char* path, char* content) (&mtl->newmtl->specular_exponent, 1, 1, 0.f, FLT_MAX, 0.f, &word_tk); } else if(!strcmp(word, "Ni")) { /* Refraction index */ res = parse_floatX - (&mtl->newmtl->refraction_index, 1, 1, 0.001f, 10.f, 0.001f, &word_tk); + (&mtl->newmtl->refraction_index, 1, 1, 0.001f, 10.f, 0.001f,&word_tk); } else if(!strcmp(word, "illum")) { /* Illumination model */ res = parse_size_t(&mtl->newmtl->illumination_model, 0, 10, &word_tk); } else if(!strcmp(word, "map_Ka")) { /* Ambient texture */ @@ -425,7 +436,8 @@ parse_mtl_file(struct aw_mtl* mtl, const char* path, char* content) } else if(!strcmp(word, "map_Ks")) { /* Specular texture */ res = parse_map(&mtl->newmtl->specular_map, MAP_COMMON, &word_tk); } else if(!strcmp(word, "map_Ns")) { /* Specular exponent texture */ - res = parse_map(&mtl->newmtl->specular_exponent_map, MAP_SCALAR, &word_tk); + res = parse_map + (&mtl->newmtl->specular_exponent_map, MAP_SCALAR, &word_tk); } else if(!strcmp(word, "bump")) { /* Bump map */ res = parse_map(&mtl->newmtl->bump_map, MAP_BUMP, &word_tk); } else {