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.

315 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. BSTR AllocSysString();
  102. BSTR SetSysString(BSTR* pbstr);
  103. // Access to string implementation buffer as "C" character array
  104. LPTSTR GetBuffer(int nMinBufLength);
  105. void ReleaseBuffer(int nNewLength = -1);
  106. LPTSTR GetBufferSetLength(int nNewLength);
  107. void FreeExtra();
  108. // Implementation
  109. public:
  110. ~CStr();
  111. int GetAllocLength() const;
  112. protected:
  113. // lengths/sizes in characters
  114. // (note: an extra character is always allocated)
  115. LPTSTR m_pchData; // actual string (zero terminated)
  116. int m_nDataLength; // does not include terminating 0
  117. int m_nAllocLength; // does not include terminating 0
  118. // implementation helpers
  119. void Init();
  120. void AllocCopy(CStr& dest, int nCopyLen, int nCopyIndex, int nExtraLen) const;
  121. void AllocBuffer(int nLen);
  122. void AssignCopy(int nSrcLen, LPCTSTR lpszSrcData);
  123. void ConcatCopy(int nSrc1Len, LPCTSTR lpszSrc1Data, int nSrc2Len, LPCTSTR lpszSrc2Data);
  124. void ConcatInPlace(int nSrcLen, LPCTSTR lpszSrcData);
  125. static void SafeDelete(LPTSTR lpch);
  126. static int SafeStrlen(LPCTSTR lpsz);
  127. };
  128. // Compare helpers
  129. BOOL STRAPI operator==(const CStr& s1, const CStr& s2);
  130. BOOL STRAPI operator==(const CStr& s1, LPCTSTR s2);
  131. BOOL STRAPI operator==(LPCTSTR s1, const CStr& s2);
  132. BOOL STRAPI operator!=(const CStr& s1, const CStr& s2);
  133. BOOL STRAPI operator!=(const CStr& s1, LPCTSTR s2);
  134. BOOL STRAPI operator!=(LPCTSTR s1, const CStr& s2);
  135. BOOL STRAPI operator<(const CStr& s1, const CStr& s2);
  136. BOOL STRAPI operator<(const CStr& s1, LPCTSTR s2);
  137. BOOL STRAPI operator<(LPCTSTR s1, const CStr& s2);
  138. BOOL STRAPI operator>(const CStr& s1, const CStr& s2);
  139. BOOL STRAPI operator>(const CStr& s1, LPCTSTR s2);
  140. BOOL STRAPI operator>(LPCTSTR s1, const CStr& s2);
  141. BOOL STRAPI operator<=(const CStr& s1, const CStr& s2);
  142. BOOL STRAPI operator<=(const CStr& s1, LPCTSTR s2);
  143. BOOL STRAPI operator<=(LPCTSTR s1, const CStr& s2);
  144. BOOL STRAPI operator>=(const CStr& s1, const CStr& s2);
  145. BOOL STRAPI operator>=(const CStr& s1, LPCTSTR s2);
  146. BOOL STRAPI operator>=(LPCTSTR s1, const CStr& s2);
  147. // conversion helpers
  148. int mmc_wcstombsz(char* mbstr, const wchar_t* wcstr, size_t count);
  149. int mmc_mbstowcsz(wchar_t* wcstr, const char* mbstr, size_t count);
  150. // Globals
  151. extern const CStr strEmptyString;
  152. extern TCHAR strChNil;
  153. // Compiler doesn't inline for DBG
  154. /////////////////////////////////////////////////////////////////////////////
  155. // Inline function declarations
  156. inline int CStr::SafeStrlen(LPCTSTR lpsz)
  157. { return (lpsz == NULL) ? NULL : _tcslen(lpsz); }
  158. inline CStr::CStr(const unsigned char* lpsz)
  159. { Init(); *this = (LPCSTR)lpsz; }
  160. inline const CStr& CStr::operator=(const unsigned char* lpsz)
  161. { *this = (LPCSTR)lpsz; return *this; }
  162. #ifdef _UNICODE
  163. inline const CStr& CStr::operator+=(char ch)
  164. { *this += (TCHAR)ch; return *this; }
  165. inline const CStr& CStr::operator=(char ch)
  166. { *this = (TCHAR)ch; return *this; }
  167. inline CStr STRAPI operator+(const CStr& string, char ch)
  168. { return string + (TCHAR)ch; }
  169. inline CStr STRAPI operator+(char ch, const CStr& string)
  170. { return (TCHAR)ch + string; }
  171. #endif
  172. inline int CStr::GetLength() const
  173. { return m_nDataLength; }
  174. inline int CStr::GetAllocLength() const
  175. { return m_nAllocLength; }
  176. inline BOOL CStr::IsEmpty() const
  177. { return m_nDataLength == 0; }
  178. inline CStr::operator LPCTSTR() const
  179. { return (LPCTSTR)m_pchData; }
  180. // String support (windows specific)
  181. inline int CStr::Compare(LPCTSTR lpsz) const
  182. { return _tcscmp(m_pchData, lpsz); } // MBCS/Unicode aware
  183. inline int CStr::CompareNoCase(LPCTSTR lpsz) const
  184. { return _tcsicmp(m_pchData, lpsz); } // MBCS/Unicode aware
  185. // CStr::Collate is often slower than Compare but is MBSC/Unicode
  186. // aware as well as locale-sensitive with respect to sort order.
  187. inline int CStr::Collate(LPCTSTR lpsz) const
  188. { return _tcscoll(m_pchData, lpsz); } // locale sensitive
  189. inline void CStr::MakeUpper()
  190. { ::CharUpper(m_pchData); }
  191. inline void CStr::MakeLower()
  192. { ::CharLower(m_pchData); }
  193. inline void CStr::MakeReverse()
  194. { _tcsrev(m_pchData); }
  195. inline TCHAR CStr::GetAt(int nIndex) const
  196. {
  197. ASSERT(nIndex >= 0);
  198. ASSERT(nIndex < m_nDataLength);
  199. return m_pchData[nIndex];
  200. }
  201. inline TCHAR CStr::operator[](int nIndex) const
  202. {
  203. // same as GetAt
  204. ASSERT(nIndex >= 0);
  205. ASSERT(nIndex < m_nDataLength);
  206. return m_pchData[nIndex];
  207. }
  208. inline void CStr::SetAt(int nIndex, TCHAR ch)
  209. {
  210. ASSERT(nIndex >= 0);
  211. ASSERT(nIndex < m_nDataLength);
  212. ASSERT(ch != 0);
  213. m_pchData[nIndex] = ch;
  214. }
  215. inline BOOL STRAPI operator==(const CStr& s1, const CStr& s2)
  216. { return s1.Compare(s2) == 0; }
  217. inline BOOL STRAPI operator==(const CStr& s1, LPCTSTR s2)
  218. { return s1.Compare(s2) == 0; }
  219. inline BOOL STRAPI operator==(LPCTSTR s1, const CStr& s2)
  220. { return s2.Compare(s1) == 0; }
  221. inline BOOL STRAPI operator!=(const CStr& s1, const CStr& s2)
  222. { return s1.Compare(s2) != 0; }
  223. inline BOOL STRAPI operator!=(const CStr& s1, LPCTSTR s2)
  224. { return s1.Compare(s2) != 0; }
  225. inline BOOL STRAPI operator!=(LPCTSTR s1, const CStr& s2)
  226. { return s2.Compare(s1) != 0; }
  227. inline BOOL STRAPI operator<(const CStr& s1, const CStr& s2)
  228. { return s1.Compare(s2) < 0; }
  229. inline BOOL STRAPI operator<(const CStr& s1, LPCTSTR s2)
  230. { return s1.Compare(s2) < 0; }
  231. inline BOOL STRAPI operator<(LPCTSTR s1, const CStr& s2)
  232. { return s2.Compare(s1) > 0; }
  233. inline BOOL STRAPI operator>(const CStr& s1, const CStr& s2)
  234. { return s1.Compare(s2) > 0; }
  235. inline BOOL STRAPI operator>(const CStr& s1, LPCTSTR s2)
  236. { return s1.Compare(s2) > 0; }
  237. inline BOOL STRAPI operator>(LPCTSTR s1, const CStr& s2)
  238. { return s2.Compare(s1) < 0; }
  239. inline BOOL STRAPI operator<=(const CStr& s1, const CStr& s2)
  240. { return s1.Compare(s2) <= 0; }
  241. inline BOOL STRAPI operator<=(const CStr& s1, LPCTSTR s2)
  242. { return s1.Compare(s2) <= 0; }
  243. inline BOOL STRAPI operator<=(LPCTSTR s1, const CStr& s2)
  244. { return s2.Compare(s1) >= 0; }
  245. inline BOOL STRAPI operator>=(const CStr& s1, const CStr& s2)
  246. { return s1.Compare(s2) >= 0; }
  247. inline BOOL STRAPI operator>=(const CStr& s1, LPCTSTR s2)
  248. { return s1.Compare(s2) >= 0; }
  249. inline BOOL STRAPI operator>=(LPCTSTR s1, const CStr& s2)
  250. { return s2.Compare(s1) <= 0; }
  251. // General Exception for memory
  252. class MemoryException
  253. {
  254. public:
  255. MemoryException(){}
  256. void DisplayMessage()
  257. {
  258. ::MessageBox(NULL, _T("Memory Exception"), _T("System Out of Memory"), MB_OK|MB_ICONSTOP);
  259. }
  260. };
  261. // General Exception for memory
  262. class ResourceException
  263. {
  264. public:
  265. ResourceException()
  266. {
  267. ::MessageBox(NULL, _T("Resource Exception"), _T("Unable to Load Resource"), MB_OK|MB_ICONSTOP);
  268. }
  269. };
  270. #endif // __STR_H__
  271. /////////////////////////////////////////////////////////////////////////////