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.

328 lines
11 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1999 - 1999
  6. //
  7. // File: cstr.h
  8. //
  9. //--------------------------------------------------------------------------
  10. #ifndef __STR_H__
  11. #define __STR_H__
  12. #include <tchar.h>
  13. #define STRAPI __stdcall
  14. struct _STR_DOUBLE { BYTE doubleBits[sizeof(double)]; };
  15. BOOL STRAPI IsValidString(LPCSTR lpsz, int nLength);
  16. BOOL STRAPI IsValidString(LPCWSTR lpsz, int nLength);
  17. BOOL STRAPI IsValidAddressz(const void* lp, UINT nBytes, BOOL bReadWrite=TRUE);
  18. int STRAPI StrLoadString(HINSTANCE hInst, UINT nID, LPTSTR lpszBuf);
  19. class CStr
  20. {
  21. public:
  22. // Constructors
  23. CStr();
  24. CStr(const CStr& stringSrc);
  25. CStr(TCHAR ch, int nRepeat = 1);
  26. CStr(LPCSTR lpsz);
  27. CStr(LPCWSTR lpsz);
  28. CStr(LPCTSTR lpch, int nLength);
  29. CStr(const unsigned char* psz);
  30. // Attributes & Operations
  31. // as an array of characters
  32. int GetLength() const;
  33. BOOL IsEmpty() const;
  34. void Empty(); // free up the data
  35. TCHAR GetAt(int nIndex) const; // 0 based
  36. TCHAR operator[](int nIndex) const; // same as GetAt
  37. void SetAt(int nIndex, TCHAR ch);
  38. operator LPCTSTR() const; // as a C string
  39. // overloaded assignment
  40. const CStr& operator=(const CStr& stringSrc);
  41. const CStr& operator=(TCHAR ch);
  42. #ifdef UNICODE
  43. const CStr& operator=(char ch);
  44. #endif
  45. const CStr& operator=(LPCSTR lpsz);
  46. const CStr& operator=(LPCWSTR lpsz);
  47. const CStr& operator=(const unsigned char* psz);
  48. // string concatenation
  49. const CStr& operator+=(const CStr& string);
  50. const CStr& operator+=(TCHAR ch);
  51. #ifdef UNICODE
  52. const CStr& operator+=(char ch);
  53. #endif
  54. const CStr& operator+=(LPCTSTR lpsz);
  55. friend CStr STRAPI operator+(const CStr& string1,
  56. const CStr& string2);
  57. friend CStr STRAPI operator+(const CStr& string, TCHAR ch);
  58. friend CStr STRAPI operator+(TCHAR ch, const CStr& string);
  59. #ifdef UNICODE
  60. friend CStr STRAPI operator+(const CStr& string, char ch);
  61. friend CStr STRAPI operator+(char ch, const CStr& string);
  62. #endif
  63. friend CStr STRAPI operator+(const CStr& string, LPCTSTR lpsz);
  64. friend CStr STRAPI operator+(LPCTSTR lpsz, const CStr& string);
  65. // string comparison
  66. int Compare(LPCTSTR lpsz) const; // straight character
  67. int CompareNoCase(LPCTSTR lpsz) const; // ignore case
  68. int Collate(LPCTSTR lpsz) const; // NLS aware
  69. // simple sub-string extraction
  70. CStr Mid(int nFirst, int nCount) const;
  71. CStr Mid(int nFirst) const;
  72. CStr Left(int nCount) const;
  73. CStr Right(int nCount) const;
  74. CStr SpanIncluding(LPCTSTR lpszCharSet) const;
  75. CStr SpanExcluding(LPCTSTR lpszCharSet) const;
  76. // upper/lower/reverse conversion
  77. void MakeUpper();
  78. void MakeLower();
  79. void MakeReverse();
  80. // trimming whitespace (either side)
  81. void TrimRight();
  82. void TrimLeft();
  83. // searching (return starting index, or -1 if not found)
  84. // look for a single character match
  85. int Find(TCHAR ch) const; // like "C" strchr
  86. int ReverseFind(TCHAR ch) const;
  87. int FindOneOf(LPCTSTR lpszCharSet) const;
  88. // look for a specific sub-string
  89. int Find(LPCTSTR lpszSub) const; // like "C" strstr
  90. // simple formatting
  91. void FormatV(LPCTSTR lpszFormat, va_list argList);
  92. void Format(LPCTSTR lpszFormat, ...);
  93. #ifndef _MAC
  94. // formatting for localization (uses FormatMessage API)
  95. void __cdecl FormatMessage(LPCTSTR lpszFormat, ...);
  96. void __cdecl FormatMessage(UINT nFormatID, ...);
  97. #endif
  98. // Windows support
  99. BOOL LoadString(HINSTANCE hInst, UINT nID); // load from string resource
  100. // 255 chars max
  101. #ifndef UNICODE
  102. // ANSI <-> OEM support (convert string in place)
  103. void AnsiToOem();
  104. void OemToAnsi();
  105. #endif
  106. BSTR AllocSysString();
  107. BSTR SetSysString(BSTR* pbstr);
  108. // Access to string implementation buffer as "C" character array
  109. LPTSTR GetBuffer(int nMinBufLength);
  110. void ReleaseBuffer(int nNewLength = -1);
  111. LPTSTR GetBufferSetLength(int nNewLength);
  112. void FreeExtra();
  113. // Implementation
  114. public:
  115. ~CStr();
  116. int GetAllocLength() const;
  117. protected:
  118. // lengths/sizes in characters
  119. // (note: an extra character is always allocated)
  120. LPTSTR m_pchData; // actual string (zero terminated)
  121. int m_nDataLength; // does not include terminating 0
  122. int m_nAllocLength; // does not include terminating 0
  123. // implementation helpers
  124. void Init();
  125. void AllocCopy(CStr& dest, int nCopyLen, int nCopyIndex, int nExtraLen) const;
  126. void AllocBuffer(int nLen);
  127. void AssignCopy(int nSrcLen, LPCTSTR lpszSrcData);
  128. void ConcatCopy(int nSrc1Len, LPCTSTR lpszSrc1Data, int nSrc2Len, LPCTSTR lpszSrc2Data);
  129. void ConcatInPlace(int nSrcLen, LPCTSTR lpszSrcData);
  130. static void SafeDelete(LPTSTR lpch);
  131. static int SafeStrlen(LPCTSTR lpsz);
  132. };
  133. // Compare helpers
  134. BOOL STRAPI operator==(const CStr& s1, const CStr& s2);
  135. BOOL STRAPI operator==(const CStr& s1, LPCTSTR s2);
  136. BOOL STRAPI operator==(LPCTSTR s1, const CStr& s2);
  137. BOOL STRAPI operator!=(const CStr& s1, const CStr& s2);
  138. BOOL STRAPI operator!=(const CStr& s1, LPCTSTR s2);
  139. BOOL STRAPI operator!=(LPCTSTR s1, const CStr& s2);
  140. BOOL STRAPI operator<(const CStr& s1, const CStr& s2);
  141. BOOL STRAPI operator<(const CStr& s1, LPCTSTR s2);
  142. BOOL STRAPI operator<(LPCTSTR s1, const CStr& s2);
  143. BOOL STRAPI operator>(const CStr& s1, const CStr& s2);
  144. BOOL STRAPI operator>(const CStr& s1, LPCTSTR s2);
  145. BOOL STRAPI operator>(LPCTSTR s1, const CStr& s2);
  146. BOOL STRAPI operator<=(const CStr& s1, const CStr& s2);
  147. BOOL STRAPI operator<=(const CStr& s1, LPCTSTR s2);
  148. BOOL STRAPI operator<=(LPCTSTR s1, const CStr& s2);
  149. BOOL STRAPI operator>=(const CStr& s1, const CStr& s2);
  150. BOOL STRAPI operator>=(const CStr& s1, LPCTSTR s2);
  151. BOOL STRAPI operator>=(LPCTSTR s1, const CStr& s2);
  152. // conversion helpers
  153. int mmc_wcstombsz(char* mbstr, const wchar_t* wcstr, size_t count);
  154. int mmc_mbstowcsz(wchar_t* wcstr, const char* mbstr, size_t count);
  155. // Globals
  156. extern const CStr strEmptyString;
  157. extern TCHAR strChNil;
  158. // Compiler doesn't inline for DBG
  159. /////////////////////////////////////////////////////////////////////////////
  160. // Inline function declarations
  161. inline int CStr::SafeStrlen(LPCTSTR lpsz)
  162. { return (lpsz == NULL) ? NULL : _tcslen(lpsz); }
  163. inline CStr::CStr(const unsigned char* lpsz)
  164. { Init(); *this = (LPCSTR)lpsz; }
  165. inline const CStr& CStr::operator=(const unsigned char* lpsz)
  166. { *this = (LPCSTR)lpsz; return *this; }
  167. #ifdef _UNICODE
  168. inline const CStr& CStr::operator+=(char ch)
  169. { *this += (TCHAR)ch; return *this; }
  170. inline const CStr& CStr::operator=(char ch)
  171. { *this = (TCHAR)ch; return *this; }
  172. inline CStr STRAPI operator+(const CStr& string, char ch)
  173. { return string + (TCHAR)ch; }
  174. inline CStr STRAPI operator+(char ch, const CStr& string)
  175. { return (TCHAR)ch + string; }
  176. #endif
  177. inline int CStr::GetLength() const
  178. { return m_nDataLength; }
  179. inline int CStr::GetAllocLength() const
  180. { return m_nAllocLength; }
  181. inline BOOL CStr::IsEmpty() const
  182. { return m_nDataLength == 0; }
  183. inline CStr::operator LPCTSTR() const
  184. { return (LPCTSTR)m_pchData; }
  185. // String support (windows specific)
  186. inline int CStr::Compare(LPCTSTR lpsz) const
  187. { return _tcscmp(m_pchData, lpsz); } // MBCS/Unicode aware
  188. inline int CStr::CompareNoCase(LPCTSTR lpsz) const
  189. { return _tcsicmp(m_pchData, lpsz); } // MBCS/Unicode aware
  190. // CStr::Collate is often slower than Compare but is MBSC/Unicode
  191. // aware as well as locale-sensitive with respect to sort order.
  192. inline int CStr::Collate(LPCTSTR lpsz) const
  193. { return _tcscoll(m_pchData, lpsz); } // locale sensitive
  194. inline void CStr::MakeUpper()
  195. { ::CharUpper(m_pchData); }
  196. inline void CStr::MakeLower()
  197. { ::CharLower(m_pchData); }
  198. inline void CStr::MakeReverse()
  199. { _tcsrev(m_pchData); }
  200. inline TCHAR CStr::GetAt(int nIndex) const
  201. {
  202. ASSERT(nIndex >= 0);
  203. ASSERT(nIndex < m_nDataLength);
  204. return m_pchData[nIndex];
  205. }
  206. inline TCHAR CStr::operator[](int nIndex) const
  207. {
  208. // same as GetAt
  209. ASSERT(nIndex >= 0);
  210. ASSERT(nIndex < m_nDataLength);
  211. return m_pchData[nIndex];
  212. }
  213. inline void CStr::SetAt(int nIndex, TCHAR ch)
  214. {
  215. ASSERT(nIndex >= 0);
  216. ASSERT(nIndex < m_nDataLength);
  217. ASSERT(ch != 0);
  218. m_pchData[nIndex] = ch;
  219. }
  220. inline BOOL STRAPI operator==(const CStr& s1, const CStr& s2)
  221. { return s1.Compare(s2) == 0; }
  222. inline BOOL STRAPI operator==(const CStr& s1, LPCTSTR s2)
  223. { return s1.Compare(s2) == 0; }
  224. inline BOOL STRAPI operator==(LPCTSTR s1, const CStr& s2)
  225. { return s2.Compare(s1) == 0; }
  226. inline BOOL STRAPI operator!=(const CStr& s1, const CStr& s2)
  227. { return s1.Compare(s2) != 0; }
  228. inline BOOL STRAPI operator!=(const CStr& s1, LPCTSTR s2)
  229. { return s1.Compare(s2) != 0; }
  230. inline BOOL STRAPI operator!=(LPCTSTR s1, const CStr& s2)
  231. { return s2.Compare(s1) != 0; }
  232. inline BOOL STRAPI operator<(const CStr& s1, const CStr& s2)
  233. { return s1.Compare(s2) < 0; }
  234. inline BOOL STRAPI operator<(const CStr& s1, LPCTSTR s2)
  235. { return s1.Compare(s2) < 0; }
  236. inline BOOL STRAPI operator<(LPCTSTR s1, const CStr& s2)
  237. { return s2.Compare(s1) > 0; }
  238. inline BOOL STRAPI operator>(const CStr& s1, const CStr& s2)
  239. { return s1.Compare(s2) > 0; }
  240. inline BOOL STRAPI operator>(const CStr& s1, LPCTSTR s2)
  241. { return s1.Compare(s2) > 0; }
  242. inline BOOL STRAPI operator>(LPCTSTR s1, const CStr& s2)
  243. { return s2.Compare(s1) < 0; }
  244. inline BOOL STRAPI operator<=(const CStr& s1, const CStr& s2)
  245. { return s1.Compare(s2) <= 0; }
  246. inline BOOL STRAPI operator<=(const CStr& s1, LPCTSTR s2)
  247. { return s1.Compare(s2) <= 0; }
  248. inline BOOL STRAPI operator<=(LPCTSTR s1, const CStr& s2)
  249. { return s2.Compare(s1) >= 0; }
  250. inline BOOL STRAPI operator>=(const CStr& s1, const CStr& s2)
  251. { return s1.Compare(s2) >= 0; }
  252. inline BOOL STRAPI operator>=(const CStr& s1, LPCTSTR s2)
  253. { return s1.Compare(s2) >= 0; }
  254. inline BOOL STRAPI operator>=(LPCTSTR s1, const CStr& s2)
  255. { return s2.Compare(s1) <= 0; }
  256. #ifndef UNICODE
  257. inline void CStr::AnsiToOem()
  258. { ::AnsiToOem(m_pchData, m_pchData); }
  259. inline void CStr::OemToAnsi()
  260. { ::OemToAnsi(m_pchData, m_pchData); }
  261. #endif // UNICODE
  262. // General Exception for memory
  263. class MemoryException
  264. {
  265. public:
  266. MemoryException(){}
  267. void DisplayMessage()
  268. {
  269. ::MessageBox(NULL, _T("Memory Exception"), _T("System Out of Memory"), MB_OK|MB_ICONSTOP);
  270. }
  271. };
  272. // General Exception for memory
  273. class ResourceException
  274. {
  275. public:
  276. ResourceException()
  277. {
  278. ::MessageBox(NULL, _T("Resource Exception"), _T("Unable to Load Resource"), MB_OK|MB_ICONSTOP);
  279. }
  280. };
  281. #endif // __STR_H__
  282. /////////////////////////////////////////////////////////////////////////////