1 #pragma once 2 3 #ifndef _CHSTRING_H 4 #define _CHSTRING_H 5 6 #include <windows.h> 7 #include <provexce.h> 8 9 struct CHStringData 10 { 11 long nRefs; 12 int nDataLength; 13 int nAllocLength; 14 15 WCHAR* data() 16 { 17 return (WCHAR*)(this+1); 18 } 19 }; 20 21 class CHString 22 { 23 public: 24 CHString(); 25 CHString(WCHAR ch, int nRepeat = 1); 26 CHString(LPCWSTR lpsz); 27 CHString(LPCWSTR lpch, int nLength); 28 CHString(LPCSTR lpsz); 29 CHString(const CHString& stringSrc); 30 CHString(const unsigned char* lpsz); 31 ~CHString(); 32 33 BSTR AllocSysString() const; 34 int Collate(LPCWSTR lpsz) const; 35 int Compare(LPCWSTR lpsz) const; 36 int CompareNoCase(LPCWSTR lpsz) const; 37 void Empty(); 38 int Find(WCHAR ch) const; 39 int Find(LPCWSTR lpszSub) const; 40 int FindOneOf(LPCWSTR lpszCharSet) const; 41 void Format(UINT nFormatID, ...); 42 void Format(LPCWSTR lpszFormat, ...); 43 void FormatMessageW(UINT nFormatID, ...); 44 void FormatMessageW(LPCWSTR lpszFormat, ...); 45 void FormatV(LPCWSTR lpszFormat, va_list argList); 46 void FreeExtra(); 47 int GetAllocLength() const; 48 WCHAR GetAt(int nIndex) const; 49 LPWSTR GetBuffer(int nMinBufLength); 50 LPWSTR GetBufferSetLength(int nNewLength); 51 int GetLength() const; 52 BOOL IsEmpty() const; 53 CHString Left(int nCount) const; 54 int LoadStringW(UINT nID); 55 LPWSTR LockBuffer(); 56 void MakeLower(); 57 void MakeReverse(); 58 void MakeUpper(); 59 CHString Mid(int nFirst) const; 60 CHString Mid(int nFirst, int nCount) const; 61 void ReleaseBuffer(int nNewLength = -1); 62 int ReverseFind(WCHAR ch) const; 63 CHString Right(int nCount) const; 64 void SetAt(int nIndex, WCHAR ch); 65 CHString SpanExcluding(LPCWSTR lpszCharSet) const; 66 CHString SpanIncluding(LPCWSTR lpszCharSet) const; 67 void TrimLeft(); 68 void TrimRight(); 69 void UnlockBuffer(); 70 71 const CHString& operator=(char ch); 72 const CHString& operator=(WCHAR ch); 73 const CHString& operator=(CHString *p); 74 const CHString& operator=(LPCSTR lpsz); 75 const CHString& operator=(LPCWSTR lpsz); 76 const CHString& operator=(const CHString& stringSrc); 77 const CHString& operator=(const unsigned char* lpsz); 78 79 const CHString& operator+=(char ch); 80 const CHString& operator+=(WCHAR ch); 81 const CHString& operator+=(LPCWSTR lpsz); 82 const CHString& operator+=(const CHString& string); 83 84 WCHAR operator[](int nIndex) const; 85 86 operator LPCWSTR() const; 87 88 friend CHString WINAPI operator+(WCHAR ch, const CHString& string); 89 friend CHString WINAPI operator+(const CHString& string, WCHAR ch); 90 friend CHString WINAPI operator+(const CHString& string, LPCWSTR lpsz); 91 friend CHString WINAPI operator+(LPCWSTR lpsz, const CHString& string); 92 friend CHString WINAPI operator+(const CHString& string1, const CHString& string2); 93 94 protected: 95 LPWSTR m_pchData; 96 97 void AllocBeforeWrite(int nLen); 98 void AllocBuffer(int nLen); 99 void AllocCopy(CHString& dest, int nCopyLen, int nCopyIndex, int nExtraLen) const; 100 void AssignCopy(int nSrcLen, LPCWSTR lpszSrcData); 101 void ConcatCopy(int nSrc1Len, LPCWSTR lpszSrc1Data, int nSrc2Len, LPCWSTR lpszSrc2Data); 102 void ConcatInPlace(int nSrcLen, LPCWSTR lpszSrcData); 103 void CopyBeforeWrite(); 104 CHStringData* GetData() const; 105 void Init(); 106 int LoadStringW(UINT nID, LPWSTR lpszBuf, UINT nMaxBuf); 107 void Release(); 108 static void WINAPI Release(CHStringData* pData); 109 static int WINAPI SafeStrlen(LPCWSTR lpsz); 110 }; 111 112 inline BOOL operator==(const CHString& s1, LPCWSTR s2) { return s1.Compare(s2) == 0; } 113 inline BOOL operator==(const CHString& s1, const CHString& s2) { return s1.Compare(s2) == 0; } 114 115 inline BOOL operator!=(const CHString& s1, LPCWSTR s2) { return s1.Compare(s2) != 0; } 116 inline BOOL operator!=(const CHString& s1, const CHString& s2) { return s1.Compare(s2) != 0; } 117 118 inline BOOL operator<(const CHString& s1, LPCWSTR s2) { return s1.Compare(s2) < 0; } 119 inline BOOL operator<(const CHString& s1, const CHString& s2) { return s1.Compare(s2) < 0; } 120 121 inline BOOL operator>(const CHString& s1, LPCWSTR s2) { return s1.Compare(s2) > 0; } 122 inline BOOL operator>(const CHString& s1, const CHString& s2) { return s1.Compare(s2) > 0; } 123 124 inline BOOL operator<=(const CHString& s1, LPCWSTR s2) { return s1.Compare(s2) <= 0; } 125 inline BOOL operator<=(const CHString& s1, const CHString& s2) { return s1.Compare(s2) <= 0; } 126 127 inline BOOL operator>=(const CHString& s1, LPCWSTR s2) { return s1.Compare(s2) >= 0; } 128 inline BOOL operator>=(const CHString& s1, const CHString& s2) { return s1.Compare(s2) >= 0; } 129 130 #endif 131