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.

239 lines
7.6 KiB

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