1 /* 2 Open Asset Import Library (assimp) 3 ---------------------------------------------------------------------- 4 5 Copyright (c) 2006-2017, assimp team 6 7 All rights reserved. 8 9 Redistribution and use of this software in source and binary forms, 10 with or without modification, are permitted provided that the 11 following conditions are met: 12 13 * Redistributions of source code must retain the above 14 copyright notice, this list of conditions and the 15 following disclaimer. 16 17 * Redistributions in binary form must reproduce the above 18 copyright notice, this list of conditions and the 19 following disclaimer in the documentation and/or other 20 materials provided with the distribution. 21 22 * Neither the name of the assimp team, nor the names of its 23 contributors may be used to endorse or promote products 24 derived from this software without specific prior 25 written permission of the assimp team. 26 27 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 28 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 29 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 30 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 31 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 32 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 33 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 34 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 35 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 36 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 37 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 39 ---------------------------------------------------------------------- 40 */ 41 42 /** @file LineSplitter.h 43 * @brief LineSplitter, a helper class to iterate through all lines 44 * of a file easily. Works with StreamReader. 45 */ 46 #ifndef INCLUDED_LINE_SPLITTER_H 47 #define INCLUDED_LINE_SPLITTER_H 48 49 #include <stdexcept> 50 51 #include "StreamReader.h" 52 #include "ParsingUtils.h" 53 54 namespace Assimp { 55 56 // ------------------------------------------------------------------------------------------------ 57 /** Usage: 58 @code 59 for(LineSplitter splitter(stream);splitter;++splitter) { 60 61 if (*splitter == "hi!") { 62 ... 63 } 64 else if (splitter->substr(0,5) == "hello") { 65 ... 66 // access the third token in the line (tokens are space-separated) 67 if (strtol(splitter[2]) > 5) { .. } 68 } 69 70 std::cout << "Current line is: " << splitter.get_index() << std::endl; 71 } 72 @endcode 73 */ 74 // ------------------------------------------------------------------------------------------------ 75 class LineSplitter { 76 public: 77 typedef size_t line_idx; 78 79 // ----------------------------------------- 80 /** construct from existing stream reader 81 note: trim is *always* assumed true if skyp_empty_lines==true 82 */ 83 LineSplitter(StreamReaderLE& stream, bool skip_empty_lines = true, bool trim = true) 84 : idx( 0 ) 85 , stream(stream) 86 , swallow() 87 , skip_empty_lines(skip_empty_lines) 88 , trim(trim) { 89 cur.reserve(1024); 90 operator++(); 91 92 idx = 0; 93 } 94 ~LineSplitter()95 ~LineSplitter() { 96 // empty 97 } 98 99 public: 100 101 // ----------------------------------------- 102 /** pseudo-iterator increment */ 103 LineSplitter& operator++() { 104 if(swallow) { 105 swallow = false; 106 return *this; 107 } 108 if (!*this) { 109 throw std::logic_error("End of file, no more lines to be retrieved."); 110 } 111 char s; 112 cur.clear(); 113 while(stream.GetRemainingSize() && (s = stream.GetI1(),1)) { 114 if (s == '\n' || s == '\r') { 115 if (skip_empty_lines) { 116 while (stream.GetRemainingSize() && ((s = stream.GetI1()) == ' ' || s == '\r' || s == '\n')); 117 if (stream.GetRemainingSize()) { 118 stream.IncPtr(-1); 119 } 120 } 121 else { 122 // skip both potential line terminators but don't read past this line. 123 if (stream.GetRemainingSize() && (s == '\r' && stream.GetI1() != '\n')) { 124 stream.IncPtr(-1); 125 } 126 if (trim) { 127 while (stream.GetRemainingSize() && ((s = stream.GetI1()) == ' ' || s == '\t')); 128 if (stream.GetRemainingSize()) { 129 stream.IncPtr(-1); 130 } 131 } 132 } 133 break; 134 } 135 cur += s; 136 } 137 ++idx; 138 return *this; 139 } 140 141 // ----------------------------------------- 142 LineSplitter& operator++(int) { 143 return ++(*this); 144 } 145 146 // ----------------------------------------- 147 /** get a pointer to the beginning of a particular token */ 148 const char* operator[] (size_t idx) const { 149 const char* s = operator->()->c_str(); 150 151 SkipSpaces(&s); 152 for(size_t i = 0; i < idx; ++i) { 153 154 for(;!IsSpace(*s); ++s) { 155 if(IsLineEnd(*s)) { 156 throw std::range_error("Token index out of range, EOL reached"); 157 } 158 } 159 SkipSpaces(&s); 160 } 161 return s; 162 } 163 164 // ----------------------------------------- 165 /** extract the start positions of N tokens from the current line*/ 166 template <size_t N> get_tokens(const char * (& tokens)[N])167 void get_tokens(const char* (&tokens)[N]) const { 168 const char* s = operator->()->c_str(); 169 170 SkipSpaces(&s); 171 for(size_t i = 0; i < N; ++i) { 172 if(IsLineEnd(*s)) { 173 174 throw std::range_error("Token count out of range, EOL reached"); 175 176 } 177 tokens[i] = s; 178 179 for(;*s && !IsSpace(*s); ++s); 180 SkipSpaces(&s); 181 } 182 } 183 184 // ----------------------------------------- 185 /** member access */ 186 const std::string* operator -> () const { 187 return &cur; 188 } 189 190 std::string operator* () const { 191 return cur; 192 } 193 194 // ----------------------------------------- 195 /** boolean context */ 196 operator bool() const { 197 return stream.GetRemainingSize()>0; 198 } 199 200 // ----------------------------------------- 201 /** line indices are zero-based, empty lines are included */ line_idx()202 operator line_idx() const { 203 return idx; 204 } 205 get_index()206 line_idx get_index() const { 207 return idx; 208 } 209 210 // ----------------------------------------- 211 /** access the underlying stream object */ get_stream()212 StreamReaderLE& get_stream() { 213 return stream; 214 } 215 216 // ----------------------------------------- 217 /** !strcmp((*this)->substr(0,strlen(check)),check) */ match_start(const char * check)218 bool match_start(const char* check) { 219 const size_t len = strlen(check); 220 221 return len <= cur.length() && std::equal(check,check+len,cur.begin()); 222 } 223 224 225 // ----------------------------------------- 226 /** swallow the next call to ++, return the previous value. */ swallow_next_increment()227 void swallow_next_increment() { 228 swallow = true; 229 } 230 231 private: 232 LineSplitter( const LineSplitter & ); 233 LineSplitter &operator = ( const LineSplitter & ); 234 235 private: 236 line_idx idx; 237 std::string cur; 238 StreamReaderLE& stream; 239 bool swallow, skip_empty_lines, trim; 240 }; 241 242 } 243 #endif // INCLUDED_LINE_SPLITTER_H 244