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.

518 lines
17 KiB

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (c) 1992-2001 Microsoft Corporation, All Rights Reserved
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10. #ifndef _PROVSTR_H_
  11. #define _PROVSTR_H_
  12. #include "provstd.h"
  13. #include <tchar.h>
  14. //use TCHAR and depend on UNICODE definition
  15. struct CStringData
  16. {
  17. long nRefs; // reference count
  18. int nDataLength;
  19. int nAllocLength;
  20. // TCHAR data[nAllocLength]
  21. TCHAR* data()
  22. { return (TCHAR*)(this+1); }
  23. };
  24. class CString
  25. {
  26. public:
  27. // Constructors
  28. CString();
  29. CString(const CString& stringSrc);
  30. CString(TCHAR ch, int nRepeat = 1);
  31. CString(LPCSTR lpsz);
  32. CString(LPCWSTR lpsz);
  33. CString(LPCTSTR lpch, int nLength);
  34. CString(const unsigned char* psz);
  35. // Attributes & Operations
  36. // as an array of characters
  37. int GetLength() const;
  38. BOOL IsEmpty() const;
  39. void Empty(); // free up the data
  40. TCHAR GetAt(int nIndex) const; // 0 based
  41. TCHAR operator[](int nIndex) const; // same as GetAt
  42. void SetAt(int nIndex, TCHAR ch);
  43. operator LPCTSTR() const; // as a C string
  44. // overloaded assignment
  45. const CString& operator=(const CString& stringSrc);
  46. const CString& operator=(TCHAR ch);
  47. #ifdef _UNICODE
  48. const CString& operator=(char ch);
  49. #endif
  50. const CString& operator=(LPCSTR lpsz);
  51. const CString& operator=(LPCWSTR lpsz);
  52. const CString& operator=(const unsigned char* psz);
  53. // string concatenation
  54. const CString& operator+=(const CString& string);
  55. const CString& operator+=(TCHAR ch);
  56. #ifdef _UNICODE
  57. const CString& operator+=(char ch);
  58. #endif
  59. const CString& operator+=(LPCTSTR lpsz);
  60. friend CString AFXAPI operator+(const CString& string1,
  61. const CString& string2);
  62. friend CString AFXAPI operator+(const CString& string, TCHAR ch);
  63. friend CString AFXAPI operator+(TCHAR ch, const CString& string);
  64. #ifdef _UNICODE
  65. friend CString AFXAPI operator+(const CString& string, char ch);
  66. friend CString AFXAPI operator+(char ch, const CString& string);
  67. #endif
  68. friend CString AFXAPI operator+(const CString& string, LPCTSTR lpsz);
  69. friend CString AFXAPI operator+(LPCTSTR lpsz, const CString& string);
  70. // string comparison
  71. int Compare(LPCTSTR lpsz) const; // straight character
  72. int CompareNoCase(LPCTSTR lpsz) const; // ignore case
  73. int Collate(LPCTSTR lpsz) const; // NLS aware
  74. // simple sub-string extraction
  75. CString Mid(int nFirst, int nCount) const;
  76. CString Mid(int nFirst) const;
  77. CString Left(int nCount) const;
  78. CString Right(int nCount) const;
  79. CString SpanIncluding(LPCTSTR lpszCharSet) const;
  80. CString SpanExcluding(LPCTSTR lpszCharSet) const;
  81. // upper/lower/reverse conversion
  82. void MakeUpper();
  83. void MakeLower();
  84. void MakeReverse();
  85. // trimming whitespace (either side)
  86. void TrimRight();
  87. void TrimLeft();
  88. // searching (return starting index, or -1 if not found)
  89. // look for a single character match
  90. int Find(TCHAR ch) const; // like "C" strchr
  91. int ReverseFind(TCHAR ch) const;
  92. int FindOneOf(LPCTSTR lpszCharSet) const;
  93. // look for a specific sub-string
  94. int Find(LPCTSTR lpszSub) const; // like "C" strstr
  95. // simple formatting
  96. void AFX_CDECL Format(LPCTSTR lpszFormat, ...);
  97. #ifndef _MAC
  98. // formatting for localization (uses FormatMessage API)
  99. void AFX_CDECL FormatMessage(LPCTSTR lpszFormat, ...);
  100. #endif
  101. #ifndef _UNICODE
  102. // ANSI <-> OEM support (convert string in place)
  103. void AnsiToOem();
  104. void OemToAnsi();
  105. #endif
  106. // OLE BSTR support (use for OLE automation)
  107. BSTR AllocSysString() const;
  108. BSTR SetSysString(BSTR* pbstr) const;
  109. // Access to string implementation buffer as "C" character array
  110. LPTSTR GetBuffer(int nMinBufLength);
  111. void ReleaseBuffer(int nNewLength = -1);
  112. LPTSTR GetBufferSetLength(int nNewLength);
  113. void FreeExtra();
  114. // Use LockBuffer/UnlockBuffer to turn refcounting off
  115. LPTSTR LockBuffer();
  116. void UnlockBuffer();
  117. // Implementation
  118. public:
  119. ~CString();
  120. int GetAllocLength() const;
  121. protected:
  122. LPTSTR m_pchData; // pointer to ref counted string data
  123. // implementation helpers
  124. CStringData* GetData() const;
  125. void Init();
  126. void AllocCopy(CString& dest, int nCopyLen, int nCopyIndex, int nExtraLen) const;
  127. void AllocBuffer(int nLen);
  128. void AssignCopy(int nSrcLen, LPCTSTR lpszSrcData);
  129. void ConcatCopy(int nSrc1Len, LPCTSTR lpszSrc1Data, int nSrc2Len, LPCTSTR lpszSrc2Data);
  130. void ConcatInPlace(int nSrcLen, LPCTSTR lpszSrcData);
  131. void FormatV(LPCTSTR lpszFormat, va_list argList);
  132. void CopyBeforeWrite();
  133. void AllocBeforeWrite(int nLen);
  134. void Release();
  135. static void PASCAL Release(CStringData* pData);
  136. static int PASCAL SafeStrlen(LPCTSTR lpsz);
  137. };
  138. // Compare helpers
  139. bool AFXAPI operator==(const CString& s1, const CString& s2);
  140. bool AFXAPI operator==(const CString& s1, LPCTSTR s2);
  141. bool AFXAPI operator==(LPCTSTR s1, const CString& s2);
  142. bool AFXAPI operator!=(const CString& s1, const CString& s2);
  143. bool AFXAPI operator!=(const CString& s1, LPCTSTR s2);
  144. bool AFXAPI operator!=(LPCTSTR s1, const CString& s2);
  145. bool AFXAPI operator<(const CString& s1, const CString& s2);
  146. bool AFXAPI operator<(const CString& s1, LPCTSTR s2);
  147. bool AFXAPI operator<(LPCTSTR s1, const CString& s2);
  148. bool AFXAPI operator>(const CString& s1, const CString& s2);
  149. bool AFXAPI operator>(const CString& s1, LPCTSTR s2);
  150. bool AFXAPI operator>(LPCTSTR s1, const CString& s2);
  151. bool AFXAPI operator<=(const CString& s1, const CString& s2);
  152. bool AFXAPI operator<=(const CString& s1, LPCTSTR s2);
  153. bool AFXAPI operator<=(LPCTSTR s1, const CString& s2);
  154. bool AFXAPI operator>=(const CString& s1, const CString& s2);
  155. bool AFXAPI operator>=(const CString& s1, LPCTSTR s2);
  156. bool AFXAPI operator>=(LPCTSTR s1, const CString& s2);
  157. //regardless of UNICODE definition always use char
  158. struct CStringDataA
  159. {
  160. long nRefs; // reference count
  161. int nDataLength;
  162. int nAllocLength;
  163. // char data[nAllocLength]
  164. char* data()
  165. { return (char*)(this+1); }
  166. };
  167. class CStringA
  168. {
  169. public:
  170. // Constructors
  171. CStringA();
  172. CStringA(const CStringA& stringSrc);
  173. CStringA(char ch, int nRepeat = 1);
  174. CStringA(LPCSTR lpsz);
  175. CStringA(LPCWSTR lpsz);
  176. CStringA(LPCSTR lpch, int nLength);
  177. CStringA(const unsigned char* psz);
  178. // Attributes & Operations
  179. // as an array of characters
  180. int GetLength() const;
  181. BOOL IsEmpty() const;
  182. void Empty(); // free up the data
  183. char GetAt(int nIndex) const; // 0 based
  184. char operator[](int nIndex) const; // same as GetAt
  185. void SetAt(int nIndex, char ch);
  186. operator LPCSTR() const; // as a C string
  187. // overloaded assignment
  188. const CStringA& operator=(const CStringA& stringSrc);
  189. const CStringA& operator=(char ch);
  190. const CStringA& operator=(LPCSTR lpsz);
  191. const CStringA& operator=(LPCWSTR lpsz);
  192. const CStringA& operator=(const unsigned char* psz);
  193. // string concatenation
  194. const CStringA& operator+=(const CStringA& string);
  195. const CStringA& operator+=(char ch);
  196. const CStringA& operator+=(LPCSTR lpsz);
  197. friend CStringA AFXAPI operator+(const CStringA& string1,
  198. const CStringA& string2);
  199. friend CStringA AFXAPI operator+(const CStringA& string, char ch);
  200. friend CStringA AFXAPI operator+(char ch, const CStringA& string);
  201. friend CStringA AFXAPI operator+(const CStringA& string, LPCSTR lpsz);
  202. friend CStringA AFXAPI operator+(LPCSTR lpsz, const CStringA& string);
  203. // string comparison
  204. int Compare(LPCSTR lpsz) const; // straight character
  205. int CompareNoCase(LPCSTR lpsz) const; // ignore case
  206. int Collate(LPCSTR lpsz) const; // NLS aware
  207. // simple sub-string extraction
  208. CStringA Mid(int nFirst, int nCount) const;
  209. CStringA Mid(int nFirst) const;
  210. CStringA Left(int nCount) const;
  211. CStringA Right(int nCount) const;
  212. CStringA SpanIncluding(LPCSTR lpszCharSet) const;
  213. CStringA SpanExcluding(LPCSTR lpszCharSet) const;
  214. // upper/lower/reverse conversion
  215. void MakeUpper();
  216. void MakeLower();
  217. void MakeReverse();
  218. // trimming whitespace (either side)
  219. void TrimRight();
  220. void TrimLeft();
  221. // searching (return starting index, or -1 if not found)
  222. // look for a single character match
  223. int Find(char ch) const; // like "C" strchr
  224. int ReverseFind(char ch) const;
  225. int FindOneOf(LPCSTR lpszCharSet) const;
  226. // look for a specific sub-string
  227. int Find(LPCSTR lpszSub) const; // like "C" strstr
  228. // simple formatting
  229. void AFX_CDECL Format(LPCSTR lpszFormat, ...);
  230. #ifndef _MAC
  231. // formatting for localization (uses FormatMessage API)
  232. void AFX_CDECL FormatMessage(LPCSTR lpszFormat, ...);
  233. #endif
  234. // ANSI <-> OEM support (convert string in place)
  235. void AnsiToOem();
  236. void OemToAnsi();
  237. // OLE BSTR support (use for OLE automation)
  238. BSTR AllocSysString() const;
  239. BSTR SetSysString(BSTR* pbstr) const;
  240. // Access to string implementation buffer as "C" character array
  241. LPSTR GetBuffer(int nMinBufLength);
  242. void ReleaseBuffer(int nNewLength = -1);
  243. LPSTR GetBufferSetLength(int nNewLength);
  244. void FreeExtra();
  245. // Use LockBuffer/UnlockBuffer to turn refcounting off
  246. LPSTR LockBuffer();
  247. void UnlockBuffer();
  248. // Implementation
  249. public:
  250. ~CStringA();
  251. int GetAllocLength() const;
  252. protected:
  253. LPSTR m_pchData; // pointer to ref counted string data
  254. // implementation helpers
  255. CStringDataA* GetData() const;
  256. void Init();
  257. void AllocCopy(CStringA& dest, int nCopyLen, int nCopyIndex, int nExtraLen) const;
  258. void AllocBuffer(int nLen);
  259. void AssignCopy(int nSrcLen, LPCSTR lpszSrcData);
  260. void ConcatCopy(int nSrc1Len, LPCSTR lpszSrc1Data, int nSrc2Len, LPCSTR lpszSrc2Data);
  261. void ConcatInPlace(int nSrcLen, LPCSTR lpszSrcData);
  262. void FormatV(LPCSTR lpszFormat, va_list argList);
  263. void CopyBeforeWrite();
  264. void AllocBeforeWrite(int nLen);
  265. void Release();
  266. static void PASCAL Release(CStringDataA* pData);
  267. static int PASCAL SafeStrlen(LPCSTR lpsz);
  268. };
  269. // Compare helpers
  270. bool AFXAPI operator==(const CStringA& s1, const CStringA& s2);
  271. bool AFXAPI operator==(const CStringA& s1, LPCSTR s2);
  272. bool AFXAPI operator==(LPCSTR s1, const CStringA& s2);
  273. bool AFXAPI operator!=(const CStringA& s1, const CStringA& s2);
  274. bool AFXAPI operator!=(const CStringA& s1, LPCSTR s2);
  275. bool AFXAPI operator!=(LPCSTR s1, const CStringA& s2);
  276. bool AFXAPI operator<(const CStringA& s1, const CStringA& s2);
  277. bool AFXAPI operator<(const CStringA& s1, LPCSTR s2);
  278. bool AFXAPI operator<(LPCSTR s1, const CStringA& s2);
  279. bool AFXAPI operator>(const CStringA& s1, const CStringA& s2);
  280. bool AFXAPI operator>(const CStringA& s1, LPCSTR s2);
  281. bool AFXAPI operator>(LPCSTR s1, const CStringA& s2);
  282. bool AFXAPI operator<=(const CStringA& s1, const CStringA& s2);
  283. bool AFXAPI operator<=(const CStringA& s1, LPCSTR s2);
  284. bool AFXAPI operator<=(LPCSTR s1, const CStringA& s2);
  285. bool AFXAPI operator>=(const CStringA& s1, const CStringA& s2);
  286. bool AFXAPI operator>=(const CStringA& s1, LPCSTR s2);
  287. bool AFXAPI operator>=(LPCSTR s1, const CStringA& s2);
  288. //regardless of UNICODE definition always use wchar
  289. struct CStringDataW
  290. {
  291. long nRefs; // reference count
  292. int nDataLength;
  293. int nAllocLength;
  294. // WCHAR data[nAllocLength]
  295. WCHAR* data()
  296. { return (WCHAR*)(this+1); }
  297. };
  298. class CStringW
  299. {
  300. public:
  301. // Constructors
  302. CStringW();
  303. CStringW(const CStringW& stringSrc);
  304. CStringW(WCHAR ch, int nRepeat = 1);
  305. CStringW(LPCSTR lpsz);
  306. CStringW(LPCWSTR lpsz);
  307. CStringW(LPCWSTR lpch, int nLength);
  308. CStringW(const unsigned char* psz);
  309. // Attributes & Operations
  310. // as an array of characters
  311. int GetLength() const;
  312. BOOL IsEmpty() const;
  313. void Empty(); // free up the data
  314. WCHAR GetAt(int nIndex) const; // 0 based
  315. WCHAR operator[](int nIndex) const; // same as GetAt
  316. void SetAt(int nIndex, WCHAR ch);
  317. operator LPCWSTR() const; // as a C string
  318. // overloaded assignment
  319. const CStringW& operator=(const CStringW& stringSrc);
  320. const CStringW& operator=(WCHAR ch);
  321. const CStringW& operator=(char ch);
  322. const CStringW& operator=(LPCSTR lpsz);
  323. const CStringW& operator=(LPCWSTR lpsz);
  324. const CStringW& operator=(const unsigned char* psz);
  325. // string concatenation
  326. const CStringW& operator+=(const CStringW& string);
  327. const CStringW& operator+=(WCHAR ch);
  328. const CStringW& operator+=(char ch);
  329. const CStringW& operator+=(LPCWSTR lpsz);
  330. friend CStringW AFXAPI operator+(const CStringW& string1,
  331. const CStringW& string2);
  332. friend CStringW AFXAPI operator+(const CStringW& string, WCHAR ch);
  333. friend CStringW AFXAPI operator+(WCHAR ch, const CStringW& string);
  334. friend CStringW AFXAPI operator+(const CStringW& string, char ch);
  335. friend CStringW AFXAPI operator+(char ch, const CStringW& string);
  336. friend CStringW AFXAPI operator+(const CStringW& string, LPCWSTR lpsz);
  337. friend CStringW AFXAPI operator+(LPCWSTR lpsz, const CStringW& string);
  338. // string comparison
  339. int Compare(LPCWSTR lpsz) const; // straight character
  340. int CompareNoCase(LPCWSTR lpsz) const; // ignore case
  341. int Collate(LPCWSTR lpsz) const; // NLS aware
  342. // simple sub-string extraction
  343. CStringW Mid(int nFirst, int nCount) const;
  344. CStringW Mid(int nFirst) const;
  345. CStringW Left(int nCount) const;
  346. CStringW Right(int nCount) const;
  347. CStringW SpanIncluding(LPCWSTR lpszCharSet) const;
  348. CStringW SpanExcluding(LPCWSTR lpszCharSet) const;
  349. // upper/lower/reverse conversion
  350. void MakeUpper();
  351. void MakeLower();
  352. void MakeReverse();
  353. // trimming whitespace (either side)
  354. void TrimRight();
  355. void TrimLeft();
  356. // searching (return starting index, or -1 if not found)
  357. // look for a single character match
  358. int Find(WCHAR ch) const; // like "C" strchr
  359. int ReverseFind(WCHAR ch) const;
  360. int FindOneOf(LPCWSTR lpszCharSet) const;
  361. // look for a specific sub-string
  362. int Find(LPCWSTR lpszSub) const; // like "C" strstr
  363. // simple formatting
  364. void AFX_CDECL Format(LPCWSTR lpszFormat, ...);
  365. #ifndef _MAC
  366. // formatting for localization (uses FormatMessage API)
  367. void AFX_CDECL FormatMessage(LPCWSTR lpszFormat, ...);
  368. #endif
  369. // OLE BSTR support (use for OLE automation)
  370. BSTR AllocSysString() const;
  371. BSTR SetSysString(BSTR* pbstr) const;
  372. // Access to string implementation buffer as "C" character array
  373. LPWSTR GetBuffer(int nMinBufLength);
  374. void ReleaseBuffer(int nNewLength = -1);
  375. LPWSTR GetBufferSetLength(int nNewLength);
  376. void FreeExtra();
  377. // Use LockBuffer/UnlockBuffer to turn refcounting off
  378. LPWSTR LockBuffer();
  379. void UnlockBuffer();
  380. // Implementation
  381. public:
  382. ~CStringW();
  383. int GetAllocLength() const;
  384. protected:
  385. LPWSTR m_pchData; // pointer to ref counted string data
  386. // implementation helpers
  387. CStringDataW* GetData() const;
  388. void Init();
  389. void AllocCopy(CStringW& dest, int nCopyLen, int nCopyIndex, int nExtraLen) const;
  390. void AllocBuffer(int nLen);
  391. void AssignCopy(int nSrcLen, LPCWSTR lpszSrcData);
  392. void ConcatCopy(int nSrc1Len, LPCWSTR lpszSrc1Data, int nSrc2Len, LPCWSTR lpszSrc2Data);
  393. void ConcatInPlace(int nSrcLen, LPCWSTR lpszSrcData);
  394. void FormatV(LPCWSTR lpszFormat, va_list argList);
  395. void CopyBeforeWrite();
  396. void AllocBeforeWrite(int nLen);
  397. void Release();
  398. static void PASCAL Release(CStringDataW* pData);
  399. static int PASCAL SafeStrlen(LPCWSTR lpsz);
  400. };
  401. // Compare helpers
  402. bool AFXAPI operator==(const CStringW& s1, const CStringW& s2);
  403. bool AFXAPI operator==(const CStringW& s1, LPCWSTR s2);
  404. bool AFXAPI operator==(LPCWSTR s1, const CStringW& s2);
  405. bool AFXAPI operator!=(const CStringW& s1, const CStringW& s2);
  406. bool AFXAPI operator!=(const CStringW& s1, LPCWSTR s2);
  407. bool AFXAPI operator!=(LPCWSTR s1, const CStringW& s2);
  408. bool AFXAPI operator<(const CStringW& s1, const CStringW& s2);
  409. bool AFXAPI operator<(const CStringW& s1, LPCWSTR s2);
  410. bool AFXAPI operator<(LPCWSTR s1, const CStringW& s2);
  411. bool AFXAPI operator>(const CStringW& s1, const CStringW& s2);
  412. bool AFXAPI operator>(const CStringW& s1, LPCWSTR s2);
  413. bool AFXAPI operator>(LPCWSTR s1, const CStringW& s2);
  414. bool AFXAPI operator<=(const CStringW& s1, const CStringW& s2);
  415. bool AFXAPI operator<=(const CStringW& s1, LPCWSTR s2);
  416. bool AFXAPI operator<=(LPCWSTR s1, const CStringW& s2);
  417. bool AFXAPI operator>=(const CStringW& s1, const CStringW& s2);
  418. bool AFXAPI operator>=(const CStringW& s1, LPCWSTR s2);
  419. bool AFXAPI operator>=(LPCWSTR s1, const CStringW& s2);
  420. // conversion helpers
  421. int AFX_CDECL _wcstombsz(char* mbstr, const wchar_t* wcstr, size_t count);
  422. int AFX_CDECL _mbstowcsz(wchar_t* wcstr, const char* mbstr, size_t count);
  423. // Globals
  424. extern TCHAR afxChNil;
  425. const CString& AFXAPI AfxGetEmptyString();
  426. #define afxEmptyString AfxGetEmptyString()
  427. extern char afxChNilA;
  428. const CStringA& AFXAPI AfxGetEmptyStringA();
  429. #define afxEmptyStringA AfxGetEmptyStringA()
  430. extern WCHAR afxChNilW;
  431. const CStringW& AFXAPI AfxGetEmptyStringW();
  432. #define afxEmptyStringW AfxGetEmptyStringW()
  433. #endif