LineSplitter.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, 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. #ifdef __GNUC__
  41. # pragma GCC system_header
  42. #endif
  43. #include <stdexcept>
  44. #include <assimp/StreamReader.h>
  45. #include <assimp/ParsingUtils.h>
  46. namespace Assimp {
  47. // ------------------------------------------------------------------------------------------------
  48. /** Usage:
  49. @code
  50. for(LineSplitter splitter(stream);splitter;++splitter) {
  51. if (*splitter == "hi!") {
  52. ...
  53. }
  54. else if (splitter->substr(0,5) == "hello") {
  55. ...
  56. // access the third token in the line (tokens are space-separated)
  57. if (strtol(splitter[2]) > 5) { .. }
  58. }
  59. ASSIMP_LOG_VERBOSE_DEBUG("Current line is: ", splitter.get_index());
  60. }
  61. @endcode
  62. */
  63. // ------------------------------------------------------------------------------------------------
  64. class LineSplitter {
  65. public:
  66. typedef size_t line_idx;
  67. // -----------------------------------------
  68. /** construct from existing stream reader
  69. note: trim is *always* assumed true if skyp_empty_lines==true
  70. */
  71. LineSplitter(StreamReaderLE& stream, bool skip_empty_lines = true, bool trim = true);
  72. ~LineSplitter() = default;
  73. // -----------------------------------------
  74. /** pseudo-iterator increment */
  75. LineSplitter& operator++();
  76. // -----------------------------------------
  77. LineSplitter& operator++(int);
  78. // -----------------------------------------
  79. /** get a pointer to the beginning of a particular token */
  80. const char* operator[] (size_t idx) const;
  81. // -----------------------------------------
  82. /** extract the start positions of N tokens from the current line*/
  83. template <size_t N>
  84. void get_tokens(const char* (&tokens)[N]) const;
  85. // -----------------------------------------
  86. /** member access */
  87. const std::string* operator -> () const;
  88. std::string operator* () const;
  89. const char *getEnd() const;
  90. // -----------------------------------------
  91. /** boolean context */
  92. operator bool() const;
  93. // -----------------------------------------
  94. /** line indices are zero-based, empty lines are included */
  95. operator line_idx() const;
  96. line_idx get_index() const;
  97. // -----------------------------------------
  98. /** access the underlying stream object */
  99. StreamReaderLE& get_stream();
  100. // -----------------------------------------
  101. /** !strcmp((*this)->substr(0,strlen(check)),check) */
  102. bool match_start(const char* check);
  103. // -----------------------------------------
  104. /** swallow the next call to ++, return the previous value. */
  105. void swallow_next_increment();
  106. LineSplitter( const LineSplitter & ) = delete;
  107. LineSplitter(LineSplitter &&) = delete;
  108. LineSplitter &operator = ( const LineSplitter & ) = delete;
  109. private:
  110. line_idx mIdx;
  111. std::string mCur;
  112. const char *mEnd;
  113. StreamReaderLE& mStream;
  114. bool mSwallow, mSkip_empty_lines, mTrim;
  115. };
  116. AI_FORCE_INLINE LineSplitter::LineSplitter(StreamReaderLE& stream, bool skip_empty_lines, bool trim ) :
  117. mIdx(0),
  118. mCur(),
  119. mEnd(nullptr),
  120. mStream(stream),
  121. mSwallow(),
  122. mSkip_empty_lines(skip_empty_lines),
  123. mTrim(trim) {
  124. mCur.reserve(1024);
  125. mEnd = mCur.c_str() + 1024;
  126. operator++();
  127. mIdx = 0;
  128. }
  129. AI_FORCE_INLINE LineSplitter& LineSplitter::operator++() {
  130. if (mSwallow) {
  131. mSwallow = false;
  132. return *this;
  133. }
  134. if (!*this) {
  135. throw std::logic_error("End of file, no more lines to be retrieved.");
  136. }
  137. char s;
  138. mCur.clear();
  139. while (mStream.GetRemainingSize() && (s = mStream.GetI1(), 1)) {
  140. if (s == '\n' || s == '\r') {
  141. if (mSkip_empty_lines) {
  142. while (mStream.GetRemainingSize() && ((s = mStream.GetI1()) == ' ' || s == '\r' || s == '\n'));
  143. if (mStream.GetRemainingSize()) {
  144. mStream.IncPtr(-1);
  145. }
  146. } else {
  147. // skip both potential line terminators but don't read past this line.
  148. if (mStream.GetRemainingSize() && (s == '\r' && mStream.GetI1() != '\n')) {
  149. mStream.IncPtr(-1);
  150. }
  151. if (mTrim) {
  152. while (mStream.GetRemainingSize() && ((s = mStream.GetI1()) == ' ' || s == '\t'));
  153. if (mStream.GetRemainingSize()) {
  154. mStream.IncPtr(-1);
  155. }
  156. }
  157. }
  158. break;
  159. }
  160. mCur += s;
  161. }
  162. ++mIdx;
  163. return *this;
  164. }
  165. AI_FORCE_INLINE LineSplitter &LineSplitter::operator++(int) {
  166. return ++(*this);
  167. }
  168. AI_FORCE_INLINE const char *LineSplitter::operator[] (size_t idx) const {
  169. const char* s = operator->()->c_str();
  170. SkipSpaces(&s, mEnd);
  171. for (size_t i = 0; i < idx; ++i) {
  172. for (; !IsSpace(*s); ++s) {
  173. if (IsLineEnd(*s)) {
  174. throw std::range_error("Token index out of range, EOL reached");
  175. }
  176. }
  177. SkipSpaces(&s, mEnd);
  178. }
  179. return s;
  180. }
  181. template <size_t N>
  182. AI_FORCE_INLINE void LineSplitter::get_tokens(const char* (&tokens)[N]) const {
  183. const char* s = operator->()->c_str();
  184. SkipSpaces(&s, mEnd);
  185. for (size_t i = 0; i < N; ++i) {
  186. if (IsLineEnd(*s)) {
  187. throw std::range_error("Token count out of range, EOL reached");
  188. }
  189. tokens[i] = s;
  190. for (; *s && !IsSpace(*s); ++s);
  191. SkipSpaces(&s, mEnd);
  192. }
  193. }
  194. AI_FORCE_INLINE const std::string* LineSplitter::operator -> () const {
  195. return &mCur;
  196. }
  197. AI_FORCE_INLINE std::string LineSplitter::operator* () const {
  198. return mCur;
  199. }
  200. AI_FORCE_INLINE const char* LineSplitter::getEnd() const {
  201. return mEnd;
  202. }
  203. AI_FORCE_INLINE LineSplitter::operator bool() const {
  204. return mStream.GetRemainingSize() > 0;
  205. }
  206. AI_FORCE_INLINE LineSplitter::operator line_idx() const {
  207. return mIdx;
  208. }
  209. AI_FORCE_INLINE LineSplitter::line_idx LineSplitter::get_index() const {
  210. return mIdx;
  211. }
  212. AI_FORCE_INLINE StreamReaderLE &LineSplitter::get_stream() {
  213. return mStream;
  214. }
  215. AI_FORCE_INLINE bool LineSplitter::match_start(const char* check) {
  216. const size_t len = ::strlen(check);
  217. return len <= mCur.length() && std::equal(check, check + len, mCur.begin());
  218. }
  219. AI_FORCE_INLINE void LineSplitter::swallow_next_increment() {
  220. mSwallow = true;
  221. }
  222. } // Namespace Assimp
  223. #endif // INCLUDED_LINE_SPLITTER_H