From e3a56816d6736d96dbeb5b126c9a921bf216861e Mon Sep 17 00:00:00 2001 From: Syoyo Fujita Date: Sun, 24 Jul 2016 15:53:05 +0900 Subject: [PATCH] Use std::getline() to read arbitrary size of line data. --- tiny_obj_loader.h | 56 +++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 29 deletions(-) diff --git a/tiny_obj_loader.h b/tiny_obj_loader.h index d130f8b..3534b91 100644 --- a/tiny_obj_loader.h +++ b/tiny_obj_loader.h @@ -62,9 +62,9 @@ THE SOFTWARE. #ifndef TINY_OBJ_LOADER_H_ #define TINY_OBJ_LOADER_H_ +#include #include #include -#include namespace tinyobj { @@ -112,10 +112,11 @@ typedef struct { typedef struct { std::vector indices; - std::vector - num_face_vertices; // The number of vertices per face. 3 = polygon, 4 = quad, ... Up to 255. - std::vector material_ids; // per-face material ID - std::vector tags; // SubD tag + std::vector num_face_vertices; // The number of vertices per + // face. 3 = polygon, 4 = quad, + // ... Up to 255. + std::vector material_ids; // per-face material ID + std::vector tags; // SubD tag } mesh_t; typedef struct { @@ -223,12 +224,12 @@ void LoadMtl(std::map *material_map, } // namespace tinyobj #ifdef TINYOBJLOADER_IMPLEMENTATION -#include -#include #include +#include #include #include -#include +#include +#include #include #include @@ -616,7 +617,8 @@ static bool exportFaceGroupToShape( shape->mesh.indices.push_back(idx); } - shape->mesh.num_face_vertices.push_back(static_cast(npolys)); + shape->mesh.num_face_vertices.push_back( + static_cast(npolys)); shape->mesh.material_ids.push_back(material_id); // per face } } @@ -926,12 +928,9 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, shape_t shape; - int maxchars = 8192; // Alloc enough size. - std::vector buf(static_cast(maxchars)); // Alloc enough size. while (inStream->peek() != -1) { - inStream->getline(&buf[0], maxchars); - - std::string linebuf(&buf[0]); + std::string linebuf; + std::getline((*inStream), linebuf); // Trim newline '\r\n' or '\n' if (linebuf.size() > 0) { @@ -1374,30 +1373,29 @@ bool LoadObjWithCallback(void *user_data, const callback_t &callback, } else { callback.group_cb(user_data, NULL, 0); } - } continue; - } + } - // object name - if (token[0] == 'o' && IS_SPACE((token[1]))) { - // @todo { multiple object name? } - char namebuf[TINYOBJ_SSCANF_BUFFER_SIZE]; - token += 2; + // object name + if (token[0] == 'o' && IS_SPACE((token[1]))) { + // @todo { multiple object name? } + char namebuf[TINYOBJ_SSCANF_BUFFER_SIZE]; + token += 2; #ifdef _MSC_VER - sscanf_s(token, "%s", namebuf, (unsigned)_countof(namebuf)); + sscanf_s(token, "%s", namebuf, (unsigned)_countof(namebuf)); #else - sscanf(token, "%s", namebuf); + sscanf(token, "%s", namebuf); #endif - std::string name = std::string(namebuf); + std::string name = std::string(namebuf); - if (callback.object_cb) { - callback.object_cb(user_data, name.c_str()); - } + if (callback.object_cb) { + callback.object_cb(user_data, name.c_str()); + } - continue; - } + continue; + } #if 0 // @todo if (token[0] == 't' && IS_SPACE(token[1])) {