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