vrshoot

view libs/assimp/ParsingUtils.h @ 0:b2f14e535253

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