Source code of Windows XP (NT5)
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.

344 lines
11 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. cstring.h
  5. Abstract:
  6. Extracted from MFC
  7. Author:
  8. Revision History:
  9. --*/
  10. #ifndef _CSTRING_H_
  11. #define _CSTRING_H_
  12. # include <ctype.h>
  13. # include <tchar.h>
  14. #include <stidebug.h>
  15. #define _AFX_NO_BSTR_SUPPORT 1
  16. #define AFX_CDECL CDECL
  17. #define AFXAPI WINAPI
  18. #define AFX_DATA
  19. #define AFX_DATADEF
  20. #define DEBUG_NEW new
  21. #define TRACE1(s,x) DPRINTF(DM_TRACE,s,x)
  22. #define VERIFY REQUIRE
  23. #define _AFX_INLINE inline
  24. BOOL
  25. AfxIsValidString(
  26. LPCWSTR lpsz,
  27. int nLength
  28. ) ;
  29. BOOL
  30. AfxIsValidString(
  31. LPCSTR lpsz,
  32. int nLength
  33. ) ;
  34. BOOL
  35. AfxIsValidAddress(
  36. const void* lp,
  37. UINT nBytes,
  38. BOOL bReadWrite
  39. );
  40. /////////////////////////////////////////////////////////////////////////////
  41. // Strings
  42. #ifndef _OLEAUTO_H_
  43. #ifdef OLE2ANSI
  44. typedef LPSTR BSTR;
  45. #else
  46. typedef LPWSTR BSTR;// must (semantically) match typedef in oleauto.h
  47. #endif
  48. #endif
  49. struct CStringData
  50. {
  51. long nRefs; // reference count
  52. int nDataLength;
  53. int nAllocLength;
  54. // TCHAR data[nAllocLength]
  55. TCHAR* data()
  56. { return (TCHAR*)(this+1); }
  57. };
  58. class CString
  59. {
  60. public:
  61. // Constructors
  62. CString();
  63. CString(const CString& stringSrc);
  64. CString(TCHAR ch, int nRepeat = 1);
  65. CString(LPCSTR lpsz);
  66. CString(LPCWSTR lpsz);
  67. CString(LPCTSTR lpch, int nLength);
  68. CString(const unsigned char* psz);
  69. // Attributes & Operations
  70. // as an array of characters
  71. int GetLength() const;
  72. BOOL IsEmpty() const;
  73. void Empty(); // free up the data
  74. TCHAR GetAt(int nIndex) const; // 0 based
  75. TCHAR operator[](int nIndex) const; // same as GetAt
  76. void SetAt(int nIndex, TCHAR ch);
  77. operator LPCTSTR() const; // as a C string
  78. // overloaded assignment
  79. const CString& operator=(const CString& stringSrc);
  80. const CString& operator=(TCHAR ch);
  81. #ifdef _UNICODE
  82. const CString& operator=(char ch);
  83. #endif
  84. const CString& operator=(LPCSTR lpsz);
  85. const CString& operator=(LPCWSTR lpsz);
  86. const CString& operator=(const unsigned char* psz);
  87. // string concatenation
  88. const CString& operator+=(const CString& string);
  89. const CString& operator+=(TCHAR ch);
  90. #ifdef _UNICODE
  91. const CString& operator+=(char ch);
  92. #endif
  93. const CString& operator+=(LPCTSTR lpsz);
  94. friend CString AFXAPI operator+(const CString& string1,
  95. const CString& string2);
  96. friend CString AFXAPI operator+(const CString& string, TCHAR ch);
  97. friend CString AFXAPI operator+(TCHAR ch, const CString& string);
  98. #ifdef _UNICODE
  99. friend CString AFXAPI operator+(const CString& string, char ch);
  100. friend CString AFXAPI operator+(char ch, const CString& string);
  101. #endif
  102. friend CString AFXAPI operator+(const CString& string, LPCTSTR lpsz);
  103. friend CString AFXAPI operator+(LPCTSTR lpsz, const CString& string);
  104. // string comparison
  105. int Compare(LPCTSTR lpsz) const; // straight character
  106. int CompareNoCase(LPCTSTR lpsz) const; // ignore case
  107. int Collate(LPCTSTR lpsz) const; // NLS aware
  108. // simple sub-string extraction
  109. CString Mid(int nFirst, int nCount) const;
  110. CString Mid(int nFirst) const;
  111. CString Left(int nCount) const;
  112. CString Right(int nCount) const;
  113. CString SpanIncluding(LPCTSTR lpszCharSet) const;
  114. CString SpanExcluding(LPCTSTR lpszCharSet) const;
  115. // upper/lower/reverse conversion
  116. void MakeUpper();
  117. void MakeLower();
  118. void MakeReverse();
  119. // trimming whitespace (either side)
  120. void TrimRight();
  121. void TrimLeft();
  122. // searching (return starting index, or -1 if not found)
  123. // look for a single character match
  124. int Find(TCHAR ch) const; // like "C" strchr
  125. int ReverseFind(TCHAR ch) const;
  126. int FindOneOf(LPCTSTR lpszCharSet) const;
  127. // look for a specific sub-string
  128. int Find(LPCTSTR lpszSub) const; // like "C" strstr
  129. // simple formatting
  130. void AFX_CDECL Format(LPCTSTR lpszFormat, ...);
  131. void AFX_CDECL Format(UINT nFormatID, ...);
  132. // formatting for localization (uses FormatMessage API)
  133. void AFX_CDECL FormatMessage(LPCTSTR lpszFormat, ...);
  134. void AFX_CDECL FormatMessage(UINT nFormatID, ...);
  135. // input and output
  136. //friend CArchive& AFXAPI operator<<(CArchive& ar, const CString& string);
  137. //friend CArchive& AFXAPI operator>>(CArchive& ar, CString& string);
  138. // Windows support
  139. BOOL LoadString(UINT nID); // load from string resource
  140. // 255 chars max
  141. #ifndef _UNICODE
  142. // ANSI <-> OEM support (convert string in place)
  143. void AnsiToOem();
  144. void OemToAnsi();
  145. #endif
  146. #ifndef _AFX_NO_BSTR_SUPPORT
  147. // OLE BSTR support (use for OLE automation)
  148. BSTR AllocSysString() const;
  149. BSTR SetSysString(BSTR* pbstr) const;
  150. #endif
  151. // Access to string implementation buffer as "C" character array
  152. LPTSTR GetBuffer(int nMinBufLength);
  153. void ReleaseBuffer(int nNewLength = -1);
  154. LPTSTR GetBufferSetLength(int nNewLength);
  155. void FreeExtra();
  156. // Use LockBuffer/UnlockBuffer to turn refcounting off
  157. LPTSTR LockBuffer();
  158. void UnlockBuffer();
  159. // Implementation
  160. public:
  161. ~CString();
  162. int GetAllocLength() const;
  163. protected:
  164. LPTSTR m_pchData; // pointer to ref counted string data
  165. // implementation helpers
  166. CStringData* GetData() const;
  167. void Init();
  168. void AllocCopy(CString& dest, int nCopyLen, int nCopyIndex, int nExtraLen) const;
  169. void AllocBuffer(int nLen);
  170. void AssignCopy(int nSrcLen, LPCTSTR lpszSrcData);
  171. void ConcatCopy(int nSrc1Len, LPCTSTR lpszSrc1Data, int nSrc2Len, LPCTSTR lpszSrc2Data);
  172. void ConcatInPlace(int nSrcLen, LPCTSTR lpszSrcData);
  173. void FormatV(LPCTSTR lpszFormat, va_list argList);
  174. void CopyBeforeWrite();
  175. void AllocBeforeWrite(int nLen);
  176. void Release();
  177. static void PASCAL Release(CStringData* pData);
  178. static int PASCAL SafeStrlen(LPCTSTR lpsz);
  179. };
  180. // Compare helpers
  181. bool AFXAPI operator==(const CString& s1, const CString& s2);
  182. bool AFXAPI operator==(const CString& s1, LPCTSTR s2);
  183. bool AFXAPI operator==(LPCTSTR s1, const CString& s2);
  184. bool AFXAPI operator!=(const CString& s1, const CString& s2);
  185. bool AFXAPI operator!=(const CString& s1, LPCTSTR s2);
  186. bool AFXAPI operator!=(LPCTSTR s1, const CString& s2);
  187. bool AFXAPI operator<(const CString& s1, const CString& s2);
  188. bool AFXAPI operator<(const CString& s1, LPCTSTR s2);
  189. bool AFXAPI operator<(LPCTSTR s1, const CString& s2);
  190. bool AFXAPI operator>(const CString& s1, const CString& s2);
  191. bool AFXAPI operator>(const CString& s1, LPCTSTR s2);
  192. bool AFXAPI operator>(LPCTSTR s1, const CString& s2);
  193. bool AFXAPI operator<=(const CString& s1, const CString& s2);
  194. bool AFXAPI operator<=(const CString& s1, LPCTSTR s2);
  195. bool AFXAPI operator<=(LPCTSTR s1, const CString& s2);
  196. bool AFXAPI operator>=(const CString& s1, const CString& s2);
  197. bool AFXAPI operator>=(const CString& s1, LPCTSTR s2);
  198. bool AFXAPI operator>=(LPCTSTR s1, const CString& s2);
  199. // conversion helpers
  200. int AFX_CDECL _wcstombsz(char* mbstr, const wchar_t* wcstr, size_t count);
  201. int AFX_CDECL _mbstowcsz(wchar_t* wcstr, const char* mbstr, size_t count);
  202. // Globals
  203. //extern AFX_DATA TCHAR afxChNil;
  204. const CString& AFXAPI AfxGetEmptyString();
  205. #define afxEmptyString AfxGetEmptyString()
  206. // CString
  207. _AFX_INLINE CStringData* CString::GetData() const
  208. { ASSERT(m_pchData != NULL); return ((CStringData*)m_pchData)-1; }
  209. _AFX_INLINE void CString::Init()
  210. { m_pchData = afxEmptyString.m_pchData; }
  211. _AFX_INLINE CString::CString(const unsigned char* lpsz)
  212. { Init(); *this = (LPCSTR)lpsz; }
  213. _AFX_INLINE const CString& CString::operator=(const unsigned char* lpsz)
  214. { *this = (LPCSTR)lpsz; return *this; }
  215. #ifdef _UNICODE
  216. _AFX_INLINE const CString& CString::operator+=(char ch)
  217. { *this += (TCHAR)ch; return *this; }
  218. _AFX_INLINE const CString& CString::operator=(char ch)
  219. { *this = (TCHAR)ch; return *this; }
  220. _AFX_INLINE CString AFXAPI operator+(const CString& string, char ch)
  221. { return string + (TCHAR)ch; }
  222. _AFX_INLINE CString AFXAPI operator+(char ch, const CString& string)
  223. { return (TCHAR)ch + string; }
  224. #endif
  225. _AFX_INLINE int CString::GetLength() const
  226. { return GetData()->nDataLength; }
  227. _AFX_INLINE int CString::GetAllocLength() const
  228. { return GetData()->nAllocLength; }
  229. _AFX_INLINE BOOL CString::IsEmpty() const
  230. { return GetData()->nDataLength == 0; }
  231. _AFX_INLINE CString::operator LPCTSTR() const
  232. { return m_pchData; }
  233. _AFX_INLINE int PASCAL CString::SafeStrlen(LPCTSTR lpsz)
  234. { return (lpsz == NULL) ? 0 : lstrlen(lpsz); }
  235. // CString support (windows specific)
  236. _AFX_INLINE int CString::Compare(LPCTSTR lpsz) const
  237. { return _tcscmp(m_pchData, lpsz); } // MBCS/Unicode aware
  238. _AFX_INLINE int CString::CompareNoCase(LPCTSTR lpsz) const
  239. { return _tcsicmp(m_pchData, lpsz); } // MBCS/Unicode aware
  240. // CString::Collate is often slower than Compare but is MBSC/Unicode
  241. // aware as well as locale-sensitive with respect to sort order.
  242. _AFX_INLINE int CString::Collate(LPCTSTR lpsz) const
  243. { return _tcscoll(m_pchData, lpsz); } // locale sensitive
  244. _AFX_INLINE TCHAR CString::GetAt(int nIndex) const
  245. {
  246. ASSERT(nIndex >= 0);
  247. ASSERT(nIndex < GetData()->nDataLength);
  248. return m_pchData[nIndex];
  249. }
  250. _AFX_INLINE TCHAR CString::operator[](int nIndex) const
  251. {
  252. // same as GetAt
  253. ASSERT(nIndex >= 0);
  254. ASSERT(nIndex < GetData()->nDataLength);
  255. return m_pchData[nIndex];
  256. }
  257. _AFX_INLINE bool AFXAPI operator==(const CString& s1, const CString& s2)
  258. { return s1.Compare(s2) == 0; }
  259. _AFX_INLINE bool AFXAPI operator==(const CString& s1, LPCTSTR s2)
  260. { return s1.Compare(s2) == 0; }
  261. _AFX_INLINE bool AFXAPI operator==(LPCTSTR s1, const CString& s2)
  262. { return s2.Compare(s1) == 0; }
  263. _AFX_INLINE bool AFXAPI operator!=(const CString& s1, const CString& s2)
  264. { return s1.Compare(s2) != 0; }
  265. _AFX_INLINE bool AFXAPI operator!=(const CString& s1, LPCTSTR s2)
  266. { return s1.Compare(s2) != 0; }
  267. _AFX_INLINE bool AFXAPI operator!=(LPCTSTR s1, const CString& s2)
  268. { return s2.Compare(s1) != 0; }
  269. _AFX_INLINE bool AFXAPI operator<(const CString& s1, const CString& s2)
  270. { return s1.Compare(s2) < 0; }
  271. _AFX_INLINE bool AFXAPI operator<(const CString& s1, LPCTSTR s2)
  272. { return s1.Compare(s2) < 0; }
  273. _AFX_INLINE bool AFXAPI operator<(LPCTSTR s1, const CString& s2)
  274. { return s2.Compare(s1) > 0; }
  275. _AFX_INLINE bool AFXAPI operator>(const CString& s1, const CString& s2)
  276. { return s1.Compare(s2) > 0; }
  277. _AFX_INLINE bool AFXAPI operator>(const CString& s1, LPCTSTR s2)
  278. { return s1.Compare(s2) > 0; }
  279. _AFX_INLINE bool AFXAPI operator>(LPCTSTR s1, const CString& s2)
  280. { return s2.Compare(s1) < 0; }
  281. _AFX_INLINE bool AFXAPI operator<=(const CString& s1, const CString& s2)
  282. { return s1.Compare(s2) <= 0; }
  283. _AFX_INLINE bool AFXAPI operator<=(const CString& s1, LPCTSTR s2)
  284. { return s1.Compare(s2) <= 0; }
  285. _AFX_INLINE bool AFXAPI operator<=(LPCTSTR s1, const CString& s2)
  286. { return s2.Compare(s1) >= 0; }
  287. _AFX_INLINE bool AFXAPI operator>=(const CString& s1, const CString& s2)
  288. { return s1.Compare(s2) >= 0; }
  289. _AFX_INLINE bool AFXAPI operator>=(const CString& s1, LPCTSTR s2)
  290. { return s1.Compare(s2) >= 0; }
  291. _AFX_INLINE bool AFXAPI operator>=(LPCTSTR s1, const CString& s2)
  292. { return s2.Compare(s1) <= 0; }
  293. #endif