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
|
/* 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/BGame.h"
#include "engines/wintermute/Base/scriptables/ScStack.h"
#include "engines/wintermute/Base/scriptables/ScValue.h"
#include "engines/wintermute/utils/utils.h"
#include "engines/wintermute/Base/scriptables/SXString.h"
#include "engines/wintermute/Base/scriptables/SXArray.h"
#include "engines/wintermute/utils/StringUtil.h"
#include "common/tokenizer.h"
namespace WinterMute {
IMPLEMENT_PERSISTENT(CSXString, false)
CBScriptable *makeSXString(CBGame *inGame, CScStack *stack) {
return new CSXString(inGame, stack);
}
//////////////////////////////////////////////////////////////////////////
CSXString::CSXString(CBGame *inGame, CScStack *stack): CBScriptable(inGame) {
_string = NULL;
_capacity = 0;
stack->correctParams(1);
CScValue *val = stack->pop();
if (val->isInt()) {
_capacity = MAX(0, val->getInt());
if (_capacity > 0) {
_string = new char[_capacity];
memset(_string, 0, _capacity);
}
} else {
setStringVal(val->getString());
}
if (_capacity == 0) setStringVal("");
}
//////////////////////////////////////////////////////////////////////////
CSXString::~CSXString() {
if (_string) delete [] _string;
}
//////////////////////////////////////////////////////////////////////////
void CSXString::setStringVal(const char *val) {
int len = strlen(val);
if (len >= _capacity) {
_capacity = len + 1;
delete[] _string;
_string = NULL;
_string = new char[_capacity];
memset(_string, 0, _capacity);
}
strcpy(_string, val);
}
//////////////////////////////////////////////////////////////////////////
const char *CSXString::scToString() {
if (_string) return _string;
else return "[null string]";
}
//////////////////////////////////////////////////////////////////////////
void CSXString::scSetString(const char *val) {
setStringVal(val);
}
//////////////////////////////////////////////////////////////////////////
ERRORCODE CSXString::scCallMethod(CScScript *script, CScStack *stack, CScStack *thisStack, const char *name) {
//////////////////////////////////////////////////////////////////////////
// Substring
//////////////////////////////////////////////////////////////////////////
if (strcmp(name, "Substring") == 0) {
stack->correctParams(2);
int start = stack->pop()->getInt();
int end = stack->pop()->getInt();
if (end < start) CBUtils::swap(&start, &end);
//try {
WideString str;
if (_gameRef->_textEncoding == TEXT_UTF8)
str = StringUtil::utf8ToWide(_string);
else
str = StringUtil::ansiToWide(_string);
//WideString subStr = str.substr(start, end - start + 1);
WideString subStr(str.c_str() + start, end - start + 1);
if (_gameRef->_textEncoding == TEXT_UTF8)
stack->pushString(StringUtil::wideToUtf8(subStr).c_str());
else
stack->pushString(StringUtil::wideToAnsi(subStr).c_str());
// } catch (std::exception &) {
// stack->pushNULL();
// }
return STATUS_OK;
}
//////////////////////////////////////////////////////////////////////////
// Substr
//////////////////////////////////////////////////////////////////////////
else if (strcmp(name, "Substr") == 0) {
stack->correctParams(2);
int start = stack->pop()->getInt();
CScValue *val = stack->pop();
int len = val->getInt();
if (!val->isNULL() && len <= 0) {
stack->pushString("");
return STATUS_OK;
}
if (val->isNULL()) len = strlen(_string) - start;
// try {
WideString str;
if (_gameRef->_textEncoding == TEXT_UTF8)
str = StringUtil::utf8ToWide(_string);
else
str = StringUtil::ansiToWide(_string);
// WideString subStr = str.substr(start, len);
WideString subStr(str.c_str() + start, len);
if (_gameRef->_textEncoding == TEXT_UTF8)
stack->pushString(StringUtil::wideToUtf8(subStr).c_str());
else
stack->pushString(StringUtil::wideToAnsi(subStr).c_str());
// } catch (std::exception &) {
// stack->pushNULL();
// }
return STATUS_OK;
}
//////////////////////////////////////////////////////////////////////////
// ToUpperCase
//////////////////////////////////////////////////////////////////////////
else if (strcmp(name, "ToUpperCase") == 0) {
stack->correctParams(0);
WideString str;
if (_gameRef->_textEncoding == TEXT_UTF8)
str = StringUtil::utf8ToWide(_string);
else
str = StringUtil::ansiToWide(_string);
str.toUppercase();
if (_gameRef->_textEncoding == TEXT_UTF8)
stack->pushString(StringUtil::wideToUtf8(str).c_str());
else
stack->pushString(StringUtil::wideToAnsi(str).c_str());
return STATUS_OK;
}
//////////////////////////////////////////////////////////////////////////
// ToLowerCase
//////////////////////////////////////////////////////////////////////////
else if (strcmp(name, "ToLowerCase") == 0) {
stack->correctParams(0);
WideString str;
if (_gameRef->_textEncoding == TEXT_UTF8)
str = StringUtil::utf8ToWide(_string);
else
str = StringUtil::ansiToWide(_string);
str.toLowercase();
if (_gameRef->_textEncoding == TEXT_UTF8)
stack->pushString(StringUtil::wideToUtf8(str).c_str());
else
stack->pushString(StringUtil::wideToAnsi(str).c_str());
return STATUS_OK;
}
//////////////////////////////////////////////////////////////////////////
// IndexOf
//////////////////////////////////////////////////////////////////////////
else if (strcmp(name, "IndexOf") == 0) {
stack->correctParams(2);
const char *strToFind = stack->pop()->getString();
int index = stack->pop()->getInt();
WideString str;
if (_gameRef->_textEncoding == TEXT_UTF8)
str = StringUtil::utf8ToWide(_string);
else
str = StringUtil::ansiToWide(_string);
WideString toFind;
if (_gameRef->_textEncoding == TEXT_UTF8)
toFind = StringUtil::utf8ToWide(strToFind);
else
toFind = StringUtil::ansiToWide(strToFind);
int indexOf = StringUtil::indexOf(str, toFind, index);
stack->pushInt(indexOf);
return STATUS_OK;
}
//////////////////////////////////////////////////////////////////////////
// Split
//////////////////////////////////////////////////////////////////////////
else if (strcmp(name, "Split") == 0) {
stack->correctParams(1);
CScValue *val = stack->pop();
char separators[MAX_PATH_LENGTH] = ",";
if (!val->isNULL()) strcpy(separators, val->getString());
CSXArray *array = new CSXArray(_gameRef);
if (!array) {
stack->pushNULL();
return STATUS_OK;
}
WideString str;
if (_gameRef->_textEncoding == TEXT_UTF8)
str = StringUtil::utf8ToWide(_string);
else
str = StringUtil::ansiToWide(_string);
WideString delims;
if (_gameRef->_textEncoding == TEXT_UTF8)
delims = StringUtil::utf8ToWide(separators);
else
delims = StringUtil::ansiToWide(separators);
Common::Array<WideString> parts;
Common::StringTokenizer tokenizer(str, delims);
while (!tokenizer.empty()) {
Common::String str2 = tokenizer.nextToken();
parts.push_back(str2);
}
// TODO: Clean this up
/*do {
pos = StringUtil::IndexOf(Common::String(str.c_str() + start), delims, start);
//pos = str.find_first_of(delims, start);
if (pos == start) {
start = pos + 1;
} else if (pos == str.size()) {
parts.push_back(Common::String(str.c_str() + start));
break;
} else {
parts.push_back(Common::String(str.c_str() + start, pos - start));
start = pos + 1;
}
//start = str.find_first_not_of(delims, start);
start = StringUtil::LastIndexOf(Common::String(str.c_str() + start), delims, start) + 1;
} while (pos != str.size());*/
for (Common::Array<WideString>::iterator it = parts.begin(); it != parts.end(); ++it) {
WideString &part = (*it);
if (_gameRef->_textEncoding == TEXT_UTF8)
val = new CScValue(_gameRef, StringUtil::wideToUtf8(part).c_str());
else
val = new CScValue(_gameRef, StringUtil::wideToAnsi(part).c_str());
array->push(val);
delete val;
val = NULL;
}
stack->pushNative(array, false);
return STATUS_OK;
}
else return STATUS_FAILED;
}
//////////////////////////////////////////////////////////////////////////
CScValue *CSXString::scGetProperty(const char *name) {
_scValue->setNULL();
//////////////////////////////////////////////////////////////////////////
// Type (RO)
//////////////////////////////////////////////////////////////////////////
if (strcmp(name, "Type") == 0) {
_scValue->setString("string");
return _scValue;
}
//////////////////////////////////////////////////////////////////////////
// Length (RO)
//////////////////////////////////////////////////////////////////////////
else if (strcmp(name, "Length") == 0) {
if (_gameRef->_textEncoding == TEXT_UTF8) {
WideString wstr = StringUtil::utf8ToWide(_string);
_scValue->setInt(wstr.size());
} else
_scValue->setInt(strlen(_string));
return _scValue;
}
//////////////////////////////////////////////////////////////////////////
// Capacity
//////////////////////////////////////////////////////////////////////////
else if (strcmp(name, "Capacity") == 0) {
_scValue->setInt(_capacity);
return _scValue;
}
else return _scValue;
}
//////////////////////////////////////////////////////////////////////////
ERRORCODE CSXString::scSetProperty(const char *name, CScValue *value) {
//////////////////////////////////////////////////////////////////////////
// Capacity
//////////////////////////////////////////////////////////////////////////
if (strcmp(name, "Capacity") == 0) {
int32 newCap = (uint32)value->getInt();
if (newCap < (int32)(strlen(_string) + 1)) _gameRef->LOG(0, "Warning: cannot lower string capacity");
else if (newCap != _capacity) {
char *newStr = new char[newCap];
if (newStr) {
memset(newStr, 0, newCap);
strcpy(newStr, _string);
delete[] _string;
_string = newStr;
_capacity = newCap;
}
}
return STATUS_OK;
}
else return STATUS_FAILED;
}
//////////////////////////////////////////////////////////////////////////
ERRORCODE CSXString::persist(CBPersistMgr *persistMgr) {
CBScriptable::persist(persistMgr);
persistMgr->transfer(TMEMBER(_capacity));
if (persistMgr->_saving) {
if (_capacity > 0) persistMgr->putBytes((byte *)_string, _capacity);
} else {
if (_capacity > 0) {
_string = new char[_capacity];
persistMgr->getBytes((byte *)_string, _capacity);
} else _string = NULL;
}
return STATUS_OK;
}
//////////////////////////////////////////////////////////////////////////
int CSXString::scCompare(CBScriptable *val) {
return strcmp(_string, ((CSXString *)val)->_string);
}
} // end of namespace WinterMute
|