forked from TheSuperHackers/GeneralsGameCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnicodeString.cpp
More file actions
417 lines (366 loc) · 10.4 KB
/
UnicodeString.cpp
File metadata and controls
417 lines (366 loc) · 10.4 KB
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
/*
** Command & Conquer Generals(tm)
** Copyright 2025 Electronic Arts Inc.
**
** 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 3 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, see <http://www.gnu.org/licenses/>.
*/
////////////////////////////////////////////////////////////////////////////////
// //
// (c) 2001-2003 Electronic Arts Inc. //
// //
////////////////////////////////////////////////////////////////////////////////
// FILE: UnicodeString.cpp
//-----------------------------------------------------------------------------
//
// Westwood Studios Pacific.
//
// Confidential Information
// Copyright (C) 2001 - All Rights Reserved
//
//-----------------------------------------------------------------------------
//
// Project: RTS3
//
// File name: UnicodeString.cpp
//
// Created: Steven Johnson, October 2001
//
// Desc: General-purpose string classes
//
//-----------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////
#include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
#include "Common/CriticalSection.h"
// -----------------------------------------------------
/*static*/ UnicodeString UnicodeString::TheEmptyString;
// -----------------------------------------------------
#ifdef RTS_DEBUG
void UnicodeString::validate() const
{
if (!m_data) return;
DEBUG_ASSERTCRASH(m_data->m_refCount > 0, ("m_refCount is zero"));
DEBUG_ASSERTCRASH(m_data->m_numCharsAllocated > 0, ("m_numCharsAllocated is zero"));
DEBUG_ASSERTCRASH(wcslen(m_data->peek())+1 <= m_data->m_numCharsAllocated,("str is too long for storage"));
}
#endif
// -----------------------------------------------------
UnicodeString::UnicodeString(const UnicodeString& stringSrc) : m_data(stringSrc.m_data)
{
ScopedCriticalSection scopedCriticalSection(TheUnicodeStringCriticalSection);
if (m_data)
++m_data->m_refCount;
validate();
}
// -----------------------------------------------------
void UnicodeString::ensureUniqueBufferOfSize(int numCharsNeeded, Bool preserveData, const WideChar* strToCopy, const WideChar* strToCat)
{
validate();
if (m_data &&
m_data->m_refCount == 1 &&
m_data->m_numCharsAllocated >= numCharsNeeded)
{
// no buffer manhandling is needed (it's already large enough, and unique to us)
if (strToCopy)
// TheSuperHackers @fix Mauller 04/04/2025 Replace wcscpy with safer memmove as memory regions can overlap when part of string is copied to itself
memmove(m_data->peek(), strToCopy, (wcslen(strToCopy) + 1) * sizeof(WideChar));
if (strToCat)
wcscat(m_data->peek(), strToCat);
return;
}
DEBUG_ASSERTCRASH(TheDynamicMemoryAllocator != NULL, ("Cannot use dynamic memory allocator before its initialization. Check static initialization order."));
DEBUG_ASSERTCRASH(numCharsNeeded <= MAX_LEN, ("UnicodeString::ensureUniqueBufferOfSize exceeds max string length %d with requested length %d", MAX_LEN, numCharsNeeded));
int minBytes = sizeof(UnicodeStringData) + numCharsNeeded*sizeof(WideChar);
int actualBytes = TheDynamicMemoryAllocator->getActualAllocationSize(minBytes);
UnicodeStringData* newData = (UnicodeStringData*)TheDynamicMemoryAllocator->allocateBytesDoNotZero(actualBytes, "STR_UnicodeString::ensureUniqueBufferOfSize");
newData->m_refCount = 1;
newData->m_numCharsAllocated = (actualBytes - sizeof(UnicodeStringData))/sizeof(WideChar);
#if defined(RTS_DEBUG)
newData->m_debugptr = newData->peek(); // just makes it easier to read in the debugger
#endif
if (m_data && preserveData)
wcscpy(newData->peek(), m_data->peek());
else
newData->peek()[0] = 0;
// do these BEFORE releasing the old buffer, so that self-copies
// or self-cats will work correctly.
if (strToCopy)
wcscpy(newData->peek(), strToCopy);
if (strToCat)
wcscat(newData->peek(), strToCat);
releaseBuffer();
m_data = newData;
validate();
}
// -----------------------------------------------------
void UnicodeString::releaseBuffer()
{
ScopedCriticalSection scopedCriticalSection(TheUnicodeStringCriticalSection);
validate();
if (m_data)
{
if (--m_data->m_refCount == 0)
{
TheDynamicMemoryAllocator->freeBytes(m_data);
}
m_data = 0;
}
}
// -----------------------------------------------------
UnicodeString::UnicodeString(const WideChar* s) : m_data(0)
{
int len = wcslen(s);
if (len)
{
ensureUniqueBufferOfSize(len + 1, false, s, NULL);
}
validate();
}
// -----------------------------------------------------
void UnicodeString::set(const UnicodeString& stringSrc)
{
ScopedCriticalSection scopedCriticalSection(TheUnicodeStringCriticalSection);
validate();
if (&stringSrc != this)
{
releaseBuffer();
m_data = stringSrc.m_data;
if (m_data)
++m_data->m_refCount;
}
validate();
}
// -----------------------------------------------------
void UnicodeString::set(const WideChar* s)
{
validate();
if (!m_data || s != peek())
{
int len = s ? wcslen(s) : 0;
if (len)
{
ensureUniqueBufferOfSize(len + 1, false, s, NULL);
}
else
{
releaseBuffer();
}
}
validate();
}
// -----------------------------------------------------
WideChar* UnicodeString::getBufferForRead(Int len)
{
validate();
DEBUG_ASSERTCRASH(len>0, ("No need to allocate 0 len strings."));
ensureUniqueBufferOfSize(len + 1, false, NULL, NULL);
validate();
return peek();
}
// -----------------------------------------------------
void UnicodeString::translate(const AsciiString& stringSrc)
{
validate();
/// @todo srj put in a real translation here; this will only work for 7-bit ascii
clear();
Int len = stringSrc.getLength();
for (Int i = 0; i < len; i++)
concat((WideChar)stringSrc.getCharAt(i));
validate();
}
// -----------------------------------------------------
void UnicodeString::concat(const WideChar* s)
{
validate();
int addlen = wcslen(s);
if (addlen == 0)
return; // my, that was easy
if (m_data)
{
ensureUniqueBufferOfSize(getLength() + addlen + 1, true, NULL, s);
}
else
{
set(s);
}
validate();
}
// -----------------------------------------------------
void UnicodeString::trim()
{
validate();
if (m_data)
{
const WideChar *c = peek();
// Strip leading white space from the string.
while (c && iswspace(*c))
{
c++;
}
if (c != peek())
{
set(c);
}
trimEnd();
}
validate();
}
// -----------------------------------------------------
void UnicodeString::trimEnd()
{
validate();
if (m_data)
{
// Clip trailing white space from the string.
const int len = wcslen(peek());
int index = len;
while (index > 0 && iswspace(getCharAt(index - 1)))
{
--index;
}
if (index < len)
{
truncateTo(index);
}
}
validate();
}
// -----------------------------------------------------
void UnicodeString::trimEnd(const WideChar c)
{
validate();
if (m_data)
{
// Clip trailing consecutive occurances of c from the string.
const int len = wcslen(peek());
int index = len;
while (index > 0 && getCharAt(index - 1) == c)
{
--index;
}
if (index < len)
{
truncateTo(index);
}
}
validate();
}
// -----------------------------------------------------
void UnicodeString::removeLastChar()
{
truncateBy(1);
}
// -----------------------------------------------------
void UnicodeString::truncateBy(const Int charCount)
{
validate();
if (m_data && charCount > 0)
{
const size_t len = wcslen(peek());
if (len > 0)
{
ensureUniqueBufferOfSize(len + 1, true, NULL, NULL);
size_t count = charCount;
if (charCount > len)
{
count = len;
}
peek()[len - count] = 0;
}
}
validate();
}
// -----------------------------------------------------
void UnicodeString::truncateTo(const Int maxLength)
{
validate();
if (m_data)
{
const size_t len = wcslen(peek());
if (len > maxLength)
{
ensureUniqueBufferOfSize(len + 1, true, NULL, NULL);
peek()[maxLength] = 0;
}
}
validate();
}
// -----------------------------------------------------
void UnicodeString::format(UnicodeString format, ...)
{
validate();
va_list args;
va_start(args, format);
format_va(format, args);
va_end(args);
validate();
}
// -----------------------------------------------------
void UnicodeString::format(const WideChar* format, ...)
{
validate();
va_list args;
va_start(args, format);
format_va(format, args);
va_end(args);
validate();
}
// -----------------------------------------------------
void UnicodeString::format_va(const UnicodeString& format, va_list args)
{
format_va(format.str(), args);
}
// -----------------------------------------------------
void UnicodeString::format_va(const WideChar* format, va_list args)
{
validate();
WideChar buf[MAX_FORMAT_BUF_LEN];
const int result = vswprintf(buf, sizeof(buf)/sizeof(WideChar), format, args);
if (result >= 0)
{
set(buf);
validate();
}
else
{
DEBUG_CRASH(("UnicodeString::format_va failed with code:%d", result));
}
}
//-----------------------------------------------------------------------------
Bool UnicodeString::nextToken(UnicodeString* tok, UnicodeString delimiters)
{
if (this->isEmpty() || tok == this)
return false;
if (delimiters.isEmpty())
delimiters = UnicodeString(L" \t\n\r");
Int offset;
offset = wcsspn(peek(), delimiters.str());
WideChar* start = peek() + offset;
offset = wcscspn(start, delimiters.str());
WideChar* end = start + offset;
if (end > start)
{
Int len = end - start;
WideChar* tmp = tok->getBufferForRead(len + 1);
memcpy(tmp, start, len*2);
tmp[len] = 0;
this->set(end);
return true;
}
else
{
this->clear();
tok->clear();
return false;
}
}