2
0

ParsingUtils.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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.
  52. // The functions below accept any character type, but know only
  53. // about ASCII. However, UTF-32 is the only safe ASCII superset to
  54. // use since it doesn't have multi-byte sequences.
  55. static const unsigned int BufferSize = 4096;
  56. // ---------------------------------------------------------------------------------
  57. template <class char_t>
  58. AI_FORCE_INLINE bool IsUpper(char_t in) {
  59. return (in >= (char_t)'A' && in <= (char_t)'Z');
  60. }
  61. // ---------------------------------------------------------------------------------
  62. template <class char_t>
  63. AI_FORCE_INLINE bool IsLower(char_t in) {
  64. return (in >= (char_t)'a' && in <= (char_t)'z');
  65. }
  66. // ---------------------------------------------------------------------------------
  67. template <class char_t>
  68. AI_FORCE_INLINE bool IsSpace(char_t in) {
  69. return (in == (char_t)' ' || in == (char_t)'\t');
  70. }
  71. // ---------------------------------------------------------------------------------
  72. template <class char_t>
  73. AI_FORCE_INLINE bool IsLineEnd(char_t in) {
  74. return (in == (char_t)'\r' || in == (char_t)'\n' || in == (char_t)'\0' || in == (char_t)'\f');
  75. }
  76. // ---------------------------------------------------------------------------------
  77. template <class char_t>
  78. AI_FORCE_INLINE bool IsSpaceOrNewLine(char_t in) {
  79. return IsSpace<char_t>(in) || IsLineEnd<char_t>(in);
  80. }
  81. // ---------------------------------------------------------------------------------
  82. template <class char_t>
  83. AI_FORCE_INLINE bool SkipSpaces(const char_t *in, const char_t **out, const char_t *end) {
  84. while ((*in == (char_t)' ' || *in == (char_t)'\t') && in != end) {
  85. ++in;
  86. }
  87. *out = in;
  88. return !IsLineEnd<char_t>(*in);
  89. }
  90. // ---------------------------------------------------------------------------------
  91. template <class char_t>
  92. AI_FORCE_INLINE bool SkipSpaces(const char_t **inout, const char_t *end) {
  93. return SkipSpaces<char_t>(*inout, inout, end);
  94. }
  95. // ---------------------------------------------------------------------------------
  96. template <class char_t>
  97. AI_FORCE_INLINE bool SkipLine(const char_t *in, const char_t **out, const char_t *end) {
  98. while ((*in != (char_t)'\r' && *in != (char_t)'\n' && *in != (char_t)'\0') && *in != (char_t)'#' && in != end) {
  99. ++in;
  100. }
  101. // files are opened in binary mode. Ergo there are both NL and CR
  102. while ((*in == (char_t)'\r' || *in == (char_t)'\n') && in != end) {
  103. ++in;
  104. }
  105. *out = in;
  106. return *in != (char_t)'\0';
  107. }
  108. // ---------------------------------------------------------------------------------
  109. template <class char_t>
  110. AI_FORCE_INLINE bool SkipLine(const char_t **inout, const char_t *end) {
  111. return SkipLine<char_t>(*inout, inout, end);
  112. }
  113. // ---------------------------------------------------------------------------------
  114. template <class char_t>
  115. AI_FORCE_INLINE bool SkipSpacesAndLineEnd(const char_t *in, const char_t **out, const char_t *end) {
  116. while ((*in == (char_t)' ' || *in == (char_t)'\t' || *in == (char_t)'\r' || *in == (char_t)'\n') && in != end) {
  117. ++in;
  118. }
  119. *out = in;
  120. return *in != '\0';
  121. }
  122. // ---------------------------------------------------------------------------------
  123. template <class char_t>
  124. AI_FORCE_INLINE bool SkipSpacesAndLineEnd(const char_t **inout, const char_t *end) {
  125. return SkipSpacesAndLineEnd<char_t>(*inout, inout, end);
  126. }
  127. // ---------------------------------------------------------------------------------
  128. template <class char_t>
  129. AI_FORCE_INLINE bool GetNextLine(const char_t *&buffer, char_t out[BufferSize]) {
  130. if ((char_t)'\0' == *buffer) {
  131. return false;
  132. }
  133. char *_out = out;
  134. char *const end = _out + BufferSize - 1;
  135. while (!IsLineEnd(*buffer) && _out < end) {
  136. *_out++ = *buffer++;
  137. }
  138. *_out = (char_t)'\0';
  139. while (IsLineEnd(*buffer) && '\0' != *buffer && buffer != end) {
  140. ++buffer;
  141. }
  142. return true;
  143. }
  144. // ---------------------------------------------------------------------------------
  145. template <class char_t>
  146. AI_FORCE_INLINE bool IsNumeric(char_t in) {
  147. return (in >= '0' && in <= '9') || '-' == in || '+' == in;
  148. }
  149. // ---------------------------------------------------------------------------------
  150. template <class char_t>
  151. AI_FORCE_INLINE bool TokenMatch(char_t *&in, const char *token, unsigned int len) {
  152. if (!::strncmp(token, in, len) && IsSpaceOrNewLine(in[len])) {
  153. if (in[len] != '\0') {
  154. in += len + 1;
  155. } else {
  156. // If EOF after the token make sure we don't go past end of buffer
  157. in += len;
  158. }
  159. return true;
  160. }
  161. return false;
  162. }
  163. // ---------------------------------------------------------------------------------
  164. /** @brief Case-ignoring version of TokenMatch
  165. * @param in Input
  166. * @param token Token to check for
  167. * @param len Number of characters to check
  168. */
  169. AI_FORCE_INLINE bool TokenMatchI(const char *&in, const char *token, unsigned int len) {
  170. if (!ASSIMP_strincmp(token, in, len) && IsSpaceOrNewLine(in[len])) {
  171. in += len + 1;
  172. return true;
  173. }
  174. return false;
  175. }
  176. // ---------------------------------------------------------------------------------
  177. AI_FORCE_INLINE void SkipToken(const char *&in, const char *end) {
  178. SkipSpaces(&in, end);
  179. while (!IsSpaceOrNewLine(*in)) {
  180. ++in;
  181. }
  182. }
  183. // ---------------------------------------------------------------------------------
  184. AI_FORCE_INLINE std::string GetNextToken(const char *&in, const char *end) {
  185. SkipSpacesAndLineEnd(&in, end);
  186. const char *cur = in;
  187. while (!IsSpaceOrNewLine(*in)) {
  188. ++in;
  189. }
  190. return std::string(cur, (size_t)(in - cur));
  191. }
  192. // ---------------------------------------------------------------------------------
  193. /** @brief Will perform a simple tokenize.
  194. * @param str String to tokenize.
  195. * @param tokens Array with tokens, will be empty if no token was found.
  196. * @param delimiters Delimiter for tokenize.
  197. * @return Number of found token.
  198. */
  199. template <class string_type>
  200. AI_FORCE_INLINE unsigned int tokenize(const string_type &str, std::vector<string_type> &tokens,
  201. const string_type &delimiters) {
  202. // Skip delimiters at beginning.
  203. typename string_type::size_type lastPos = str.find_first_not_of(delimiters, 0);
  204. // Find first "non-delimiter".
  205. typename string_type::size_type pos = str.find_first_of(delimiters, lastPos);
  206. while (string_type::npos != pos || string_type::npos != lastPos) {
  207. // Found a token, add it to the vector.
  208. string_type tmp = str.substr(lastPos, pos - lastPos);
  209. if (!tmp.empty() && ' ' != tmp[0])
  210. tokens.push_back(tmp);
  211. // Skip delimiters. Note the "not_of"
  212. lastPos = str.find_first_not_of(delimiters, pos);
  213. // Find next "non-delimiter"
  214. pos = str.find_first_of(delimiters, lastPos);
  215. }
  216. return static_cast<unsigned int>(tokens.size());
  217. }
  218. inline std::string ai_stdStrToLower(const std::string &str) {
  219. std::string out(str);
  220. for (size_t i = 0; i < str.size(); ++i) {
  221. out[i] = (char) tolower((unsigned char)out[i]);
  222. }
  223. return out;
  224. }
  225. } // namespace Assimp
  226. #endif // ! AI_PARSING_UTILS_H_INC