Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
olestring.cpp
Go to the documentation of this file.
1/*
2** Command & Conquer Generals Zero Hour(tm)
3** Copyright 2025 Electronic Arts Inc.
4**
5** This program is free software: you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation, either version 3 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19//
20// OLEString.cpp
21//
22
23
24#include "stdAfx.h"
25#include <assert.h>
26#include "olestring.h"
27
28template void StripSpaces<OLECHAR> ( OLECHAR *string);
29template void StripSpaces<char> ( char *string );
30template void StripSpacesFromMetaString<OLECHAR> ( OLECHAR *string );
31template void StripSpacesFromMetaString<char> ( char *string);
32template void ConvertMetaChars<OLECHAR> ( OLECHAR *string);
33template void ConvertMetaChars<char> ( char *string);
34template int SameFormat<char> ( char *string1, char *string2 );
35template int SameFormat<OLECHAR> ( OLECHAR *string1, OLECHAR *string2 );
36template void EncodeFormat<char> ( char *string );
37template void EncodeFormat<OLECHAR> ( OLECHAR *string );
38template void DecodeFormat<char> ( char *string1 );
39template void DecodeFormat<OLECHAR> ( OLECHAR *string );
40template int IsFormatTypeChar<char> ( char string1 );
41template int IsFormatTypeChar<OLECHAR> ( OLECHAR string );
42
43
44
45static char *format_type = "cCdiouxXeEfgGnpsS"; // printf type as in %<width>.<presicion>{h|l|i64|L}<type>
46
47template <typename text>int IsFormatTypeChar ( text ch )
48{
49 char *ptr = format_type;
50
51 while ( *ptr )
52 {
53 if ( (unsigned int)*ptr++ == (unsigned int) ch )
54 {
55 return TRUE;
56 }
57 }
58 return FALSE;
59}
60
61
62
64{
65 ole = NULL;
66 sb = NULL;
67 len = 0;
68
69 Unlock ();
70 Set ("");
71
72}
73
75{
76 delete [] ole;
77 delete [] sb;
78 ole = NULL;
79 sb = NULL;
80 len = 0;
81
82}
83
84void OLEString::Set ( OLECHAR *new_ole )
85{
86
87 if ( !locked )
88 {
89 delete [] ole;
90 delete [] sb;
91 ole = NULL;
92 sb = NULL;
93
94 len = wcslen ( new_ole );
95 {
96 ole = new OLECHAR[len+1];
97 wcscpy ( ole, new_ole );
98 sb = new char[len+1];
99 sprintf ( sb, "%S", ole );
100 }
101 }
102
103}
104
105void OLEString::Set ( char *new_sb )
106{
107
108 if ( !locked )
109 {
110 delete [] ole;
111 delete [] sb;
112 ole = NULL;
113 sb = NULL;
114
115 len = strlen ( new_sb );
116
117 {
118 ole = new OLECHAR[len+1];
119 swprintf ( ole, L"%S", new_sb );
120 sb = new char[len+1];
121 strcpy ( sb, new_sb );
122 }
123 }
124}
125
127{
128 if ( locked )
129 {
130 return;
131 }
132
133 if ( ole )
134 {
135 ::StripSpaces ( ole );
136 }
137 if ( sb )
138 {
139 ::StripSpaces ( sb );
140 }
141}
142
143
145{
146 char *str, *ptr;
147 char ch, last = -1;
148 int skipall = TRUE;
149 int slash = FALSE;
150
151 if ( !len || locked )
152 {
153 return;
154 }
155
156 char *string = new char[len*2];
157
158 str = string;
159 ptr = sb;
160
161 while ( (ch = *ptr++) )
162 {
163 if ( ch == ' ' )
164 {
165 if ( last == ' ' )
166 {
167 continue;
168 }
169 }
170
171 skipall= FALSE;
172
173 if ( ch == '\\' )
174 {
175 char esc;
176 slash = !slash;
177
178 if ( slash && ((esc = *ptr) == 'n' || esc == 't') )
179 {
180 // remove last space
181 if ( last != ' ' )
182 {
183 *str++ = ' ';
184 }
185
186 *str++ = '\\';
187 ptr++;
188 *str++ = esc;
189 last = *str++ = ' ';
190 continue;
191 }
192 }
193 else
194 {
195 slash = FALSE;
196 }
197
198 last = *str++ = ch;
199 }
200
201 if ( last == ' ' )
202 {
203 str--;
204 }
205
206 *str = 0;
207
208 Set ( string );
209 delete [] string;
210 string = NULL;
211}
212
213template <typename text> void StripSpaces ( text *string )
214{
215 text *str, *ptr;
216 text ch, last = -1;
217 int skipall = TRUE;
218
219 str = ptr = string;
220
221 while ( (ch = *ptr++) )
222 {
223 if ( ch == ' ' )
224 {
225 if ( last == ' ' || skipall )
226 {
227 continue;
228 }
229 }
230
231 if ( ch == '\n' || ch == '\t' )
232 {
233 // remove last space
234 if ( last == ' ' )
235 {
236 str--;
237 }
238
239 skipall = TRUE; // skip all spaces
240 last = *str++ = ch;
241 continue;
242 }
243
244 last = *str++ = ch;
245 skipall = FALSE;
246 }
247
248 if ( last == ' ' )
249 {
250 str--;
251 }
252
253 *str = 0;
254}
255
256template <typename text> void StripSpacesFromMetaString ( text *string )
257{
258 text *str, *ptr;
259 text ch, last = -1;
260 int skipall = TRUE;
261 int slash = FALSE;
262
263 str = ptr = string;
264
265 while ( (ch = *ptr++) )
266 {
267 if ( ch == ' ' )
268 {
269 if ( last == ' ' || skipall )
270 {
271 continue;
272 }
273 }
274
275 skipall= FALSE;
276
277 if ( ch == '\\' )
278 {
279 text esc;
280 slash = !slash;
281
282 if ( slash && (esc = *ptr) == 'n' || esc == 't' )
283 {
284 // remove last space
285 if ( last == ' ' )
286 {
287 str--;
288 }
289
290 skipall = TRUE; // skip all spaces
291 *str++ = '\\';
292 ptr++;
293 last = *str++ = esc;
294 continue;
295 }
296 }
297 else
298 {
299 slash = FALSE;
300 }
301
302 last = *str++ = ch;
303 }
304
305 if ( last == ' ' )
306 {
307 str--;
308 }
309
310 *str = 0;
311}
312
313
314template <typename text> void ConvertMetaChars ( text *string )
315{
316 text *ptr;
317 text ch;
318
319 ptr = string;
320
321 while ( (ch = *string++) )
322 {
323 if ( ch == '\\' )
324 {
325 if ( ch = *string )
326 {
327 switch ( ch )
328 {
329 case 'n':
330 case 'N':
331 ch = '\n';
332 break;
333 case 't':
334 case 'T':
335 ch = '\t';
336 break;
337 }
338 string++;
339 }
340 }
341
342 *ptr++ = ch;
343 }
344
345 *ptr = 0;
346}
347
348template <typename text> int SameFormat ( text *string1, text *string2 )
349{
350
351 while ( *string1 && *string2 )
352 {
353
354 while ( *string1 )
355 {
356 if (*string1 == '%')
357 {
358 string1++;
359 break;
360 }
361
362 if ( *string1 == '\\' )
363 {
364 string1++;
365 }
366
367 if ( *string1 )
368 {
369 string1++;
370 }
371 }
372
373 while ( *string2 )
374 {
375 if (*string2 == '%')
376 {
377 string2++;
378 break;
379 }
380
381 if ( *string2 == '\\' )
382 {
383 string2++;
384 }
385 if ( *string2)
386 {
387 string2++;
388 }
389 }
390
391 if ( !*string1 && !*string2)
392 {
393 return TRUE;
394 }
395
396 int found_type = FALSE;
397
398 while ( *string1 && *string2 && !found_type )
399 {
400 found_type = IsFormatTypeChar ( *string1 );
401
402 if ( *string1 != *string2 )
403 {
404 return FALSE;
405 }
406
407 string1++;
408 string2++;
409 }
410
411 }
412 return TRUE;
413}
414
415template <typename text> void EncodeFormat ( text *string )
416{
417
418}
419
420template <typename text> void DecodeFormat ( text *string )
421{
422
423}
424
#define NULL
Definition BaseType.h:92
#define TRUE
Definition BaseType.h:109
#define FALSE
Definition BaseType.h:113
void Set(OLECHAR *new_ole)
Definition olestring.cpp:84
void Unlock(void)
Definition olestring.h:48
void FormatMetaString(void)
void StripSpaces(void)
OLEString(void)
Definition olestring.cpp:63
template int IsFormatTypeChar< OLECHAR >(OLECHAR string)
template void StripSpaces< char >(char *string)
void EncodeFormat(text *string)
template void StripSpaces< OLECHAR >(OLECHAR *string)
int SameFormat(text *string1, text *string2)
template void EncodeFormat< OLECHAR >(OLECHAR *string)
template int IsFormatTypeChar< char >(char string1)
template void EncodeFormat< char >(char *string)
template void StripSpacesFromMetaString< char >(char *string)
template int SameFormat< OLECHAR >(OLECHAR *string1, OLECHAR *string2)
template void StripSpacesFromMetaString< OLECHAR >(OLECHAR *string)
template void DecodeFormat< OLECHAR >(OLECHAR *string)
template int SameFormat< char >(char *string1, char *string2)
void StripSpacesFromMetaString(text *string)
void ConvertMetaChars(text *string)
template void ConvertMetaChars< char >(char *string)
template void ConvertMetaChars< OLECHAR >(OLECHAR *string)
template void DecodeFormat< char >(char *string1)
void DecodeFormat(text *string)
void StripSpaces(text *string)
int IsFormatTypeChar(text ch)
Definition olestring.cpp:47