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.

241 lines
7.8 KiB

  1. #ifndef _CSTRING_HPP_
  2. #define _CSTRING_HPP_
  3. #include <nmutil.h>
  4. // These two header files contain definitions that used to be in this file.
  5. // To allow source files which include this file to continue to work
  6. // unmodified, we include them here.
  7. #include <strutil.h>
  8. #include <custring.h>
  9. #define REMAFXAPI
  10. #define REMAFX_DATADEF
  11. #define REMAFX_DATA
  12. #define REMAFX_CDECL
  13. #define REM_AFX_INLINE inline
  14. // BUGBUG - How are these used?
  15. #ifndef PUBLIC_CODE
  16. #define PUBLIC_CODE
  17. #define PUBLIC_DATA
  18. #define PRIVATE_CODE PUBLIC_CODE
  19. #define PRIVATE_DATA PUBLIC_DATA
  20. #endif
  21. struct CSTRINGData
  22. {
  23. long nRefs; // reference count
  24. int nDataLength;
  25. int nAllocLength;
  26. // TCHAR data[nAllocLength]
  27. TCHAR* data()
  28. { return (TCHAR*)(this+1); }
  29. };
  30. class CSTRING
  31. {
  32. public:
  33. // Constructors
  34. CSTRING();
  35. CSTRING(const CSTRING& stringSrc);
  36. CSTRING(TCHAR ch, int nRepeat = 1);
  37. CSTRING(LPCSTR lpsz);
  38. CSTRING(LPCWSTR lpsz);
  39. CSTRING(LPCTSTR lpch, int nLength);
  40. CSTRING(const unsigned char* psz);
  41. // Attributes & Operations
  42. // as an array of characters
  43. int GetLength() const;
  44. BOOL IsEmpty() const;
  45. void Empty(); // free up the data
  46. TCHAR GetAt(int nIndex) const; // 0 based
  47. TCHAR operator[](int nIndex) const; // same as GetAt
  48. void SetAt(int nIndex, TCHAR ch);
  49. operator LPCTSTR() const; // as a C string
  50. // overloaded assignment
  51. const CSTRING& operator=(const CSTRING& stringSrc);
  52. const CSTRING& operator=(TCHAR ch);
  53. #ifdef _UNICODE
  54. const CSTRING& operator=(char ch);
  55. const CSTRING& operator=(LPCSTR lpsz);
  56. #else
  57. const CSTRING& operator=(LPCWSTR lpsz);
  58. #endif
  59. const CSTRING& operator=(const unsigned char* psz);
  60. const CSTRING& operator=(LPCTSTR lpsz);
  61. // string concatenation
  62. const CSTRING& operator+=(const CSTRING& string);
  63. const CSTRING& operator+=(TCHAR ch);
  64. #ifdef _UNICODE
  65. const CSTRING& operator+=(char ch);
  66. #endif
  67. const CSTRING& operator+=(LPCTSTR lpsz);
  68. friend CSTRING REMAFXAPI operator+(const CSTRING& string1,
  69. const CSTRING& string2);
  70. friend CSTRING REMAFXAPI operator+(const CSTRING& string, TCHAR ch);
  71. friend CSTRING REMAFXAPI operator+(TCHAR ch, const CSTRING& string);
  72. #ifdef _UNICODE
  73. friend CSTRING REMAFXAPI operator+(const CSTRING& string, char ch);
  74. friend CSTRING REMAFXAPI operator+(char ch, const CSTRING& string);
  75. #endif
  76. friend CSTRING REMAFXAPI operator+(const CSTRING& string, LPCTSTR lpsz);
  77. friend CSTRING REMAFXAPI operator+(LPCTSTR lpsz, const CSTRING& string);
  78. void Append (LPCTSTR lpszSrcData, int nSrcLen);
  79. // string comparison
  80. int Compare(LPCTSTR lpsz) const; // straight character
  81. int CompareNoCase(LPCTSTR lpsz) const; // ignore case
  82. BOOL FEqual (const CSTRING& s2) const; // length-sensitive comparison
  83. int Collate(LPCTSTR lpsz) const; // NLS aware
  84. // simple sub-string extraction
  85. CSTRING Mid(int nFirst, int nCount) const;
  86. CSTRING Mid(int nFirst) const;
  87. CSTRING Left(int nCount) const;
  88. CSTRING Right(int nCount) const;
  89. CSTRING SpanIncluding(LPCTSTR lpszCharSet) const;
  90. CSTRING SpanExcluding(LPCTSTR lpszCharSet) const;
  91. // upper/lower/reverse conversion
  92. void MakeUpper();
  93. void MakeLower();
  94. // trimming whitespace (either side)
  95. void TrimRight();
  96. void TrimLeft();
  97. // searching (return starting index, or -1 if not found)
  98. // look for a single character match
  99. int Find(TCHAR ch) const; // like "C" strchr
  100. // simple formatting
  101. void REMAFX_CDECL Format(LPCTSTR lpszFormat, ...);
  102. void REMAFX_CDECL Format(UINT nFormatID, ...);
  103. // formatting for localization (uses FormatMessage API)
  104. void REMAFX_CDECL FormatMessage(LPCTSTR lpszFormat, ...);
  105. void REMAFX_CDECL FormatMessage(UINT nFormatID, ...);
  106. // Windows support
  107. BOOL LoadString(HINSTANCE hInstance, UINT nID); // load from string resource
  108. // 255 chars max
  109. #ifndef _UNICODE
  110. // ANSI <-> OEM support (convert string in place)
  111. void AnsiToUnicode();
  112. void AnsiToOem();
  113. void OemToAnsi();
  114. #endif
  115. #ifndef _AFX_NO_BSTR_SUPPORT
  116. // OLE BSTR support (use for OLE automation)
  117. BSTR AllocSysString() const;
  118. BSTR SetSysString(BSTR* pbstr) const;
  119. #endif
  120. // Access to string implementation buffer as "C" character array
  121. LPTSTR GetBuffer(int nMinBufLength);
  122. void ReleaseBuffer(int nNewLength = -1);
  123. LPTSTR GetBufferSetLength(int nNewLength);
  124. void FreeExtra();
  125. // Use LockBuffer/UnlockBuffer to turn refcounting off
  126. LPTSTR LockBuffer();
  127. void UnlockBuffer();
  128. // Implementation
  129. public:
  130. ~CSTRING();
  131. int GetAllocLength() const;
  132. protected:
  133. LPTSTR m_pchData; // pointer to ref counted string data
  134. // implementation helpers
  135. CSTRINGData* GetData() const;
  136. void Init();
  137. void AllocCopy(CSTRING& dest, int nCopyLen, int nCopyIndex, int nExtraLen) const;
  138. void AllocBuffer(int nLen);
  139. void AssignCopy(int nSrcLen, LPCTSTR lpszSrcData);
  140. void ConcatCopy(int nSrc1Len, LPCTSTR lpszSrc1Data, int nSrc2Len, LPCTSTR lpszSrc2Data);
  141. void ConcatInPlace(int nSrcLen, LPCTSTR lpszSrcData);
  142. void FormatV(LPCTSTR lpszFormat, va_list argList);
  143. void CopyBeforeWrite();
  144. void AllocBeforeWrite(int nLen);
  145. void Release();
  146. static void PASCAL Release(CSTRINGData* pData);
  147. static int PASCAL SafeStrlen(LPCTSTR lpsz);
  148. };
  149. // conversion helpers
  150. int REMAFX_CDECL _wcstombsz(char* mbstr, const wchar_t* wcstr, size_t count);
  151. int REMAFX_CDECL _mbstowcsz(wchar_t* wcstr, const char* mbstr, size_t count);
  152. // Globals
  153. extern REMAFX_DATA TCHAR AFXChNil;
  154. const CSTRING& REMAFXAPI AFXGetEmptyString();
  155. #define AFXEmptyString AFXGetEmptyString()
  156. // inlines
  157. REM_AFX_INLINE CSTRINGData* CSTRING::GetData() const
  158. { ASSERT(m_pchData != NULL); return ((CSTRINGData*)m_pchData)-1; }
  159. REM_AFX_INLINE void CSTRING::Init()
  160. { m_pchData = AFXEmptyString.m_pchData; }
  161. REM_AFX_INLINE CSTRING::CSTRING(const unsigned char* lpsz)
  162. { Init(); *this = (LPCSTR)lpsz; }
  163. REM_AFX_INLINE const CSTRING& CSTRING::operator=(const unsigned char* lpsz)
  164. { *this = (LPCSTR)lpsz; return *this; }
  165. #ifdef _UNICODE
  166. REM_AFX_INLINE const CSTRING& CSTRING::operator+=(char ch)
  167. { *this += (TCHAR)ch; return *this; }
  168. REM_AFX_INLINE const CSTRING& CSTRING::operator=(char ch)
  169. { *this = (TCHAR)ch; return *this; }
  170. REM_AFX_INLINE CSTRING REMAFXAPI operator+(const CSTRING& string, char ch)
  171. { return string + (TCHAR)ch; }
  172. REM_AFX_INLINE CSTRING REMAFXAPI operator+(char ch, const CSTRING& string)
  173. { return (TCHAR)ch + string; }
  174. #endif
  175. REM_AFX_INLINE int CSTRING::GetLength() const
  176. { return GetData()->nDataLength; }
  177. REM_AFX_INLINE int CSTRING::GetAllocLength() const
  178. { return GetData()->nAllocLength; }
  179. REM_AFX_INLINE BOOL CSTRING::IsEmpty() const
  180. { return GetData()->nDataLength == 0; }
  181. REM_AFX_INLINE CSTRING::operator LPCTSTR() const
  182. { return m_pchData; }
  183. REM_AFX_INLINE int PASCAL CSTRING::SafeStrlen(LPCTSTR lpsz)
  184. { return (lpsz == NULL) ? 0 : lstrlen(lpsz); }
  185. REM_AFX_INLINE void CSTRING::Append (LPCTSTR lpszSrcData, int nSrcLen)
  186. { ConcatInPlace(nSrcLen, lpszSrcData); }
  187. REM_AFX_INLINE BOOL REMAFXAPI operator==(const CSTRING& s1, const CSTRING& s2)
  188. { return s1.FEqual(s2); }
  189. REM_AFX_INLINE BOOL REMAFXAPI operator==(const CSTRING& s1, LPCTSTR s2)
  190. { return s1.Compare(s2) == 0; }
  191. REM_AFX_INLINE BOOL REMAFXAPI operator==(LPCTSTR s1, const CSTRING& s2)
  192. { return s2.Compare(s1) == 0; }
  193. REM_AFX_INLINE BOOL REMAFXAPI operator!=(const CSTRING& s1, const CSTRING& s2)
  194. { return s1.FEqual(s2) == FALSE; }
  195. REM_AFX_INLINE BOOL REMAFXAPI operator!=(const CSTRING& s1, LPCTSTR s2)
  196. { return s1.Compare(s2) != 0; }
  197. REM_AFX_INLINE BOOL REMAFXAPI operator!=(LPCTSTR s1, const CSTRING& s2)
  198. { return s2.Compare(s1) != 0; }
  199. // Commented out for Unicode because Win95 doesn't support lstrcmpW
  200. #ifndef UNICODE
  201. REM_AFX_INLINE int CSTRING::Compare(LPCTSTR lpsz) const
  202. { return lstrcmp(m_pchData, lpsz); } // MBCS/Unicode aware
  203. #endif // UNICODE
  204. #endif // ndef CSTRING_HPP