No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

239 líneas
6.2 KiB

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