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
8.7 KiB

  1. #ifndef _INC_DSKQUOTA_STRCLASS_H
  2. #define _INC_DSKQUOTA_STRCLASS_H
  3. ///////////////////////////////////////////////////////////////////////////////
  4. /* File: strclass.h
  5. Description: Typical class to handle strings.
  6. Revision History:
  7. Date Description Programmer
  8. -------- --------------------------------------------------- ----------
  9. 07/01/97 Initial creation. BrianAu
  10. */
  11. ///////////////////////////////////////////////////////////////////////////////
  12. #ifndef _WINDOWS_
  13. # include <windows.h>
  14. #endif
  15. #ifndef _INC_STDIO
  16. # include <stdio.h>
  17. #endif
  18. #ifndef _INC_STDARG
  19. # include <stdarg.h> // For va_list stuff.
  20. #endif
  21. #ifndef _INC_DSKQUOTA_EXCEPT_H
  22. # include "except.h"
  23. #endif
  24. #undef StrCpyA
  25. //
  26. // class String implements a reference-counted string class with all
  27. // the typical string class operations.
  28. //
  29. class CString
  30. {
  31. public:
  32. CString(VOID);
  33. explicit CString(INT cch);
  34. explicit CString(LPCSTR pszA);
  35. explicit CString(LPCWSTR pszW);
  36. CString(const CString& rhs);
  37. CString(HINSTANCE hInst, INT idMsg, ...);
  38. virtual ~CString(VOID);
  39. //
  40. // Basic operations.
  41. // - Assignment.
  42. // - Concatenation.
  43. // - Comparison.
  44. // - Array subscript.
  45. //
  46. CString& operator = (const CString& rhs);
  47. CString& operator = (LPCSTR rhsA);
  48. CString& operator = (LPCWSTR rhsW);
  49. CString operator + (const CString& rhs) const;
  50. CString operator + (LPCSTR rhsA) const;
  51. CString operator + (LPCWSTR rhsW) const;
  52. friend CString operator + (LPCSTR pszA, const CString& s);
  53. friend CString operator + (LPCWSTR pszW, const CString& s);
  54. CString& operator += (const CString& rhs);
  55. BOOL operator == (const CString& rhs) const;
  56. BOOL operator != (const CString& rhs) const;
  57. BOOL operator < (const CString& rhs) const;
  58. BOOL operator <= (const CString& rhs) const;
  59. BOOL operator > (const CString& rhs) const;
  60. BOOL operator >= (const CString& rhs) const;
  61. TCHAR operator[] (INT index) const;
  62. TCHAR& operator[] (INT index);
  63. //
  64. // Type conversion. Give read access to nul terminated c-string.
  65. //
  66. operator LPCTSTR(VOID) const
  67. { return m_pValue->m_psz; }
  68. operator LPCTSTR(VOID)
  69. { return m_pValue->m_psz; }
  70. operator LPTSTR(VOID)
  71. { CopyOnWrite(); return m_pValue->m_psz; }
  72. //
  73. // Same thing as (LPCTSTR) conversion but in function form.
  74. //
  75. LPCTSTR Cstr(void) const
  76. { return m_pValue->m_psz; }
  77. //
  78. // Return a pointer to a specifically-sized buffer.
  79. //
  80. LPTSTR GetBuffer(INT cchMax = -1);
  81. void ReleaseBuffer(void);
  82. //
  83. // Trim trailing or leading whitespace.
  84. //
  85. void Rtrim(void);
  86. void Ltrim(void);
  87. void Trim(void)
  88. { Ltrim(); Rtrim(); }
  89. //
  90. // Character location.
  91. //
  92. INT First(TCHAR ch) const;
  93. INT Last(TCHAR ch) const;
  94. //
  95. // Extract a substring.
  96. //
  97. CString SubString(INT iFirst, INT cch = -1);
  98. //
  99. // Convert characters to upper/lower case.
  100. //
  101. VOID ToUpper(INT iFirst = 0, INT cch = -1);
  102. VOID ToLower(INT iFirst = 0, INT cch = -1);
  103. //
  104. // Load string from resource or message table.
  105. // Supports FormatMessage-style variable arg formatting.
  106. //
  107. BOOL Format(HINSTANCE hInst, UINT idFmt, ...);
  108. BOOL Format(LPCTSTR pszFmt, ...);
  109. BOOL Format(HINSTANCE hInst, UINT idFmt, va_list *pargs);
  110. BOOL Format(LPCTSTR pszFmt, va_list *pargs);
  111. //
  112. // Minimum size display rect.
  113. //
  114. bool GetDisplayRect(HDC hdc, LPRECT prc) const;
  115. //
  116. // Expand any embedded environment strings.
  117. //
  118. VOID ExpandEnvironmentStrings(VOID);
  119. //
  120. // Compare with a normal 'C' string.
  121. //
  122. INT Compare(LPCWSTR rhsW) const;
  123. INT Compare(LPCSTR rhsA) const;
  124. INT CompareNoCase(LPCWSTR rhsW) const;
  125. INT CompareNoCase(LPCSTR rhsA) const;
  126. //
  127. // Clear a string's contents. Leaves in new-object state.
  128. //
  129. VOID Empty(VOID);
  130. //
  131. // Does the object have no content?
  132. //
  133. BOOL IsEmpty(VOID) const;
  134. //
  135. // Length of string, excluding nul terminator.
  136. //
  137. INT Length(VOID) const;
  138. INT LengthBytes(VOID) const
  139. { return Length() * sizeof(TCHAR); }
  140. VOID Size(INT cch);
  141. INT Size(VOID) const
  142. { return m_pValue->m_cchAlloc; }
  143. INT SizeBytes(VOID) const
  144. { return m_pValue->m_cchAlloc * sizeof(TCHAR); }
  145. VOID DebugOut(BOOL bNewline = TRUE) const;
  146. //
  147. // Replacements for standard string functions.
  148. // The Ansi versions are DBCS-aware.
  149. //
  150. static LPSTR StrCpyA(LPSTR pszDest, LPCSTR pszSrc);
  151. static LPWSTR StrCpyW(LPWSTR pszDest, LPCWSTR pszSrc);
  152. static INT StrLenA(LPCSTR psz);
  153. static INT StrLenW(LPCWSTR psz);
  154. static LPSTR StrCpyNA(LPSTR pszDest, LPCSTR pszSrc, INT cch);
  155. static LPWSTR StrCpyNW(LPWSTR pszDest, LPCWSTR pszSrc, INT cch);
  156. private:
  157. //
  158. // class StringValue contains actual string data and a reference count.
  159. // Class CString has a pointer to one of these. If a CString
  160. // object is initialized with or assigned another CString, their StringValue
  161. // pointers reference the same StringValue object. The StringValue
  162. // object maintains a reference count to keep track of how many
  163. // CString objects reference it. The CString object implements
  164. // copy-on-write so that when it is modified, a private copy of the
  165. // StringValue is created so other CString objects are left unmodified.
  166. //
  167. struct StringValue
  168. {
  169. LPTSTR m_psz; // Ptr to nul-term character string.
  170. INT m_cchAlloc; // Number of characters allocated in buffer.
  171. LONG m_cRef; // Number of CString objects referencing this value.
  172. mutable INT m_cch; // Number of characters in buffer (excl nul term).
  173. StringValue(VOID);
  174. StringValue(INT cch);
  175. StringValue(LPCSTR pszA);
  176. StringValue(LPCWSTR pszW);
  177. ~StringValue(VOID);
  178. INT Length(VOID) const;
  179. static LPSTR WideToAnsi(LPCWSTR pszW, INT *pcch = NULL);
  180. static LPWSTR AnsiToWide(LPCSTR pszA, INT *pcch = NULL);
  181. static LPWSTR Dup(LPCWSTR pszW, INT len = 0);
  182. static LPSTR Dup(LPCSTR pszA, INT len = 0);
  183. static LPTSTR Concat(StringValue *psv1, StringValue *psv2);
  184. };
  185. StringValue *m_pValue; // Pointer to string representation.
  186. BOOL ValidIndex(INT index) const;
  187. VOID CopyOnWrite(VOID);
  188. inline bool IsWhiteSpace(TCHAR ch) const;
  189. };
  190. inline bool
  191. CString::IsWhiteSpace(
  192. TCHAR ch
  193. ) const
  194. {
  195. return (TEXT(' ') == ch ||
  196. TEXT('\t') == ch ||
  197. TEXT('\n') == ch);
  198. }
  199. inline BOOL
  200. CString::ValidIndex(
  201. INT index
  202. ) const
  203. {
  204. return (0 <= index && index < Length());
  205. }
  206. inline BOOL
  207. CString::operator != (
  208. const CString& rhs
  209. ) const
  210. {
  211. return !(this->operator == (rhs));
  212. }
  213. inline BOOL
  214. CString::operator <= (
  215. const CString& rhs
  216. ) const
  217. {
  218. return (*this < rhs || *this == rhs);
  219. }
  220. inline BOOL
  221. CString::operator > (
  222. const CString& rhs
  223. ) const
  224. {
  225. return !(*this <= rhs);
  226. }
  227. inline BOOL
  228. CString::operator >= (
  229. const CString& rhs
  230. ) const
  231. {
  232. return !(*this < rhs);
  233. }
  234. inline LPWSTR
  235. CString::StrCpyW(
  236. LPWSTR pszDest,
  237. LPCWSTR pszSrc
  238. )
  239. {
  240. return lstrcpyW(pszDest, pszSrc);
  241. }
  242. inline LPSTR
  243. CString::StrCpyA(
  244. LPSTR pszDest,
  245. LPCSTR pszSrc
  246. )
  247. {
  248. return lstrcpyA(pszDest, pszSrc);
  249. }
  250. inline INT
  251. CString::StrLenW(
  252. LPCWSTR psz
  253. )
  254. {
  255. return lstrlenW(psz);
  256. }
  257. inline INT
  258. CString::StrLenA(
  259. LPCSTR psz
  260. )
  261. {
  262. return lstrlenA(psz);
  263. }
  264. inline LPWSTR
  265. CString::StrCpyNW(
  266. LPWSTR pszDest,
  267. LPCWSTR pszSrc,
  268. INT cch
  269. )
  270. {
  271. return lstrcpynW(pszDest, pszSrc, cch);
  272. }
  273. inline LPSTR
  274. CString::StrCpyNA(
  275. LPSTR pszDest,
  276. LPCSTR pszSrc,
  277. INT cch
  278. )
  279. {
  280. return lstrcpynA(pszDest, pszSrc, cch);
  281. }
  282. CString
  283. operator + (const LPCWSTR pszW, const CString& s);
  284. CString
  285. operator + (const LPCSTR pszA, const CString& s);
  286. #endif // _INC_DSKQUOTA_STRCLASS_H