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.

314 lines
11 KiB

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