forked from naclander/openmugen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenizer.cpp
423 lines (363 loc) · 12 KB
/
tokenizer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#include "global.h"
static const int skTokDefaultOperatorCount = 23;
static char sTokDefaultCommentChars[] = ";";
static char* sTokDefaultOperators[skTokDefaultOperatorCount] =
{
"~",
"!",
"+",
"-",
"=",
"!=",
"[",
"]",
"(",
")",
"/",
"$",
",",
"<",
">",
"<=",
">=",
"&",
"|",
"^",
"&&",
"||",
"*",
};
CTokenizer::CTokenizer( int bufferSize, char* commentChars, char** operators, int operatorCount )
{
// Assert(( bufferSize > 0 ) && ( bufferSize < ( 1 << 30 ) );
m_BufferSize = bufferSize;
m_Buffer = new char[ bufferSize ];
m_CommentChars = commentChars ? commentChars : sTokDefaultCommentChars;
if( operators )
{
m_Operators = operators;
// Assert( operatorCount >= 0 );
m_OperatorCount = operatorCount;
}
else
{
m_Operators = sTokDefaultOperators;
m_OperatorCount = skTokDefaultOperatorCount;
}
m_NumOperatorCharsRead = 0;
m_Filename[0] = 0;
m_FileBuffer = NULL;
m_FileSize = 0;
m_CurrFilePos = 0;
m_CurrFileLine = 0;
m_LastLinePos = 0;
m_BufferIsNextToken = false;
m_AtEndOfLine = false;
m_AtEndOfFile = false;
m_ReturnNegativeSeperatelyFromNumber = false;
m_IsCaseSensitive = true;
m_LastTokenWasQuotedString = false;
}
CTokenizer::~CTokenizer()
{
if( m_FileBuffer )
delete m_FileBuffer;
delete m_Buffer;
}
bool CTokenizer::OpenFile( const char* filename )
{
if( m_FileBuffer )
return false;
strcpy( m_Filename, filename );
FILE* file = fopen( filename, "rb" );
if( !file )
{
throw(CError("CTokenizer::OpenFile: File %s not found",filename));
return false;
}
fseek( file, 0, SEEK_END );
m_FileSize = ftell( file );
if( !m_FileSize )
return false;
m_FileBuffer = new char[ m_FileSize ];
fseek( file, 0, SEEK_SET );
fread( m_FileBuffer, m_FileSize, 1, file );
fclose( file );
m_CurrFilePos = 0;
m_CurrFileLine = 0;
m_LastLinePos = 0;
m_BufferIsNextToken = false;
m_AtEndOfLine = false;
m_AtEndOfFile = false;
return true;
}
bool CTokenizer::CloseFile()
{
if( !m_FileBuffer )
return false;
delete m_FileBuffer;
m_FileSize = 0;
m_Filename[0] = 0;
m_FileBuffer = NULL;
m_FileSize = 0;
m_CurrFilePos = 0;
m_CurrFileLine = 0;
m_LastLinePos = 0;
m_BufferIsNextToken = false;
m_AtEndOfLine = false;
m_AtEndOfFile = false;
return true;
}
bool CTokenizer::AtEndOfLine()
{
return m_AtEndOfLine;
}
bool CTokenizer::AtEndOfFile()
{
return m_AtEndOfFile;
}
const char* CTokenizer::GetToken()
{
if( !m_BufferIsNextToken )
{
if( m_AtEndOfFile || !m_FileBuffer )
return NULL;
m_LastTokenWasQuotedString = false;
m_AtEndOfLine = false;
bool haveHitSecondWhitespace = false;
char* buf = m_Buffer;
while( !m_AtEndOfFile && ( buf - m_Buffer < m_BufferSize ))
{
char c = m_FileBuffer[ m_CurrFilePos++ ];
if( m_CurrFilePos >= m_FileSize )
{
m_AtEndOfFile = true;
break;
}
// read quoted strings in literally with no processing
if( c == '\"' )
{
// if there is something already in buffer, backup and return what is already there
if( buf > m_Buffer )
{
// rewind file pointer by one
m_CurrFilePos--;
}
else
{
while(1)
{
c = m_FileBuffer[ m_CurrFilePos++ ];
if( m_CurrFilePos >= m_FileSize )
{
m_AtEndOfFile = true;
break;
}
if( c == '\"' )
{
// check for end of line/end of file
while( 1 )
{
c = m_FileBuffer[ m_CurrFilePos++ ];
if( m_CurrFilePos >= m_FileSize )
{
m_AtEndOfFile = true;
break;
}
if( c == '\n' )
{
m_AtEndOfLine = true;
m_CurrFileLine++;
m_LastLinePos = m_CurrFilePos;
}
if( !isspace( c ) )
{
// rewind file pointer by one
m_CurrFilePos--;
break;
}
}
m_LastTokenWasQuotedString = true;
break;
}
(*buf) = c;
buf++;
}
}
break;
}
// skip chars after comment chars till end of line
bool hitCommentChar = false;
int commentCharCount = m_CommentChars ? strlen( m_CommentChars ) : 0;
for( int a = 0; a < commentCharCount; a++ )
{
if( c == m_CommentChars[ a ] )
{
hitCommentChar = true;
while( 1 )
{
c = m_FileBuffer[ m_CurrFilePos++ ];
if( m_CurrFilePos >= m_FileSize )
{
m_AtEndOfFile = true;
break;
}
if( c == '\n' )
{
m_CurrFileLine++;
m_LastLinePos = m_CurrFilePos;
break;
}
}
}
}
if( hitCommentChar )
{
if( buf > m_Buffer )
break;
}
else if( !isspace( c ) )
{
// check for operators and return them as seperate strings from other things, even if no whitespace between
bool hitOperator = false, breakToReturnToken = false, negativeNumber = false;
if( ( c == '-' ) && !m_ReturnNegativeSeperatelyFromNumber )
{
char nextC = m_FileBuffer[ m_CurrFilePos ];
if( isdigit( nextC ) || ( nextC == '.' ) )
{
negativeNumber = true;
}
}
if( !negativeNumber )
{
for( int a = 0; a < m_OperatorCount; a++ )
{
bool prevCharsMatched = ( strlen( m_Operators[ a ] ) > m_NumOperatorCharsRead );
for( int b = 0; prevCharsMatched && ( b < m_NumOperatorCharsRead ); b++ )
{
if( m_Buffer[ b ] != ( m_Operators[ a ] )[ b ] )
prevCharsMatched = false;
}
if( prevCharsMatched && ( c == ( m_Operators[ a ] )[ m_NumOperatorCharsRead ] ))
{
hitOperator = true;
// if there is something already in buffer, backup and return what is already there
if( !m_NumOperatorCharsRead && ( buf > m_Buffer ))
{
// rewind file pointer by one
m_CurrFilePos--;
breakToReturnToken = true;
}
else
{
(*buf) = c;
buf++;
m_NumOperatorCharsRead++;
}
break;
}
}
}
if( breakToReturnToken )
break;
if( !hitOperator )
{
// we want to check whitespace after token to see if end of line/file bits should be set
if( !m_NumOperatorCharsRead && !haveHitSecondWhitespace )
{
(*buf) = c;
buf++;
}
else
{
// rewind file pointer by one
m_CurrFilePos--;
break;
}
}
}
else
{
if( c == '\n' )
{
m_AtEndOfLine = true;
m_CurrFileLine++;
m_LastLinePos = m_CurrFilePos;
}
haveHitSecondWhitespace = ( buf > m_Buffer );
}
}
*buf = 0;
m_NumOperatorCharsRead = 0;
}
m_BufferIsNextToken = false;
return m_Buffer;
}
bool CTokenizer::GetToken( char* destString, int maxLength )
{
if( !GetToken() )
return false;
strncpy( destString, m_Buffer, maxLength > m_BufferSize ? m_BufferSize : maxLength );
return true;
}
bool CTokenizer::CheckToken( const char* stringToLookFor, bool consumeIfMatch )
{
if( !m_BufferIsNextToken )
{
if( !GetToken() )
return false;
}
bool result = m_IsCaseSensitive ? ( strcmp( stringToLookFor, m_Buffer ) == 0 ) : ( strcmp( stringToLookFor, m_Buffer ) == 0 );
m_BufferIsNextToken = consumeIfMatch ? !result : true;
return result;
}
float CTokenizer::GetFloat()
{
if( !m_BufferIsNextToken )
{
if( !GetToken() )
return 0.0f;
}
m_BufferIsNextToken = false;
return (float)atof( m_Buffer );
}
int CTokenizer::GetInt()
{
if( !m_BufferIsNextToken )
{
if( !GetToken() )
return 0;
}
m_BufferIsNextToken = false;
return atoi( m_Buffer );
}
bool CTokenizer::CheckTokenIsNumber()
{
if( !m_BufferIsNextToken )
{
if( !GetToken() )
return false;
}
m_BufferIsNextToken = true;
if( m_LastTokenWasQuotedString )
return false;
int len = strlen( m_Buffer );
char* c = m_Buffer;
for( int a = 0; a < len; a++ )
{
if( (( *c < '0' ) || ( *c > '9' )) && ( *c != '.' ) && !( !m_ReturnNegativeSeperatelyFromNumber && ( a == 0 ) && ( *c == '-' ) && ( len > 1 ) ) )
return false;
c++;
}
return ( len > 0 );
}
bool CTokenizer::CheckTokenIsQuotedString()
{
if( !m_BufferIsNextToken )
{
if( !GetToken() )
return false;
}
m_BufferIsNextToken = true;
return m_LastTokenWasQuotedString;
}