LineSplitter.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. /// The current line index in the data block.
  67. using line_idx = size_t;
  68. // -----------------------------------------
  69. /// @brief The class constructor.
  70. /// @note trim is *always* assumed true if skyp_empty_lines==true
  71. LineSplitter(StreamReaderLE& stream, bool skip_empty_lines = true, bool trim = true);
  72. // -----------------------------------------
  73. /// @brief The class destructor.
  74. ~LineSplitter() = default;
  75. // -----------------------------------------
  76. /// @brief pseudo-iterator increment
  77. LineSplitter& operator++();
  78. // -----------------------------------------
  79. /// @brief pseudo-iterator increment
  80. LineSplitter& operator++(int);
  81. // -----------------------------------------
  82. /// @brief Get a pointer to the beginning of a particular token.
  83. /// @param idx The index into the token.
  84. /// @return The token.
  85. const char* operator[] (size_t idx) const;
  86. // -----------------------------------------
  87. /** extract the start positions of N tokens from the current line*/
  88. template <size_t N>
  89. void get_tokens(const char* (&tokens)[N]) const;
  90. // -----------------------------------------
  91. /// member access via -> operator.
  92. const std::string* operator -> () const;
  93. // -----------------------------------------
  94. /// member access via * operator.
  95. std::string operator* () const;
  96. /// @brief Will return the end marker, end of the buffer plus one.
  97. /// @return The end pointer marker.
  98. const char *getEnd() const;
  99. // -----------------------------------------
  100. /// boolean context.
  101. operator bool() const;
  102. // -----------------------------------------
  103. /// line indices are zero-based, empty lines are included
  104. operator line_idx() const;
  105. /// @brief Will return the current index.
  106. /// @return The current index.
  107. line_idx get_index() const;
  108. // -----------------------------------------
  109. /// @brief Access the underlying stream object.
  110. /// @return Reference to the stream reader.
  111. StreamReaderLE& get_stream();
  112. // -----------------------------------------
  113. /// !strcmp((*this)->substr(0,strlen(check)),check)
  114. /// @return true if token matches.
  115. bool match_start(const char* check);
  116. // -----------------------------------------
  117. /// @brief Swallow the next call to ++, return the previous value.
  118. void swallow_next_increment();
  119. LineSplitter( const LineSplitter & ) = delete;
  120. LineSplitter(LineSplitter &&) = delete;
  121. LineSplitter &operator = ( const LineSplitter & ) = delete;
  122. private:
  123. line_idx mIdx{0};
  124. std::string mCur{};
  125. const char *mEnd{nullptr};
  126. StreamReaderLE &mStream;
  127. bool mSwallow{ false };
  128. bool mSkip_empty_lines{ false };1
  129. bool mTrim{ false };
  130. };
  131. AI_FORCE_INLINE LineSplitter::LineSplitter(StreamReaderLE& stream, bool skip_empty_lines, bool trim ) :
  132. mStream(stream),
  133. mSkip_empty_lines(skip_empty_lines),
  134. mTrim(trim) {
  135. mCur.reserve(1024);
  136. mEnd = mCur.c_str() + 1024;
  137. operator++();
  138. mIdx = 0;
  139. }
  140. AI_FORCE_INLINE LineSplitter& LineSplitter::operator++() {
  141. if (mSwallow) {
  142. mSwallow = false;
  143. return *this;
  144. }
  145. if (!*this) {
  146. throw std::logic_error("End of file, no more lines to be retrieved.");
  147. }
  148. char s;
  149. mCur.clear();
  150. while (mStream.GetRemainingSize() && (s = mStream.GetI1(), 1)) {
  151. if (s == '\n' || s == '\r') {
  152. if (mSkip_empty_lines) {
  153. while (mStream.GetRemainingSize() && ((s = mStream.GetI1()) == ' ' || s == '\r' || s == '\n'));
  154. if (mStream.GetRemainingSize()) {
  155. mStream.IncPtr(-1);
  156. }
  157. } else {
  158. // skip both potential line terminators but don't read past this line.
  159. if (mStream.GetRemainingSize() && (s == '\r' && mStream.GetI1() != '\n')) {
  160. mStream.IncPtr(-1);
  161. }
  162. if (mTrim) {
  163. while (mStream.GetRemainingSize() && ((s = mStream.GetI1()) == ' ' || s == '\t'));
  164. if (mStream.GetRemainingSize()) {
  165. mStream.IncPtr(-1);
  166. }
  167. }
  168. }
  169. break;
  170. }
  171. mCur += s;
  172. }
  173. ++mIdx;
  174. return *this;
  175. }
  176. AI_FORCE_INLINE LineSplitter &LineSplitter::operator++(int) {
  177. return ++(*this);
  178. }
  179. AI_FORCE_INLINE const char *LineSplitter::operator[] (size_t idx) const {
  180. const char* s = operator->()->c_str();
  181. SkipSpaces(&s, mEnd);
  182. for (size_t i = 0; i < idx; ++i) {
  183. for (; !IsSpace(*s); ++s) {
  184. if (IsLineEnd(*s)) {
  185. throw std::range_error("Token index out of range, EOL reached");
  186. }
  187. }
  188. SkipSpaces(&s, mEnd);
  189. }
  190. return s;
  191. }
  192. template <size_t N>
  193. AI_FORCE_INLINE void LineSplitter::get_tokens(const char* (&tokens)[N]) const {
  194. const char* s = operator->()->c_str();
  195. SkipSpaces(&s, mEnd);
  196. for (size_t i = 0; i < N; ++i) {
  197. if (IsLineEnd(*s)) {
  198. throw std::range_error("Token count out of range, EOL reached");
  199. }
  200. tokens[i] = s;
  201. for (; *s && !IsSpace(*s); ++s);
  202. SkipSpaces(&s, mEnd);
  203. }
  204. }
  205. AI_FORCE_INLINE const std::string* LineSplitter::operator -> () const {
  206. return &mCur;
  207. }
  208. AI_FORCE_INLINE std::string LineSplitter::operator* () const {
  209. return mCur;
  210. }
  211. AI_FORCE_INLINE const char* LineSplitter::getEnd() const {
  212. return mEnd;
  213. }
  214. AI_FORCE_INLINE LineSplitter::operator bool() const {
  215. return mStream.GetRemainingSize() > 0;
  216. }
  217. AI_FORCE_INLINE LineSplitter::operator line_idx() const {
  218. return mIdx;
  219. }
  220. AI_FORCE_INLINE LineSplitter::line_idx LineSplitter::get_index() const {
  221. return mIdx;
  222. }
  223. AI_FORCE_INLINE StreamReaderLE &LineSplitter::get_stream() {
  224. return mStream;
  225. }
  226. AI_FORCE_INLINE bool LineSplitter::match_start(const char* check) {
  227. const size_t len = ::strlen(check);
  228. return len <= mCur.length() && std::equal(check, check + len, mCur.begin());
  229. }
  230. AI_FORCE_INLINE void LineSplitter::swallow_next_increment() {
  231. mSwallow = true;
  232. }
  233. } // Namespace Assimp
  234. #endif // INCLUDED_LINE_SPLITTER_H