From 934788785e04c7d0f8009e78f520735156581c8c Mon Sep 17 00:00:00 2001 From: Syoyo Fujita Date: Wed, 10 Oct 2018 13:33:11 +0900 Subject: [PATCH] Show line number for some warning and error message. --- loader_example.cc | 2 +- tiny_obj_loader.h | 26 +++++++++++++++++--------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/loader_example.cc b/loader_example.cc index 82de6b3..462e011 100644 --- a/loader_example.cc +++ b/loader_example.cc @@ -385,7 +385,7 @@ static bool TestStreamLoadObj() { std::string* err) { (void)err; (void)matId; - LoadMtl(matMap, materials, &m_matSStream, warn); + LoadMtl(matMap, materials, &m_matSStream, warn, err); return true; } diff --git a/tiny_obj_loader.h b/tiny_obj_loader.h index 8ef44a7..01bb406 100644 --- a/tiny_obj_loader.h +++ b/tiny_obj_loader.h @@ -1325,9 +1325,11 @@ void LoadMtl(std::map *material_map, std::stringstream warn_ss; + size_t line_no = 0; std::string linebuf; while (inStream->peek() != -1) { safeGetline(*inStream, linebuf); + line_no++; // Trim trailing whitespace. if (linebuf.size() > 0) { @@ -1467,7 +1469,8 @@ void LoadMtl(std::map *material_map, if (has_tr) { warn_ss << "Both `d` and `Tr` parameters defined for \"" - << material.name << "\". Use the value of `d` for dissolve." + << material.name << "\". Use the value of `d` for dissolve (line " + << line_no << " in .mtl.)" << std::endl; } has_d = true; @@ -1478,7 +1481,8 @@ void LoadMtl(std::map *material_map, if (has_d) { // `d` wins. Ignore `Tr` value. warn_ss << "Both `d` and `Tr` parameters defined for \"" - << material.name << "\". Use the value of `d` for dissolve." + << material.name << "\". Use the value of `d` for dissolve (line " + << line_no << " in .mtl.)" << std::endl; } else { // We invert value of Tr(assume Tr is in range [0, 1]) @@ -1929,7 +1933,9 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, static_cast(vn.size() / 3), static_cast(vt.size() / 2), &vi)) { if (err) { - (*err) = "Failed parse `f' line(e.g. zero value for face index).\n"; + std::stringstream ss; + ss << "Failed parse `f' line(e.g. zero value for face index. line " << line_num << ".)\n"; + (*err) += ss.str(); } return false; } @@ -1988,9 +1994,11 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, if (filenames.empty()) { if (warn) { - (*warn) += - "Looks like empty filename for mtllib. Use default " - "material. \n"; + std::stringstream ss; + ss << "Looks like empty filename for mtllib. Use default " + "material (line " << line_num << ".)\n"; + + (*warn) += ss.str(); } } else { bool found = false; @@ -2197,21 +2205,21 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, if (greatest_v_idx >= static_cast(v.size() / 3)) { if (warn) { std::stringstream ss; - ss << "Vertex indices out of bounds.\n" << std::endl; + ss << "Vertex indices out of bounds (line " << line_num << ".)\n" << std::endl; (*warn) += ss.str(); } } if (greatest_vn_idx >= static_cast(vn.size() / 3)) { if (warn) { std::stringstream ss; - ss << "Vertex normal indices out of bounds.\n" << std::endl; + ss << "Vertex normal indices out of bounds (line " << line_num << ".)\n" << std::endl; (*warn) += ss.str(); } } if (greatest_vt_idx >= static_cast(vt.size() / 2)) { if (warn) { std::stringstream ss; - ss << "Vertex texcoord indices out of bounds.\n" << std::endl; + ss << "Vertex texcoord indices out of bounds (line " << line_num << ".)\n" << std::endl; (*warn) += ss.str(); } }