~hikiko/nux/arb-srgba-shader

« back to all changes in this revision

Viewing changes to NuxCore/NParsing.cpp

  • Committer: Neil Jagdish Patel
  • Date: 2010-09-01 19:25:37 UTC
  • Revision ID: neil.patel@canonical.com-20100901192537-mfz7rm6q262pewg6
Import and build NuxCore

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "NKernel.h" 
 
2
#include "NParsing.h"
 
3
 
 
4
#define CHAR_TAB    9   // '"'
 
5
#define CHAR_QUOTE  34  // '"'
 
6
 
 
7
NAMESPACE_BEGIN
 
8
 
 
9
//
 
10
// Get a string from a text string.
 
11
//
 
12
BOOL ParseTCHAR
 
13
(
 
14
        const TCHAR* Stream, 
 
15
        const TCHAR* Match,
 
16
        TCHAR*           Value,
 
17
    INT          Size,
 
18
        INT                      MaxLen
 
19
)
 
20
{
 
21
        const TCHAR* Found = Strfind(Stream, Match);
 
22
        const TCHAR* Start;
 
23
 
 
24
        if( Found )
 
25
        {
 
26
                Start = Found + StringLength(Match);
 
27
                if( *Start == '\x22' ) // Character '"'
 
28
                {
 
29
                        // The value begins with the quotation mark character: ". We skip it.
 
30
                        Strncpy( Value, Size, Start+1, MaxLen );
 
31
                        Value[MaxLen-1]=0;
 
32
                        TCHAR* Temp = Strstr( Value, TEXT("\x22") );
 
33
                        if( Temp != NULL )
 
34
            {
 
35
                // We read in the termination quotation mark. Set it t0 0 to null terminate the Value buffer.
 
36
                *Temp=0;
 
37
            }
 
38
                }
 
39
                else
 
40
                {
 
41
                        // Non-quoted string without spaces.
 
42
                        Strncpy( Value, Size, Start, MaxLen );
 
43
                        Value[MaxLen-1]=0;
 
44
                        TCHAR* Temp;
 
45
                        Temp = Strstr( Value, TEXT(" ")  ); if( Temp ) *Temp=0;
 
46
                        Temp = Strstr( Value, TEXT("\r") ); if( Temp ) *Temp=0;
 
47
                        Temp = Strstr( Value, TEXT("\n") ); if( Temp ) *Temp=0;
 
48
                        Temp = Strstr( Value, TEXT("\t") ); if( Temp ) *Temp=0;
 
49
                        Temp = Strstr( Value, TEXT(",")  ); if( Temp ) *Temp=0;
 
50
                }
 
51
                return 1;
 
52
        }
 
53
        else
 
54
        return 0;
 
55
}
 
56
 
 
57
//
 
58
// Checks if a command-line parameter exists in the stream.
 
59
//
 
60
BOOL ParseParam( const TCHAR* Stream, const TCHAR* Param )
 
61
{
 
62
        const TCHAR* Start = Stream;
 
63
        if( *Stream )
 
64
        {
 
65
                while( (Start=Strfind(Start+1,Param)) != NULL )
 
66
                {
 
67
                        if( Start>Stream && (Start[-1]=='-' || Start[-1]=='/') )
 
68
                        {
 
69
                                const TCHAR* End = Start + StringLength(Param);
 
70
                                if ( End == NULL || *End == 0 || IsWhitespaceChar(*End) )
 
71
                    return 1;
 
72
                        }
 
73
                }
 
74
        }
 
75
        return 0;
 
76
}
 
77
 
 
78
// 
 
79
// Parse a string.
 
80
//
 
81
BOOL ParseFString( const TCHAR* Stream, const TCHAR* Match, NString& Value )
 
82
{
 
83
        TCHAR Temp[4096]=TEXT("");
 
84
        if( ParseTCHAR( Stream, Match, Temp, INL_ARRAY_COUNT(Temp), INL_ARRAY_COUNT(Temp) ) )
 
85
        {
 
86
                Value = Temp;
 
87
                return 1;
 
88
        }
 
89
        else return 0;
 
90
}
 
91
 
 
92
//
 
93
// Parse a quadword.
 
94
//
 
95
BOOL ParseQWORD( const TCHAR* Stream, const TCHAR* Match, QWORD& Value )
 
96
{
 
97
        return ParseSQWORD( Stream, Match, *(SQWORD*)&Value );
 
98
}
 
99
 
 
100
//
 
101
// Parse a signed quadword.
 
102
//
 
103
BOOL ParseSQWORD( const TCHAR* Stream, const TCHAR* Match, SQWORD& Value )
 
104
{
 
105
        TCHAR Temp[4096]=TEXT(""), *Ptr=Temp;
 
106
        if( ParseTCHAR( Stream, Match, Temp, INL_ARRAY_COUNT(Temp), INL_ARRAY_COUNT(Temp) ) )
 
107
        {
 
108
                Value = 0;
 
109
                BOOL Negative = (*Ptr=='-');
 
110
                Ptr += Negative;
 
111
                while( *Ptr>='0' && *Ptr<='9' )
 
112
                        Value = Value*10 + *Ptr++ - '0';
 
113
                if( Negative )
 
114
                        Value = -Value;
 
115
                return 1;
 
116
        }
 
117
        else return 0;
 
118
}
 
119
 
 
120
//
 
121
// Get a DWORD.
 
122
//
 
123
BOOL ParseDWORD( const TCHAR* Stream, const TCHAR* Match, DWORD& Value )
 
124
{
 
125
        const TCHAR* Temp = Strfind(Stream, Match);
 
126
        TCHAR* End;
 
127
        if( Temp==NULL )
 
128
                return 0;
 
129
        Value = Strtoi( Temp + StringLength(Match), &End, 10 );
 
130
 
 
131
        return 1;
 
132
}
 
133
 
 
134
//
 
135
// Get a byte.
 
136
//
 
137
BOOL ParseBYTE( const TCHAR* Stream, const TCHAR* Match, BYTE& Value )
 
138
{
 
139
        const TCHAR* Temp = Strfind(Stream,Match);
 
140
        if( Temp==NULL )
 
141
                return 0;
 
142
        Temp += StringLength( Match );
 
143
        Value = (BYTE)CharToInteger( Temp );
 
144
        return Value!=0 || IsDigitChar(Temp[0]);
 
145
}
 
146
 
 
147
//
 
148
// Get a signed byte.
 
149
//
 
150
BOOL ParseSBYTE( const TCHAR* Stream, const TCHAR* Match, SBYTE& Value )
 
151
{
 
152
        const TCHAR* Temp = Strfind(Stream,Match);
 
153
        if( Temp==NULL )
 
154
                return 0;
 
155
        Temp += StringLength( Match );
 
156
    Value = CharToInteger( Temp );
 
157
        return Value!=0 || IsDigitChar(Temp[0]);
 
158
}
 
159
 
 
160
//
 
161
// Get a word.
 
162
//
 
163
BOOL ParseWORD( const TCHAR* Stream, const TCHAR* Match, WORD& Value )
 
164
{
 
165
        const TCHAR* Temp = Strfind( Stream, Match );
 
166
        if( Temp==NULL )
 
167
                return 0;
 
168
        Temp += StringLength( Match );
 
169
        Value = (WORD)CharToInteger( Temp );
 
170
        return Value!=0 || IsDigitChar(Temp[0]);
 
171
}
 
172
 
 
173
//
 
174
// Get a signed word.
 
175
//
 
176
BOOL ParseSWORD( const TCHAR* Stream, const TCHAR* Match, SWORD& Value )
 
177
{
 
178
        const TCHAR* Temp = Strfind( Stream, Match );
 
179
        if( Temp==NULL )
 
180
                return 0;
 
181
        Temp += StringLength( Match );
 
182
        Value = (SWORD)CharToInteger( Temp );
 
183
        return Value!=0 || IsDigitChar(Temp[0]);
 
184
}
 
185
 
 
186
//
 
187
// Get a floating-point number.
 
188
//
 
189
BOOL ParseFLOAT( const TCHAR* Stream, const TCHAR* Match, float& Value )
 
190
{
 
191
        const TCHAR* Temp = Strfind( Stream, Match );
 
192
        if( Temp==NULL )
 
193
                return 0;
 
194
    Value = CharToFloat(Temp+StringLength(Match));
 
195
        return 1;
 
196
}
 
197
 
 
198
//
 
199
// Get a signed double word.
 
200
//
 
201
BOOL ParseINT( const TCHAR* Stream, const TCHAR* Match, INT& Value )
 
202
{
 
203
        const TCHAR* Temp = Strfind( Stream, Match );
 
204
        if( Temp==NULL )
 
205
                return 0;
 
206
        Value = CharToInteger( Temp + StringLength(Match) );
 
207
        return 1;
 
208
}
 
209
 
 
210
//
 
211
// Get a boolean value.
 
212
//
 
213
BOOL ParseUBOOL( const TCHAR* Stream, const TCHAR* Match, BOOL& OnOff )
 
214
{
 
215
        TCHAR TempStr[16];
 
216
        if( ParseTCHAR( Stream, Match, TempStr, INL_ARRAY_COUNT(TempStr), INL_ARRAY_COUNT(TempStr)-1 ) )
 
217
        {
 
218
                OnOff
 
219
                =       !Stricmp(TempStr,TEXT("On"))
 
220
                ||      !Stricmp(TempStr,TEXT("True"))
 
221
                ||      !Stricmp(TempStr,TEXT("1"));
 
222
                return 1;
 
223
        }
 
224
        else
 
225
        return 0;
 
226
}
 
227
 
 
228
//
 
229
// Sees if Stream starts with the named command.  If it does,
 
230
// skips through the command and blanks past it.  Returns 1 of match,
 
231
// 0 if not.
 
232
//
 
233
BOOL ParseCommand
 
234
(
 
235
        const TCHAR** Stream, 
 
236
        const TCHAR*  Match
 
237
)
 
238
{
 
239
        while( (**Stream==' ')||(**Stream==9) )
 
240
                (*Stream)++;
 
241
 
 
242
        if( TCharStringNICompare(*Stream,Match,StringLength(Match))==0 )
 
243
        {
 
244
                *Stream += StringLength(Match);
 
245
                if( !IsAlphanumericChar(**Stream) )
 
246
                {
 
247
                        while ((**Stream==' ')||(**Stream==9)) (*Stream)++;
 
248
                        return 1; // Success.
 
249
                }
 
250
                else
 
251
                {
 
252
                        *Stream -= StringLength(Match);
 
253
                        return 0; // Only found partial match.
 
254
                }
 
255
        }
 
256
        else return 0; // No match.
 
257
}
 
258
 
 
259
//
 
260
// Get next command.  Skips past comments and cr's.
 
261
//
 
262
void ParseNext( const TCHAR** Stream )
 
263
{
 
264
        // Skip over spaces, tabs, cr's, and linefeeds.
 
265
        SkipJunk:
 
266
        while( **Stream==' ' || **Stream==9 || **Stream==13 || **Stream==10 )
 
267
                ++*Stream;
 
268
 
 
269
        if( **Stream==';' )
 
270
        {
 
271
                // Skip past comments.
 
272
                while( **Stream!=0 && **Stream!=10 && **Stream!=13 )
 
273
                        ++*Stream;
 
274
                goto SkipJunk;
 
275
        }
 
276
 
 
277
        // Upon exit, *Stream either points to valid Stream or a nul.
 
278
}
 
279
 
 
280
//
 
281
// Grab the next space-delimited string from the input stream.
 
282
// If quoted, gets entire quoted string.
 
283
//
 
284
BOOL ParseToken( const TCHAR*& Str, TCHAR* Result, INT MaxLen, BOOL UseEscape )
 
285
{
 
286
        INT Len=0;
 
287
 
 
288
        // Skip spaces and tabs.
 
289
        while( *Str==' ' || *Str==9 )
 
290
                Str++;
 
291
        if( *Str == 34 )
 
292
        {
 
293
                // Get quoted string.
 
294
                Str++;
 
295
                while( *Str && *Str!=34 && (Len+1)<MaxLen )
 
296
                {
 
297
                        TCHAR c = *Str++;
 
298
                        if( c=='\\' && UseEscape )
 
299
                        {
 
300
                                // Get escape.
 
301
                                c = *Str++;
 
302
                                if( !c )
 
303
                                        break;
 
304
                        }
 
305
                        if( (Len+1)<MaxLen )
 
306
                                Result[Len++] = c;
 
307
                }
 
308
                if( *Str==34 )
 
309
                        Str++;
 
310
        }
 
311
        else
 
312
        {
 
313
                // Get unquoted string.
 
314
                for( ; *Str && *Str!=' ' && *Str!=9; Str++ )
 
315
                        if( (Len+1)<MaxLen )
 
316
                                Result[Len++] = *Str;
 
317
        }
 
318
        Result[Len]=0;
 
319
        return Len!=0;
 
320
}
 
321
BOOL ParseToken( const TCHAR*& Str, NString& Arg, BOOL UseEscape )
 
322
{
 
323
        Arg.Clear();
 
324
 
 
325
        // Skip spaces and tabs.
 
326
        while( IsWhitespaceChar(*Str) )
 
327
                Str++;
 
328
 
 
329
        if ( *Str == TEXT('"') )
 
330
        {
 
331
                // Get quoted string.
 
332
                Str++;
 
333
                while( *Str && *Str != TEXT('"') )
 
334
                {
 
335
                        TCHAR c = *Str++;
 
336
                        if( c == TEXT('\\') && UseEscape )
 
337
                        {
 
338
                                // Get escape.
 
339
                                c = *Str++;
 
340
                                if( !c )
 
341
                                        break;
 
342
                        }
 
343
 
 
344
                        Arg += c;
 
345
                }
 
346
 
 
347
                if ( *Str == TEXT('"') )
 
348
                        Str++;
 
349
        }
 
350
        else
 
351
        {
 
352
                // Get unquoted string.
 
353
                for( ; *Str && !IsWhitespaceChar(*Str); Str++ )
 
354
                {
 
355
                        Arg += *Str;
 
356
                }
 
357
        }
 
358
 
 
359
        return Arg.Length() > 0;
 
360
}
 
361
NString ParseToken( const TCHAR*& Str, BOOL UseEscape )
 
362
{
 
363
        TCHAR Buffer[1024];
 
364
        if( ParseToken( Str, Buffer, INL_ARRAY_COUNT(Buffer), UseEscape ) )
 
365
                return Buffer;
 
366
        else
 
367
                return TEXT("");
 
368
}
 
369
 
 
370
//
 
371
// Get a line of Stream (everything up to, but not including, CR/LF.
 
372
// Returns 0 if ok, nonzero if at end of stream and returned 0-length string.
 
373
//
 
374
BOOL ParseLine
 
375
(
 
376
        const TCHAR**   Stream,
 
377
        TCHAR*                  Result,
 
378
        INT                             MaxLen,
 
379
        BOOL                    Exact
 
380
)
 
381
{
 
382
        BOOL GotStream=0;
 
383
        BOOL IsQuoted=0;
 
384
        BOOL Ignore=0;
 
385
 
 
386
        *Result=0;
 
387
        while( **Stream!=0 && **Stream!=10 && **Stream!=13 && --MaxLen>0 )
 
388
        {
 
389
                // Start of comments.
 
390
                if( !IsQuoted && !Exact && (*Stream)[0]=='/' && (*Stream)[1]=='/' )
 
391
                        Ignore = 1;
 
392
                
 
393
                // Command chaining.
 
394
                if( !IsQuoted && !Exact && **Stream=='|' )
 
395
                        break;
 
396
 
 
397
                // Check quoting.
 
398
                IsQuoted = IsQuoted ^ (**Stream==34);
 
399
                GotStream=1;
 
400
 
 
401
                // Got stuff.
 
402
                if( !Ignore )
 
403
                        *(Result++) = *((*Stream)++);
 
404
                else
 
405
                        (*Stream)++;
 
406
        }
 
407
        if( Exact )
 
408
        {
 
409
                // Eat up exactly one CR/LF.
 
410
                if( **Stream == 13 )
 
411
                        (*Stream)++;
 
412
                if( **Stream == 10 )
 
413
                        (*Stream)++;
 
414
        }
 
415
        else
 
416
        {
 
417
                // Eat up all CR/LF's.
 
418
                while( **Stream==10 || **Stream==13 || **Stream=='|' )
 
419
                        (*Stream)++;
 
420
        }
 
421
        *Result=0;
 
422
        return **Stream!=0 || GotStream;
 
423
}
 
424
BOOL ParseLine
 
425
(
 
426
        const TCHAR**   Stream,
 
427
        NString&                Result,
 
428
        BOOL                    Exact
 
429
)
 
430
{
 
431
        BOOL GotStream=0;
 
432
        BOOL IsQuoted=0;
 
433
        BOOL Ignore=0;
 
434
 
 
435
        Result = TEXT("");
 
436
 
 
437
        while( **Stream!=0 && **Stream!=10 && **Stream!=13 )
 
438
        {
 
439
                // Start of comments.
 
440
                if( !IsQuoted && !Exact && (*Stream)[0]=='/' && (*Stream)[1]=='/' )
 
441
                        Ignore = 1;
 
442
 
 
443
                // Command chaining.
 
444
                if( !IsQuoted && !Exact && **Stream=='|' )
 
445
                        break;
 
446
 
 
447
                // Check quoting.
 
448
                IsQuoted = IsQuoted ^ (**Stream==34);
 
449
                GotStream=1;
 
450
 
 
451
                // Got stuff.
 
452
                if( !Ignore )
 
453
                {
 
454
                        Result += ( *((*Stream)++) );
 
455
                }
 
456
                else
 
457
                {
 
458
                        (*Stream)++;
 
459
                }
 
460
        }
 
461
        if( Exact )
 
462
        {
 
463
                // Eat up exactly one CR/LF.
 
464
                if( **Stream == 13 )
 
465
                        (*Stream)++;
 
466
                if( **Stream == 10 )
 
467
                        (*Stream)++;
 
468
        }
 
469
        else
 
470
        {
 
471
                // Eat up all CR/LF's.
 
472
                while( **Stream==10 || **Stream==13 || **Stream=='|' )
 
473
                        (*Stream)++;
 
474
        }
 
475
 
 
476
        return **Stream!=0 || GotStream;
 
477
}
 
478
 
 
479
NAMESPACE_END