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.

190 lines
5.1 KiB

  1. //
  2. // MODULE: APGTSSTR.H
  3. //
  4. // PURPOSE: header file for DLL Growable string object CString
  5. // (pretty much a la MFC, but avoids all that MFC overhead)
  6. //
  7. // PROJECT: Generic Troubleshooter DLL for Microsoft AnswerPoint
  8. //
  9. // COMPANY: Saltmine Creative, Inc. (206)-284-7511 [email protected]
  10. //
  11. // AUTHOR: Joe Mabel Joe Mabel (reworked code from Microsoft's MFC sources)
  12. //
  13. // ORIGINAL DATE: 8-2-96 Roman Mach; totally re-implemented 1/15/99 Joe Mabel
  14. //
  15. // NOTES:
  16. // 1. As of 1/99, re-implemented based on MFC's implementation. Pared down
  17. // to what we use.
  18. //
  19. // Version Date By Comments
  20. //--------------------------------------------------------------------
  21. // V0.1 - RM Original
  22. // V3.0 7-24-98 JM Abstracted this out as a separate header.
  23. // V3.1 1-15-99 JM Redo based on MFC implementation
  24. //
  25. #ifndef __APGTSSTR_H_
  26. #define __APGTSSTR_H_ 1
  27. #include <windows.h>
  28. #include <tchar.h>
  29. #include "apgtsassert.h"
  30. // determine number of elements in an array (not bytes)
  31. #define _countof(array) (sizeof(array)/sizeof(array[0]))
  32. struct CStringData
  33. {
  34. long nRefs; // reference count
  35. int nDataLength;
  36. int nAllocLength;
  37. // TCHAR data[nAllocLength]
  38. TCHAR* data()
  39. { return (TCHAR*)(this+1); }
  40. };
  41. class CString {
  42. public:
  43. // Constructors
  44. CString();
  45. CString(LPCTSTR string);
  46. CString(const CString &string);
  47. ~CString();
  48. // Attributes & Operations
  49. // as an array of characters
  50. int GetLength() const;
  51. bool IsEmpty() const;
  52. TCHAR GetAt(int nIndex) const; // 0 based
  53. TCHAR operator[](int nIndex) const; // same as GetAt
  54. operator LPCTSTR() const;
  55. // overloaded assignment
  56. const CString& operator=(const CString &string);
  57. const CString& operator=(LPCTSTR string);
  58. const CString& operator=(TCHAR ch);
  59. #ifdef _UNICODE
  60. const CString& operator=(LPCSTR lpsz);
  61. #else // !_UNICODE
  62. const CString& operator=(LPCWSTR lpsz);
  63. #endif // !_UNICODE
  64. // string concatenation
  65. const CString& operator+=(const CString &string);
  66. const CString& operator+=(LPCTSTR string);
  67. CString operator+(const CString& string2);
  68. CString operator+(LPCTSTR lpsz);
  69. // string comparison
  70. int CString::CompareNoCase(LPCTSTR lpsz) const
  71. { return _tcsicmp(m_pchData, lpsz); } // MBCS/Unicode aware
  72. LPTSTR GetBuffer(int);
  73. void Empty();
  74. LPTSTR GetBufferSetLength(int nNewLength);
  75. void ReleaseBuffer(int nNewLength = -1);
  76. // simple sub-string extraction
  77. CString Mid(int Left, int Count) const;
  78. CString Mid(int Left) const;
  79. CString Left(int amount) const;
  80. CString Right(int amount) const;
  81. // upper/lower/reverse conversion
  82. void MakeLower();
  83. // trimming whitespace (either side)
  84. void TrimRight();
  85. void TrimLeft();
  86. // look for a specific sub-string
  87. int Find(LPCTSTR lpszSub) const;
  88. int Find(LPCTSTR lpszSub, int nStart) const; // Added function - RAB19991112.
  89. int Find(TCHAR c) const;
  90. int ReverseFind(TCHAR ch) const;
  91. enum
  92. {
  93. // Define the code returned when a find is unsuccessful.
  94. FIND_FAILED= -1
  95. } ;
  96. // simple formatting
  97. void Format( LPCTSTR lpszFormat, ... );
  98. // load from resource
  99. BOOL LoadString(UINT nID);
  100. protected:
  101. LPTSTR m_pchData; // pointer to ref counted string data
  102. // implementation helpers
  103. CStringData* GetData() const;
  104. void Init();
  105. void AllocCopy(CString& dest, int nCopyLen, int nCopyIndex, int nExtraLen) const;
  106. void AllocBuffer(int nLen);
  107. void AssignCopy(int nSrcLen, LPCTSTR lpszSrcData);
  108. void ConcatCopy(int nSrc1Len, LPCTSTR lpszSrc1Data, int nSrc2Len, LPCTSTR lpszSrc2Data);
  109. void ConcatInPlace(int nSrcLen, LPCTSTR lpszSrcData);
  110. void FormatV(LPCTSTR lpszFormat, va_list argList);
  111. void CopyBeforeWrite();
  112. void AllocBeforeWrite(int nL65en);
  113. void Release();
  114. static void PASCAL Release(CStringData* pData);
  115. static int PASCAL SafeStrlen(LPCTSTR lpsz);
  116. };
  117. // Compare helpers
  118. bool __stdcall operator ==(const CString& s1, const CString& s2);
  119. bool __stdcall operator ==(const CString& s1, LPCTSTR s2);
  120. bool __stdcall operator ==(LPCTSTR s1, const CString& s2);
  121. bool __stdcall operator !=(const CString& s1, const CString& s2);
  122. bool __stdcall operator !=(const CString& s1, LPCTSTR s2);
  123. bool __stdcall operator !=(LPCTSTR s1, const CString& s2);
  124. bool __stdcall operator < (const CString& s1, const CString& s2);
  125. bool __stdcall operator < (const CString& s1, LPCTSTR s2);
  126. bool __stdcall operator < (LPCTSTR s1, const CString& s2);
  127. CString operator+(LPCTSTR lpsz, const CString& string);
  128. /////////////////////////////////////////////////////////////
  129. // From Afx.inl
  130. // These were all inlines, but some of them don't seem to happily work that way
  131. inline CString::operator LPCTSTR() const
  132. { return m_pchData; }
  133. inline int PASCAL CString::SafeStrlen(LPCTSTR lpsz)
  134. { return (lpsz == NULL) ? 0 : lstrlen(lpsz); }
  135. inline TCHAR CString::operator[](int nIndex) const
  136. {
  137. // same as GetAt
  138. ASSERT(nIndex >= 0);
  139. ASSERT(nIndex < GetData()->nDataLength);
  140. return m_pchData[nIndex];
  141. }
  142. inline int CString::GetLength() const
  143. { return GetData()->nDataLength; }
  144. inline bool CString::IsEmpty() const
  145. { return GetData()->nDataLength == 0; }
  146. inline TCHAR CString::GetAt(int nIndex) const
  147. {
  148. ASSERT(nIndex >= 0);
  149. ASSERT(nIndex < GetData()->nDataLength);
  150. return m_pchData[nIndex];
  151. }
  152. #endif // __APGTSSTR_H_ 1