Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

266 lines
10 KiB

  1. //***************************************************************************
  2. //
  3. // Copyright � Microsoft Corporation. All rights reserved.
  4. //
  5. // CHSTRING.h
  6. //
  7. // Purpose: Utility library version of MFC CString
  8. //
  9. //***************************************************************************
  10. #if _MSC_VER > 1000
  11. #pragma once
  12. #endif
  13. #ifndef _CHSTRING_H
  14. #define _CHSTRING_H
  15. /////////////////////////////////////////////////////////////////////////////
  16. #include <windows.h>
  17. #include <limits.h>
  18. #include <tchar.h>
  19. #include <polarity.h>
  20. #pragma warning( disable : 4290 ) // Ignore 'C++ Exception Specification ignored'
  21. #include <ProvExce.h>
  22. /////////////////////////////////////////////////////////////////////////////
  23. struct _DOUBLE { BYTE doubleBits[sizeof(double)]; };
  24. #ifdef FRAMEWORK_ALLOW_DEPRECATED
  25. void POLARITY WINAPI SetCHStringResourceHandle(HINSTANCE handle);
  26. #endif
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CHString formatting
  29. /////////////////////////////////////////////////////////////////////////////
  30. #define TCHAR_ARG WCHAR
  31. #define WCHAR_ARG WCHAR
  32. #define CHAR_ARG char
  33. #if defined(_68K_) || defined(_X86_)
  34. #define DOUBLE_ARG _DOUBLE
  35. #else
  36. #define DOUBLE_ARG double
  37. #endif
  38. struct CHStringData
  39. {
  40. long nRefs;
  41. int nDataLength;
  42. int nAllocLength;
  43. WCHAR* data()
  44. {
  45. return (WCHAR*)(this+1);
  46. }
  47. };
  48. /////////////////////////////////////////////////////////////////////////////
  49. class POLARITY CHString
  50. {
  51. protected:
  52. LPWSTR m_pchData; // pointer to ref counted string data
  53. protected:
  54. // implementation helpers
  55. CHStringData* GetData() const; // returns data pointer
  56. void Init();
  57. void AllocCopy(CHString& dest, int nCopyLen, int nCopyIndex, int nExtraLen) const throw ( CHeap_Exception ) ;
  58. void AllocBuffer(int nLen) throw ( CHeap_Exception ) ;
  59. void AssignCopy(int nSrcLen, LPCWSTR lpszSrcData) throw ( CHeap_Exception ) ;
  60. void ConcatCopy(int nSrc1Len, LPCWSTR lpszSrc1Data, int nSrc2Len, LPCWSTR lpszSrc2Data) throw ( CHeap_Exception ) ;
  61. void ConcatInPlace(int nSrcLen, LPCWSTR lpszSrcData);
  62. void CopyBeforeWrite() throw ( CHeap_Exception ) ;
  63. void AllocBeforeWrite(int nLen) throw ( CHeap_Exception ) ;
  64. void Release();
  65. static void WINAPI Release(CHStringData* pData);
  66. static inline int WINAPI SafeStrlen(LPCWSTR lpsz) { return (lpsz == NULL) ? 0 : wcslen(lpsz); }
  67. // Helper function used to load resource into lpszBuf buffer.
  68. #ifdef FRAMEWORK_ALLOW_DEPRECATED
  69. int LoadStringW(UINT nID, LPWSTR lpszBuf, UINT nMaxBuf) throw ( CHeap_Exception ) ;
  70. #endif
  71. public:
  72. // Constructors/Destruction
  73. CHString();
  74. CHString(const CHString& stringSrc);
  75. CHString(WCHAR ch, int nRepeat = 1) throw ( CHeap_Exception ) ;
  76. CHString(LPCSTR lpsz) throw ( CHeap_Exception ) ;
  77. CHString(LPCWSTR lpsz) throw ( CHeap_Exception ) ;
  78. CHString(LPCWSTR lpch, int nLength) throw ( CHeap_Exception ) ;
  79. inline CHString(const unsigned char* lpsz) { Init(); *this = (LPCSTR)lpsz; }
  80. ~CHString();
  81. // Functions
  82. void SetAt(int nIndex, WCHAR ch) throw ( CHeap_Exception ) ;
  83. void Empty();
  84. // inlines
  85. inline int GetLength() const { return GetData()->nDataLength; }
  86. inline BOOL IsEmpty() const { return GetData()->nDataLength == 0; }
  87. #if (!defined DEBUG && !defined _DEBUG)
  88. inline WCHAR GetAt(int nIndex) const{ return m_pchData[nIndex]; }
  89. inline WCHAR operator[](int nIndex) const{ return m_pchData[nIndex]; }
  90. #else
  91. WCHAR GetAt(int nIndex) const;
  92. WCHAR operator[](int nIndex) const;
  93. #endif
  94. inline operator LPCWSTR() const { return m_pchData; }
  95. inline int GetAllocLength() const { return GetData()->nAllocLength; }
  96. // overloaded assignment
  97. const CHString& operator=(const CHString& stringSrc) throw ( CHeap_Exception ) ;
  98. const CHString& operator=(WCHAR ch) throw ( CHeap_Exception ) ;
  99. const CHString& operator=(LPCSTR lpsz) throw ( CHeap_Exception ) ;
  100. const CHString& operator=(LPCWSTR lpsz) throw ( CHeap_Exception ) ;
  101. inline const CHString& operator=(const unsigned char* lpsz) throw ( CHeap_Exception ) { *this = (LPCSTR)lpsz; return *this; }
  102. inline const CHString& operator=(CHString *p) throw ( CHeap_Exception ) { *this = *p; return *this; }
  103. inline const CHString& operator=(char ch) throw ( CHeap_Exception ) { *this = (WCHAR)ch; return *this; }
  104. inline const CHString& CHString::operator+=(char ch) throw ( CHeap_Exception ) { *this += (WCHAR)ch; return *this; }
  105. friend inline CHString operator+(const CHString& string, char ch) throw ( CHeap_Exception ) { return string + (WCHAR)ch; }
  106. friend inline CHString operator+(char ch, const CHString& string) throw ( CHeap_Exception ) { return (WCHAR)ch + string; }
  107. const CHString& operator+=(const CHString& string) throw ( CHeap_Exception ) ;
  108. const CHString& operator+=(WCHAR ch) throw ( CHeap_Exception ) ;
  109. const CHString& operator+=(LPCWSTR lpsz) throw ( CHeap_Exception ) ;
  110. friend CHString POLARITY WINAPI operator+(const CHString& string1, const CHString& string2) throw ( CHeap_Exception ) ;
  111. friend CHString POLARITY WINAPI operator+(const CHString& string, WCHAR ch) throw ( CHeap_Exception ) ;
  112. friend CHString POLARITY WINAPI operator+(WCHAR ch, const CHString& string) throw ( CHeap_Exception ) ;
  113. friend CHString POLARITY WINAPI operator+(const CHString& string, LPCWSTR lpsz) throw ( CHeap_Exception ) ;
  114. friend CHString POLARITY WINAPI operator+(LPCWSTR lpsz, const CHString& string) throw ( CHeap_Exception ) ;
  115. // string comparison
  116. int Compare(LPCWSTR lpsz) const;
  117. inline int CompareNoCase(LPCWSTR lpsz) const
  118. {
  119. // ignore case
  120. return _wcsicmp(m_pchData, lpsz);
  121. } // MBCS/Unicode aware
  122. inline int Collate(LPCWSTR lpsz) const
  123. {
  124. // NLS aware
  125. // CHString::Collate is often slower than Compare but is MBSC/Unicode
  126. // aware as well as locale-sensitive with respect to sort order.
  127. return wcscoll(m_pchData, lpsz);
  128. } // locale sensitive
  129. // Load string from resource file.
  130. #ifdef FRAMEWORK_ALLOW_DEPRECATED
  131. BOOL LoadStringW(UINT nID) throw ( CHeap_Exception ) ;
  132. #endif
  133. // Access to string implementation buffer as "C" character array
  134. LPWSTR GetBuffer(int nMinBufLength) throw ( CHeap_Exception ) ;
  135. void ReleaseBuffer(int nNewLength = -1) throw ( CHeap_Exception ) ;
  136. LPWSTR GetBufferSetLength(int nNewLength) throw ( CHeap_Exception ) ;
  137. void FreeExtra() throw ( CHeap_Exception ) ;
  138. // Use LockBuffer/UnlockBuffer to turn refcounting off
  139. LPWSTR LockBuffer() ;
  140. void UnlockBuffer();
  141. // searching (return starting index, or -1 if not found)
  142. // look for a single character match
  143. int Find(WCHAR ch) const; // like "C" strchr
  144. int FindOneOf(LPCWSTR lpszCharSet) const;
  145. int ReverseFind(WCHAR ch) const;
  146. // look for a specific sub-string
  147. int Find(LPCWSTR lpszSub) const; // like "C" strstr
  148. // upper/lower/reverse conversion
  149. void MakeUpper() throw ( CHeap_Exception ) ;
  150. void MakeLower() throw ( CHeap_Exception ) ;
  151. void MakeReverse() throw ( CHeap_Exception ) ;
  152. // simple sub-string extraction
  153. CHString Mid(int nFirst, int nCount) const throw ( CHeap_Exception ) ;
  154. CHString Mid(int nFirst) const throw ( CHeap_Exception ) ;
  155. CHString Left(int nCount) const throw ( CHeap_Exception ) ;
  156. CHString Right(int nCount) const throw ( CHeap_Exception ) ;
  157. CHString SpanIncluding(LPCWSTR lpszCharSet) const throw ( CHeap_Exception ) ;
  158. CHString SpanExcluding(LPCWSTR lpszCharSet) const throw ( CHeap_Exception ) ;
  159. // trimming whitespace (either side)
  160. void TrimRight() throw ( CHeap_Exception ) ;
  161. void TrimLeft() throw ( CHeap_Exception ) ;
  162. // printf-like formatting using passed string
  163. void __cdecl Format(LPCWSTR lpszFormat, ...) throw ( CHeap_Exception ) ;
  164. void FormatV(LPCWSTR lpszFormat, va_list argList);
  165. // printf-like formatting using referenced string resource
  166. #ifdef FRAMEWORK_ALLOW_DEPRECATED
  167. void __cdecl Format(UINT nFormatID, ...) throw ( CHeap_Exception ) ;
  168. #endif
  169. // format using FormatMessage API on passed string
  170. // Warning: if you pass string inserts to this function, they must
  171. // be LPCSTRs on Win9x and LPCWSTRs on NT.
  172. void __cdecl FormatMessageW(LPCWSTR lpszFormat, ...) throw ( CHeap_Exception ) ;
  173. // format using FormatMessage API on referenced string resource
  174. // Warning: if you pass string inserts to this function, they must
  175. // be LPCSTRs on Win9x and LPCWSTRs on NT.
  176. #ifdef FRAMEWORK_ALLOW_DEPRECATED
  177. void __cdecl FormatMessageW(UINT nFormatID, ...) throw ( CHeap_Exception ) ;
  178. #endif
  179. #ifndef _NO_BSTR_SUPPORT
  180. // OLE BSTR support (use for OLE automation)
  181. BSTR AllocSysString() const throw ( CHeap_Exception ) ;
  182. #endif
  183. };
  184. inline BOOL operator==(const CHString& s1, const CHString& s2) { return s1.Compare(s2) == 0; }
  185. inline BOOL operator==(const CHString& s1, LPCWSTR s2) { return s1.Compare(s2) == 0; }
  186. inline BOOL operator!=(const CHString& s1, const CHString& s2) { return s1.Compare(s2) != 0; }
  187. inline BOOL operator!=(const CHString& s1, LPCWSTR s2) { return s1.Compare(s2) != 0; }
  188. inline BOOL operator<(const CHString& s1, const CHString& s2) { return s1.Compare(s2) < 0; }
  189. inline BOOL operator<(const CHString& s1, LPCWSTR s2) { return s1.Compare(s2) < 0; }
  190. inline BOOL operator>(const CHString& s1, const CHString& s2) { return s1.Compare(s2) > 0; }
  191. inline BOOL operator>(const CHString& s1, LPCWSTR s2) { return s1.Compare(s2) > 0; }
  192. inline BOOL operator<=(const CHString& s1, const CHString& s2) { return s1.Compare(s2) <= 0; }
  193. inline BOOL operator<=(const CHString& s1, LPCWSTR s2) { return s1.Compare(s2) <= 0; }
  194. inline BOOL operator>=(const CHString& s1, const CHString& s2) { return s1.Compare(s2) >= 0; }
  195. inline BOOL operator>=(const CHString& s1, LPCWSTR s2) { return s1.Compare(s2) >= 0; }
  196. #endif