LineSplitter.h 7.7 KB

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