2
0

ParsingUtils.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 ParsingUtils.h
  34. * @brief Defines helper functions for text parsing
  35. */
  36. #pragma once
  37. #ifndef AI_PARSING_UTILS_H_INC
  38. #define AI_PARSING_UTILS_H_INC
  39. #ifdef __GNUC__
  40. #pragma GCC system_header
  41. #endif
  42. #include <assimp/StringComparison.h>
  43. #include <assimp/StringUtils.h>
  44. #include <assimp/defs.h>
  45. #include <vector>
  46. #include <algorithm>
  47. namespace Assimp {
  48. /// NOTE: the functions below are mostly intended as replacement for
  49. /// std::upper, std::lower, std::isupper, std::islower, std::isspace.
  50. /// we don't bother of locales. We don't want them. We want reliable
  51. /// (i.e. identical) results across all locales, because we had a lot
  52. /// of issues in the past .
  53. /// The functions below accept any character type, but know only
  54. /// about ASCII. However, UTF-32 is the only safe ASCII superset to
  55. /// use since it doesn't have multi-byte sequences.
  56. static constexpr unsigned int BufferSize = 4096;
  57. // ---------------------------------------------------------------------------------
  58. /// @brief Returns true, if the character is upper-case.
  59. /// @param in The character to test.
  60. /// @return true if upper-case, false if not.
  61. template <class char_t>
  62. AI_FORCE_INLINE bool IsUpper(char_t in) {
  63. return (in >= (char_t)'A' && in <= (char_t)'Z');
  64. }
  65. // ---------------------------------------------------------------------------------
  66. /// @brief Returns true, if the character is lower-case.
  67. /// @param in The character to test.
  68. /// @return true if lower-case, false if not.
  69. template <class char_t>
  70. AI_FORCE_INLINE bool IsLower(char_t in) {
  71. return (in >= (char_t)'a' && in <= (char_t)'z');
  72. }
  73. // ---------------------------------------------------------------------------------
  74. /// @brief Returns true, if the character is a space.
  75. /// @param in The character to test.
  76. /// @return true if a space, false if not.
  77. template <class char_t>
  78. AI_FORCE_INLINE bool IsSpace(char_t in) {
  79. return (in == (char_t)' ' || in == (char_t)'\t');
  80. }
  81. // ---------------------------------------------------------------------------------
  82. /// @brief Returns true, if the character is a line end.
  83. /// @param in The character to test.
  84. /// @return true if a line end, false if not.
  85. template <class char_t>
  86. AI_FORCE_INLINE bool IsLineEnd(char_t in) {
  87. return (in == (char_t)'\r' || in == (char_t)'\n' || in == (char_t)'\0' || in == (char_t)'\f');
  88. }
  89. // ---------------------------------------------------------------------------------
  90. /// @brief Returns true, if the character is a space or a line end.
  91. /// @param in The character to test.
  92. /// @return true if a space or a line end, false if not.
  93. template <class char_t>
  94. AI_FORCE_INLINE bool IsSpaceOrNewLine(char_t in) {
  95. return IsSpace<char_t>(in) || IsLineEnd<char_t>(in);
  96. }
  97. // ---------------------------------------------------------------------------------
  98. /// @brief Will skip all spaces in a buffer.
  99. /// @param in The incoming buffer.
  100. /// @param out The buffer with skipped data.
  101. /// @param end The end of the buffer.
  102. /// @return true if valid.
  103. template <class char_t>
  104. AI_FORCE_INLINE bool SkipSpaces(const char_t *in, const char_t **out, const char_t *end) {
  105. while ((*in == (char_t)' ' || *in == (char_t)'\t') && in != end) {
  106. ++in;
  107. }
  108. *out = in;
  109. return !IsLineEnd<char_t>(*in);
  110. }
  111. // ---------------------------------------------------------------------------------
  112. /// @brief Will skip all spaces in a buffer in-situ.
  113. /// @param inout The in/out buffer.
  114. /// @param end The end of the buffer.
  115. /// @return true if valid.
  116. template <class char_t>
  117. AI_FORCE_INLINE bool SkipSpaces(const char_t **inout, const char_t *end) {
  118. return SkipSpaces<char_t>(*inout, inout, end);
  119. }
  120. // ---------------------------------------------------------------------------------
  121. /// @brief Will skip a line.
  122. /// @param in The incoming buffer.
  123. /// @param out The buffer with skipped data.
  124. /// @param end The end of the buffer.
  125. /// @return true if valid.
  126. template <class char_t>
  127. AI_FORCE_INLINE bool SkipLine(const char_t *in, const char_t **out, const char_t *end) {
  128. while ((*in != (char_t)'\r' && *in != (char_t)'\n' && *in != (char_t)'\0') && *in != (char_t)'#' && in != end) {
  129. ++in;
  130. }
  131. // files are opened in binary mode. Ergo there are both NL and CR
  132. while ((*in == (char_t)'\r' || *in == (char_t)'\n') && in != end) {
  133. ++in;
  134. }
  135. *out = in;
  136. return *in != (char_t)'\0';
  137. }
  138. // ---------------------------------------------------------------------------------
  139. /// @brief Will skip a line in-situ.
  140. /// @param in The in/out buffer.
  141. /// @param end The end of the buffer.
  142. /// @return true if valid.
  143. template <class char_t>
  144. AI_FORCE_INLINE bool SkipLine(const char_t **inout, const char_t *end) {
  145. return SkipLine<char_t>(*inout, inout, end);
  146. }
  147. // ---------------------------------------------------------------------------------
  148. /// @brief Returns true, if the character is a space or a line end.
  149. /// @param in The character to test.
  150. /// @param out The buffer with the skipped data.
  151. /// @return true if valid.
  152. template <class char_t>
  153. AI_FORCE_INLINE bool SkipSpacesAndLineEnd(const char_t *in, const char_t **out, const char_t *end) {
  154. while ((*in == (char_t)' ' || *in == (char_t)'\t' || *in == (char_t)'\r' || *in == (char_t)'\n') && in != end) {
  155. ++in;
  156. }
  157. *out = in;
  158. return *in != '\0';
  159. }
  160. // ---------------------------------------------------------------------------------
  161. /// @brief Returns true, if the character is a space or a line end.
  162. /// @param in The character to test.
  163. /// @param out The buffer with the skipped data.
  164. /// @return true if valid.
  165. template <class char_t>
  166. AI_FORCE_INLINE bool SkipSpacesAndLineEnd(const char_t **inout, const char_t *end) {
  167. return SkipSpacesAndLineEnd<char_t>(*inout, inout, end);
  168. }
  169. // ---------------------------------------------------------------------------------
  170. /// @brief Will return point showing to the next line.
  171. /// @param buffer The in buffer.
  172. /// @param out The next line.
  173. /// @return true if a new lne was found, else false.
  174. template <class char_t>
  175. AI_FORCE_INLINE bool GetNextLine(const char_t *&buffer, char_t out[BufferSize]) {
  176. if ((char_t)'\0' == *buffer) {
  177. return false;
  178. }
  179. char *_out = out;
  180. char *const end = _out + BufferSize - 1;
  181. while (!IsLineEnd(*buffer) && _out < end) {
  182. *_out++ = *buffer++;
  183. }
  184. *_out = (char_t)'\0';
  185. while (IsLineEnd(*buffer) && '\0' != *buffer && buffer != end) {
  186. ++buffer;
  187. }
  188. return true;
  189. }
  190. // ---------------------------------------------------------------------------------
  191. /// @brief Returns true, if the character is a number.
  192. /// @param in The character to test.
  193. /// @return true if a number, false if not.
  194. template <class char_t>
  195. AI_FORCE_INLINE bool IsNumeric(char_t in) {
  196. return (in >= '0' && in <= '9') || '-' == in || '+' == in;
  197. }
  198. // ---------------------------------------------------------------------------------
  199. /// @brief Will check an incoming buffer for a given token.
  200. /// @param in The incoming buffer.
  201. /// @param token The token to check for.
  202. /// @param len the buffer length.
  203. /// @return true if token was found, false if not.
  204. template <class char_t>
  205. AI_FORCE_INLINE bool TokenMatch(char_t *&in, const char *token, unsigned int len) {
  206. if (!::strncmp(token, in, len) && IsSpaceOrNewLine(in[len])) {
  207. if (in[len] != '\0') {
  208. in += len + 1;
  209. } else {
  210. // If EOF after the token make sure we don't go past end of buffer
  211. in += len;
  212. }
  213. return true;
  214. }
  215. return false;
  216. }
  217. // ---------------------------------------------------------------------------------
  218. /// @brief Case-ignoring version of TokenMatch
  219. /// @param in Input
  220. /// @param token Token to check for
  221. /// @param len Number of characters to check
  222. /// @return true if token was found, false if not.
  223. AI_FORCE_INLINE bool TokenMatchI(const char *&in, const char *token, unsigned int len) {
  224. if (!ASSIMP_strincmp(token, in, len) && IsSpaceOrNewLine(in[len])) {
  225. in += len + 1;
  226. return true;
  227. }
  228. return false;
  229. }
  230. // ---------------------------------------------------------------------------------
  231. /// @brief Will skip the next token.
  232. /// @param in The incoming buffer.
  233. /// @param end The end marker of the buffer.
  234. AI_FORCE_INLINE void SkipToken(const char *&in, const char *end) {
  235. SkipSpaces(&in, end);
  236. while (!IsSpaceOrNewLine(*in)) {
  237. ++in;
  238. }
  239. }
  240. // ---------------------------------------------------------------------------------
  241. /// @brief Will return the next token as a string.
  242. /// @param in The incoming buffer.
  243. /// @param end The end marker of the buffer.
  244. /// @return The next token.
  245. AI_FORCE_INLINE std::string GetNextToken(const char *&in, const char *end) {
  246. SkipSpacesAndLineEnd(&in, end);
  247. const char *cur = in;
  248. while (!IsSpaceOrNewLine(*in)) {
  249. ++in;
  250. }
  251. return std::string(cur, (size_t)(in - cur));
  252. }
  253. // ---------------------------------------------------------------------------------
  254. /// @brief Will perform a simple tokenize.
  255. /// @param str String to tokenize.
  256. /// @param tokens Array with tokens, will be empty if no token was found.
  257. /// @param delimiters Delimiter for tokenize.
  258. /// @return Number of found token.
  259. template <class string_type>
  260. AI_FORCE_INLINE unsigned int tokenize(const string_type &str, std::vector<string_type> &tokens,
  261. const string_type &delimiters) {
  262. // Skip delimiters at beginning.
  263. typename string_type::size_type lastPos = str.find_first_not_of(delimiters, 0);
  264. // Find first "non-delimiter".
  265. typename string_type::size_type pos = str.find_first_of(delimiters, lastPos);
  266. while (string_type::npos != pos || string_type::npos != lastPos) {
  267. // Found a token, add it to the vector.
  268. string_type tmp = str.substr(lastPos, pos - lastPos);
  269. if (!tmp.empty() && ' ' != tmp[0])
  270. tokens.push_back(tmp);
  271. // Skip delimiters. Note the "not_of"
  272. lastPos = str.find_first_not_of(delimiters, pos);
  273. // Find next "non-delimiter"
  274. pos = str.find_first_of(delimiters, lastPos);
  275. }
  276. return static_cast<unsigned int>(tokens.size());
  277. }
  278. // ---------------------------------------------------------------------------------
  279. /// @brief Will convert the given string to lowercase with stl-strings.
  280. /// @param str The stl-string to convert.
  281. /// @return The lowercase string as a stl-string.
  282. inline std::string ai_stdStrToLower(const std::string &str) {
  283. std::string out(str);
  284. for (size_t i = 0; i < str.size(); ++i) {
  285. out[i] = (char) tolower((unsigned char)out[i]);
  286. }
  287. return out;
  288. }
  289. } // namespace Assimp
  290. #endif // ! AI_PARSING_UTILS_H_INC