From 28722f4c94300810fb1b8f33ca484694a807ca9b Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Mon, 18 Apr 2011 12:35:09 -0400 Subject: [PATCH] add WFObj::getLine() --- WFObj.cc | 31 +++++++++++++++++++++++++++---- WFObj.h | 1 + 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/WFObj.cc b/WFObj.cc index 2fa2ee5..528397f 100644 --- a/WFObj.cc +++ b/WFObj.cc @@ -123,11 +123,10 @@ bool WFObj::load(const char *fname) bool WFObj::load(const WFObj::Buffer &buff) { - string buildup; - while (istr.good()) + for (size_t idx = 0; idx < buff.size;) { - istr.getline(buf, size+1); - string input = trimString(buf); + string line = getLine(buff, idx, &idx) + string input = trimString(line); int sz = input.size(); if (sz == 0 || input[0] == '#') continue; @@ -145,6 +144,30 @@ bool WFObj::load(const WFObj::Buffer &buff) return true; } +string WFObj::getLine(const Buff & buff, size_t idx, size_t *update_idx) +{ + size_t len = 0; + while (idx + len < buff.length) + { + uint8_t ch = buff.data[idx + len]; + if (ch == 0) + { + *update_idx = idx + len + 1; + break; + } + if (ch == '\r' || ch == '\n') + { + *update_idx = idx + len + 1; + uint8_t nextch = buff.data[*update_idx]; + if (ch == '\r' && nextch == '\n') + (*update_idx)++; + break; + } + len++; + } + return string((const char *) &buff.data[idx], len); +} + void WFObj::processInputLine(const std::string & input) { string line = input; diff --git a/WFObj.h b/WFObj.h index 31fb12c..7c6fe7d 100644 --- a/WFObj.h +++ b/WFObj.h @@ -77,6 +77,7 @@ protected: void parseVertexIndices(const std::string & vtxref, int * ret); void updateAABB(const float * const vertex); static Buffer loadfile(const char *path); + std::string getLine(const Buff & buff, size_t idx, size_t *update_idx); /* variables */ std::vector< std::vector > m_data;