1 /*-----------------------------------------------------------------------------------------------
2 The MIT License (MIT)
3 
4 Copyright (c) 2014-2015 Kim Kulling
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy of
7 this software and associated documentation files (the "Software"), to deal in
8 the Software without restriction, including without limitation the rights to
9 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10 the Software, and to permit persons to whom the Software is furnished to do so,
11 subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 -----------------------------------------------------------------------------------------------*/
23 #pragma once
24 
25 #include <openddlparser/OpenDDLCommon.h>
26 
27 BEGIN_ODDLPARSER_NS
28 
29 template<class T>
30 inline
isUpperCase(T in)31 bool isUpperCase( T in ) {
32     return ( in >= 'A' && in <= 'Z' );
33 }
34 
35 template<class T>
36 inline
isLowerCase(T in)37 bool isLowerCase( T in ) {
38     return ( in >= 'a' && in <= 'z' );
39 }
40 
41 template<class T>
42 inline
isSpace(const T in)43 bool isSpace( const T in ) {
44     return ( ' ' == in || '\t' == in );
45 }
46 
47 template<class T>
48 inline
isNewLine(const T in)49 bool isNewLine( const T in ) {
50     return ( '\n' == in || ( '\r' == in ) );
51 }
52 
53 template<class T>
54 inline
isSeparator(T in)55 bool isSeparator( T in ) {
56     if( isSpace( in ) || ',' == in || '{' == in || '}' == in || '[' == in || '(' == in || ')' == in ) {
57         return true;
58     }
59     return false;
60 }
61 
62 template<class T>
63 inline
isNumeric(const T in)64 bool isNumeric( const T in ) {
65     return ( in >= '0' && in <= '9' );
66 }
67 
68 template<class T>
69 inline
isNotEndOfToken(T * in,T * end)70 bool isNotEndOfToken( T *in, T *end ) {
71     return ( '}' != *in && ',' != *in && !isSpace( *in ) && ')' != *in && in != end );
72 }
73 
74 template<class T>
75 inline
isInteger(T * in,T * end)76 bool isInteger( T *in, T *end ) {
77     if( in != end ) {
78         if( *in == '-' ) {
79             ++in;
80         }
81     }
82 
83     bool result( false );
84     while( isNotEndOfToken( in, end ) ) {
85         result = isNumeric( *in );
86         if( !result ) {
87             break;
88         }
89         ++in;
90     }
91 
92     return result;
93 }
94 
95 template<class T>
96 inline
isFloat(T * in,T * end)97 bool isFloat( T *in, T *end ) {
98     if( in != end ) {
99         if( *in == '-' ) {
100             ++in;
101         }
102     }
103 
104     // check for <1>.0f
105     bool result( false );
106     while( isNotEndOfToken( in, end ) ) {
107         if( *in == '.' ) {
108             result = true;
109             break;
110         }
111         result = isNumeric( *in );
112         if( !result ) {
113             return false;
114         }
115         ++in;
116     }
117 
118     // check for 1<.>0f
119     if( *in == '.' ) {
120         ++in;
121     } else {
122         return false;
123     }
124 
125     // check for 1.<0>f
126     while( isNotEndOfToken( in, end ) ) {
127         result = isNumeric( *in );
128         if( !result ) {
129             return false;
130         }
131         ++in;
132     }
133 
134     return result;
135 }
136 
137 template<class T>
138 inline
isCharacter(const T in)139 bool isCharacter( const T in ) {
140     return ( ( in >= 'a' && in <= 'z' ) || ( in >= 'A' && in <= 'Z' ) );
141 }
142 
143 template<class T>
144 inline
isStringLiteral(const T in)145 bool isStringLiteral( const T in ) {
146     return ( in == '\"' );
147 }
148 
149 template<class T>
150 inline
isHexLiteral(T * in,T * end)151 bool isHexLiteral( T *in, T *end ) {
152     if( *in == '0' ) {
153         if( in + 1 != end ) {
154             if( *( in + 1 ) == 'x' || *( in + 1 ) == 'X' ) {
155                 return true;
156             }
157         }
158     }
159 
160     return false;
161 }
162 
163 template<class T>
164 inline
isReference(T * in,T * end)165 bool isReference( T *in, T *end ) {
166     if( *in == 'r' ) {
167         if( *(in+1) == 'e' ) {
168             if( *(in+2) == 'f' ) {
169                 if( ( in + 2 ) != end ) {
170                     return true;
171                 }
172             }
173         }
174     }
175 
176     return false;
177 }
178 
179 template<class T>
180 inline
isEndofLine(const T in)181 bool isEndofLine( const T in ) {
182     return ( '\n' == in );
183 }
184 
185 template<class T>
186 inline
getNextSeparator(T * in,T * end)187 static T *getNextSeparator( T *in, T *end ) {
188     while( !isSeparator( *in ) || in == end ) {
189         ++in;
190     }
191     return in;
192 }
193 
194 static const int ErrorHex2Decimal = 9999999;
195 
196 inline
hex2Decimal(char in)197 int hex2Decimal( char in ) {
198     if( isNumeric( in ) ) {
199         return ( in - 48 );
200     }
201 
202     char hexCodeLower( 'a' ), hexCodeUpper( 'A' );
203     for( int i = 0; i<16; i++ ) {
204         if( in == hexCodeLower + i || in == hexCodeUpper + i ) {
205             return ( i+10 );
206         }
207     }
208 
209     return ErrorHex2Decimal;
210 }
211 
212 template<class T>
213 inline
isComment(T * in,T * end)214 bool isComment( T *in, T *end ) {
215     if ( *in=='/' ) {
216         if ( in+1!=end ) {
217             if ( *( in+1 )=='/' ) {
218                 char *drive( ( in+2 ) );
219                 if ( (isUpperCase<T>( *drive )||isLowerCase<T>( *drive ))&&*( drive+1 )=='/' )  {
220                     return false;
221                 } else {
222                     return true;
223                 }
224             }
225         }
226     }
227 
228     return false;
229 }
230 
231 template<class T>
232 inline
isCommentOpenTag(T * in,T * end)233 bool isCommentOpenTag(T *in, T *end ) {
234     if (*in == '/') {
235         if (in + 1 != end) {
236             if (*(in + 1) == '*') {
237                 return true;
238             }
239         }
240     }
241 
242     return false;
243 }
244 
245 template<class T>
246 inline
isCommentCloseTag(T * in,T * end)247 bool isCommentCloseTag(T *in, T *end) {
248     if (*in == '*') {
249         if (in + 1 != end) {
250             if (*(in + 1) == '/') {
251                 return true;
252             }
253         }
254     }
255 
256     return false;
257 }
258 
259 END_ODDLPARSER_NS
260 
261