Making a quick OBJ 3d model parser, I've pulled in the readstream from the file reader and while reading through it using a slightly modified version of the token struct when debugging I've noticed that the stream pointer (At) gets corrupted at the end of it's data. It's not clear to me why this is happening except maybe it is reading off the end of the stream as the contents? It doesn't fail as the contents.count hits 0 and breaks the reading, but I can't figure out why it's doing this.
Also is there a better way to handle this text stream data reading? I wanted to learn how to tokenize strings so I went this direction in-lieu of the fscanf and FILE handle and while it works, I'm not convinced this is the data approach Casey's buffer and streams were intended as I'm having to cast to a (char*) to convert to a value.
Any thoughts are appreciated.
Sample code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | static void ConsumeLine(FString* Source) { for(;;) { FToken Token = PopToken(Source); if(StringsAreEqual(Token.Text, "\n")) { break; } } } static FOBJModelData ParseOBJ(FMemoryArena* Arena, FStream File) { FStream* At = &File; while(At->Contents.Count > 0) { FToken = PopToken(&At->Contents); if(StringsAreEqual(Token.Text, "#")) { ConsumeLine(&At->Contents); } else if(StringsAreEqual(Token.Text, "v")) { Vertices[VertexCount++] = vec3{strtof((char*)PopToken(&At->Contents).Text.Data, 0), strtof((char*)PopToken(&At->Contents).Text.Data, 0), strtof((char*)PopToken(&At->Contents).Text.Data, 0)}; PopToken(&At->Contents); } ... } } } |