nuclear@0: /* nuclear@0: Open Asset Import Library (assimp) nuclear@0: ---------------------------------------------------------------------- nuclear@0: nuclear@0: Copyright (c) 2006-2012, assimp team nuclear@0: All rights reserved. nuclear@0: nuclear@0: Redistribution and use of this software in source and binary forms, nuclear@0: with or without modification, are permitted provided that the nuclear@0: following conditions are met: nuclear@0: nuclear@0: * Redistributions of source code must retain the above nuclear@0: copyright notice, this list of conditions and the nuclear@0: following disclaimer. nuclear@0: nuclear@0: * Redistributions in binary form must reproduce the above nuclear@0: copyright notice, this list of conditions and the nuclear@0: following disclaimer in the documentation and/or other nuclear@0: materials provided with the distribution. nuclear@0: nuclear@0: * Neither the name of the assimp team, nor the names of its nuclear@0: contributors may be used to endorse or promote products nuclear@0: derived from this software without specific prior nuclear@0: written permission of the assimp team. nuclear@0: nuclear@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS nuclear@0: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT nuclear@0: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR nuclear@0: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT nuclear@0: OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, nuclear@0: SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT nuclear@0: LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, nuclear@0: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY nuclear@0: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT nuclear@0: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE nuclear@0: OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. nuclear@0: nuclear@0: ---------------------------------------------------------------------- nuclear@0: */ nuclear@0: nuclear@0: /** @file LineSplitter.h nuclear@0: * @brief LineSplitter, a helper class to iterate through all lines nuclear@0: * of a file easily. Works with StreamReader. nuclear@0: */ nuclear@0: #ifndef INCLUDED_LINE_SPLITTER_H nuclear@0: #define INCLUDED_LINE_SPLITTER_H nuclear@0: nuclear@0: #include nuclear@0: nuclear@0: #include "StreamReader.h" nuclear@0: #include "ParsingUtils.h" nuclear@0: nuclear@0: namespace Assimp { nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: /** Usage: nuclear@0: @code nuclear@0: for(LineSplitter splitter(stream);splitter;++splitter) { nuclear@0: nuclear@0: if (*splitter == "hi!") { nuclear@0: ... nuclear@0: } nuclear@0: else if (splitter->substr(0,5) == "hello") { nuclear@0: ... nuclear@0: // access the third token in the line (tokens are space-separated) nuclear@0: if (strtol(splitter[2]) > 5) { .. } nuclear@0: } nuclear@0: nuclear@0: std::cout << "Current line is: " << splitter.get_index() << std::endl; nuclear@0: } nuclear@0: @endcode */ nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: class LineSplitter nuclear@0: { nuclear@0: public: nuclear@0: nuclear@0: typedef size_t line_idx; nuclear@0: nuclear@0: public: nuclear@0: nuclear@0: // ----------------------------------------- nuclear@0: /** construct from existing stream reader nuclear@0: note: trim is *always* assumed true if skyp_empty_lines==true nuclear@0: */ nuclear@0: LineSplitter(StreamReaderLE& stream, bool skip_empty_lines = true, bool trim = true) nuclear@0: : stream(stream) nuclear@0: , swallow() nuclear@0: , skip_empty_lines(skip_empty_lines) nuclear@0: , trim(trim) nuclear@0: { nuclear@0: cur.reserve(1024); nuclear@0: operator++(); nuclear@0: nuclear@0: idx = 0; nuclear@0: } nuclear@0: nuclear@0: public: nuclear@0: nuclear@0: // ----------------------------------------- nuclear@0: /** pseudo-iterator increment */ nuclear@0: LineSplitter& operator++() { nuclear@0: if(swallow) { nuclear@0: swallow = false; nuclear@0: return *this; nuclear@0: } nuclear@0: nuclear@0: if (!*this) { nuclear@0: throw std::logic_error("End of file, no more lines to be retrieved."); nuclear@0: } nuclear@0: nuclear@0: char s; nuclear@0: nuclear@0: cur.clear(); nuclear@0: while(stream.GetRemainingSize() && (s = stream.GetI1(),1)) { nuclear@0: if (s == '\n' || s == '\r') { nuclear@0: if (skip_empty_lines) { nuclear@0: while (stream.GetRemainingSize() && ((s = stream.GetI1()) == ' ' || s == '\r' || s == '\n')); nuclear@0: if (stream.GetRemainingSize()) { nuclear@0: stream.IncPtr(-1); nuclear@0: } nuclear@0: } nuclear@0: else { nuclear@0: // skip both potential line terminators but don't read past this line. nuclear@0: if (stream.GetRemainingSize() && (s == '\r' && stream.GetI1() != '\n')) { nuclear@0: stream.IncPtr(-1); nuclear@0: } nuclear@0: nuclear@0: if (trim) { nuclear@0: while (stream.GetRemainingSize() && ((s = stream.GetI1()) == ' ' || s == '\t')); nuclear@0: if (stream.GetRemainingSize()) { nuclear@0: stream.IncPtr(-1); nuclear@0: } nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: break; nuclear@0: } nuclear@0: cur += s; nuclear@0: } nuclear@0: nuclear@0: ++idx; nuclear@0: return *this; nuclear@0: } nuclear@0: nuclear@0: // ----------------------------------------- nuclear@0: LineSplitter& operator++(int) { nuclear@0: return ++(*this); nuclear@0: } nuclear@0: nuclear@0: // ----------------------------------------- nuclear@0: /** get a pointer to the beginning of a particular token */ nuclear@0: const char* operator[] (size_t idx) const { nuclear@0: const char* s = operator->()->c_str(); nuclear@0: nuclear@0: SkipSpaces(&s); nuclear@0: for(size_t i = 0; i < idx; ++i) { nuclear@0: nuclear@0: for(;!IsSpace(*s); ++s) { nuclear@0: if(IsLineEnd(*s)) { nuclear@0: throw std::range_error("Token index out of range, EOL reached"); nuclear@0: } nuclear@0: } nuclear@0: SkipSpaces(&s); nuclear@0: } nuclear@0: return s; nuclear@0: } nuclear@0: nuclear@0: // ----------------------------------------- nuclear@0: /** extract the start positions of N tokens from the current line*/ nuclear@0: template nuclear@0: void get_tokens(const char* (&tokens)[N]) const { nuclear@0: const char* s = operator->()->c_str(); nuclear@0: nuclear@0: SkipSpaces(&s); nuclear@0: for(size_t i = 0; i < N; ++i) { nuclear@0: if(IsLineEnd(*s)) { nuclear@0: throw std::range_error("Token count out of range, EOL reached"); nuclear@0: } nuclear@0: tokens[i] = s; nuclear@0: nuclear@0: for(;*s && !IsSpace(*s); ++s); nuclear@0: SkipSpaces(&s); nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: // ----------------------------------------- nuclear@0: /** member access */ nuclear@0: const std::string* operator -> () const { nuclear@0: return &cur; nuclear@0: } nuclear@0: nuclear@0: std::string operator* () const { nuclear@0: return cur; nuclear@0: } nuclear@0: nuclear@0: // ----------------------------------------- nuclear@0: /** boolean context */ nuclear@0: operator bool() const { nuclear@0: return stream.GetRemainingSize()>0; nuclear@0: } nuclear@0: nuclear@0: // ----------------------------------------- nuclear@0: /** line indices are zero-based, empty lines are included */ nuclear@0: operator line_idx() const { nuclear@0: return idx; nuclear@0: } nuclear@0: nuclear@0: line_idx get_index() const { nuclear@0: return idx; nuclear@0: } nuclear@0: nuclear@0: // ----------------------------------------- nuclear@0: /** access the underlying stream object */ nuclear@0: StreamReaderLE& get_stream() { nuclear@0: return stream; nuclear@0: } nuclear@0: nuclear@0: // ----------------------------------------- nuclear@0: /** !strcmp((*this)->substr(0,strlen(check)),check) */ nuclear@0: bool match_start(const char* check) { nuclear@0: const size_t len = strlen(check); nuclear@0: nuclear@0: return len <= cur.length() && std::equal(check,check+len,cur.begin()); nuclear@0: } nuclear@0: nuclear@0: nuclear@0: // ----------------------------------------- nuclear@0: /** swallow the next call to ++, return the previous value. */ nuclear@0: void swallow_next_increment() { nuclear@0: swallow = true; nuclear@0: } nuclear@0: nuclear@0: private: nuclear@0: nuclear@0: line_idx idx; nuclear@0: std::string cur; nuclear@0: StreamReaderLE& stream; nuclear@0: bool swallow, skip_empty_lines, trim; nuclear@0: }; nuclear@0: nuclear@0: } nuclear@0: #endif // INCLUDED_LINE_SPLITTER_H