Path Tracer
ParsingUtils.h
Go to the documentation of this file.
1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
4 
5 Copyright (c) 2006-2020, assimp team
6 
7 All rights reserved.
8 
9 Redistribution and use of this software in source and binary forms,
10 with or without modification, are permitted provided that the
11 following conditions are met:
12 
13 * Redistributions of source code must retain the above
14  copyright notice, this list of conditions and the
15  following disclaimer.
16 
17 * Redistributions in binary form must reproduce the above
18  copyright notice, this list of conditions and the
19  following disclaimer in the documentation and/or other
20  materials provided with the distribution.
21 
22 * Neither the name of the assimp team, nor the names of its
23  contributors may be used to endorse or promote products
24  derived from this software without specific prior
25  written permission of the assimp team.
26 
27 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 
39 ----------------------------------------------------------------------
40 */
41 
45 #pragma once
46 #ifndef AI_PARSING_UTILS_H_INC
47 #define AI_PARSING_UTILS_H_INC
48 
49 #ifdef __GNUC__
50 #pragma GCC system_header
51 #endif
52 
53 #include <assimp/StringComparison.h>
54 #include <assimp/StringUtils.h>
55 #include <assimp/defs.h>
56 #include <vector>
57 
58 namespace Assimp {
59 
60 // NOTE: the functions below are mostly intended as replacement for
61 // std::upper, std::lower, std::isupper, std::islower, std::isspace.
62 // we don't bother of locales. We don't want them. We want reliable
63 // (i.e. identical) results across all locales.
64 
65 // The functions below accept any character type, but know only
66 // about ASCII. However, UTF-32 is the only safe ASCII superset to
67 // use since it doesn't have multi-byte sequences.
68 
69 static const unsigned int BufferSize = 4096;
70 
71 // ---------------------------------------------------------------------------------
72 template <class char_t>
73 AI_FORCE_INLINE char_t ToLower(char_t in) {
74  return (in >= (char_t)'A' && in <= (char_t)'Z') ? (char_t)(in + 0x20) : in;
75 }
76 
77 // ---------------------------------------------------------------------------------
78 template <class char_t>
79 AI_FORCE_INLINE char_t ToUpper(char_t in) {
80  return (in >= (char_t)'a' && in <= (char_t)'z') ? (char_t)(in - 0x20) : in;
81 }
82 
83 // ---------------------------------------------------------------------------------
84 template <class char_t>
85 AI_FORCE_INLINE bool IsUpper(char_t in) {
86  return (in >= (char_t)'A' && in <= (char_t)'Z');
87 }
88 
89 // ---------------------------------------------------------------------------------
90 template <class char_t>
91 AI_FORCE_INLINE bool IsLower(char_t in) {
92  return (in >= (char_t)'a' && in <= (char_t)'z');
93 }
94 
95 // ---------------------------------------------------------------------------------
96 template <class char_t>
97 AI_FORCE_INLINE bool IsSpace(char_t in) {
98  return (in == (char_t)' ' || in == (char_t)'\t');
99 }
100 
101 // ---------------------------------------------------------------------------------
102 template <class char_t>
103 AI_FORCE_INLINE bool IsLineEnd(char_t in) {
104  return (in == (char_t)'\r' || in == (char_t)'\n' || in == (char_t)'\0' || in == (char_t)'\f');
105 }
106 
107 // ---------------------------------------------------------------------------------
108 template <class char_t>
109 AI_FORCE_INLINE bool IsSpaceOrNewLine(char_t in) {
110  return IsSpace<char_t>(in) || IsLineEnd<char_t>(in);
111 }
112 
113 // ---------------------------------------------------------------------------------
114 template <class char_t>
115 AI_FORCE_INLINE bool SkipSpaces(const char_t *in, const char_t **out) {
116  while (*in == (char_t)' ' || *in == (char_t)'\t') {
117  ++in;
118  }
119  *out = in;
120  return !IsLineEnd<char_t>(*in);
121 }
122 
123 // ---------------------------------------------------------------------------------
124 template <class char_t>
125 AI_FORCE_INLINE bool SkipSpaces(const char_t **inout) {
126  return SkipSpaces<char_t>(*inout, inout);
127 }
128 
129 // ---------------------------------------------------------------------------------
130 template <class char_t>
131 AI_FORCE_INLINE bool SkipLine(const char_t *in, const char_t **out) {
132  while (*in != (char_t)'\r' && *in != (char_t)'\n' && *in != (char_t)'\0') {
133  ++in;
134  }
135 
136  // files are opened in binary mode. Ergo there are both NL and CR
137  while (*in == (char_t)'\r' || *in == (char_t)'\n') {
138  ++in;
139  }
140  *out = in;
141  return *in != (char_t)'\0';
142 }
143 
144 // ---------------------------------------------------------------------------------
145 template <class char_t>
146 AI_FORCE_INLINE bool SkipLine(const char_t **inout) {
147  return SkipLine<char_t>(*inout, inout);
148 }
149 
150 // ---------------------------------------------------------------------------------
151 template <class char_t>
152 AI_FORCE_INLINE bool SkipSpacesAndLineEnd(const char_t *in, const char_t **out) {
153  while (*in == (char_t)' ' || *in == (char_t)'\t' || *in == (char_t)'\r' || *in == (char_t)'\n') {
154  ++in;
155  }
156  *out = in;
157  return *in != '\0';
158 }
159 
160 // ---------------------------------------------------------------------------------
161 template <class char_t>
162 AI_FORCE_INLINE bool SkipSpacesAndLineEnd(const char_t **inout) {
163  return SkipSpacesAndLineEnd<char_t>(*inout, inout);
164 }
165 
166 // ---------------------------------------------------------------------------------
167 template <class char_t>
168 AI_FORCE_INLINE bool GetNextLine(const char_t *&buffer, char_t out[BufferSize]) {
169  if ((char_t)'\0' == *buffer) {
170  return false;
171  }
172 
173  char *_out = out;
174  char *const end = _out + BufferSize;
175  while (!IsLineEnd(*buffer) && _out < end) {
176  *_out++ = *buffer++;
177  }
178  *_out = (char_t)'\0';
179 
180  while (IsLineEnd(*buffer) && '\0' != *buffer) {
181  ++buffer;
182  }
183 
184  return true;
185 }
186 
187 // ---------------------------------------------------------------------------------
188 template <class char_t>
189 AI_FORCE_INLINE bool IsNumeric(char_t in) {
190  return (in >= '0' && in <= '9') || '-' == in || '+' == in;
191 }
192 
193 // ---------------------------------------------------------------------------------
194 template <class char_t>
195 AI_FORCE_INLINE bool TokenMatch(char_t *&in, const char *token, unsigned int len) {
196  if (!::strncmp(token, in, len) && IsSpaceOrNewLine(in[len])) {
197  if (in[len] != '\0') {
198  in += len + 1;
199  } else {
200  // If EOF after the token make sure we don't go past end of buffer
201  in += len;
202  }
203  return true;
204  }
205 
206  return false;
207 }
208 // ---------------------------------------------------------------------------------
214 AI_FORCE_INLINE bool TokenMatchI(const char *&in, const char *token, unsigned int len) {
215  if (!ASSIMP_strincmp(token, in, len) && IsSpaceOrNewLine(in[len])) {
216  in += len + 1;
217  return true;
218  }
219  return false;
220 }
221 
222 // ---------------------------------------------------------------------------------
223 AI_FORCE_INLINE void SkipToken(const char *&in) {
224  SkipSpaces(&in);
225  while (!IsSpaceOrNewLine(*in)) {
226  ++in;
227  }
228 }
229 
230 // ---------------------------------------------------------------------------------
231 AI_FORCE_INLINE std::string GetNextToken(const char *&in) {
232  SkipSpacesAndLineEnd(&in);
233  const char *cur = in;
234  while (!IsSpaceOrNewLine(*in)) {
235  ++in;
236  }
237  return std::string(cur, (size_t)(in - cur));
238 }
239 
240 // ---------------------------------------------------------------------------------
247 template <class string_type>
248 AI_FORCE_INLINE unsigned int tokenize(const string_type &str, std::vector<string_type> &tokens,
249  const string_type &delimiters) {
250  // Skip delimiters at beginning.
251  typename string_type::size_type lastPos = str.find_first_not_of(delimiters, 0);
252 
253  // Find first "non-delimiter".
254  typename string_type::size_type pos = str.find_first_of(delimiters, lastPos);
255  while (string_type::npos != pos || string_type::npos != lastPos) {
256  // Found a token, add it to the vector.
257  string_type tmp = str.substr(lastPos, pos - lastPos);
258  if (!tmp.empty() && ' ' != tmp[0])
259  tokens.push_back(tmp);
260 
261  // Skip delimiters. Note the "not_of"
262  lastPos = str.find_first_not_of(delimiters, pos);
263 
264  // Find next "non-delimiter"
265  pos = str.find_first_of(delimiters, lastPos);
266  }
267 
268  return static_cast<unsigned int>(tokens.size());
269 }
270 
271 inline std::string ai_stdStrToLower(const std::string &str) {
272  std::string out(str);
273  for (size_t i = 0; i < str.size(); ++i) {
274  out[i] = (char) tolower(out[i]);
275  }
276  return out;
277 }
278 
279 } // namespace Assimp
280 
281 #endif // ! AI_PARSING_UTILS_H_INC
Assimp::ASSIMP_strincmp
AI_FORCE_INLINE int ASSIMP_strincmp(const char *s1, const char *s2, unsigned int n)
Helper function to do platform independent string comparison.
Definition: StringComparison.h:188
Assimp::tokenize
AI_FORCE_INLINE unsigned int tokenize(const string_type &str, std::vector< string_type > &tokens, const string_type &delimiters)
Will perform a simple tokenize.
Definition: ParsingUtils.h:248
defs.h
Assimp build configuration setup. See the notes in the comment blocks to find out how to customize yo...
Assimp
Definition: ai_assert.h:50
Assimp::TokenMatchI
AI_FORCE_INLINE bool TokenMatchI(const char *&in, const char *token, unsigned int len)
Case-ignoring version of TokenMatch.
Definition: ParsingUtils.h:214