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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
/*
* This file is based on WME Lite.
* http://dead-code.org/redir.php?target=wmelite
* Copyright (c) 2011 Jan Nedoma
*/
#include "engines/wintermute/base/base_parser.h"
#include "engines/wintermute/base/base_game.h"
#include "engines/wintermute/base/base_engine.h"
#include "engines/wintermute/platform_osystem.h"
#include "common/str.h"
#include "common/util.h"
#define WHITESPACE " \t\n\r"
namespace Wintermute {
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
BaseParser::BaseParser() {
_whiteSpace = new char [strlen(WHITESPACE) + 1];
strcpy(_whiteSpace, WHITESPACE);
}
//////////////////////////////////////////////////////////////////////
BaseParser::~BaseParser() {
if (_whiteSpace != nullptr) {
delete[] _whiteSpace;
}
}
//////////////////////////////////////////////////////////////////////
char *BaseParser::getLastOffender() {
return _lastOffender;
}
//////////////////////////////////////////////////////////////////////
int32 BaseParser::getObject(char **buf, const TokenDesc *tokens, char **name, char **data) {
skipCharacters(buf, _whiteSpace);
// skip comment lines.
while (**buf == ';') {
*buf = strchr(*buf, '\n');
_parserLine++;
skipCharacters(buf, _whiteSpace);
}
if (! **buf) { // at end of file
return PARSERR_EOF;
}
// find the token.
// TODO: for now just use brute force. Improve later.
while (tokens->id != 0) {
if (!scumm_strnicmp(tokens->token, *buf, strlen(tokens->token))) {
// here we could be matching PART of a string
// we could detect this here or the token list
// could just have the longer tokens first in the list
break;
}
++tokens;
}
if (tokens->id == 0) {
char *p = strchr(*buf, '\n');
if (p && p > *buf) {
strncpy(_lastOffender, *buf, MIN((uint32)255, (uint32)(p - *buf))); // TODO, clean
} else {
strcpy(_lastOffender, "");
}
return PARSERR_TOKENNOTFOUND;
}
// skip the token
*buf += strlen(tokens->token);
skipCharacters(buf, _whiteSpace);
// get optional name
*name = getSubText(buf, '\'', '\''); // single quotes
skipCharacters(buf, _whiteSpace);
// get optional data
if (**buf == '=') { // An assignment rather than a command/object.
*data = getAssignmentText(buf);
} else {
*data = getSubText(buf, '{', '}');
}
return tokens->id;
}
//////////////////////////////////////////////////////////////////////
int32 BaseParser::getCommand(char **buf, const TokenDesc *tokens, char **params) {
if (!*buf) {
return PARSERR_TOKENNOTFOUND;
}
BaseEngine::instance().getGameRef()->miniUpdate();
char *name;
return getObject(buf, tokens, &name, params);
}
//////////////////////////////////////////////////////////////////////
void BaseParser::skipCharacters(char **buf, const char *toSkip) {
char ch;
while ((ch = **buf) != 0) {
if (ch == '\n') {
_parserLine++;
}
if (strchr(toSkip, ch) == nullptr) {
return;
}
++*buf; // skip this character
}
// we must be at the end of the buffer if we get here
}
//////////////////////////////////////////////////////////////////////
char *BaseParser::getSubText(char **buf, char open, char close) {
if (**buf == 0 || **buf != open) {
return 0;
}
++*buf; // skip opening delimiter
char *result = *buf;
// now find the closing delimiter
char theChar;
long skip = 1;
if (open == close) { // we cant nest identical delimiters
open = 0;
}
while ((theChar = **buf) != 0) {
if (theChar == open) {
++skip;
}
if (theChar == close) {
if (--skip == 0) {
**buf = 0; // null terminate the result string
++*buf; // move past the closing delimiter
break;
}
}
++*buf; // try next character
}
return result;
}
//////////////////////////////////////////////////////////////////////
char *BaseParser::getAssignmentText(char **buf) {
++*buf; // skip the '='
skipCharacters(buf, _whiteSpace);
char *result = *buf;
if (*result == '"') {
result = getSubText(buf, '"', '"');
} else {
// now, we need to find the next whitespace to end the data
char theChar;
while ((theChar = **buf) != 0) {
if (theChar <= 0x20) { // space and control chars
break;
}
++*buf;
}
**buf = 0; // null terminate it
if (theChar) { // skip the terminator
++*buf;
}
}
return result;
}
//////////////////////////////////////////////////////////////////////
Common::String BaseParser::getToken(char **buf) {
char token[100]; // TODO: Remove static
char *b = *buf, *t = token;
while (true) {
while (*b && (*b == ' ' || *b == '\n' || *b == 13 || *b == 10 || *b == '\t')) {
b++;
}
if (*b == ';')
while (*b && *b != '\n' && *b != 13 && *b != 10) {
b++;
}
else {
break;
}
}
if (*b == '\'') {
b++;
while (*b && *b != '\'') {
*t++ = *b++;
}
*t++ = 0;
if (*b == '\'') {
b++;
}
} else if (*b == '(' || *b == ')' || *b == '=' || *b == ',' || *b == '[' || *b == ']' ||
*b == '%' || *b == ':' || *b == '{' || *b == '}') {
*t++ = *b++;
*t++ = 0;
} else if (*b == '.' && (*(b + 1) < '0' || *(b + 1) > '9')) {
*t++ = *b++;
*t++ = 0;
} else if ((*b >= '0' && *b <= '9') || *b == '.' || *b == '-') {
while (*b && ((*b >= '0' && *b <= '9') || *b == '.' || *b == '-')) {
*t++ = *b++;
}
*t++ = 0;
} else if ((*b >= 'A' && *b <= 'Z') || (*b >= 'a' && *b <= 'z') || *b == '_') {
while (*b && ((*b >= 'A' && *b <= 'Z') || (*b >= 'a' && *b <= 'z') || *b == '_')) {
*t++ = *b++;
}
*t++ = 0;
} else if (*b == 0) {
*buf = b;
return nullptr;
} else {
// Error.
return nullptr;
}
*buf = b;
return token;
}
//////////////////////////////////////////////////////////////////////
float BaseParser::getTokenFloat(char **buf) {
Common::String token = getToken(buf);
const char *t = token.c_str();
if (!((*t >= '0' && *t <= '9') || *t == '-' || *t == '.')) {
// Error situation. We handle this by return 0.
return 0.;
}
float rc = (float)atof(t);
return rc;
}
//////////////////////////////////////////////////////////////////////
int BaseParser::getTokenInt(char **buf) {
Common::String token = getToken(buf);
const char *t = token.c_str();
if (!((*t >= '0' && *t <= '9') || *t == '-')) {
// Error situation. We handle this by return 0.
return 0;
}
int rc = atoi(t);
return rc;
}
//////////////////////////////////////////////////////////////////////
void BaseParser::skipToken(char **buf, char *tok, char * /*msg*/) {
Common::String token = getToken(buf);
const char *t = token.c_str();
if (strcmp(t, tok)) {
return; // Error
}
}
//////////////////////////////////////////////////////////////////////
int BaseParser::scanStr(const char *in, const char *format, ...) {
va_list arg;
va_start(arg, format);
int num = 0;
in += strspn(in, " \t\n\f");
while (*format && *in) {
if (*format == '%') {
format++;
switch (*format) {
case 'd': {
int *a = va_arg(arg, int *);
in += strspn(in, " \t\n\f");
*a = atoi(in);
in += strspn(in, "0123456789+- \t\n\f");
num++;
break;
}
case 'D': {
int i;
int *list = va_arg(arg, int *);
int *nr = va_arg(arg, int *);
in += strspn(in, " \t\n\f");
i = 0;
while ((*in >= '0' && *in <= '9') || *in == '+' || *in == '-') {
list[i++] = atoi(in);
in += strspn(in, "0123456789+-");
in += strspn(in, " \t\n\f");
if (*in != ',') {
break;
}
in++;
in += strspn(in, " \t\n\f");
}
*nr = i;
num++;
break;
}
case 'b': {
bool *a = va_arg(arg, bool *);
in += strspn(in, " \t\n\f");
const char *in2 = in + strspn(in, "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
int l = (int)(in2 - in);
*a = (bool)(!scumm_strnicmp(in, "yes", l) || !scumm_strnicmp(in, "true", l) || !scumm_strnicmp(in, "on", l) ||
!scumm_strnicmp(in, "1", l));
in = in2 + strspn(in2, " \t\n\f");
num++;
break;
}
case 'f': {
float *a = va_arg(arg, float *);
in += strspn(in, " \t\n\f");
*a = (float)atof(in);
in += strspn(in, "0123456789.eE+- \t\n\f");
num++;
break;
}
case 'F': {
int i;
float *list = va_arg(arg, float *);
int *nr = va_arg(arg, int *);
in += strspn(in, " \t\n\f");
i = 0;
while ((*in >= '0' && *in <= '9') || *in == '.' || *in == '+' || *in == '-' || *in == 'e' || *in == 'E') {
list[i++] = (float)atof(in);
in += strspn(in, "0123456789.eE+-");
in += strspn(in, " \t\n\f");
if (*in != ',') {
break;
}
in++;
in += strspn(in, " \t\n\f");
}
*nr = i;
num++;
break;
}
case 's': {
char *a = va_arg(arg, char *);
in += strspn(in, " \t\n\f");
if (*in == '\'') {
in++;
const char *in2 = strchr(in, '\'');
if (in2) {
Common::strlcpy(a, in, (int)(in2 - in) + 1);
in = in2 + 1;
} else {
strcpy(a, in);
in = strchr(in, 0);
}
} else {
const char *in2 = in + strspn(in, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789.");
Common::strlcpy(a, in, (int)(in2 - in) + 1);
in = in2;
}
in += strspn(in, " \t\n\f");
num++;
break;
}
case 'S': {
char *a = va_arg(arg, char *);
in += strspn(in, " \t\n\f");
if (*in == '\"') {
in++;
while (*in != '\"') {
if (*in == '\\') {
in++;
switch (*in) {
case '\\':
*a++ = '\\';
break;
case 'n':
*a++ = '\n';
break;
case 'r':
*a++ = '\r';
break;
case 't':
*a++ = '\t';
break;
case '"':
*a++ = '"';
break;
default:
*a++ = '\\';
*a++ = *in;
break;
} //switch
in++;
} else {
*a++ = *in++;
}
} //while in string
in++;
num++;
} //if string started
//terminate string
*a = '\0';
break;
}
}
if (*format) {
format++;
}
} else if (*format == ' ') {
format++;
in += strspn(in, " \t\n\f");
} else if (*in == *format) {
in++;
format++;
} else {
num = -1;
break;
}
}
va_end(arg);
return num;
}
} // end of namespace Wintermute
|