vrshoot
diff libs/assimp/LineSplitter.h @ 0:b2f14e535253
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 01 Feb 2014 19:58:19 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/assimp/LineSplitter.h Sat Feb 01 19:58:19 2014 +0200 1.3 @@ -0,0 +1,242 @@ 1.4 +/* 1.5 +Open Asset Import Library (assimp) 1.6 +---------------------------------------------------------------------- 1.7 + 1.8 +Copyright (c) 2006-2012, assimp team 1.9 +All rights reserved. 1.10 + 1.11 +Redistribution and use of this software in source and binary forms, 1.12 +with or without modification, are permitted provided that the 1.13 +following conditions are met: 1.14 + 1.15 +* Redistributions of source code must retain the above 1.16 + copyright notice, this list of conditions and the 1.17 + following disclaimer. 1.18 + 1.19 +* Redistributions in binary form must reproduce the above 1.20 + copyright notice, this list of conditions and the 1.21 + following disclaimer in the documentation and/or other 1.22 + materials provided with the distribution. 1.23 + 1.24 +* Neither the name of the assimp team, nor the names of its 1.25 + contributors may be used to endorse or promote products 1.26 + derived from this software without specific prior 1.27 + written permission of the assimp team. 1.28 + 1.29 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.30 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.31 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.32 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.33 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.34 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.35 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.36 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.37 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.38 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.39 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.40 + 1.41 +---------------------------------------------------------------------- 1.42 +*/ 1.43 + 1.44 +/** @file LineSplitter.h 1.45 + * @brief LineSplitter, a helper class to iterate through all lines 1.46 + * of a file easily. Works with StreamReader. 1.47 + */ 1.48 +#ifndef INCLUDED_LINE_SPLITTER_H 1.49 +#define INCLUDED_LINE_SPLITTER_H 1.50 + 1.51 +#include <stdexcept> 1.52 + 1.53 +#include "StreamReader.h" 1.54 +#include "ParsingUtils.h" 1.55 + 1.56 +namespace Assimp { 1.57 + 1.58 +// ------------------------------------------------------------------------------------------------ 1.59 +/** Usage: 1.60 +@code 1.61 +for(LineSplitter splitter(stream);splitter;++splitter) { 1.62 + 1.63 + if (*splitter == "hi!") { 1.64 + ... 1.65 + } 1.66 + else if (splitter->substr(0,5) == "hello") { 1.67 + ... 1.68 + // access the third token in the line (tokens are space-separated) 1.69 + if (strtol(splitter[2]) > 5) { .. } 1.70 + } 1.71 + 1.72 + std::cout << "Current line is: " << splitter.get_index() << std::endl; 1.73 +} 1.74 +@endcode */ 1.75 +// ------------------------------------------------------------------------------------------------ 1.76 +class LineSplitter 1.77 +{ 1.78 +public: 1.79 + 1.80 + typedef size_t line_idx; 1.81 + 1.82 +public: 1.83 + 1.84 + // ----------------------------------------- 1.85 + /** construct from existing stream reader 1.86 + note: trim is *always* assumed true if skyp_empty_lines==true 1.87 + */ 1.88 + LineSplitter(StreamReaderLE& stream, bool skip_empty_lines = true, bool trim = true) 1.89 + : stream(stream) 1.90 + , swallow() 1.91 + , skip_empty_lines(skip_empty_lines) 1.92 + , trim(trim) 1.93 + { 1.94 + cur.reserve(1024); 1.95 + operator++(); 1.96 + 1.97 + idx = 0; 1.98 + } 1.99 + 1.100 +public: 1.101 + 1.102 + // ----------------------------------------- 1.103 + /** pseudo-iterator increment */ 1.104 + LineSplitter& operator++() { 1.105 + if(swallow) { 1.106 + swallow = false; 1.107 + return *this; 1.108 + } 1.109 + 1.110 + if (!*this) { 1.111 + throw std::logic_error("End of file, no more lines to be retrieved."); 1.112 + } 1.113 + 1.114 + char s; 1.115 + 1.116 + cur.clear(); 1.117 + while(stream.GetRemainingSize() && (s = stream.GetI1(),1)) { 1.118 + if (s == '\n' || s == '\r') { 1.119 + if (skip_empty_lines) { 1.120 + while (stream.GetRemainingSize() && ((s = stream.GetI1()) == ' ' || s == '\r' || s == '\n')); 1.121 + if (stream.GetRemainingSize()) { 1.122 + stream.IncPtr(-1); 1.123 + } 1.124 + } 1.125 + else { 1.126 + // skip both potential line terminators but don't read past this line. 1.127 + if (stream.GetRemainingSize() && (s == '\r' && stream.GetI1() != '\n')) { 1.128 + stream.IncPtr(-1); 1.129 + } 1.130 + 1.131 + if (trim) { 1.132 + while (stream.GetRemainingSize() && ((s = stream.GetI1()) == ' ' || s == '\t')); 1.133 + if (stream.GetRemainingSize()) { 1.134 + stream.IncPtr(-1); 1.135 + } 1.136 + } 1.137 + } 1.138 + 1.139 + break; 1.140 + } 1.141 + cur += s; 1.142 + } 1.143 + 1.144 + ++idx; 1.145 + return *this; 1.146 + } 1.147 + 1.148 + // ----------------------------------------- 1.149 + LineSplitter& operator++(int) { 1.150 + return ++(*this); 1.151 + } 1.152 + 1.153 + // ----------------------------------------- 1.154 + /** get a pointer to the beginning of a particular token */ 1.155 + const char* operator[] (size_t idx) const { 1.156 + const char* s = operator->()->c_str(); 1.157 + 1.158 + SkipSpaces(&s); 1.159 + for(size_t i = 0; i < idx; ++i) { 1.160 + 1.161 + for(;!IsSpace(*s); ++s) { 1.162 + if(IsLineEnd(*s)) { 1.163 + throw std::range_error("Token index out of range, EOL reached"); 1.164 + } 1.165 + } 1.166 + SkipSpaces(&s); 1.167 + } 1.168 + return s; 1.169 + } 1.170 + 1.171 + // ----------------------------------------- 1.172 + /** extract the start positions of N tokens from the current line*/ 1.173 + template <size_t N> 1.174 + void get_tokens(const char* (&tokens)[N]) const { 1.175 + const char* s = operator->()->c_str(); 1.176 + 1.177 + SkipSpaces(&s); 1.178 + for(size_t i = 0; i < N; ++i) { 1.179 + if(IsLineEnd(*s)) { 1.180 + throw std::range_error("Token count out of range, EOL reached"); 1.181 + } 1.182 + tokens[i] = s; 1.183 + 1.184 + for(;*s && !IsSpace(*s); ++s); 1.185 + SkipSpaces(&s); 1.186 + } 1.187 + } 1.188 + 1.189 + // ----------------------------------------- 1.190 + /** member access */ 1.191 + const std::string* operator -> () const { 1.192 + return &cur; 1.193 + } 1.194 + 1.195 + std::string operator* () const { 1.196 + return cur; 1.197 + } 1.198 + 1.199 + // ----------------------------------------- 1.200 + /** boolean context */ 1.201 + operator bool() const { 1.202 + return stream.GetRemainingSize()>0; 1.203 + } 1.204 + 1.205 + // ----------------------------------------- 1.206 + /** line indices are zero-based, empty lines are included */ 1.207 + operator line_idx() const { 1.208 + return idx; 1.209 + } 1.210 + 1.211 + line_idx get_index() const { 1.212 + return idx; 1.213 + } 1.214 + 1.215 + // ----------------------------------------- 1.216 + /** access the underlying stream object */ 1.217 + StreamReaderLE& get_stream() { 1.218 + return stream; 1.219 + } 1.220 + 1.221 + // ----------------------------------------- 1.222 + /** !strcmp((*this)->substr(0,strlen(check)),check) */ 1.223 + bool match_start(const char* check) { 1.224 + const size_t len = strlen(check); 1.225 + 1.226 + return len <= cur.length() && std::equal(check,check+len,cur.begin()); 1.227 + } 1.228 + 1.229 + 1.230 + // ----------------------------------------- 1.231 + /** swallow the next call to ++, return the previous value. */ 1.232 + void swallow_next_increment() { 1.233 + swallow = true; 1.234 + } 1.235 + 1.236 +private: 1.237 + 1.238 + line_idx idx; 1.239 + std::string cur; 1.240 + StreamReaderLE& stream; 1.241 + bool swallow, skip_empty_lines, trim; 1.242 +}; 1.243 + 1.244 +} 1.245 +#endif // INCLUDED_LINE_SPLITTER_H