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.

309 lines
10 KiB

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