-
Notifications
You must be signed in to change notification settings - Fork 4
/
stdstring.h
4376 lines (3803 loc) · 134 KB
/
stdstring.h
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
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// =============================================================================
// FILE: StdString.h
// AUTHOR: Joe O'Leary (with outside help noted in comments)
//
// If you find any bugs in this code, please let me know:
//
// jmoleary@earthlink.net
// http://www.joeo.net/stdstring.htm (a bit outdated)
//
// The latest version of this code should always be available at the
// following link:
//
// http://www.joeo.net/code/StdString.zip (Dec 6, 2003)
//
//
// REMARKS:
// This header file declares the CStdStr template. This template derives
// the Standard C++ Library basic_string<> template and add to it the
// the following conveniences:
// - The full MFC CString set of functions (including implicit cast)
// - writing to/reading from COM IStream interfaces
// - Functional objects for use in STL algorithms
//
// From this template, we intstantiate two classes: CStdStringA and
// CStdStringW. The name "CStdString" is just a #define of one of these,
// based upone the UNICODE macro setting
//
// This header also declares our own version of the MFC/ATL UNICODE-MBCS
// conversion macros. Our version looks exactly like the Microsoft's to
// facilitate portability.
//
// NOTE:
// If you you use this in an MFC or ATL build, you should include either
// afx.h or atlbase.h first, as appropriate.
//
// PEOPLE WHO HAVE CONTRIBUTED TO THIS CLASS:
//
// Several people have helped me iron out problems and othewise improve
// this class. OK, this is a long list but in my own defense, this code
// has undergone two major rewrites. Many of the improvements became
// necessary after I rewrote the code as a template. Others helped me
// improve the CString facade.
//
// Anyway, these people are (in chronological order):
//
// - Pete the Plumber (???)
// - Julian Selman
// - Chris (of Melbsys)
// - Dave Plummer
// - John C Sipos
// - Chris Sells
// - Nigel Nunn
// - Fan Xia
// - Matthew Williams
// - Carl Engman
// - Mark Zeren
// - Craig Watson
// - Rich Zuris
// - Karim Ratib
// - Chris Conti
// - Baptiste Lepilleur
// - Greg Pickles
// - Jim Cline
// - Jeff Kohn
// - Todd Heckel
// - Ullrich Poll鋒ne
// - Joe Vitaterna
// - Joe Woodbury
// - Aaron (no last name)
// - Joldakowski (???)
// - Scott Hathaway
// - Eric Nitzche
// - Pablo Presedo
// - Farrokh Nejadlotfi
// - Jason Mills
// - Igor Kholodov
// - Mike Crusader
// - John James
// - Wang Haifeng
// - Tim Dowty
// - Arnt Witteveen
// - Glen Maynard
// - Paul DeMarco
// - Bagira (full name?)
// - Ronny Schulz
// - Jakko Van Hunen
// - Charles Godwin
// - Henk Demper
// - Greg Marr
// - Bill Carducci
// - Brian Groose
// - MKingman
// - Don Beusee
//
// REVISION HISTORY
//
// 2005-JAN-10 - Thanks to Don Beusee for pointing out the danger in mapping
// length-checked formatting functions to non-length-checked
// CRT equivalents. Also thanks to him for motivating me to
// optimize my implementation of Replace()
//
// 2004-APR-22 - A big, big thank you to "MKingman" (whoever you are) for
// finally spotting a silly little error in StdCodeCvt that
// has been causing me (and users of CStdString) problems for
// years in some relatively rare conversions. I had reversed
// two length arguments.
//
// 2003-NOV-24 - Thanks to a bunch of people for helping me clean up many
// compiler warnings (and yes, even a couple of actual compiler
// errors). These include Henk Demper for figuring out how
// to make the Intellisense work on with CStdString on VC6,
// something I was never able to do. Greg Marr pointed out
// a compiler warning about an unreferenced symbol and a
// problem with my version of Load in MFC builds. Bill
// Carducci took a lot of time with me to help me figure out
// why some implementations of the Standard C++ Library were
// returning error codes for apparently successful conversions
// between ASCII and UNICODE. Finally thanks to Brian Groose
// for helping me fix compiler signed unsigned warnings in
// several functions.
//
// 2003-JUL-10 - Thanks to Charles Godwin for making me realize my 'FmtArg'
// fixes had inadvertently broken the DLL-export code (which is
// normally commented out. I had to move it up higher. Also
// this helped me catch a bug in ssicoll that would prevent
// compilation, otherwise.
//
// 2003-MAR-14 - Thanks to Jakko Van Hunen for pointing out a copy-and-paste
// bug in one of the overloads of FmtArg.
//
// 2003-MAR-10 - Thanks to Ronny Schulz for (twice!) sending me some changes
// to help CStdString build on SGI and for pointing out an
// error in placement of my preprocessor macros for ssfmtmsg.
//
// 2002-NOV-26 - Thanks to Bagira for pointing out that my implementation of
// SpanExcluding was not properly handling the case in which
// the string did NOT contain any of the given characters
//
// 2002-OCT-21 - Many thanks to Paul DeMarco who was invaluable in helping me
// get this code working with Borland's free compiler as well
// as the Dev-C++ compiler (available free at SourceForge).
//
// 2002-SEP-13 - Thanks to Glen Maynard who helped me get rid of some loud
// but harmless warnings that were showing up on g++. Glen
// also pointed out that some pre-declarations of FmtArg<>
// specializations were unnecessary (and no good on G++)
//
// 2002-JUN-26 - Thanks to Arnt Witteveen for pointing out that I was using
// static_cast<> in a place in which I should have been using
// reinterpret_cast<> (the ctor for unsigned char strings).
// That's what happens when I don't unit-test properly!
// Arnt also noticed that CString was silently correcting the
// 'nCount' argument to Left() and Right() where CStdString was
// not (and crashing if it was bad). That is also now fixed!
//
// 2002-FEB-25 - Thanks to Tim Dowty for pointing out (and giving me the fix
// for) a conversion problem with non-ASCII MBCS characters.
// CStdString is now used in my favorite commercial MP3 player!
//
// 2001-DEC-06 - Thanks to Wang Haifeng for spotting a problem in one of the
// assignment operators (for _bstr_t) that would cause compiler
// errors when refcounting protection was turned off.
//
// 2001-NOV-27 - Remove calls to operator!= which involve reverse_iterators
// due to a conflict with the rel_ops operator!=. Thanks to
// John James for pointing this out.
//
// 2001-OCT-29 - Added a minor range checking fix for the Mid function to
// make it as forgiving as CString's version is. Thanks to
// Igor Kholodov for noticing this.
// - Added a specialization of std::swap for CStdString. Thanks
// to Mike Crusader for suggesting this! It's commented out
// because you're not supposed to inject your own code into the
// 'std' namespace. But if you don't care about that, it's
// there if you want it
// - Thanks to Jason Mills for catching a case where CString was
// more forgiving in the Delete() function than I was.
//
// 2001-JUN-06 - I was violating the Standard name lookup rules stated
// in [14.6.2(3)]. None of the compilers I've tried so
// far apparently caught this but HP-UX aCC 3.30 did. The
// fix was to add 'this->' prefixes in many places.
// Thanks to Farrokh Nejadlotfi for this!
//
// 2001-APR-27 - StreamLoad was calculating the number of BYTES in one
// case, not characters. Thanks to Pablo Presedo for this.
//
// 2001-FEB-23 - Replace() had a bug which caused infinite loops if the
// source string was empty. Fixed thanks to Eric Nitzsche.
//
// 2001-FEB-23 - Scott Hathaway was a huge help in providing me with the
// ability to build CStdString on Sun Unix systems. He
// sent me detailed build reports about what works and what
// does not. If CStdString compiles on your Unix box, you
// can thank Scott for it.
//
// 2000-DEC-29 - Joldakowski noticed one overload of Insert failed to do a
// range check as CString's does. Now fixed -- thanks!
//
// 2000-NOV-07 - Aaron pointed out that I was calling static member
// functions of char_traits via a temporary. This was not
// technically wrong, but it was unnecessary and caused
// problems for poor old buggy VC5. Thanks Aaron!
//
// 2000-JUL-11 - Joe Woodbury noted that the CString::Find docs don't match
// what the CString::Find code really ends up doing. I was
// trying to match the docs. Now I match the CString code
// - Joe also caught me truncating strings for GetBuffer() calls
// when the supplied length was less than the current length.
//
// 2000-MAY-25 - Better support for STLPORT's Standard library distribution
// - Got rid of the NSP macro - it interfered with Koenig lookup
// - Thanks to Joe Woodbury for catching a TrimLeft() bug that
// I introduced in January. Empty strings were not getting
// trimmed
//
// 2000-APR-17 - Thanks to Joe Vitaterna for pointing out that ReverseFind
// is supposed to be a const function.
//
// 2000-MAR-07 - Thanks to Ullrich Poll鋒ne for catching a range bug in one
// of the overloads of assign.
//
// 2000-FEB-01 - You can now use CStdString on the Mac with CodeWarrior!
// Thanks to Todd Heckel for helping out with this.
//
// 2000-JAN-23 - Thanks to Jim Cline for pointing out how I could make the
// Trim() function more efficient.
// - Thanks to Jeff Kohn for prompting me to find and fix a typo
// in one of the addition operators that takes _bstr_t.
// - Got rid of the .CPP file - you only need StdString.h now!
//
// 1999-DEC-22 - Thanks to Greg Pickles for helping me identify a problem
// with my implementation of CStdString::FormatV in which
// resulting string might not be properly NULL terminated.
//
// 1999-DEC-06 - Chris Conti pointed yet another basic_string<> assignment
// bug that MS has not fixed. CStdString did nothing to fix
// it either but it does now! The bug was: create a string
// longer than 31 characters, get a pointer to it (via c_str())
// and then assign that pointer to the original string object.
// The resulting string would be empty. Not with CStdString!
//
// 1999-OCT-06 - BufferSet was erasing the string even when it was merely
// supposed to shrink it. Fixed. Thanks to Chris Conti.
// - Some of the Q172398 fixes were not checking for assignment-
// to-self. Fixed. Thanks to Baptiste Lepilleur.
//
// 1999-AUG-20 - Improved Load() function to be more efficient by using
// SizeOfResource(). Thanks to Rich Zuris for this.
// - Corrected resource ID constructor, again thanks to Rich.
// - Fixed a bug that occurred with UNICODE characters above
// the first 255 ANSI ones. Thanks to Craig Watson.
// - Added missing overloads of TrimLeft() and TrimRight().
// Thanks to Karim Ratib for pointing them out
//
// 1999-JUL-21 - Made all calls to GetBuf() with no args check length first.
//
// 1999-JUL-10 - Improved MFC/ATL independence of conversion macros
// - Added SS_NO_REFCOUNT macro to allow you to disable any
// reference-counting your basic_string<> impl. may do.
// - Improved ReleaseBuffer() to be as forgiving as CString.
// Thanks for Fan Xia for helping me find this and to
// Matthew Williams for pointing it out directly.
//
// 1999-JUL-06 - Thanks to Nigel Nunn for catching a very sneaky bug in
// ToLower/ToUpper. They should call GetBuf() instead of
// data() in order to ensure the changed string buffer is not
// reference-counted (in those implementations that refcount).
//
// 1999-JUL-01 - Added a true CString facade. Now you can use CStdString as
// a drop-in replacement for CString. If you find this useful,
// you can thank Chris Sells for finally convincing me to give
// in and implement it.
// - Changed operators << and >> (for MFC CArchive) to serialize
// EXACTLY as CString's do. So now you can send a CString out
// to a CArchive and later read it in as a CStdString. I have
// no idea why you would want to do this but you can.
//
// 1999-JUN-21 - Changed the CStdString class into the CStdStr template.
// - Fixed FormatV() to correctly decrement the loop counter.
// This was harmless bug but a bug nevertheless. Thanks to
// Chris (of Melbsys) for pointing it out
// - Changed Format() to try a normal stack-based array before
// using to _alloca().
// - Updated the text conversion macros to properly use code
// pages and to fit in better in MFC/ATL builds. In other
// words, I copied Microsoft's conversion stuff again.
// - Added equivalents of CString::GetBuffer, GetBufferSetLength
// - new sscpy() replacement of CStdString::CopyString()
// - a Trim() function that combines TrimRight() and TrimLeft().
//
// 1999-MAR-13 - Corrected the "NotSpace" functional object to use _istpace()
// instead of _isspace() Thanks to Dave Plummer for this.
//
// 1999-FEB-26 - Removed errant line (left over from testing) that #defined
// _MFC_VER. Thanks to John C Sipos for noticing this.
//
// 1999-FEB-03 - Fixed a bug in a rarely-used overload of operator+() that
// caused infinite recursion and stack overflow
// - Added member functions to simplify the process of
// persisting CStdStrings to/from DCOM IStream interfaces
// - Added functional objects (e.g. StdStringLessNoCase) that
// allow CStdStrings to be used as keys STL map objects with
// case-insensitive comparison
// - Added array indexing operators (i.e. operator[]). I
// originally assumed that these were unnecessary and would be
// inherited from basic_string. However, without them, Visual
// C++ complains about ambiguous overloads when you try to use
// them. Thanks to Julian Selman to pointing this out.
//
// 1998-FEB-?? - Added overloads of assign() function to completely account
// for Q172398 bug. Thanks to "Pete the Plumber" for this
//
// 1998-FEB-?? - Initial submission
//
// COPYRIGHT:
// 2002 Joseph M. O'Leary. This code is 100% free. Use it anywhere you
// want. Rewrite it, restructure it, whatever. If you can write software
// that makes money off of it, good for you. I kinda like capitalism.
// Please don't blame me if it causes your $30 billion dollar satellite
// explode in orbit. If you redistribute it in any form, I'd appreciate it
// if you would leave this notice here.
// =============================================================================
// Avoid multiple inclusion
#ifndef STDSTRING_H
#define STDSTRING_H
// When using VC, turn off browser references
// Turn off unavoidable compiler warnings
#if defined(_MSC_VER) && (_MSC_VER > 1100)
#pragma component(browser, off, references, "CStdString")
#pragma warning (disable : 4290) // C++ Exception Specification ignored
#pragma warning (disable : 4127) // Conditional expression is constant
#pragma warning (disable : 4097) // typedef name used as synonym for class name
#endif
#ifndef _UNICODE
#define SS_ANSI
#endif
// Borland warnings to turn off
#ifdef __BORLANDC__
#pragma option push -w-inl
// #pragma warn -inl // Turn off inline function warnings
#endif
// SS_IS_INTRESOURCE
// -----------------
// A copy of IS_INTRESOURCE from VC7. Because old VC6 version of winuser.h
// doesn't have this.
#define SS_IS_INTRESOURCE(_r) (false)
#if !defined (SS_ANSI) && defined(_MSC_VER)
#undef SS_IS_INTRESOURCE
#if defined(_WIN64)
#define SS_IS_INTRESOURCE(_r) (((unsigned __int64)(_r) >> 16) == 0)
#else
#define SS_IS_INTRESOURCE(_r) (((unsigned long)(_r) >> 16) == 0)
#endif
#endif
// MACRO: SS_UNSIGNED
// ------------------
// This macro causes the addition of a constructor and assignment operator
// which take unsigned characters. CString has such functions and in order
// to provide maximum CString-compatability, this code needs them as well.
// In practice you will likely never need these functions...
//#define SS_UNSIGNED
#ifdef SS_ALLOW_UNSIGNED_CHARS
#define SS_UNSIGNED
#endif
// MACRO: SS_SAFE_FORMAT
// ---------------------
// This macro provides limited compatability with a questionable CString
// "feature". You can define it in order to avoid a common problem that
// people encounter when switching from CString to CStdString.
//
// To illustrate the problem -- With CString, you can do this:
//
// CString sName("Joe");
// CString sTmp;
// sTmp.Format("My name is %s", sName); // WORKS!
//
// However if you were to try this with CStdString, your program would
// crash.
//
// CStdString sName("Joe");
// CStdString sTmp;
// sTmp.Format("My name is %s", sName); // CRASHES!
//
// You must explicitly call c_str() or cast the object to the proper type
//
// sTmp.Format("My name is %s", sName.c_str()); // WORKS!
// sTmp.Format("My name is %s", static_cast<PCSTR>(sName));// WORKS!
// sTmp.Format("My name is %s", (PCSTR)sName); // WORKS!
//
// This is because it is illegal to pass anything but a POD type as a
// variadic argument to a variadic function (i.e. as one of the "..."
// arguments). The type const char* is a POD type. The type CStdString
// is not. Of course, neither is the type CString, but CString lets you do
// it anyway due to the way they laid out the class in binary. I have no
// control over this in CStdString since I derive from whatever
// implementation of basic_string is available.
//
// However if you have legacy code (which does this) that you want to take
// out of the MFC world and you don't want to rewrite all your calls to
// Format(), then you can define this flag and it will no longer crash.
//
// Note however that this ONLY works for Format(), not sprintf, fprintf,
// etc. If you pass a CStdString object to one of those functions, your
// program will crash. Not much I can do to get around this, short of
// writing substitutes for those functions as well.
#define SS_SAFE_FORMAT // use new template style Format() function
// MACRO: SS_NO_IMPLICIT_CAST
// --------------------------
// Some people don't like the implicit cast to const char* (or rather to
// const CT*) that CStdString (and MFC's CString) provide. That was the
// whole reason I created this class in the first place, but hey, whatever
// bakes your cake. Just #define this macro to get rid of the the implicit
// cast.
//#define SS_NO_IMPLICIT_CAST // gets rid of operator const CT*()
// MACRO: SS_NO_REFCOUNT
// ---------------------
// turns off reference counting at the assignment level. Only needed
// for the version of basic_string<> that comes with Visual C++ versions
// 6.0 or earlier, and only then in some heavily multithreaded scenarios.
// Uncomment it if you feel you need it.
//#define SS_NO_REFCOUNT
// MACRO: SS_WIN32
// ---------------
// When this flag is set, we are building code for the Win32 platform and
// may use Win32 specific functions (such as LoadString). This gives us
// a couple of nice extras for the code.
//
// Obviously, Microsoft's is not the only compiler available for Win32 out
// there. So I can't just check to see if _MSC_VER is defined to detect
// if I'm building on Win32. So for now, if you use MS Visual C++ or
// Borland's compiler, I turn this on. Otherwise you may turn it on
// yourself, if you prefer
#if defined(_MSC_VER) || defined(__BORLANDC__) || defined(_WIN32)
#define SS_WIN32
#endif
// MACRO: SS_ANSI
// --------------
// When this macro is defined, the code attempts only to use ANSI/ISO
// standard library functions to do it's work. It will NOT attempt to use
// any Win32 of Visual C++ specific functions -- even if they are
// available. You may define this flag yourself to prevent any Win32
// of VC++ specific functions from being called.
// If we're not on Win32, we MUST use an ANSI build
#ifndef SS_WIN32
#if !defined(SS_NO_ANSI)
#define SS_ANSI
#endif
#endif
// MACRO: SS_ALLOCA
// ----------------
// Some implementations of the Standard C Library have a non-standard
// function known as alloca(). This functions allows one to allocate a
// variable amount of memory on the stack. It is needed to implement
// the ASCII/MBCS conversion macros.
//
// I wanted to find some way to determine automatically if alloca() is
// available on this platform via compiler flags but that is asking for
// trouble. The crude test presented here will likely need fixing on
// other platforms. Therefore I'll leave it up to you to fiddle with
// this test to determine if it exists. Just make sure SS_ALLOCA is or
// is not defined as appropriate and you control this feature.
#if defined(_MSC_VER) && !defined(SS_ANSI)
#define SS_ALLOCA
#endif
// MACRO: SS_MBCS
// --------------
// Setting this macro means you are using MBCS characters. In MSVC builds,
// this macro gets set automatically by detection of the preprocessor flag
// _MBCS. For other platforms you may set it manually if you wish. The
// only effect it currently has is to cause the allocation of more space
// for wchar_t --> char conversions.
// Note that MBCS does not mean UNICODE.
//
// #define SS_MBCS
//
#ifdef _MBCS
#define SS_MBCS
#endif
// MACRO SS_NO_LOCALE
// ------------------
// If your implementation of the Standard C++ Library lacks the <locale> header,
// you can #define this macro to make your code build properly. Note that this
// is some of my newest code and frankly I'm not very sure of it, though it does
// pass my unit tests.
// #define SS_NO_LOCALE
// Compiler Error regarding _UNICODE and UNICODE
// -----------------------------------------------
// Microsoft header files are screwy. Sometimes they depend on a preprocessor
// flag named "_UNICODE". Other times they check "UNICODE" (note the lack of
// leading underscore in the second version". In several places, they silently
// "synchronize" these two flags this by defining one of the other was defined.
// In older version of this header, I used to try to do the same thing.
//
// However experience has taught me that this is a bad idea. You get weird
// compiler errors that seem to indicate things like LPWSTR and LPTSTR not being
// equivalent in UNICODE builds, stuff like that (when they MUST be in a proper
// UNICODE build). You end up scratching your head and saying, "But that HAS
// to compile!".
//
// So what should you do if you get this error?
//
// Make sure that both macros (_UNICODE and UNICODE) are defined before this
// file is included. You can do that by either
//
// a) defining both yourself before any files get included
// b) including the proper MS headers in the proper order
// c) including this file before any other file, uncommenting
// the #defines below, and commenting out the #errors
//
// Personally I recommend solution a) but it's your call.
#ifdef _MSC_VER
#if defined (_UNICODE) && !defined (UNICODE)
#error UNICODE defined but not UNICODE
// #define UNICODE // no longer silently fix this
#endif
#if defined (UNICODE) && !defined (_UNICODE)
#error Warning, UNICODE defined but not _UNICODE
// #define _UNICODE // no longer silently fix this
#endif
#endif
// -----------------------------------------------------------------------------
// MIN and MAX. The Standard C++ template versions go by so many names (at
// at least in the MS implementation) that you never know what's available
// -----------------------------------------------------------------------------
template<class Type>
inline const Type& SSMIN(const Type& arg1, const Type& arg2)
{
return arg2 < arg1 ? arg2 : arg1;
}
template<class Type>
inline const Type& SSMAX(const Type& arg1, const Type& arg2)
{
return arg2 > arg1 ? arg2 : arg1;
}
// If they have not #included W32Base.h (part of my W32 utility library) then
// we need to define some stuff. Otherwise, this is all defined there.
#if !defined(W32BASE_H)
// If they want us to use only standard C++ stuff (no Win32 stuff)
#ifdef SS_ANSI
// On Win32 we have TCHAR.H so just include it. This is NOT violating
// the spirit of SS_ANSI as we are not calling any Win32 functions here.
#ifdef SS_WIN32
#include <TCHAR.H>
#include <WTYPES.H>
#ifndef STRICT
#define STRICT
#endif
// ... but on non-Win32 platforms, we must #define the types we need.
#else
typedef const char* PCSTR;
typedef char* PSTR;
typedef const wchar_t* PCWSTR;
typedef wchar_t* PWSTR;
#ifdef UNICODE
typedef wchar_t TCHAR;
#else
typedef char TCHAR;
#endif
typedef wchar_t OLECHAR;
#endif // #ifndef _WIN32
// Make sure ASSERT and verify are defined using only ANSI stuff
#ifndef ASSERT
#include <assert.h>
#define ASSERT(f) assert((f))
#endif
#ifndef VERIFY
#ifdef _DEBUG
#define VERIFY(x) ASSERT((x))
#else
#define VERIFY(x) x
#endif
#endif
#else // ...else SS_ANSI is NOT defined
#include <TCHAR.H>
#include <WTYPES.H>
#ifndef STRICT
#define STRICT
#endif
// Make sure ASSERT and verify are defined
#ifndef ASSERT
#include <crtdbg.h>
#define ASSERT(f) _ASSERTE((f))
#endif
#ifndef VERIFY
#ifdef _DEBUG
#define VERIFY(x) ASSERT((x))
#else
#define VERIFY(x) x
#endif
#endif
#endif // #ifdef SS_ANSI
#ifndef UNUSED
#define UNUSED(x) x
#endif
#endif // #ifndef W32BASE_H
// Standard headers needed
#include <string> // basic_string
#include <algorithm> // for_each, etc.
#include <functional> // for StdStringLessNoCase, et al
#ifndef SS_NO_LOCALE
#include <locale> // for various facets
#endif
// If this is a recent enough version of VC include comdef.h, so we can write
// member functions to deal with COM types & compiler support classes e.g.
// _bstr_t
#if defined (_MSC_VER) && (_MSC_VER >= 1100)
#include <comdef.h>
#define SS_INC_COMDEF // signal that we #included MS comdef.h file
#define STDSTRING_INC_COMDEF
#define SS_NOTHROW __declspec(nothrow)
#else
#define SS_NOTHROW
#endif
#ifndef TRACE
#define TRACE_DEFINED_HERE
#define TRACE
#endif
// Microsoft defines PCSTR, PCWSTR, etc, but no PCTSTR. I hate to use the
// versions with the "L" in front of them because that's a leftover from Win 16
// days, even though it evaluates to the same thing. Therefore, Define a PCSTR
// as an LPCTSTR.
#if !defined(PCTSTR) && !defined(PCTSTR_DEFINED)
typedef const TCHAR* PCTSTR;
#define PCTSTR_DEFINED
#endif
#if !defined(PCOLESTR) && !defined(PCOLESTR_DEFINED)
typedef const OLECHAR* PCOLESTR;
#define PCOLESTR_DEFINED
#endif
#if !defined(POLESTR) && !defined(POLESTR_DEFINED)
typedef OLECHAR* POLESTR;
#define POLESTR_DEFINED
#endif
#if !defined(PCUSTR) && !defined(PCUSTR_DEFINED)
typedef const unsigned char* PCUSTR;
typedef unsigned char* PUSTR;
#define PCUSTR_DEFINED
#endif
// SGI compiler 7.3 doesnt know these types - oh and btw, remember to use
// -LANG:std in the CXX Flags
#if defined(__sgi)
typedef unsigned long DWORD;
typedef void * LPCVOID;
#endif
// SS_USE_FACET macro and why we need it:
//
// Since I'm a good little Standard C++ programmer, I use locales. Thus, I
// need to make use of the use_facet<> template function here. Unfortunately,
// this need is complicated by the fact the MS' implementation of the Standard
// C++ Library has a non-standard version of use_facet that takes more
// arguments than the standard dictates. Since I'm trying to write CStdString
// to work with any version of the Standard library, this presents a problem.
//
// The upshot of this is that I can't do 'use_facet' directly. The MS' docs
// tell me that I have to use a macro, _USE() instead. Since _USE obviously
// won't be available in other implementations, this means that I have to write
// my OWN macro -- SS_USE_FACET -- that evaluates either to _USE or to the
// standard, use_facet.
//
// If you are having trouble with the SS_USE_FACET macro, in your implementation
// of the Standard C++ Library, you can define your own version of SS_USE_FACET.
#ifndef schMSG
#define schSTR(x) #x
#define schSTR2(x) schSTR(x)
#define schMSG(desc) message(__FILE__ "(" schSTR2(__LINE__) "):" #desc)
#endif
#ifndef SS_USE_FACET
// STLPort #defines a macro (__STL_NO_EXPLICIT_FUNCTION_TMPL_ARGS) for
// all MSVC builds, erroneously in my opinion. It causes problems for
// my SS_ANSI builds. In my code, I always comment out that line. You'll
// find it in \stlport\config\stl_msvc.h
#if defined(__SGI_STL_PORT) && (__SGI_STL_PORT >= 0x400 )
#if defined(__STL_NO_EXPLICIT_FUNCTION_TMPL_ARGS) && defined(_MSC_VER)
#ifdef SS_ANSI
#pragma schMSG(__STL_NO_EXPLICIT_FUNCTION_TMPL_ARGS defined!!)
#endif
#endif
#define SS_USE_FACET(loc, fac) std::use_facet<fac >(loc)
#elif defined(_MSC_VER )
#define SS_USE_FACET(loc, fac) std::_USE(loc, fac)
// ...and
#elif defined(_RWSTD_NO_TEMPLATE_ON_RETURN_TYPE)
#define SS_USE_FACET(loc, fac) std::use_facet(loc, (fac*)0)
#else
#define SS_USE_FACET(loc, fac) std::use_facet<fac >(loc)
#endif
#endif
// =============================================================================
// UNICODE/MBCS conversion macros. Made to work just like the MFC/ATL ones.
// =============================================================================
#include <wchar.h> // Added to Std Library with Amendment #1.
// First define the conversion helper functions. We define these regardless of
// any preprocessor macro settings since their names won't collide.
// Not sure if we need all these headers. I believe ANSI says we do.
#include <stdio.h>
#include <stdarg.h>
#include <wctype.h>
#include <ctype.h>
#include <stdlib.h>
#ifndef va_start
#include <varargs.h>
#endif
#ifdef SS_NO_LOCALE
#if defined(_WIN32) || defined (_WIN32_WCE)
inline PWSTR StdCodeCvt(PWSTR pDstW, int nDst, PCSTR pSrcA, int nSrc,
UINT acp=CP_ACP)
{
ASSERT(0 != pSrcA);
ASSERT(0 != pDstW);
pDstW[0] = '\0';
MultiByteToWideChar(acp, 0, pSrcA, nSrc, pDstW, nDst);
return pDstW;
}
inline PWSTR StdCodeCvt(PWSTR pDstW, int nDst, PCUSTR pSrcA, int nSrc,
UINT acp=CP_ACP)
{
return StdCodeCvt(pDstW, nDst, (PCSTR)pSrcA, nSrc, acp);
}
inline PSTR StdCodeCvt(PSTR pDstA, int nDst, PCWSTR pSrcW, int nSrc,
UINT acp=CP_ACP)
{
ASSERT(0 != pDstA);
ASSERT(0 != pSrcW);
pDstA[0] = '\0';
WideCharToMultiByte(acp, 0, pSrcW, nSrc, pDstA, nDst, 0, 0);
return pDstA;
}
inline PUSTR StdCodeCvt(PUSTR pDstA, int nDst, PCWSTR pSrcW, int nSrc,
UINT acp=CP_ACP)
{
return (PUSTR)StdCodeCvt((PSTR)pDstA, nDst, pSrcW, nSrc, acp);
}
#else
#endif
#else
// StdCodeCvt - made to look like Win32 functions WideCharToMultiByte
// and MultiByteToWideChar but uses locales in SS_ANSI
// builds. There are a number of overloads.
// First argument is the destination buffer.
// Second argument is the source buffer
//#if defined (SS_ANSI) || !defined (SS_WIN32)
// 'SSCodeCvt' - shorthand name for the codecvt facet we use
typedef std::codecvt<wchar_t, char, mbstate_t> SSCodeCvt;
inline PWSTR StdCodeCvt(PWSTR pDstW, int nDst, PCSTR pSrcA, int nSrc,
const std::locale& loc=std::locale())
{
ASSERT(0 != pSrcA);
ASSERT(0 != pDstW);
pDstW[0] = '\0';
if ( nSrc > 0 )
{
PCSTR pNextSrcA = pSrcA;
PWSTR pNextDstW = pDstW;
SSCodeCvt::result res = SSCodeCvt::ok;
const SSCodeCvt& conv = SS_USE_FACET(loc, SSCodeCvt);
SSCodeCvt::state_type st= { 0 };
res = conv.in(st,
pSrcA, pSrcA + nSrc, pNextSrcA,
pDstW, pDstW + nDst, pNextDstW);
ASSERT(SSCodeCvt::ok == res);
ASSERT(SSCodeCvt::error != res);
ASSERT(pNextDstW >= pDstW);
ASSERT(pNextSrcA >= pSrcA);
// Null terminate the converted string
if ( pNextDstW - pDstW > nDst )
*(pDstW + nDst) = '\0';
else
*pNextDstW = '\0';
}
return pDstW;
}
inline PWSTR StdCodeCvt(PWSTR pDstW, int nDst, PCUSTR pSrcA, int nSrc,
const std::locale& loc=std::locale())
{
return StdCodeCvt(pDstW, nDst, (PCSTR)pSrcA, nSrc, loc);
}
inline PSTR StdCodeCvt(PSTR pDstA, int nDst, PCWSTR pSrcW, int nSrc,
const std::locale& loc=std::locale())
{
ASSERT(0 != pDstA);
ASSERT(0 != pSrcW);
pDstA[0] = '\0';
if ( nSrc > 0 )
{
PSTR pNextDstA = pDstA;
PCWSTR pNextSrcW = pSrcW;
SSCodeCvt::result res = SSCodeCvt::ok;
const SSCodeCvt& conv = SS_USE_FACET(loc, SSCodeCvt);
SSCodeCvt::state_type st= { 0 };
res = conv.out(st,
pSrcW, pSrcW + nSrc, pNextSrcW,
pDstA, pDstA + nDst, pNextDstA);
ASSERT(SSCodeCvt::error != res);
ASSERT(SSCodeCvt::ok == res); // strict, comment out for sanity
ASSERT(pNextDstA >= pDstA);
ASSERT(pNextSrcW >= pSrcW);
// Null terminate the converted string
if ( pNextDstA - pDstA > nDst )
*(pDstA + nDst) = '\0';
else
*pNextDstA = '\0';
}
return pDstA;
}
inline PUSTR StdCodeCvt(PUSTR pDstA, int nDst, PCWSTR pSrcW, int nSrc,
const std::locale& loc=std::locale())
{
return (PUSTR)StdCodeCvt((PSTR)pDstA, nDst, pSrcW, nSrc, loc);
}
#endif
// Unicode/MBCS conversion macros are only available on implementations of
// the "C" library that have the non-standard _alloca function. As far as I
// know that's only Microsoft's though I've heard that the function exists
// elsewhere.
#if defined(SS_ALLOCA) && !defined SS_NO_CONVERSION
#include <malloc.h> // needed for _alloca
// Define our conversion macros to look exactly like Microsoft's to
// facilitate using this stuff both with and without MFC/ATL
#ifdef _CONVERSION_USES_THREAD_LOCALE
#ifndef _DEBUG
#define SSCVT int _cvt; _cvt; UINT _acp=GetACP(); \
_acp; PCWSTR _pw; _pw; PCSTR _pa; _pa
#else
#define SSCVT int _cvt = 0; _cvt; UINT _acp=GetACP();\
_acp; PCWSTR _pw=0; _pw; PCSTR _pa=0; _pa
#endif
#define SSA2W(pa) (\
((_pa = pa) == 0) ? 0 : (\
_cvt = (sslen(_pa)),\
StdCodeCvt((PWSTR) _alloca((_cvt+1)*2), (_cvt+1)*2, \
_pa, _cvt, _acp)))
#define SSW2A(pw) (\
((_pw = pw) == 0) ? 0 : (\
_cvt = sslen(_pw), \
StdCodeCvt((LPSTR) _alloca((_cvt+1)*2), (_cvt+1)*2, \
_pw, _cvt, _acp)))
#else
#ifndef _DEBUG
#define SSCVT int _cvt; _cvt; UINT _acp=CP_ACP; _acp;\
PCWSTR _pw; _pw; PCSTR _pa; _pa
#else
#define SSCVT int _cvt = 0; _cvt; UINT _acp=CP_ACP; \
_acp; PCWSTR _pw=0; _pw; PCSTR _pa=0; _pa
#endif
#define SSA2W(pa) (\
((_pa = pa) == 0) ? 0 : (\
_cvt = (sslen(_pa)),\
StdCodeCvt((PWSTR) _alloca((_cvt+1)*2), (_cvt+1)*2, \
_pa, _cvt)))
#define SSW2A(pw) (\
((_pw = pw) == 0) ? 0 : (\
_cvt = (sslen(_pw)),\
StdCodeCvt((LPSTR) _alloca((_cvt+1)*2), (_cvt+1)*2, \
_pw, _cvt)))
#endif
#define SSA2CW(pa) ((PCWSTR)SSA2W((pa)))
#define SSW2CA(pw) ((PCSTR)SSW2A((pw)))
#ifdef UNICODE
#define SST2A SSW2A
#define SSA2T SSA2W
#define SST2CA SSW2CA
#define SSA2CT SSA2CW
// (Did you get a compiler error here about not being able to convert
// PTSTR into PWSTR? Then your _UNICODE and UNICODE flags are messed
// up. Best bet: #define BOTH macros before including any MS headers.)
inline PWSTR SST2W(PTSTR p) { return p; }
inline PTSTR SSW2T(PWSTR p) { return p; }
inline PCWSTR SST2CW(PCTSTR p) { return p; }
inline PCTSTR SSW2CT(PCWSTR p) { return p; }
#else
#define SST2W SSA2W
#define SSW2T SSW2A
#define SST2CW SSA2CW