ParsingUtils.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 ParsingUtils.h
  34. * @brief Defines helper functions for text parsing
  35. */
  36. #ifndef AI_PARSING_UTILS_H_INC
  37. #define AI_PARSING_UTILS_H_INC
  38. #include "StringComparison.h"
  39. namespace Assimp {
  40. // NOTE: the functions below are mostly intended as replacement for
  41. // std::upper, std::lower, std::isupper, std::islower, std::isspace.
  42. // we don't bother of locales. We don't want them. We want reliable
  43. // (i.e. identical) results across all locales.
  44. // The functions below accept any character type, but know only
  45. // about ASCII. However, UTF-32 is the only safe ASCII superset to
  46. // use since it doesn't have multibyte sequences.
  47. // ---------------------------------------------------------------------------------
  48. template <class char_t>
  49. AI_FORCE_INLINE char_t ToLower( char_t in)
  50. {
  51. return (in >= (char_t)'A' && in <= (char_t)'Z') ? (char_t)(in+0x20) : in;
  52. }
  53. // ---------------------------------------------------------------------------------
  54. template <class char_t>
  55. AI_FORCE_INLINE char_t ToUpper( char_t in)
  56. {
  57. return (in >= (char_t)'a' && in <= (char_t)'z') ? (char_t)(in-0x20) : in;
  58. }
  59. // ---------------------------------------------------------------------------------
  60. template <class char_t>
  61. AI_FORCE_INLINE bool IsUpper( char_t in)
  62. {
  63. return (in >= (char_t)'A' && in <= (char_t)'Z');
  64. }
  65. // ---------------------------------------------------------------------------------
  66. template <class char_t>
  67. AI_FORCE_INLINE bool IsLower( char_t in)
  68. {
  69. return (in >= (char_t)'a' && in <= (char_t)'z');
  70. }
  71. // ---------------------------------------------------------------------------------
  72. template <class char_t>
  73. AI_FORCE_INLINE bool IsSpace( char_t in)
  74. {
  75. return (in == (char_t)' ' || in == (char_t)'\t');
  76. }
  77. // ---------------------------------------------------------------------------------
  78. template <class char_t>
  79. AI_FORCE_INLINE bool IsLineEnd( char_t in)
  80. {
  81. return (in == (char_t)'\r' || in == (char_t)'\n' || in == (char_t)'\0');
  82. }
  83. // ---------------------------------------------------------------------------------
  84. template <class char_t>
  85. AI_FORCE_INLINE bool IsSpaceOrNewLine( char_t in)
  86. {
  87. return IsSpace<char_t>(in) || IsLineEnd<char_t>(in);
  88. }
  89. // ---------------------------------------------------------------------------------
  90. template <class char_t>
  91. AI_FORCE_INLINE bool SkipSpaces( const char_t* in, const char_t** out)
  92. {
  93. while (*in == (char_t)' ' || *in == (char_t)'\t')in++;
  94. *out = in;
  95. return !IsLineEnd<char_t>(*in);
  96. }
  97. // ---------------------------------------------------------------------------------
  98. template <class char_t>
  99. AI_FORCE_INLINE bool SkipSpaces( const char_t** inout)
  100. {
  101. return SkipSpaces<char_t>(*inout,inout);
  102. }
  103. // ---------------------------------------------------------------------------------
  104. template <class char_t>
  105. inline bool SkipLine( const char_t* in, const char_t** out)
  106. {
  107. while (*in != (char_t)'\r' && *in != (char_t)'\n' && *in != (char_t)'\0')in++;
  108. // files are opened in binary mode. Ergo there are both NL and CR
  109. while (*in == (char_t)'\r' || *in == (char_t)'\n')in++;
  110. *out = in;
  111. return *in != (char_t)'\0';
  112. }
  113. // ---------------------------------------------------------------------------------
  114. template <class char_t>
  115. inline bool SkipLine( const char_t** inout)
  116. {
  117. return SkipLine<char_t>(*inout,inout);
  118. }
  119. // ---------------------------------------------------------------------------------
  120. template <class char_t>
  121. inline bool SkipSpacesAndLineEnd( const char_t* in, const char_t** out)
  122. {
  123. while (*in == (char_t)' ' || *in == (char_t)'\t' ||
  124. *in == (char_t)'\r' || *in == (char_t)'\n')in++;
  125. *out = in;
  126. return *in != '\0';
  127. }
  128. // ---------------------------------------------------------------------------------
  129. template <class char_t>
  130. inline bool SkipSpacesAndLineEnd( const char_t** inout)
  131. {
  132. return SkipSpacesAndLineEnd<char_t>(*inout,inout);
  133. }
  134. // ---------------------------------------------------------------------------------
  135. template <class char_t>
  136. inline bool GetNextLine(const char_t*& buffer, char_t out[4096])
  137. {
  138. if ((char_t)'\0' == *buffer)return false;
  139. char* _out = out;
  140. char* const end = _out+4096;
  141. while (!IsLineEnd( *buffer ) && _out < end)
  142. *_out++ = *buffer++;
  143. *_out = (char_t)'\0';
  144. while (IsLineEnd( *buffer ) && '\0' != *buffer)++buffer;
  145. return true;
  146. }
  147. // ---------------------------------------------------------------------------------
  148. template <class char_t>
  149. AI_FORCE_INLINE bool IsNumeric( char_t in)
  150. {
  151. return ( in >= '0' && in <= '9' ) || '-' == in || '+' == in;
  152. }
  153. // ---------------------------------------------------------------------------------
  154. template <class char_t>
  155. AI_FORCE_INLINE bool TokenMatch(char_t*& in, const char* token, unsigned int len)
  156. {
  157. if (!::strncmp(token,in,len) && IsSpaceOrNewLine(in[len]))
  158. {
  159. in += len+1;
  160. return true;
  161. }
  162. return false;
  163. }
  164. // ---------------------------------------------------------------------------------
  165. /** @brief Case-ignoring version of TokenMatch
  166. * @param in Input
  167. * @param token Token to check for
  168. * @param len Number of characters to check
  169. */
  170. AI_FORCE_INLINE bool TokenMatchI(const char*& in, const char* token, unsigned int len)
  171. {
  172. if (!ASSIMP_strincmp(token,in,len) && IsSpaceOrNewLine(in[len]))
  173. {
  174. in += len+1;
  175. return true;
  176. }
  177. return false;
  178. }
  179. // ---------------------------------------------------------------------------------
  180. AI_FORCE_INLINE void SkipToken(const char*& in)
  181. {
  182. SkipSpaces(&in);
  183. while (!IsSpaceOrNewLine(*in))++in;
  184. }
  185. // ---------------------------------------------------------------------------------
  186. AI_FORCE_INLINE std::string GetNextToken(const char*& in)
  187. {
  188. SkipSpacesAndLineEnd(&in);
  189. const char* cur = in;
  190. while (!IsSpaceOrNewLine(*in))++in;
  191. return std::string(cur,(size_t)(in-cur));
  192. }
  193. } // ! namespace Assimp
  194. #endif // ! AI_PARSING_UTILS_H_INC