forked from Open-Shell/Open-Shell-Menu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringUtils.cpp
More file actions
484 lines (450 loc) · 9.57 KB
/
StringUtils.cpp
File metadata and controls
484 lines (450 loc) · 9.57 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
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
479
480
481
482
483
484
// Classic Shell (c) 2009-2017, Ivo Beltchev
// Open-Shell (c) 2017-2018, The Open-Shell Team
// Confidential information of Ivo Beltchev. Not for disclosure or distribution without prior written consent from the author
// StringUtils is also available under the CPOL license as part of the FormatString article
// on CodeProject: http://www.codeproject.com/KB/string/FormatString.aspx
#include <stdafx.h>
#include "StringUtils.h"
#include <stdio.h>
#include <assert.h>
#ifndef STR_USE_WIN32_CONV
#include <stdlib.h>
#endif
///////////////////////////////////////////////////////////////////////////////
#ifdef STR_USE_WIN32_DBCS
static int g_MaxCharSize=0;
#endif
// Returns 1 if the ANSI code page is single-byte, or 2 if it is double-byte (DBCS)
int GetMaxCharSize( void )
{
#ifdef STR_USE_WIN32_DBCS
if (!g_MaxCharSize)
{
CPINFO info;
if (GetCPInfo(CP_ACP,&info))
g_MaxCharSize=info.MaxCharSize;
}
return g_MaxCharSize;
#else
return MB_CUR_MAX;
#endif
}
// Copies src to dst. size is the size of dst in bytes, including the terminating 0.
// Returns the number of characters copied, excluding the terminating 0.
// The return value is <=size-1. If size is 0, returns 0 and does nothing.
// The result in dst is always 0 terminated.
int Strcpy( char *dst, int size, const char *src )
{
assert(dst);
assert(src);
assert(size>0);
if (size<=0) return 0;
char *dst0=dst;
if (GetMaxCharSize()==1)
{
// SBCS version
while (size>1)
{
if (*src==0) break;
*dst++=*src++;
size--;
}
}
else
{
// DBCS version
while (size>1)
{
if (*src==0) break;
#ifdef STR_USE_WIN32_DBCS
if (IsDBCSLeadByte((BYTE)*src))
{
#else
if (isleadbyte((unsigned char)*src))
{
#endif
if (size==2) break;
if (src[1]==0) break;
*dst++=*src++;
*dst++=*src++;
size-=2;
}
else
{
*dst++=*src++;
size--;
}
}
}
*dst=0;
return (int)(dst-dst0);
}
int Strcpy( wchar_t *dst, int size, const wchar_t *src )
{
assert(dst);
assert(src);
assert(size>0);
if (size<=0) return 0;
int len=Strlen(src);
if (len>size-1)
len=size-1;
if (len>0 && src[len]>=0xDC00 && src[len]<=0xDFFF) // check for trailing surrogate
len--;
memcpy(dst,src,len*sizeof(wchar_t));
dst[len]=0;
return len;
}
// Copies src to dst. size is the size of dst in characters, including the terminating 0.
// Copies up to len characters and always appends terminating 0.
int Strncpy( char *dst, int size, const char *src, int len )
{
assert(dst);
assert(src);
assert(size>0);
if (size<=0) return 0;
char *dst0=dst;
const char *end=src+len;
if (GetMaxCharSize()==1)
{
// SBCS version
while (size>1)
{
if (src==end) break;
*dst++=*src++;
size--;
}
}
else
{
// DBCS version
while (size>1)
{
if (src==end) break;
#ifdef STR_USE_WIN32_DBCS
if (IsDBCSLeadByte((BYTE)*src))
{
#else
if (isleadbyte((unsigned char)*src))
{
#endif
if (size==2) break;
if (src+1==end) break;
*dst++=*src++;
*dst++=*src++;
size-=2;
}
else
{
*dst++=*src++;
size--;
}
}
}
*dst=0;
return (int)(dst-dst0);
}
int Strncpy( wchar_t *dst, int size, const wchar_t *src, int len )
{
assert(dst);
assert(src);
assert(size>0);
if (size<=0) return 0;
if (len>size-1)
len=size-1;
if (len>0 && src[len]>=0xDC00 && src[len]<=0xDFFF) // check for trailing surrogate
len--;
memcpy(dst,src,len*sizeof(wchar_t));
dst[len]=0;
return len;
}
// Appends src to dst. size is the size of dst in bytes, including the terminating 0.
// Returns the number of characters copied, excluding the terminating 0.
// The return value is <=size-1-strlen(dst). If size>=strlen(dst), returns 0 and does nothing.
// The result in dst is always 0 terminated.
int Strcat( char *dst, int size, const char *src )
{
assert(dst);
int len=Strlen(dst);
assert(len<size);
return Strcpy(dst+len,size-len,src);
}
int Strcat( wchar_t *dst, int size, const wchar_t *src )
{
assert(dst);
int len=Strlen(dst);
assert(len<size);
return Strcpy(dst+len,size-len,src);
}
// Writes formatted string to dst. size is the size of dst in characters, including the terminating 0.
// Returns the number of characters written, excluding the terminating 0.
// The return value is <=size-1. If size is 0, returns 0 and does nothing.
// The result in dst is always 0 terminated.
int Sprintf( char *dst, int size, const char *format, ... )
{
va_list args;
va_start(args,format);
int len=Vsprintf(dst,size,format,args);
va_end(args);
return len;
}
int Sprintf( wchar_t *dst, int size, const wchar_t *format, ... )
{
va_list args;
va_start(args,format);
int len=Vsprintf(dst,size,format,args);
va_end(args);
return len;
}
int Vsprintf( char *dst, int size, const char *format, va_list args )
{
assert(dst);
assert(format);
assert(size>0);
if (size<=0) return 0;
#if _MSC_VER>=1400 // VC8.0
int len=_vsnprintf_s(dst,size,size-1,format,args);
#else
int len=_vsnprintf(dst,size-1,format,args);
#endif
if (len<0)
len=size-1;
dst[len]=0;
return len;
}
int Vsprintf( wchar_t *dst, int size, const wchar_t *format, va_list args )
{
assert(dst);
assert(format);
assert(size>0);
if (size<=0) return 0;
#if _MSC_VER>=1400 // VC8.0
int len=_vsnwprintf_s(dst,size,size-1,format,args);
#else
int len=_vsnwprintf(dst,size-1,format,args);
#endif
if (len<0)
len=size-1;
dst[len]=0;
return len;
}
// Outputs a formatted debug string
void Trace( const char *format, ... )
{
va_list args;
va_start(args,format);
char buf[1024];
Vsprintf(buf,_countof(buf)-2,format,args);
Strcat(buf,_countof(buf),"\r\n");
OutputDebugStringA(buf);
va_end(args);
}
void Trace( const wchar_t *format, ... )
{
va_list args;
va_start(args,format);
wchar_t buf[1024];
Vsprintf(buf,_countof(buf)-2,format,args);
Strcat(buf,_countof(buf),L"\r\n");
OutputDebugStringW(buf);
va_end(args);
}
// Convert between multi-byte and wide characters. size is the size of dst in characters, including the
// terminating 0.
// Return the number of characters copied, excluding the terminating 0.
// The return value is <=size-1. If size is 0, returns 0 and does nothing.
// The result in dst is always 0 terminated.
#ifdef STR_USE_WIN32_CONV
int MbsToWcs( wchar_t *dst, int size, const char *src, int codePage )
{
if (!dst)
return MultiByteToWideChar(codePage,0,src,Strlen(src),NULL,0);
assert(size);
if (size==0) return 0;
if (size==1)
{
dst[0]=0;
return 0;
}
int len=Strlen(src);
dst[size-2]=0;
int res=MultiByteToWideChar(codePage,0,src,len,dst,size-1);
if (res)
{
// the result fits
dst[res]=0;
return res;
}
if (GetLastError()!=ERROR_INSUFFICIENT_BUFFER)
{ // some unknown error
dst[0]=0;
return 0;
}
if (!dst[size-2]) // could not fit a surrogate pair
return size-2;
dst[size-1]=0;
return size-1;
}
int WcsToMbs( char *dst, int size, const wchar_t *src, int codePage )
{
if (!dst)
return WideCharToMultiByte(codePage,0,src,Strlen(src),NULL,0,NULL,NULL);
assert(size);
if (size==0) return 0;
if (size==1)
{
dst[0]=0;
return 0;
}
int len=Strlen(src);
int l=size;
if (l>10) l=10;
memset(dst+size-l,0,l); // fill the end with zeros (up to 10 bytes)
int res=WideCharToMultiByte(codePage,0,src,len,dst,size-1,NULL,NULL);
if (res)
{
// the result fits
dst[res]=0;
return res;
}
if (GetLastError()!=ERROR_INSUFFICIENT_BUFFER) // some unknown error
{
dst[0]=0;
return 0;
}
// find the last non-zero to return the correct length
for (len=size-1;len>0;len--)
if (dst[len-1])
return len;
return 0;
}
#else
int MbsToWcs( wchar_t *dst, int size, const char *src )
{
if (!dst)
{
#if _MSC_VER>=1400 // VC8.0
size_t res;
if (mbstowcs_s(&res,NULL,0,src,0)!=0)
return 0;
return (int)res-1;
#else
int res=(int)mbstowcs(NULL,src,0);
if (res<0) return 0;
return res;
#endif
}
assert(size);
if (size==0) return 0;
if (size==1)
{
dst[0]=0;
return 0;
}
#if _MSC_VER>=1400 // VC8.0
size_t res;
mbstowcs_s(&res,dst,size,src,_TRUNCATE);
return (int)res-1;
#else
int res=(int)mbstowcs(dst,src,size-1);
if (res<0)
{
dst[0]=0;
return 0;
}
if (res==size-1)
dst[res]=0;
return res;
#endif
}
int WcsToMbs( char *dst, int size, const wchar_t *src )
{
if (!dst)
{
#if _MSC_VER>=1400 // VC8.0
size_t res;
if (wcstombs_s(&res,NULL,0,src,0)!=0)
return 0;
return (int)res-1;
#else
int res=(int)wcstombs(NULL,src,0);
if (res<0) return 0;
return res;
#endif
}
assert(size);
if (size==0) return 0;
if (size==1)
{
dst[0]=0;
return 0;
}
#if _MSC_VER>=1400 // VC8.0
size_t res;
if (wcstombs_s(&res,dst,size,src,_TRUNCATE)!=0)
{
dst[0]=0;
return 0;
}
return (int)res-1;
#else
int res=(int)wcstombs(dst,src,size-1);
if (res<0)
{
dst[0]=0;
return 0;
}
if (res==size-1)
dst[res]=0;
return res;
#endif
}
#endif
///////////////////////////////////////////////////////////////////////////////
const char *GetToken( const char *text, char *token, int size, const char *separators )
{
while (*text && strchr(separators,*text))
text++;
const char *c1=text,*c2;
if (text[0]=='\"')
{
c1++;
c2=strchr(c1,'\"');
}
else
{
c2=c1;
while (*c2!=0 && !strchr(separators,*c2))
c2++;
}
if (!c2) c2=text+strlen(text);
int l=(int)(c2-c1);
if (l>size-1) l=size-1;
memcpy(token,c1,l);
token[l]=0;
if (*c2) return c2+1;
else return c2;
}
const wchar_t *GetToken( const wchar_t *text, wchar_t *token, int size, const wchar_t *separators )
{
while (*text && wcschr(separators,*text))
text++;
const wchar_t *c1=text,*c2;
if (text[0]=='\"')
{
c1++;
c2=wcschr(c1,'\"');
}
else
{
c2=c1;
while (*c2!=0 && !wcschr(separators,*c2))
c2++;
}
if (!c2) c2=text+wcslen(text);
int l=(int)(c2-c1);
if (l>size-1) l=size-1;
memcpy(token,c1,l*2);
token[l]=0;
if (*c2) return c2+1;
else return c2;
}