forked from CaravelGames/drod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWchar.cpp
More file actions
964 lines (869 loc) · 27.6 KB
/
Copy pathWchar.cpp
File metadata and controls
964 lines (869 loc) · 27.6 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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
// $Id$
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Deadly Rooms of Death.
*
* The Initial Developer of the Original Code is
* Caravel Software.
* Portions created by the Initial Developer are Copyright (C) 2003, 2005
* Caravel Software. All Rights Reserved.
*
* Contributor(s):
* Matt Schikore (schik)
* Mike Rimer (mrimer)
*
* ***** END LICENSE BLOCK ***** */
#include "Wchar.h"
#include "Assert.h"
#include "Types.h"
#include "Ports.h" // for towlower, iswspace
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <sstream>
//Some common small strings. Larger strings are localizable and should be kept in database.
const WCHAR wszAmpersand[] = { We('&'),We(0) };
const WCHAR wszAsterisk[] = { We('*'),We(0) };
const WCHAR wszOpenAngle[] = { We('<'),We(0) };
const WCHAR wszCloseAngle[] = { We('>'),We(0) };
const WCHAR wszColon[] = { We(':'),We(0) };
const WCHAR wszComma[] = { We(','),We(0) };
const WCHAR wszCommaSpace[] = { We(','),We(' '),We(0) };
#ifdef WIN32
const WCHAR wszCRLF[] = { We('\r'),We('\n'),We(0) };
#else
const WCHAR wszCRLF[] = { We('\n'),We(0) };
#endif
const WCHAR wszDollarSign[] = { We('$'),We(0) };
const WCHAR wszElipsis[] = { We('.'),We('.'), We('.'),We(0) };
const WCHAR wszEmpty[] = { We(0) };
const WCHAR wszEqual[] = { We('='),We(0) };
const WCHAR wszExclamation[] = { We('!'),We(0) };
const WCHAR wszForwardSlash[] = { We('/'),We(0) };
const WCHAR wszTimes[] = { We('x'),We(0) };
const WCHAR wszHyphen[] = { We('-'),We(0) };
const WCHAR wszTilde[] = { We('~'),We(0) };
const WCHAR wszParentDir[] = { We('.'),We('.'),We(0) };
const WCHAR wszPercent[] = { We('%'),We(0) };
const WCHAR wszPeriod[] = { We('.'),We(0) };
const WCHAR wszPoundSign[] = { We('#'),We(0) };
const WCHAR wszPlus[] = { We('+'),We(0) };
const WCHAR wszQuestionMark[] = { We('?'),We(0) };
const WCHAR wszQuote[] = { We('\"'),We(0) };
const WCHAR wszLeftBracket[] = { We('['),We(0) };
const WCHAR wszRightBracket[] = { We(']'),We(0) };
const WCHAR wszLeftParen[] = { We('('),We(0) };
const WCHAR wszRightParen[] = { We(')'),We(0) };
const WCHAR wszSemicolon[] = { We(';'),We(0) };
const WCHAR wszSpace[] = { We(' '),We(0) };
const WCHAR wszUnderscore[] = { We('_'),We(0) };
const WCHAR wszZero[] = { We('0'),We(0) };
const WCHAR wszStringToken[] = { We('%'),We('s'),We(0) };
const WCHAR wszOne[] = { We('1'),We(0) };
const WCHAR wszTwo[] = { We('2'),We(0) };
#ifdef WIN32
const WCHAR wszSlash[] = { We('\\'),We(0) };
#else
const WCHAR wszSlash[] = { We('/'),We(0) };
#endif
//HTML formatting strings.
const WCHAR wszHtml[] = { We('<'),We('h'),We('t'),We('m'),We('l'),We('>'), We(0) };
const WCHAR wszEndHtml[] = { We('<'),We('/'),We('h'),We('t'),We('m'),We('l'),We('>'), We(0) };
const WCHAR wszBody[] = { We('<'),We('b'),We('o'),We('d'),We('y'),We('>'), We(0) };
const WCHAR wszEndBody[] = { We('<'),We('/'),We('b'),We('o'),We('d'),We('y'),We('>'), We(0) };
const WCHAR wszHeader[] = { We('<'),We('h'),We('3'),We('>'), We(0) };
const WCHAR wszEndHeader[] = { We('<'),We('/'),We('h'),We('3'),We('>'), We(0) };
const WCHAR wszBTag[] = { We('<'),We('b'),We('>'), We(0) };
const WCHAR wszEndBTag[] = { We('<'),We('/'),We('b'),We('>'), We(0) };
const WCHAR wszITag[] = { We('<'),We('i'),We('>'), We(0) };
const WCHAR wszEndITag[] = { We('<'),We('/'),We('i'),We('>'), We(0) };
const WCHAR wszPTag[] = { We('<'),We('p'),We('>'), We(0) };
static char buffer[WCHAR_INTERNAL_BUFFER];
//*****************************************************************************
void SanitizeSingleLineString(WSTRING &wstr)
//Remove any unwanted characters from a single-line string. Currently strips:
// * both newline characters
// * horizontal tab
// Ultimately if one day we can define the list of allowed characters we can convert it to a whitelist
{
wstr.erase(std::remove(wstr.begin(), wstr.end(), '\n'), wstr.end());
wstr.erase(std::remove(wstr.begin(), wstr.end(), '\r'), wstr.end());
wstr.erase(std::remove(wstr.begin(), wstr.end(), '\t'), wstr.end());
}
//*****************************************************************************
void SanitizeMultiLineString(WSTRING &wstr)
//Remove any unwanted characters from a multi-line string. Currently strips:
// * horizontal tab
{
wstr.erase(std::remove(wstr.begin(), wstr.end(), '\t'), wstr.end());
}
//*****************************************************************************
void AsciiToUnicode(const char *psz, WSTRING &wstr)
{
WCHAR *pwczConvertBuffer = new WCHAR[strlen(psz) + 1];
const unsigned char *pszRead = (unsigned char*)psz;
WCHAR *pwczWrite = pwczConvertBuffer;
while (*pszRead != '\0')
{
pWCv(pwczWrite++) = static_cast<WCHAR_t>(*(pszRead++));
}
pWCv(pwczWrite) = 0;
wstr = pwczConvertBuffer;
delete[] pwczConvertBuffer;
}
//*****************************************************************************
void AsciiToUnicode(const std::string &str, WSTRING &wstr) {
AsciiToUnicode(str.c_str(), wstr);
}
//*****************************************************************************
const WSTRING AsciiToUnicode(const char *psz) {
WSTRING res;
AsciiToUnicode(psz, res);
return res;
}
//******************************************************************************
void CTextToUnicode(const char *psz, WSTRING &wstr)
{
UINT len = wstr.length();
if (!UTF8ToUnicode(psz, wstr))
{
wstr.resize(len);
AsciiToUnicode(psz, wstr);
}
}
//******************************************************************************
bool UTF8ToUnicode(const char *psz, WSTRING &wstr)
{
return UTF8ToUnicode(psz, strlen(psz), wstr);
}
//*****************************************************************************
bool UnicodeToAscii(const WSTRING& wstr, char *psz)
{
WCHAR_t w = 0;
unsigned char* pszWrite = (unsigned char*)psz;
for (WSTRING::const_iterator i = wstr.begin(); i != wstr.end(); ++i)
{
*(pszWrite++) = (unsigned char)pWCv(i);
w |= pWCv(i);
}
*(pszWrite++) = 0;
return !(w & 0xff00);
}
//*****************************************************************************
bool UnicodeToAscii(const WSTRING& wstr, string &str)
{
WCHAR_t w = 0;
str.clear();
str.reserve(wstr.size());
for (WSTRING::const_iterator i = wstr.begin(); i != wstr.end(); ++i)
{
str += (unsigned char)pWCv(i);
w |= pWCv(i);
}
return !(w & 0xff00);
}
//*****************************************************************************
bool UnicodeToAscii(const WCHAR* wsz, string &str)
{
WCHAR_t w = 0;
UINT len = WCSlen(wsz);
str.clear();
str.reserve(len);
for (UINT c = 0; c < len; c++)
{
str += (unsigned char)WCv(wsz[c]);
w |= WCv(wsz[c]);
}
return !(w & 0xff00);
}
//*****************************************************************************
string UnicodeToAscii(const WCHAR* wsz)
{
string str;
UnicodeToAscii(wsz, str);
return str;
}
//*****************************************************************************
void UnicodeToUTF8(const WCHAR *pwsz, string &str)
{
BYTE* bytes = NULL;
UINT len = to_utf8(pwsz, bytes);
str.clear();
str.reserve(len);
for (UINT i = 0; i < len; ++i)
str += bytes[i];
delete[] bytes;
}
//*****************************************************************************
#define MB_BLOCK_BEGIN(mask,cmp) \
ucs4 = (ch & mask) << 6; \
if ((ch = *++*ppsz) < cmp && !ucs4) \
success = false; \
if (ch) do \
{ \
ucs4 += (ch & 0x3f)
#define MB_BLOCK_END \
} while (0)
#define ADD_NEXT_BYTE \
if ((ch = *++*ppsz)) \
{ \
ucs4 <<= 6; \
ucs4 += (ch & 0x3f); \
} \
else return 0xffffffff
unsigned int UTF8ToUCS4Char(
//Convert UTF-8 to UCS-4 (Unicode). (Based on info from RFC 2279.)
//
//Params:
const char **ppsz) //(in/out) Pointer to pointer to UTF-8 char-data.
// The size of the char-data is determined
// automagically, so no need for 0-termination.
// The pointer will be updated to point to the
// end of the current UTF-8 char after the
// call, even if it was an invalid char. If a
// zero byte is encountered, conversion will
// abort and the pointer will point to that.
//
//Returns:
//UCS-4 char, or 0xffffffff if the UTF-8 code was invalid.
{
ASSERTP(ppsz && *ppsz, "NULL pointer passed to UTF8ToUCS4Char");
unsigned char ch = **ppsz;
bool success = true;
UINT ucs4 = 0;
// Could be done with a loop, but this is faster
if (ch < 0x80) // 00-7f: 1 byte
ucs4 = ch;
else if (ch < 0xc2) // 80-c1: error
success = false;
else if (ch < 0xe0) // c2-df: 2 bytes
{
ucs4 = (ch & 0x1f);
ADD_NEXT_BYTE;
}
else if (ch < 0xf0) // e0-ef: 3 bytes
{
MB_BLOCK_BEGIN(0x0f,0xa0);
ADD_NEXT_BYTE;
MB_BLOCK_END;
}
else if (ch < 0xf8) // f0-f7: 4 bytes
{
MB_BLOCK_BEGIN(0x07,0x90);
ADD_NEXT_BYTE;
ADD_NEXT_BYTE;
MB_BLOCK_END;
}
else if (ch < 0xfc) // f8-fb: 5 bytes
{
MB_BLOCK_BEGIN(0x03,0x88);
ADD_NEXT_BYTE;
ADD_NEXT_BYTE;
ADD_NEXT_BYTE;
MB_BLOCK_END;
}
else if (ch < 0xfe) // fc-fd: 6 bytes
{
MB_BLOCK_BEGIN(0x01,0x84);
ADD_NEXT_BYTE;
ADD_NEXT_BYTE;
ADD_NEXT_BYTE;
ADD_NEXT_BYTE;
MB_BLOCK_END;
}
else // fe-ff: error
success = false;
return success ? ucs4 : 0xffffffff;
}
#undef MB_BLOCK_BEGIN
#undef MB_BLOCK_END
#undef ADD_NEXT_BYTE
//*****************************************************************************
bool UTF8ToUnicode(const char* s, const UINT len, WSTRING &wstr)
{
bool ok = true;
wstr.resize(0);
wstr.reserve(len);
for (const char *s_end = s + len; s < s_end; ++s)
{
//UTF-8 to UTF-16 conversion
UINT wc = UTF8ToUCS4Char(&s);
if (wc > 0x10ffff || (wc >= 0xd800 && wc <= 0xdfff))
{
// unknown unicode character
wstr += W_t(0xfffd);
ok = false;
}
else if (wc >= 0x10000)
{
// surrogate pair
wc -= 0x10000;
wstr += W_t(0xd800 + (wc >> 10));
wstr += W_t(0xdc00 + (wc & 0x3ff));
}
else
{
wstr += W_t(wc);
}
}
return ok;
}
//*****************************************************************************
void UTF8ToAscii(const char* s, const UINT len, string &str)
{
str.resize(0);
str.reserve(len);
for (const char *s_end = s + len; s < s_end; ++s)
{
// UTF-8 to ASCII conversion (UCS-4 is just stripped)
str += (unsigned char)UTF8ToUCS4Char(&s);
}
}
//*****************************************************************************
UINT utf8len(const WCHAR* pwczText)
//Returns: byte length of 16-bit Unicode string, converted to utf-8
{
UINT wLength=0;
while (WCv(*pwczText))
{
const UINT ch=WCv(*pwczText++);
if (ch <= 0x7F)
++wLength;
else if (ch <= 0x7FF)
wLength+=2;
else if (ch <= 0xFFFF)
wLength+=3;
else if (ch <= 0x1FFFFF)
wLength+=4;
else if (ch <= 0x3FFFFFF)
wLength+=5;
else
wLength+=6;
}
return wLength;
}
//*****************************************************************************
UINT utf8len(const char* pczText)
//Returns: byte length of ASCII string, converted to utf-8
{
UINT wLength;
for (wLength = 0; *pczText; ++pczText)
{
const unsigned char ch=*((unsigned char*)pczText);
if (ch <= 0x7F)
++wLength;
else wLength+=2;
}
return wLength;
}
//*****************************************************************************
UINT to_utf8(const WCHAR *pwStr, BYTE* &pbOutStr)
//OUT: allocated byte buffer of Unicode-2 text passed in, converted to utf-8.
//Caller must delete allocated byte buffer.
{
ASSERT(pwStr);
ASSERT(!pbOutStr);
const UINT wByteLength = utf8len(pwStr);
pbOutStr = new BYTE[wByteLength+1]; //null terminated
BYTE *res = pbOutStr;
while (WCv(*pwStr))
{
//Convert one Unicode character and append to byte buffer.
const UINT ch=WCv(*pwStr++);
if (ch <= 0x7F) {
*res++=static_cast<char unsigned>(ch);
} else if (ch <= 0x7FF) {
*res++=static_cast<char unsigned>(0xC0 | ((ch >> 6) & 0x1F));
*res++=static_cast<char unsigned>(0x80 | ((ch >> 0) & 0x3F));
} else if (ch <= 0xFFFF) {
*res++=static_cast<char unsigned>(0xE0 | ((ch >> 12) & 0x0F));
*res++=static_cast<char unsigned>(0x80 | ((ch >> 6) & 0x3F));
*res++=static_cast<char unsigned>(0x80 | ((ch >> 0) & 0x3F));
} else if (ch <= 0x1FFFFF) {
*res++=static_cast<char unsigned>(0xF0 | ((ch >> 18) & 0x07));
*res++=static_cast<char unsigned>(0x80 | ((ch >> 12) & 0x3F));
*res++=static_cast<char unsigned>(0x80 | ((ch >> 6) & 0x3F));
*res++=static_cast<char unsigned>(0x80 | ((ch >> 0) & 0x3F));
} else if (ch <= 0x3FFFFFF) {
*res++=static_cast<char unsigned>(0xF8 | ((ch >> 24) & 0x03));
*res++=static_cast<char unsigned>(0x80 | ((ch >> 18) & 0x3F));
*res++=static_cast<char unsigned>(0x80 | ((ch >> 12) & 0x3F));
*res++=static_cast<char unsigned>(0x80 | ((ch >> 6) & 0x3F));
*res++=static_cast<char unsigned>(0x80 | ((ch >> 0) & 0x3F));
} else {
*res++=static_cast<char unsigned>(0xFC | ((ch >> 30) & 0x03));
*res++=static_cast<char unsigned>(0x80 | ((ch >> 24) & 0x3F));
*res++=static_cast<char unsigned>(0x80 | ((ch >> 18) & 0x3F));
*res++=static_cast<char unsigned>(0x80 | ((ch >> 12) & 0x3F));
*res++=static_cast<char unsigned>(0x80 | ((ch >> 6) & 0x3F));
*res++=static_cast<char unsigned>(0x80 | ((ch >> 0) & 0x3F));
}
}
*res = 0;
return wByteLength;
}
//*****************************************************************************
UINT to_utf8(const char *pStr, BYTE* &pbOutStr)
//OUT: allocated byte buffer of ASCII text passed in, converted to utf-8.
//Caller must delete allocated byte buffer.
{
ASSERT(pStr);
ASSERT(!pbOutStr);
const UINT wByteLength = utf8len(pStr);
pbOutStr = new BYTE[wByteLength+1]; //null terminated
BYTE *res = pbOutStr;
for (; *pStr; ++pStr)
{
//Convert one Unicode character and append to byte buffer.
const unsigned char ch=*((unsigned char*)pStr);
if (ch <= 0x7f) {
*res++ = ch;
} else {
*res++ = 0xc0 | ((ch >> 6) & 0x1f);
*res++ = 0x80 | ((ch >> 0) & 0x3f);
}
}
*res = 0;
return wByteLength;
}
//*****************************************************************************
bool charFilenameSafe(const WCHAR wc)
//Returns: whether char can be used in filenames
//
//ASSUME: alphanumeric and space and some punctuation are usable
{
const WCHAR lwc = towlower(wc);
if (wc >= 256)
return true; //assume Unicode chars are usable
return
(lwc >= 'a' && lwc <= 'z') ||
(lwc >= '0' && lwc <= '9') ||
lwc == ' ' || lwc == '-' || lwc == '_';
}
//*****************************************************************************
WSTRING filenameFilter(const WSTRING &wstr)
//Returns: input string with all characters, unusable in filenames, removed
{
WSTRING filteredStr;
for (WSTRING::const_iterator i = wstr.begin(); i != wstr.end(); ++i)
{
const WCHAR wc = (WCHAR)*i;
if (charFilenameSafe(wc))
filteredStr += wc;
}
return filteredStr;
}
//*****************************************************************************
WSTRING filterFirstLettersAndNumbers(const WSTRING &wstr)
//Returns: string with only the first letter from each word, and digits
{
WSTRING filteredStr;
bool bAfterWhiteSpace = true;
for (WSTRING::const_iterator i = wstr.begin(); i != wstr.end(); ++i)
{
const WCHAR wc = (WCHAR)*i;
if (iswspace(wc))
bAfterWhiteSpace = true;
else if ((bAfterWhiteSpace && iswalpha(wc)) || iswdigit(wc))
{
filteredStr += wc;
bAfterWhiteSpace = false;
}
}
return filteredStr;
}
//*****************************************************************************
WSTRING filterUpperCase(const WSTRING &wstr)
//Returns: string with only upper-case characters
{
WSTRING filteredStr;
for (WSTRING::const_iterator i = wstr.begin(); i != wstr.end(); ++i)
{
const WCHAR wc = (WCHAR)*i;
if (iswupper(wc))
filteredStr += wc;
}
return filteredStr;
}
//*****************************************************************************
WCHAR* getFilenameFromPath(const WCHAR *wstrFilepath) //(in) file path + name
//Returns: pointer to filename part of path only
{
const WCHAR *wszFilename = wstrFilepath;
while (*wszFilename != wszEmpty[0]) ++wszFilename; //Search to end.
while (wszFilename >= wstrFilepath && *wszFilename != wszSlash[0])
--wszFilename; //Search backwards to last backslash.
if (*wszFilename == wszSlash[0])
++wszFilename; //go past backslash
return (WCHAR*)wszFilename;
}
//*****************************************************************************
bool isWInteger(const WCHAR* wcz)
//Returns: whether the text is an integer of a form accepted by _Wtoi,
//i.e. "[whitespace] [+|-] digits [whitespace]",
//with nothing else afterwards.
{
string str;
UnicodeToAscii(wcz, str);
return isInteger(str.c_str());
}
//*****************************************************************************
bool isInteger(const char* pcz)
//Returns: whether the text is an integer of a form accepted by atoi,
//i.e. "[whitespace] [+|-] digits [whitespace]",
//with nothing else afterwards.
{
int index=0;
while (isspace(pcz[index]))
++index;
if (pcz[index] == '-' || pcz[index] == '+')
++index;
if (!isdigit(pcz[index]))
return false;
++index; //we just encountered a digit, so don't need to check it again
while (isdigit(pcz[index]))
++index;
while (isspace(pcz[index])) //allow trailing whitespace
++index;
return !pcz[index]; //if nothing is left, this string is only an integer
}
//*****************************************************************************
int _Wtoi(const WCHAR* wsz)
{
string str;
UnicodeToAscii(wsz, str);
return atoi(str.c_str());
}
//*****************************************************************************
WCHAR* _itoW(int value, WCHAR* wcz, int radix, int bufferLength)
{
// Internal buffer's max length should never ever be exceeded
bufferLength = std::min(WCHAR_INTERNAL_BUFFER, bufferLength);
int i = 0;
bool bNegative = false;
if (value < 0) //negative?
{
value = -value;
bNegative = true;
bufferLength--; // We have one character fewers for numbers because we need negative sign
}
bufferLength--; // Make space for null termination char
do {
if (i >= bufferLength) {
ASSERT(!"_itoW: ran out of space in the provided buffer - it will be trimmed.");
break;
}
int val = value % radix;
if (val < 10)
buffer[i++] = (value % radix) + '0';
else buffer[i++] = (value % radix) - 10 + 'A';
} while ((value /= radix) > 0);
if (bNegative)
buffer[i++] = '-';
buffer[i] = '\0';
char c;
int j;
for (i = 0, j = strlen(buffer)-1; i<j; ++i, --j) {
c = buffer[i];
buffer[i] = buffer[j];
buffer[j] = c;
}
WSTRING wStr;
AsciiToUnicode(buffer, wStr);
WCScpy(wcz, wStr.c_str());
return wcz;
}
//*****************************************************************************
UINT WCSlen(const WCHAR* wsz)
{
UINT length = 0;
const WCHAR* wszRead = wsz;
while (*wszRead++ != 0)
++length;
return length;
}
//*****************************************************************************
WCHAR* WCScpy(WCHAR* pwczDest, const WCHAR* pwczSrc)
{
UINT index = 0;
while (pwczSrc[index] != 0)
++index;
memcpy(pwczDest, pwczSrc, (index+1) * sizeof(WCHAR));
return pwczDest;
}
//*****************************************************************************
WCHAR* WCSncpy(WCHAR* pwczDest, const WCHAR* pwczSrc, UINT n)
{
WCHAR *pwczWrite = pwczDest;
const WCHAR *pwczRead = pwczSrc;
while ((*pwczRead != 0) && n--)
{
*(pwczWrite++) = *(pwczRead++);
}
pWCv(pwczWrite) = 0;
return pwczDest;
}
//*****************************************************************************
WCHAR* WCScat(WCHAR *pwcz1, const WCHAR *pwcz2)
{
WCHAR *pwczWrite = pwcz1;
while (*pwczWrite != 0)
++pwczWrite;
const WCHAR *pwczRead = pwcz2;
while (*pwczRead != 0)
*(pwczWrite++) = *(pwczRead++);
pWCv(pwczWrite) = 0;
return pwcz1;
}
//*****************************************************************************
int WCScmp(const WCHAR* pwcz1, const WCHAR* pwcz2)
{
const UINT len1 = WCSlen(pwcz1);
const UINT len2 = WCSlen(pwcz2);
UINT index = 0;
while (index < len1 && index < len2) {
if (pwcz1[index] < pwcz2[index]) return -1;
if (pwcz2[index] < pwcz1[index]) return 1;
++index;
}
// If we get to this point, either they're the same or one string
// is longer than the other.
if (len1 == len2) return 0;
if (len1 < len2) return -1;
return 1;
}
//*****************************************************************************
int WCSicmp(const WCHAR* pwcz1, const WCHAR* pwcz2)
{
const UINT len1 = WCSlen(pwcz1);
const UINT len2 = WCSlen(pwcz2);
UINT index = 0;
while (index < len1 && index < len2) {
if (towlower(pwcz1[index]) < towlower(pwcz2[index])) return -1;
if (towlower(pwcz2[index]) < towlower(pwcz1[index])) return 1;
++index;
}
// If we get to this point, either they're the same or one string
// is longer than the other.
if (len1 == len2) return 0;
if (len1 < len2) return -1;
return 1;
}
//*****************************************************************************
int WCSncmp(const WCHAR* pwcz1, const WCHAR* pwcz2, const UINT n)
{
UINT len1 = 0, len2 = 0;
while (len1 < n && pwcz1[len1] != 0) ++len1;
while (len2 < n && pwcz2[len2] != 0) ++len2;
UINT index = 0;
while (index < len1 && index < len2 && index < n) {
if (pwcz1[index] < pwcz2[index]) return -1;
if (pwcz2[index] < pwcz1[index]) return 1;
++index;
}
// If we get to this point, either n chars have been matched,
// or they're the same, or one string is longer than the other.
if (index == n) return 0;
if (len1 == len2) return 0;
if (len1 < len2) return -1;
return 1;
}
//*****************************************************************************
int WCSnicmp(const WCHAR* pwcz1, const WCHAR* pwcz2, const UINT n)
{
UINT len1 = 0, len2 = 0;
while (len1 < n && pwcz1[len1] != 0) ++len1;
while (len2 < n && pwcz2[len2] != 0) ++len2;
UINT index = 0;
while (index < len1 && index < len2 && index < n) {
if (towlower(pwcz1[index]) < towlower(pwcz2[index])) return -1;
if (towlower(pwcz2[index]) < towlower(pwcz1[index])) return 1;
++index;
}
// If we get to this point, either n chars have been matched,
// or they're the same, or one string is longer than the other.
if (index == n) return 0;
if (len1 == len2) return 0;
if (len1 < len2) return -1;
return 1;
}
//*****************************************************************************
WCHAR* WCStok(WCHAR *wszStart, const WCHAR *wszDelim)
{
static WCHAR *wszNext = NULL;
if ((NULL == wszStart) && (NULL == (wszStart = wszNext)))
return NULL;
UINT dindex;
// Skip initial delimiters
for (; pWCv(wszStart); ++wszStart)
{
for (dindex = 0;
wszDelim[dindex] && (*wszStart != wszDelim[dindex]);
++dindex)
{ }
if (!wszDelim[dindex]) break; // No more initial delimiters
}
// Now hunt for next delimiters
for (wszNext = wszStart; pWCv(wszNext); ++wszNext)
{
for (dindex = 0;
wszDelim[dindex] && (*wszNext != wszDelim[dindex]);
++dindex)
{ }
if (!wszDelim[dindex]) continue;
// Found a delimiter: Terminate token and prepare for next (if any)
pWCv(wszNext++) = 0;
wszNext = (pWCv(wszNext) ? wszNext : NULL);
return wszStart;
}
// No delimiters, end of string
wszNext = NULL;
return wszStart;
}
//*****************************************************************************
WCHAR* fgetWs(WCHAR* pwcz, int n, FILE* pFile)
{
ASSERT(pwcz != NULL);
WCHAR c;
WCHAR* wszWrite = pwcz;
int numread;
do {
numread = fread( &c, sizeof(WCHAR), 1, pFile );
if (numread == 0)
{
//Reached end of file data.
pWCv(wszWrite++) = '\0'; //null terminate string
break;
}
*(wszWrite++) = c;
--n;
} while (c != 0 && n > 0);
return pwcz;
}
//*****************************************************************************
void fputWs (const WCHAR *wsz, FILE *pFile)
{
for (; pWCv(wsz); ++wsz)
putc((unsigned char)pWCv(wsz), pFile);
}
//*****************************************************************************
std::string strReplace(
//Replaces all occurrences of a string within another string.
//Returns: copy of transformed string
std::string const &source, //Source string.
std::string const &from, //The substring to find.
std::string const &to) //Replace instances of 'from'.
{
std::string result;
std::string::size_type processed=0;
for (;;) {
std::string::size_type loc = source.find(from, processed);
if (loc == std::string::npos)
return result + source.substr(processed);
ASSERT(loc>=processed);
result += source.substr(processed, loc-processed)+to;
processed = loc + from.size();
}
}
//*****************************************************************************
WSTRING WCSReplace(
//Replaces all occurrences of a string within another string.
//Returns: copy of transformed string
WSTRING const &source, //Source string.
WSTRING const &from, //The substring to find.
WSTRING const &to) //Replace instances of 'from'.
{
WSTRING result;
WSTRING::size_type processed=0;
for (;;) {
WSTRING::size_type loc = source.find(from, processed);
if (loc == std::string::npos)
return result + source.substr(processed);
ASSERT(loc>=processed);
result += source.substr(processed, loc-processed)+to;
processed = loc + from.size();
}
}
//*****************************************************************************
WSTRING WCSToLower(WSTRING const &source)
//Converts WSTRING to lowercase
{
WSTRING lowercased;
lowercased.reserve(source.size());
for (WSTRING::const_iterator it = source.begin(); it != source.end(); ++it)
lowercased += towlower(*it);
return lowercased;
}
//*****************************************************************************
const std::vector<WSTRING> WCSExplode(WSTRING const &source, WCHAR const delimiter)
// Explodes a string into pieces
// Adapted from: https://stackoverflow.com/a/12967010
{
// Split manually rather than via std::basic_istringstream<WCHAR>/std::getline:
// libc++ (macOS) does not provide the std::ctype<char16_t> facet those require.
std::vector<WSTRING> result;
for (WSTRING::size_type start = 0; start <= source.size(); )
{
const WSTRING::size_type pos = source.find(delimiter, start);
const WSTRING token = (pos == WSTRING::npos)
? source.substr(start)
: source.substr(start, pos - start);
if (!token.empty())
result.push_back(token);
if (pos == WSTRING::npos)
break;
start = pos + 1;
}
return result;
}
//*****************************************************************************
const std::set<WSTRING> WCSExplodeSet(WSTRING const& source, WCHAR const delimiter)
// Explodes a string into pieces, and put them into a set
{
// See WCSExplode: avoid char16_t streams, unsupported by libc++ (macOS).
std::set<WSTRING> result;
for (WSTRING::size_type start = 0; start <= source.size(); )
{
const WSTRING::size_type pos = source.find(delimiter, start);
const WSTRING token = (pos == WSTRING::npos)
? source.substr(start)
: source.substr(start, pos - start);
if (!token.empty())
result.insert(token);
if (pos == WSTRING::npos)
break;
start = pos + 1;
}
return result;
}
//*****************************************************************************
bool WCSContainsAll(WSTRING const &haystack, std::vector<WSTRING> const &needles)
// Returns true if haystack contains every string in needle, even if they overlap
{
for (std::vector<WSTRING>::const_iterator iter = needles.begin(); iter < needles.end(); ++iter)
{
if (haystack.find(*iter) == WSTRING::npos)
return false;
}
return true;
}
//*****************************************************************************
WSTRING to_WSTRING(int value)
// Returns: string representation of input
{
WCHAR temp[32];
_itoW(value, temp, 10, 32);
WSTRING string(temp);
return string;
}