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
468
469
470
471
472
473
474
475
476
477
478
|
// Copyright (C) 2003 Dolphin Project.
// 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, version 2.0.
// 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 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/
#include <stdlib.h>
#include <stdio.h>
#include "StringUtil.h"
// faster than sscanf
bool AsciiToHex(const char* _szValue, u32& result)
{
char *endptr = NULL;
u32 value = strtoul(_szValue,&endptr,16);
if (!endptr || *endptr != '\0')
return false;
result = value;
return true;
}
// Convert AB to it's ascii table entry numbers 0x4142
u32 Ascii2Hex(std::string _Text)
{
// Reset the return value zero
u32 Result = 0;
// Max 32-bit values are supported
size_t Length = _Text.length();
if (Length > 4)
Length = 4;
for (int i = 0; i < (int)Length; i++)
{
// Add up the values, for example RSPE becomes, 0x52000000, then 0x52530000 and so on
Result += _Text.c_str()[i] << ((Length - 1 - i) * 8);
}
// Return the value
return Result;
}
// Convert it back again
std::string Hex2Ascii(u32 _Text)
{
// Create temporary storate
char Result[5]; // need space for the final \0
// Go through the four characters
sprintf(Result, "%c%c%c%c", _Text >> 24, _Text >> 16, _Text >> 8, _Text);
// Return the string
std::string StrResult = Result;
return StrResult;
}
bool CharArrayFromFormatV(char* out, int outsize, const char* format, va_list args)
{
int writtenCount = vsnprintf(out, outsize, format, args);
if (writtenCount > 0 && writtenCount < outsize)
{
out[writtenCount] = '\0';
return true;
}
else
{
out[outsize - 1] = '\0';
return false;
}
}
// Expensive!
void ToStringFromFormat(std::string* out, const char* format, ...)
{
int writtenCount = -1;
int newSize = (int)strlen(format) + 4;
char *buf = 0;
va_list args;
while (writtenCount < 0)
{
delete [] buf;
buf = new char[newSize + 1];
va_start(args, format);
writtenCount = vsnprintf(buf, newSize, format, args);
va_end(args);
if (writtenCount >= (int)newSize) {
writtenCount = -1;
}
// ARGH! vsnprintf does no longer return -1 on truncation in newer libc!
// WORKAROUND! let's fake the old behaviour (even though it's less efficient).
// TODO: figure out why the fix causes an invalid read in strlen called from vsnprintf :(
// if (writtenCount >= (int)newSize)
// writtenCount = -1;
newSize *= 2;
}
buf[writtenCount] = '\0';
*out = buf;
delete[] buf;
}
std::wstring StringFromFormat(const wchar_t* format, ...)
{
int writtenCount = -1;
int newSize = (int)wcslen(format) + 4;
wchar_t *buf = 0;
va_list args;
while (writtenCount < 0)
{
delete [] buf;
buf = new wchar_t[newSize + 1];
va_start(args, format);
writtenCount = vswprintf(buf, newSize, format, args);
va_end(args);
if (writtenCount >= (int)newSize) {
writtenCount = -1;
}
// ARGH! vsnprintf does no longer return -1 on truncation in newer libc!
// WORKAROUND! let's fake the old behaviour (even though it's less efficient).
// TODO: figure out why the fix causes an invalid read in strlen called from vsnprintf :(
// if (writtenCount >= (int)newSize)
// writtenCount = -1;
newSize *= 2;
}
buf[writtenCount] = '\0';
std::wstring temp = buf;
delete[] buf;
return temp;
}
std::string StringFromFormat(const char* format, ...)
{
int writtenCount = -1;
int newSize = (int)strlen(format) + 4;
char *buf = 0;
va_list args;
while (writtenCount < 0)
{
delete [] buf;
buf = new char[newSize + 1];
va_start(args, format);
writtenCount = vsnprintf(buf, newSize, format, args);
va_end(args);
if (writtenCount >= (int)newSize) {
writtenCount = -1;
}
// ARGH! vsnprintf does no longer return -1 on truncation in newer libc!
// WORKAROUND! let's fake the old behaviour (even though it's less efficient).
// TODO: figure out why the fix causes an invalid read in strlen called from vsnprintf :(
// if (writtenCount >= (int)newSize)
// writtenCount = -1;
newSize *= 2;
}
buf[writtenCount] = '\0';
std::string temp = buf;
delete[] buf;
return temp;
}
// For Debugging. Read out an u8 array.
std::string ArrayToString(const u8 *data, u32 size, u32 offset, int line_len, bool Spaces)
{
std::string Tmp, Spc;
if (Spaces) Spc = " "; else Spc = "";
for (u32 i = 0; i < size; i++)
{
Tmp += StringFromFormat("%02x%s", data[i + offset], Spc.c_str());
if(i > 1 && (i + 1) % line_len == 0) Tmp.append("\n"); // break long lines
}
return Tmp;
}
// Turns " hej " into "hej". Also handles tabs.
std::string StripSpaces(const std::string &str)
{
std::string s = str;
int i;
for (i = 0; i < (int)s.size(); i++)
{
if ((s[i] != ' ') && (s[i] != 9))
{
break;
}
}
s = s.substr(i);
for (i = (int)s.size() - 1; i > 0; i--)
{
if ((s[i] != ' ') && (s[i] != 9))
{
break;
}
}
return s.substr(0, i + 1);
}
// "\"hello\"" is turned to "hello"
// This one assumes that the string has already been space stripped in both
// ends, as done by StripSpaces above, for example.
std::string StripQuotes(const std::string& s)
{
if ((s[0] == '\"') && (s[s.size() - 1] == '\"'))
return s.substr(1, s.size() - 2);
else
return s;
}
// "\"hello\"" is turned to "hello"
// This one assumes that the string has already been space stripped in both
// ends, as done by StripSpaces above, for example.
std::string StripNewline(const std::string& s)
{
if (!s.size())
return s;
else if (s[s.size() - 1] == '\n')
return s.substr(0, s.size() - 1);
else
return s;
}
bool TryParseInt(const char* str, int* outVal)
{
char *endptr = NULL;
int value = strtol(str,&endptr,10);
if (!endptr || *endptr != '\0')
return false;
*outVal = value;
return true;
}
bool TryParseBool(const char* str, bool* output)
{
if ((str[0] == '1') || !strcasecmp(str, "true"))
{
*output = true;
return true;
}
else if (str[0] == '0' || !strcasecmp(str, "false"))
{
*output = false;
return true;
}
return false;
}
std::string StringFromInt(int value)
{
char temp[16];
sprintf(temp, "%i", value);
return std::string(temp);
}
std::string StringFromBool(bool value)
{
return value ? "True" : "False";
}
#ifdef _WIN32
bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _pFilename, std::string* _pExtension)
{
char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
char fname[_MAX_FNAME];
char ext[_MAX_EXT];
if (_splitpath_s(full_path.c_str(), drive, _MAX_DRIVE, dir, _MAX_DIR, fname, _MAX_FNAME, ext, _MAX_EXT) == 0)
{
if (_pPath)
{
*_pPath = std::string(drive) + std::string(dir);
}
if (_pFilename != 0)
{
*_pFilename = fname;
}
if (_pExtension != 0)
{
*_pExtension = ext;
}
return true;
}
return false;
}
#else
bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _pFilename, std::string* _pExtension)
{
size_t last_slash = full_path.rfind('/');
if (last_slash == std::string::npos)
{
return false; // FIXME return the filename
}
size_t last_dot = full_path.rfind('.');
if ((last_dot == std::string::npos) || (last_dot < last_slash))
{
return false; // FIXME why missing . is critical?
}
if (_pPath)
{
*_pPath = full_path.substr(0, last_slash + 1);
}
if (_pFilename)
{
*_pFilename = full_path.substr(last_slash + 1, last_dot - (last_slash + 1));
}
if (_pExtension)
{
*_pExtension = full_path.substr(last_dot + 1);
_pExtension->insert(0, ".");
}
else if (_pFilename)
{
*_pFilename += full_path.substr(last_dot);
}
return true;
}
#endif
void BuildCompleteFilename(std::string& _CompleteFilename, const std::string& _Path, const std::string& _Filename)
{
_CompleteFilename = _Path;
// check for seperator
if (_CompleteFilename[_CompleteFilename.size() - 1] != DIR_SEP_CHR)
{
#ifdef _WIN32
if (_CompleteFilename[_CompleteFilename.size() - 1] != '\\')
#endif
_CompleteFilename += DIR_SEP_CHR;
}
// add the filename
_CompleteFilename += _Filename;
}
void SplitString(const std::string& str, const std::string& delim, std::vector<std::string>& output)
{
output.clear();
size_t offset = 0;
size_t delimIndex = 0;
delimIndex = str.find(delim, offset);
while (delimIndex != std::string::npos)
{
output.push_back(str.substr(offset, delimIndex - offset));
offset += delimIndex - offset + delim.length();
delimIndex = str.find(delim, offset);
}
output.push_back(str.substr(offset));
}
bool TryParseUInt(const std::string& str, u32* output)
{
char *endptr = NULL;
u32 value = strtoul(str.c_str(),&endptr,0);
if (!endptr || *endptr != '\0')
return false;
*output = value;
return true;
}
int ChooseStringFrom(const char* str, const char* * items)
{
int i = 0;
while (items[i] != 0)
{
if (!strcmp(str, items[i]))
return i;
i++;
}
return -1;
}
// Thousand separator. Turns 12345678 into 12,345,678
std::string ThS(int Integer, bool Unsigned, int Spaces)
{
// Create storage space
char cbuf[20];
// Determine treatment of signed or unsigned
if(Unsigned) sprintf(cbuf, "%u", Integer); else sprintf(cbuf, "%i", Integer);
std::string Sbuf = cbuf;
for (u32 i = 0; i < Sbuf.length(); ++i)
{
if((i & 3) == 3)
{
Sbuf.insert(Sbuf.length() - i, ",");
}
}
// Spaces
std::string Spc = "";
for (int i = 0; i < (int)(Spaces - Sbuf.length()); i++) Spc += " ";
return Spc + Sbuf;
}
void NormalizeDirSep(std::string* str)
{
#ifdef _WIN32
int i;
while ((i = (int)str->find_first_of('\\')) >= 0)
{
str->replace(i, 1, DIR_SEP);
}
#endif
}
std::string TabsToSpaces(int tab_size, const std::string &in)
{
std::string out;
int len = 0;
// First, compute the size of the new string.
for (unsigned i = 0; i < in.size(); i++)
{
if (in[i] == '\t')
len += tab_size;
else
len += 1;
}
out.resize(len);
int out_ctr = 0;
for (unsigned i = 0; i < in.size(); i++)
{
if (in[i] == '\t')
{
for (int j = 0; j < tab_size; j++)
out[out_ctr++] = ' ';
}
else
{
out[out_ctr++] = in[i];
}
}
return out;
}
std::string PathToFilename(std::string Path)
{
std::string Name, Ending;
SplitPath(Path, 0, &Name, &Ending);
return Name + Ending;
}
|