Skip to content

Commit

Permalink
extern: Fix fast_obj to trim trainling whitespace for names
Browse files Browse the repository at this point in the history
Some .obj files have extra whitespace after usemtl and other statements;
this may interfere with parsing, for example by adding a space to the
material name which can result in inability to find it in .mtl file.
  • Loading branch information
zeux committed Aug 13, 2023
1 parent 577c585 commit 6e8922f
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions extern/fast_obj.h
Original file line number Diff line number Diff line change
Expand Up @@ -410,12 +410,6 @@ int is_whitespace(char c)
return (c == ' ' || c == '\t' || c == '\r');
}

static
int is_end_of_name(char c)
{
return (c == '\t' || c == '\r' || c == '\n');
}

static
int is_newline(char c)
{
Expand All @@ -437,6 +431,21 @@ int is_exponent(char c)
}


static
const char* skip_name(const char* ptr)
{
const char* s = ptr;

while (!is_newline(*ptr))
ptr++;

while (ptr > s && is_whitespace(*(ptr - 1)))
ptr--;

return ptr;
}


static
const char* skip_whitespace(const char* ptr)
{
Expand Down Expand Up @@ -771,9 +780,7 @@ const char* parse_object(fastObjData* data, const char* ptr)
ptr = skip_whitespace(ptr);

s = ptr;
while (!is_end_of_name(*ptr))
ptr++;

ptr = skip_name(ptr);
e = ptr;

flush_object(data);
Expand All @@ -793,9 +800,7 @@ const char* parse_group(fastObjData* data, const char* ptr)
ptr = skip_whitespace(ptr);

s = ptr;
while (!is_end_of_name(*ptr))
ptr++;

ptr = skip_name(ptr);
e = ptr;

flush_group(data);
Expand Down Expand Up @@ -874,9 +879,7 @@ const char* parse_usemtl(fastObjData* data, const char* ptr)

/* Parse the material name */
s = ptr;
while (!is_end_of_name(*ptr))
ptr++;

ptr = skip_name(ptr);
e = ptr;

/* Find an existing material with the same name */
Expand Down Expand Up @@ -972,9 +975,7 @@ const char* read_map(fastObjData* data, const char* ptr, fastObjTexture* map)

/* Read name */
s = ptr;
while (!is_end_of_name(*ptr))
ptr++;

ptr = skip_name(ptr);
e = ptr;

name = string_copy(s, e);
Expand Down Expand Up @@ -1048,8 +1049,7 @@ int read_mtllib(fastObjData* data, void* file, const fastObjCallbacks* callbacks
p++;

s = p;
while (!is_end_of_name(*p))
p++;
p = skip_name(p);

mtl.name = string_copy(s, p);
}
Expand Down Expand Up @@ -1191,9 +1191,7 @@ const char* parse_mtllib(fastObjData* data, const char* ptr, const fastObjCallba
ptr = skip_whitespace(ptr);

s = ptr;
while (!is_end_of_name(*ptr))
ptr++;

ptr = skip_name(ptr);
e = ptr;

lib = string_concat(data->base, s, e);
Expand Down

0 comments on commit 6e8922f

Please sign in to comment.