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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
|
/*
Miranda NG: the free IM client for Microsoft* Windows*
Copyright (C) 2012-24 Miranda NG team (https://miranda-ng.org)
all portions of this codebase are copyrighted to the people
listed in contributors.txt.
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; either version 2
of the License, or (at your option) any later version.
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 for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#pragma once
#ifndef M_STRING_H__
#define M_STRING_H__
#include <stdio.h>
#include <string.h>
#ifdef _MSC_VER
#include <mbstring.h>
#else
#include <ctype.h>
#endif // _WINDOWS
#include <wchar.h>
#include <m_core.h>
#ifdef __MINGW32__
#include <limits.h>
__inline size_t strnlen(const char *string, size_t maxlen)
{
const char *end = (const char *)memchr ((const void *)string, '\0', maxlen);
return end ? (size_t) (end - string) : maxlen;
}
__inline size_t wcsnlen(const wchar_t *string, size_t maxlen)
{
const wchar_t *end = wmemchr (string, L'\0', maxlen);
return end ? (size_t) (end - string) : maxlen;
}
/* FIXME: This may be wrong assumption about Langpack_GetDefaultCodePage */
#define Langpack_GetDefaultCodePage() CP_THREAD_ACP
/* FIXME: This is unsafe */
#define memcpy_s(dest,size,src,count) memcpy(dest,src,count)
/* FIXME: This is quite silly implementation of _mbsstr */
#define _mbsstr(str,search) strstr((const char *)str,(const char *)search)
#define __max(x,y) (((x)<(y))?(y):(x))
#endif /* __MINGW32__ */
/////////////////////////////////////////////////////////////////////////////////////////
struct CMStringData;
MIR_CORE_DLL(CMStringData*) mirstr_allocate(int nChars, int nCharSize);
MIR_CORE_DLL(void) mirstr_free(CMStringData *pData);
MIR_CORE_DLL(CMStringData*) mirstr_realloc(CMStringData* pData, int nChars, int nCharSize);
MIR_CORE_DLL(CMStringData*) mirstr_getNil();
MIR_CORE_DLL(void) mirstr_lock(CMStringData* pThis);
MIR_CORE_DLL(void) mirstr_release(CMStringData* pThis);
MIR_CORE_DLL(void) mirstr_unlock(CMStringData* pThis);
/////////////////////////////////////////////////////////////////////////////////////////
enum CMStringDataFormat { FORMAT };
struct CMStringData
{
int nDataLength; // Length of currently used data in XCHARs (not including terminating null)
int nAllocLength; // Length of allocated data in XCHARs (not including terminating null)
long nRefs; // Reference count: negative == locked
__forceinline void* data() { return (this + 1); }
__forceinline void AddRef() { InterlockedIncrement(&nRefs); }
__forceinline bool IsLocked() const { return nRefs < 0; }
__forceinline bool IsShared() const { return nRefs > 1; }
__forceinline void Lock() { mirstr_lock(this); }
__forceinline void Release() { mirstr_release(this); }
__forceinline void Unlock() { mirstr_unlock(this); }
};
template< typename BaseType = char >
class ChTraitsBase
{
public:
typedef char XCHAR;
typedef LPSTR PXSTR;
typedef LPCSTR PCXSTR;
typedef wchar_t YCHAR;
typedef LPWSTR PYSTR;
typedef LPCWSTR PCYSTR;
};
template<>
class ChTraitsBase< wchar_t >
{
public:
typedef wchar_t XCHAR;
typedef LPWSTR PXSTR;
typedef LPCWSTR PCXSTR;
typedef char YCHAR;
typedef LPSTR PYSTR;
typedef LPCSTR PCYSTR;
};
template< typename BaseType >
class CMSimpleStringT
{
public:
typedef typename ChTraitsBase< BaseType >::XCHAR XCHAR;
typedef typename ChTraitsBase< BaseType >::PXSTR PXSTR;
typedef typename ChTraitsBase< BaseType >::PCXSTR PCXSTR;
typedef typename ChTraitsBase< BaseType >::YCHAR YCHAR;
typedef typename ChTraitsBase< BaseType >::PYSTR PYSTR;
typedef typename ChTraitsBase< BaseType >::PCYSTR PCYSTR;
public:
explicit CMSimpleStringT();
CMSimpleStringT(const CMSimpleStringT& strSrc);
CMSimpleStringT(PCXSTR pszSrc);
CMSimpleStringT(const XCHAR* pchSrc, int nLength);
~CMSimpleStringT();
CMSimpleStringT& operator=(const CMSimpleStringT& strSrc);
__forceinline CMSimpleStringT& operator=(PCXSTR pszSrc)
{ SetString(pszSrc);
return *this;
}
__forceinline CMSimpleStringT& operator+=(const CMSimpleStringT& strSrc)
{ Append(strSrc);
return *this;
}
__forceinline CMSimpleStringT& operator+=(PCXSTR pszSrc)
{ Append(pszSrc);
return *this;
}
__forceinline CMSimpleStringT& operator+=(char ch)
{ AppendChar(XCHAR(ch));
return *this;
}
__forceinline CMSimpleStringT& operator+=(unsigned char ch)
{ AppendChar(XCHAR(ch));
return *this;
}
__forceinline CMSimpleStringT& operator+=(wchar_t ch)
{ AppendChar(XCHAR(ch));
return *this;
}
__forceinline XCHAR operator[](int iChar) const
{ return m_pszData[iChar];
}
__forceinline operator PCXSTR() const
{ return m_pszData;
}
__forceinline PCXSTR c_str() const
{ return m_pszData;
}
__forceinline int GetAllocLength() const
{ return GetData()->nAllocLength;
}
__forceinline XCHAR GetAt(int iChar) const
{ return m_pszData[iChar];
}
__forceinline PXSTR GetBuffer(int nMinBufferLength)
{ return PrepareWrite(nMinBufferLength);
}
__forceinline int GetLength() const
{ return GetData()->nDataLength;
}
__forceinline PCXSTR GetString() const
{ return m_pszData;
}
__forceinline PCXSTR GetTail() const
{ return m_pszData + GetData()->nDataLength;
}
__forceinline bool IsEmpty() const
{ return GetLength() == 0;
}
__forceinline void Preallocate(int nLength)
{ PrepareWrite(nLength);
}
__forceinline void ReleaseBufferSetLength(int nNewLength)
{ SetLength(nNewLength);
}
void Append(PCXSTR pszSrc);
void Append(PCXSTR pszSrc, int nLength);
void AppendChar(XCHAR ch);
void Append(const CMSimpleStringT& strSrc);
void Empty();
void FreeExtra();
PXSTR GetBuffer();
PXSTR GetBufferSetLength(int nLength);
PXSTR LockBuffer();
void UnlockBuffer();
void ReleaseBuffer(int nNewLength = -1);
void Truncate(int nNewLength);
void SetAt(int iChar, XCHAR ch);
void SetString(PCXSTR pszSrc);
void SetString(PCXSTR pszSrc, int nLength);
public:
template <typename Basetype>
friend CMSimpleStringT<BaseType> operator+(const CMSimpleStringT<BaseType> &str1, const CMSimpleStringT<BaseType> &str2);
template <typename Basetype>
friend CMSimpleStringT<BaseType> operator+(const CMSimpleStringT<BaseType> &str1, PCXSTR psz2);
template <typename Basetype>
friend CMSimpleStringT<BaseType> operator+(PCXSTR psz1, const CMSimpleStringT<BaseType> &str2);
static void MIR_SYSCALL CopyChars(XCHAR* pchDest, const XCHAR* pchSrc, int nChars);
static void MIR_SYSCALL CopyChars(XCHAR* pchDest, size_t nDestLen, const XCHAR* pchSrc, int nChars);
static void MIR_SYSCALL CopyCharsOverlapped(XCHAR* pchDest, const XCHAR* pchSrc, int nChars);
static void MIR_SYSCALL CopyCharsOverlapped(XCHAR* pchDest, size_t nDestLen, const XCHAR* pchSrc, int nChars);
static int MIR_SYSCALL StringLength(const char* psz);
static int MIR_SYSCALL StringLength(const wchar_t* psz);
static int MIR_SYSCALL StringLengthN(const char* psz, size_t sizeInXChar);
static int MIR_SYSCALL StringLengthN(const wchar_t* psz, size_t sizeInXChar);
static void MIR_SYSCALL Concatenate(CMSimpleStringT& strResult, PCXSTR psz1, int nLength1, PCXSTR psz2, int nLength2);
// Implementation
private:
__forceinline CMStringData* GetData() const
{ return (reinterpret_cast<CMStringData *>(m_pszData)-1);
}
void Attach(CMStringData* pData);
void Fork(int nLength);
PXSTR PrepareWrite(int nLength);
void PrepareWrite2(int nLength);
void Reallocate(int nLength);
void SetLength(int nLength);
static CMStringData* MIR_SYSCALL CloneData(CMStringData* pData);
private:
PXSTR m_pszData;
};
template< typename _CharType = char >
class ChTraitsCRT : public ChTraitsBase < _CharType >
{
public:
static char* MIR_SYSCALL CharNext(const char *p)
{
#ifdef _MSC_VER
return reinterpret_cast<char*>(_mbsinc(reinterpret_cast<const unsigned char*>(p)));
#else
return reinterpret_cast<char*>(p+1);
#endif
}
static int MIR_SYSCALL IsDigit(char ch)
{
#ifdef _MSC_VER
return _ismbcdigit(ch);
#else
return isdigit(ch);
#endif
}
static int MIR_SYSCALL IsSpace(char ch)
{
#ifdef _MSC_VER
return _ismbcspace(ch);
#else
return isspace(ch);
#endif
}
static int MIR_SYSCALL StringCompare(const char *pszA, const char *pszB)
{
#ifdef _MSC_VER
return _mbscmp(reinterpret_cast<const unsigned char*>(pszA), reinterpret_cast<const unsigned char*>(pszB));
#else
return strcmp(pszA, pszB);
#endif
}
static int MIR_SYSCALL StringCompareIgnore(const char *pszA, const char *pszB)
{
#ifdef _MSC_VER
return _mbsicmp(reinterpret_cast<const unsigned char*>(pszA), reinterpret_cast<const unsigned char*>(pszB));
#else
return strcasecmp(pszA, pszB);
#endif
}
static int MIR_SYSCALL StringCollate(const char *pszA, const char *pszB)
{
#ifdef _MSC_VER
return _mbscoll(reinterpret_cast<const unsigned char*>(pszA), reinterpret_cast<const unsigned char*>(pszB));
#else
return strcoll(pszA, pszB);
#endif
}
static int MIR_SYSCALL StringCollateIgnore(const char *pszA, const char *pszB)
{
#ifdef _MSC_VER
return _mbsicoll(reinterpret_cast<const unsigned char*>(pszA), reinterpret_cast<const unsigned char*>(pszB));
#else
return strcoll(pszA, pszB);
#endif
}
static const char* MIR_SYSCALL StringFindString(const char *pszBlock, const char *pszMatch)
{
#ifdef _MSC_VER
return reinterpret_cast<const char*>(_mbsstr(reinterpret_cast<const unsigned char*>(pszBlock),
reinterpret_cast<const unsigned char*>(pszMatch)));
#else
return strstr(pszBlock, pszMatch);
#endif
}
static char* MIR_SYSCALL StringFindString(char *pszBlock, const char *pszMatch)
{
return const_cast<char*>(StringFindString(const_cast<const char*>(pszBlock), pszMatch));
}
static const char* MIR_SYSCALL StringFindChar(const char *pszBlock, char chMatch)
{
#ifdef _MSC_VER
return reinterpret_cast<const char*>(_mbschr(reinterpret_cast<const unsigned char*>(pszBlock), (unsigned char)chMatch));
#else
return strchr(pszBlock, chMatch);
#endif
}
static const char* MIR_SYSCALL StringFindCharRev(const char *psz, char ch)
{
#ifdef _MSC_VER
return reinterpret_cast<const char*>(_mbsrchr(reinterpret_cast<const unsigned char*>(psz), (unsigned char)ch));
#else
return strrchr(psz, ch);
#endif
}
static const char* MIR_SYSCALL StringScanSet(const char *pszBlock, const char *pszMatch)
{
#ifdef _MSC_VER
return reinterpret_cast<const char*>(_mbspbrk(reinterpret_cast<const unsigned char*>(pszBlock),
reinterpret_cast<const unsigned char*>(pszMatch)));
#else
return strpbrk(pszBlock, pszMatch);
#endif
}
static int MIR_SYSCALL StringSpanIncluding(const char *pszBlock, const char *pszSet)
{
#ifdef _MSC_VER
return (int)_mbsspn(reinterpret_cast<const unsigned char*>(pszBlock), reinterpret_cast<const unsigned char*>(pszSet));
#else
return (int)strspn(pszBlock, pszSet);
#endif
}
static int MIR_SYSCALL StringSpanExcluding(const char *pszBlock, const char *pszSet)
{
#ifdef _MSC_VER
return (int)_mbscspn(reinterpret_cast<const unsigned char*>(pszBlock), reinterpret_cast<const unsigned char*>(pszSet));
#else
return (int)strcspn(pszBlock, pszSet);
#endif
}
static char* MIR_SYSCALL StringUppercase(char *psz)
{
#ifdef _MSC_VER
CharUpperBuffA(psz, (uint32_t)strlen(psz));
#else
strupr(psz);
#endif
return psz;
}
static char* MIR_SYSCALL StringLowercase(char *psz)
{
#ifdef _MSC_VER
CharLowerBuffA(psz, (uint32_t)strlen(psz));
#else
strlwr(psz);
#endif
return psz;
}
static char* MIR_SYSCALL StringUppercase(char *psz, size_t size)
{
#ifdef _MSC_VER
CharUpperBuffA(psz, (uint32_t)size);
#else
#endif
return psz;
}
static char* MIR_SYSCALL StringLowercase(char *psz, size_t size)
{
#ifdef _MSC_VER
CharLowerBuffA(psz, (uint32_t)size);
#endif
return psz;
}
static char* MIR_SYSCALL StringReverse(char *psz)
{
#ifdef _MSC_VER
return reinterpret_cast<LPSTR>(_mbsrev(reinterpret_cast<unsigned char*>(psz)));
#else
return strrev(psz);
#endif
}
static int MIR_SYSCALL GetFormattedLength(_Printf_format_string_ const char *pszFormat, va_list args)
{
#ifdef _MSC_VER
return _vscprintf(pszFormat, args);
#else
return 0; // !!!!!!!!!!
#endif
}
static int MIR_SYSCALL Format(char *pszBuffer, size_t nlength, _Printf_format_string_ const char *pszFormat, va_list args)
{
#ifdef _MSC_VER
return vsprintf_s(pszBuffer, nlength, pszFormat, args);
#else
return 0; // !!!!!!!!!!
#endif
}
static int MIR_SYSCALL GetBaseTypeLength(const char *pszSrc)
{
// Returns required buffer length in XCHARs
return int(strlen(pszSrc));
}
static int MIR_SYSCALL GetBaseTypeLength(const char *pszSrc, int nLength)
{
(void)pszSrc;
// Returns required buffer length in XCHARs
return nLength;
}
static int MIR_SYSCALL GetBaseTypeLength(const wchar_t *pszSource)
{
// Returns required buffer length in XCHARs
#ifdef _MSC_VER
return ::WideCharToMultiByte(Langpack_GetDefaultCodePage(), 0, pszSource, -1, NULL, 0, NULL, NULL) - 1;
#else
return 0;
#endif
}
static int MIR_SYSCALL GetBaseTypeLength(const wchar_t *pszSource, int nLength)
{
// Returns required buffer length in XCHARs
#ifdef _MSC_VER
return ::WideCharToMultiByte(Langpack_GetDefaultCodePage(), 0, pszSource, nLength, NULL, 0, NULL, NULL);
#else
return 0;
#endif
}
static void MIR_SYSCALL ConvertToBaseType(char*pszDest, int nDestLength, const char *pszSrc, int nSrcLength = -1)
{
if (nSrcLength == -1) { nSrcLength = 1 + GetBaseTypeLength(pszSrc); }
// nLen is in XCHARs
memcpy_s(pszDest, nDestLength*sizeof(char), pszSrc, nSrcLength*sizeof(char));
}
static void MIR_SYSCALL ConvertToBaseType(char*pszDest, int nDestLength, const wchar_t *pszSrc, int nSrcLength = -1)
{
// nLen is in XCHARs
#ifdef _MSC_VER
::WideCharToMultiByte(Langpack_GetDefaultCodePage(), 0, pszSrc, nSrcLength, pszDest, nDestLength, NULL, NULL);
#endif
}
static void ConvertToOem(_CharType* pstrString)
{
#ifdef _MSC_VER
bool fSuccess = ::CharToOemA(pstrString, pstrString);
#endif // _MSC_VER
}
static void ConvertToAnsi(_CharType* pstrString)
{
#ifdef _MSC_VER
bool fSuccess = ::OemToCharA(pstrString, pstrString);
#endif
}
static void ConvertToOem(_CharType* pstrString, size_t size)
{
#ifdef _MSC_VER
uint32_t dwSize = static_cast<uint32_t>(size);
::CharToOemBuffA(pstrString, pstrString, dwSize);
#endif
}
static void ConvertToAnsi(_CharType* pstrString, size_t size)
{
#ifdef _MSC_VER
uint32_t dwSize = static_cast<uint32_t>(size);
::OemToCharBuffA(pstrString, pstrString, dwSize);
#endif
}
static void MIR_SYSCALL FloodCharacters(char ch, int nLength, char* pch)
{
// nLength is in XCHARs
memset(pch, ch, nLength);
}
static int MIR_SYSCALL SafeStringLen(const char *psz)
{
// returns length in bytes
return (psz != NULL) ? int(strlen(psz)) : 0;
}
static int MIR_SYSCALL SafeStringLen(const wchar_t *psz)
{
// returns length in wchar_ts
return (psz != NULL) ? int(wcslen(psz)) : 0;
}
static int MIR_SYSCALL GetCharLen(const wchar_t *pch)
{
// returns char length
return 1;
}
static int MIR_SYSCALL GetCharLen(const char *pch)
{
// returns char length
#ifdef _MSC_VER
return int(_mbclen(reinterpret_cast<const unsigned char*>(pch)));
#else
return mblen(pch, strlen(pch));
#endif
}
static uint32_t MIR_SYSCALL GetEnvironmentVariable(const char *pszVar, char*pszBuffer, uint32_t dwSize)
{
#ifdef _MSC_VER
return ::GetEnvironmentVariableA(pszVar, pszBuffer, dwSize);
#else
return 0; // !!!!!!!!!!
#endif
}
static char* MirCopy(const char *pstrString, size_t size)
{
return mir_strndup(pstrString, size);
}
};
// specialization for wchar_t
template<>
class ChTraitsCRT< wchar_t > : public ChTraitsBase< wchar_t >
{
static uint32_t MIR_SYSCALL _GetEnvironmentVariableW(const wchar_t *pszName, wchar_t *pszBuffer, uint32_t nSize)
{
#ifdef _MSC_VER
return ::GetEnvironmentVariableW(pszName, pszBuffer, nSize);
#else
return 0; // !!!!!!!!!!
#endif
}
public:
static wchar_t* MIR_SYSCALL CharNext(const wchar_t *psz)
{
return const_cast<wchar_t*>(psz+1);
}
static int MIR_SYSCALL IsDigit(wchar_t ch)
{
#ifdef _MSC_VER
return iswdigit(static_cast<unsigned short>(ch));
#else
return 0;
#endif
}
static int MIR_SYSCALL IsSpace(wchar_t ch)
{
#ifdef _MSC_VER
return iswspace(static_cast<unsigned short>(ch));
#else
return 0;
#endif
}
static int MIR_SYSCALL StringCompare(const wchar_t *pszA, const wchar_t *pszB)
{
return wcscmp(pszA, pszB);
}
static int MIR_SYSCALL StringCompareIgnore(const wchar_t *pszA, const wchar_t *pszB)
{
#ifdef _MSC_VER
return _wcsicmp(pszA, pszB);
#else
return 0;
#endif
}
static int MIR_SYSCALL StringCollate(const wchar_t *pszA, const wchar_t *pszB)
{
return wcscoll(pszA, pszB);
}
static int MIR_SYSCALL StringCollateIgnore(const wchar_t *pszA, const wchar_t *pszB)
{
#ifdef _MSC_VER
return _wcsicoll(pszA, pszB);
#else
return 0;
#endif
}
static const wchar_t* MIR_SYSCALL StringFindString(const wchar_t *pszBlock, const wchar_t *pszMatch)
{
return wcsstr(pszBlock, pszMatch);
}
static wchar_t* MIR_SYSCALL StringFindString(wchar_t *pszBlock, const wchar_t *pszMatch)
{
return const_cast<wchar_t*>(wcsstr(pszBlock, pszMatch));
}
static const wchar_t* MIR_SYSCALL StringFindChar(const wchar_t *pszBlock, wchar_t chMatch)
{
return wcschr(pszBlock, chMatch);
}
static const wchar_t* MIR_SYSCALL StringFindCharRev(const wchar_t *psz, wchar_t ch)
{
return wcsrchr(psz, ch);
}
static const wchar_t* MIR_SYSCALL StringScanSet(const wchar_t *pszBlock, const wchar_t *pszMatch)
{
return wcspbrk(pszBlock, pszMatch);
}
static int MIR_SYSCALL StringSpanIncluding(const wchar_t *pszBlock, const wchar_t *pszSet)
{
return (int)wcsspn(pszBlock, pszSet);
}
static int MIR_SYSCALL StringSpanExcluding(const wchar_t *pszBlock, const wchar_t *pszSet)
{
return (int)wcscspn(pszBlock, pszSet);
}
static wchar_t* MIR_SYSCALL StringUppercase(wchar_t *psz)
{
#ifdef _MSC_VER
CharUpperBuffW(psz, (uint32_t)wcslen(psz));
#endif
return psz;
}
static wchar_t* MIR_SYSCALL StringLowercase(wchar_t *psz)
{
#ifdef _MSC_VER
CharLowerBuffW(psz, (uint32_t)wcslen(psz));
#endif
return psz;
}
static wchar_t* MIR_SYSCALL StringUppercase(wchar_t *psz, size_t len)
{
#ifdef _MSC_VER
CharUpperBuffW(psz, (uint32_t)len);
#endif
return psz;
}
static wchar_t* MIR_SYSCALL StringLowercase(wchar_t *psz, size_t len)
{
#ifdef _MSC_VER
CharLowerBuffW(psz, (uint32_t)len);
#endif
return psz;
}
static wchar_t* MIR_SYSCALL StringReverse(wchar_t *psz)
{
#ifdef _MSC_VER
return _wcsrev(psz);
#else
return psz;
#endif
}
static int MIR_SYSCALL GetFormattedLength(_Printf_format_string_ const wchar_t *pszFormat, va_list args)
{
#ifdef _MSC_VER
return _vscwprintf(pszFormat, args);
#else
return 0;
#endif
}
static int MIR_SYSCALL Format(wchar_t *pszBuffer, _Printf_format_string_ const wchar_t *pszFormat, va_list args)
{
#pragma warning(push)
#pragma warning(disable : 4996)
#ifdef _MSC_VER
return vswprintf(pszBuffer, pszFormat, args);
#else
return 0;
#endif
#pragma warning(pop)
}
static int MIR_SYSCALL Format(wchar_t *pszBuffer, size_t nLength, _Printf_format_string_ const wchar_t *pszFormat, va_list args)
{
#pragma warning(push)
#pragma warning(disable : 4996)
#ifdef _MSC_VER
return _vsnwprintf(pszBuffer, nLength, pszFormat, args);
#else
return 0;
#endif
#pragma warning(pop)
}
static int MIR_SYSCALL GetBaseTypeLength(const char *pszSrc)
{
// Returns required buffer size in wchar_ts
#ifdef _MSC_VER
return ::MultiByteToWideChar(CP_ACP, 0, pszSrc, -1, nullptr, 0)-1;
#else
return 0;
#endif
}
static int MIR_SYSCALL GetBaseTypeLength(const char *pszSrc, int nLength)
{
// Returns required buffer size in wchar_ts
#ifdef _MSC_VER
return ::MultiByteToWideChar(CP_ACP, 0, pszSrc, nLength, nullptr, 0);
#else
return 0;
#endif
}
static int MIR_SYSCALL GetBaseTypeLength(const wchar_t *pszSrc)
{
// Returns required buffer size in wchar_ts
return (int)wcslen(pszSrc);
}
static int MIR_SYSCALL GetBaseTypeLength(const wchar_t *pszSrc, int nLength)
{
(void)pszSrc;
// Returns required buffer size in wchar_ts
return nLength;
}
static void MIR_SYSCALL ConvertToBaseType(wchar_t *pszDest, int nDestLength, const char *pszSrc, int nSrcLength = -1)
{
// nLen is in wchar_ts
#ifdef _MSC_VER
::MultiByteToWideChar(CP_ACP, 0, pszSrc, nSrcLength, pszDest, nDestLength);
#endif
}
static void MIR_SYSCALL ConvertToBaseType(wchar_t *pszDest, int nDestLength, const wchar_t *pszSrc, int nSrcLength = -1)
{
if (nSrcLength == -1) { nSrcLength=1 + GetBaseTypeLength(pszSrc); }
// nLen is in wchar_ts
#if _MSC_VER >= 1400
wmemcpy_s(pszDest, nDestLength, pszSrc, nSrcLength);
#else
wmemcpy(pszDest, pszSrc, nDestLength);
#endif
}
static void MIR_SYSCALL FloodCharacters(wchar_t ch, int nLength, wchar_t *psz)
{
// nLength is in XCHARs
for (int i = 0; i < nLength; i++)
{
psz[i] = ch;
}
}
static int MIR_SYSCALL SafeStringLen(const char *psz)
{
// returns length in bytes
return (psz != nullptr) ? (int)strlen(psz) : 0;
}
static int MIR_SYSCALL SafeStringLen(const wchar_t *psz)
{
// returns length in wchar_ts
return (psz != nullptr) ? (int)wcslen(psz) : 0;
}
static int MIR_SYSCALL GetCharLen(const wchar_t* pch)
{
(void)pch;
// returns char length
return 1;
}
static int MIR_SYSCALL GetCharLen(const char* pch)
{
// returns char length
#ifdef _MSC_VER
return (int)(_mbclen(reinterpret_cast< const unsigned char* >(pch)));
#else
return mblen(pch, strlen(pch));
#endif
}
static uint32_t MIR_SYSCALL GetEnvironmentVariable(const wchar_t *pszVar, wchar_t *pszBuffer, uint32_t dwSize)
{
return _GetEnvironmentVariableW(pszVar, pszBuffer, dwSize);
}
static void MIR_SYSCALL ConvertToOem(wchar_t* /*psz*/)
{
}
static void MIR_SYSCALL ConvertToAnsi(wchar_t* /*psz*/)
{
}
static void MIR_SYSCALL ConvertToOem(wchar_t* /*psz*/, size_t)
{
}
static void MIR_SYSCALL ConvertToAnsi(wchar_t* /*psz*/, size_t)
{
}
static wchar_t* MirCopy(const wchar_t *pstrString, size_t size)
{
return mir_wstrndup(pstrString, size);
}
};
template< typename BaseType, class StringTraits >
class MIR_CORE_EXPORT CMStringT : public CMSimpleStringT< BaseType >
{
public:
typedef CMSimpleStringT< BaseType> CThisSimpleString;
typedef typename CThisSimpleString::XCHAR XCHAR;
typedef typename CThisSimpleString::PXSTR PXSTR;
typedef typename CThisSimpleString::PCXSTR PCXSTR;
typedef typename CThisSimpleString::YCHAR YCHAR;
typedef typename CThisSimpleString::PYSTR PYSTR;
typedef typename CThisSimpleString::PCYSTR PCYSTR;
public:
CMStringT();
// Copy constructor
CMStringT(const CMStringT& strSrc);
CMStringT(const XCHAR* pszSrc);
CMStringT(CMStringDataFormat, _Printf_format_string_ const XCHAR* pszFormat, ...);
CMStringT(const YCHAR* pszSrc);
CMStringT(const unsigned char* pszSrc);
CMStringT(char ch, int nLength = 1);
CMStringT(wchar_t ch, int nLength = 1);
CMStringT(const XCHAR* pch, int nLength);
CMStringT(const YCHAR* pch, int nLength);
// Destructor
~CMStringT();
// Assignment operators
CMStringT& operator=(const CMStringT& strSrc);
CMStringT& operator=(PCXSTR pszSrc);
CMStringT& operator=(PCYSTR pszSrc);
CMStringT& operator=(const unsigned char* pszSrc);
CMStringT& operator=(char ch);
CMStringT& operator=(wchar_t ch);
CMStringT& operator+=(const CMStringT& str);
CMStringT& operator+=(const CThisSimpleString& str);
CMStringT& operator+=(PCXSTR pszSrc);
CMStringT& operator+=(PCYSTR pszSrc);
CMStringT& operator+=(char ch);
CMStringT& operator+=(unsigned char ch);
CMStringT& operator+=(wchar_t ch);
// Comparison
int Compare(PCXSTR psz) const;
int CompareNoCase(PCXSTR psz) const;
int Collate(PCXSTR psz) const;
int CollateNoCase(PCXSTR psz) const;
// Advanced manipulation
// Delete 'nCount' characters, starting at index 'iIndex'
int Delete(int iIndex, int nCount = 1);
// Insert character 'ch' before index 'iIndex'
int Insert(int iIndex, XCHAR ch);
// Insert string 'psz' before index 'iIndex'
int Insert(int iIndex, PCXSTR psz);
// Replace all occurrences of character 'chOld' with character 'chNew'
int Replace(XCHAR chOld, XCHAR chNew);
// Replace all occurrences of string 'pszOld' with string 'pszNew'
int Replace(PCXSTR pszOld, PCXSTR pszNew);
// Remove all occurrences of character 'chRemove'
int Remove(XCHAR chRemove);
CMStringT Tokenize(PCXSTR pszTokens, int& iStart) const;
// find routines
// Find the first occurrence of character 'ch', starting at index 'iStart'
int Find(XCHAR ch, int iStart = 0) const;
// look for a specific sub-string
// Find the first occurrence of string 'pszSub', starting at index 'iStart'
int Find(PCXSTR pszSub, int iStart = 0) const;
// Find the first occurrence of any of the characters in string 'pszCharSet'
int FindOneOf(PCXSTR pszCharSet) const;
// Find the last occurrence of character 'ch'
int ReverseFind(XCHAR ch) const;
// manipulation
// Convert the string to uppercase
CMStringT& MakeUpper();
// Convert the string to lowercase
CMStringT& MakeLower();
// Reverse the string
CMStringT& MakeReverse();
// trimming
// Remove all trailing whitespace
CMStringT& TrimRight();
// Remove all leading whitespace
CMStringT& TrimLeft();
// Remove all leading and trailing whitespace
CMStringT& Trim();
// Remove all leading and trailing occurrences of character 'chTarget'
CMStringT& Trim(XCHAR chTarget);
// Remove all leading and trailing occurrences of any of the characters in the string 'pszTargets'
CMStringT& Trim(PCXSTR pszTargets);
// trimming anything (either side)
// Remove all trailing occurrences of character 'chTarget'
CMStringT& TrimRight(XCHAR chTarget);
// Remove all trailing occurrences of any of the characters in string 'pszTargets'
CMStringT& TrimRight(PCXSTR pszTargets);
// Remove all leading occurrences of character 'chTarget'
CMStringT& TrimLeft(XCHAR chTarget);
// Remove all leading occurrences of any of the characters in string 'pszTargets'
CMStringT& TrimLeft(PCXSTR pszTargets);
// Convert the string to the OEM character set
void AnsiToOem();
// Convert the string to the ANSI character set
void OemToAnsi();
// Very simple sub-string extraction
// Return the substring starting at index 'iFirst'
CMStringT Mid(int iFirst) const;
// Return the substring starting at index 'iFirst', with length 'nCount'
CMStringT Mid(int iFirst, int nCount) const;
// Return the substring consisting of the rightmost 'nCount' characters
CMStringT Right(int nCount) const;
// Return the substring consisting of the leftmost 'nCount' characters
CMStringT Left(int nCount) const;
// Return the substring consisting of the leftmost characters in the set 'pszCharSet'
CMStringT SpanIncluding(PCXSTR pszCharSet) const;
// Return the substring consisting of the leftmost characters not in the set 'pszCharSet'
CMStringT SpanExcluding(PCXSTR pszCharSet) const;
// Format data using format string 'pszFormat'
PCXSTR Format(PCXSTR _Printf_format_string_ pszFormat, ...);
PCXSTR FormatV(PCXSTR _Printf_format_string_ pszFormat, va_list args);
// Append formatted data using format string 'pszFormat'
PCXSTR AppendFormat(PCXSTR _Printf_format_string_ pszFormat, ...);
void AppendFormatV(PCXSTR _Printf_format_string_ pszFormat, va_list args);
// return a copy of string to be freed by mir_free()
PXSTR Detach() const;
// Set the string to the value of environment variable 'pszVar'
BOOL GetEnvironmentVariable(PCXSTR pszVar);
friend bool __forceinline operator==(const CMStringT& str1, const CMStringT& str2) { return str1.Compare(str2) == 0; }
friend bool __forceinline operator==(const CMStringT& str1, PCXSTR psz2) { return str1.Compare(psz2) == 0; }
friend bool __forceinline operator==(PCXSTR psz1, const CMStringT& str2) { return str2.Compare(psz1) == 0; }
friend bool __forceinline operator==(const CMStringT& str1, PCYSTR psz2) { return str1 == CMStringT(psz2); }
friend bool __forceinline operator==(PCYSTR psz1, const CMStringT& str2) { return CMStringT(psz1) == str2; }
friend bool __forceinline operator!=(const CMStringT& str1, const CMStringT& str2) { return str1.Compare(str2) != 0; }
friend bool __forceinline operator!=(const CMStringT& str1, PCXSTR psz2) { return str1.Compare(psz2) != 0; }
friend bool __forceinline operator!=(PCXSTR psz1, const CMStringT& str2) { return str2.Compare(psz1) != 0; }
friend bool __forceinline operator!=(const CMStringT& str1, PCYSTR psz2) { return str1 != CMStringT(psz2); }
friend bool __forceinline operator!=(PCYSTR psz1, const CMStringT& str2) { return CMStringT(psz1) != str2; }
friend bool __forceinline operator<(const CMStringT& str1, const CMStringT& str2) { return str1.Compare(str2) < 0; }
friend bool __forceinline operator<(const CMStringT& str1, PCXSTR psz2) { return str1.Compare(psz2) < 0; }
friend bool __forceinline operator<(PCXSTR psz1, const CMStringT& str2) { return str2.Compare(psz1) > 0; }
friend bool __forceinline operator>(const CMStringT& str1, const CMStringT& str2) { return str1.Compare(str2) > 0; }
friend bool __forceinline operator>(const CMStringT& str1, PCXSTR psz2) { return str1.Compare(psz2) > 0; }
friend bool __forceinline operator>(PCXSTR psz1, const CMStringT& str2) { return str2.Compare(psz1) < 0; }
friend bool __forceinline operator<=(const CMStringT& str1, const CMStringT& str2) { return str1.Compare(str2) <= 0; }
friend bool __forceinline operator<=(const CMStringT& str1, PCXSTR psz2) { return str1.Compare(psz2) <= 0; }
friend bool __forceinline operator<=(PCXSTR psz1, const CMStringT& str2) { return str2.Compare(psz1) >= 0; }
friend bool __forceinline operator>=(const CMStringT& str1, const CMStringT& str2) { return str1.Compare(str2) >= 0; }
friend bool __forceinline operator>=(const CMStringT& str1, PCXSTR psz2) { return str1.Compare(psz2) >= 0; }
friend bool __forceinline operator>=(PCXSTR psz1, const CMStringT& str2) { return str2.Compare(psz1) <= 0; }
friend bool __forceinline operator==(XCHAR ch1, const CMStringT& str2) { return (str2.GetLength() == 1) && (str2[0] == ch1); }
friend bool __forceinline operator==(const CMStringT& str1, XCHAR ch2) { return (str1.GetLength() == 1) && (str1[0] == ch2); }
friend bool __forceinline operator!=(XCHAR ch1, const CMStringT& str2) { return (str2.GetLength() != 1) || (str2[0] != ch1); }
friend bool __forceinline operator!=(const CMStringT& str1, XCHAR ch2) { return (str1.GetLength() != 1) || (str1[0] != ch2); }
};
template< typename BaseType, class StringTraits >
MIR_CORE_EXPORT CMStringT<BaseType, StringTraits> CALLBACK operator+(const CMStringT<BaseType, StringTraits>& str1, const CMStringT<BaseType, StringTraits>& str2);
template< typename BaseType, class StringTraits >
MIR_CORE_EXPORT CMStringT<BaseType, StringTraits> CALLBACK operator+(const CMStringT<BaseType, StringTraits>& str1, typename CMStringT<BaseType, StringTraits>::PCXSTR psz2);
template< typename BaseType, class StringTraits >
MIR_CORE_EXPORT CMStringT<BaseType, StringTraits> CALLBACK operator+(typename CMStringT<BaseType, StringTraits>::PCXSTR psz1, const CMStringT<BaseType, StringTraits>& str2);
template< typename BaseType, class StringTraits >
MIR_CORE_EXPORT CMStringT<BaseType, StringTraits> CALLBACK operator+(const CMStringT<BaseType, StringTraits>& str1, wchar_t ch2);
template< typename BaseType, class StringTraits >
MIR_CORE_EXPORT CMStringT<BaseType, StringTraits> CALLBACK operator+(const CMStringT<BaseType, StringTraits>& str1, char ch2);
template< typename BaseType, class StringTraits >
MIR_CORE_EXPORT CMStringT<BaseType, StringTraits> CALLBACK operator + (wchar_t ch1, const CMStringT<BaseType, StringTraits>& str2);
template< typename BaseType, class StringTraits >
MIR_CORE_EXPORT CMStringT<BaseType, StringTraits> CALLBACK operator+(char ch1, const CMStringT<BaseType, StringTraits>& str2);
typedef CMStringT< wchar_t, ChTraitsCRT< wchar_t > > CMStringW;
typedef CMStringT< char, ChTraitsCRT< char > > CMStringA;
#endif // M_STRING_H__
|