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.

232 lines
5.3 KiB

  1. /*++
  2. 1998 Seagate Software, Inc. All rights reserved.
  3. Module Name:
  4. wsbpstrg.h
  5. Abstract:
  6. This component is C++ object representations a smart string pointer. It
  7. is similar to the CComPtr, in that it takes care of allocating and
  8. freeing the memory needed to represent the string automatically. This
  9. simplifies cleanup of functions in error handling cases and it limits
  10. the need for FinalConstruct() and FinalRelease() calls in objects that
  11. derive from CComObjectRoot.
  12. Author:
  13. Chuck Bardeen [cbardeen] 11-Dec-1996
  14. Revision History:
  15. --*/
  16. #ifndef _WSBPSTRG_
  17. #define _WSBPSTRG_
  18. /*++
  19. Class Name:
  20. CWsbStringPtr
  21. Class Description:
  22. This component is C++ object representations a smart string pointer. It
  23. is similar to the CComPtr, in that it takes care of allocating and
  24. freeing the memory needed to represent the string automatically. This
  25. simplifies cleanup of functions in error handling cases and it limits
  26. the need for FinalConstruct() and FinalRelease() calls in objects that
  27. derive from CComObjectRoot.
  28. --*/
  29. class WSB_EXPORT CWsbStringPtr
  30. {
  31. // Constructors
  32. public:
  33. CWsbStringPtr();
  34. CWsbStringPtr(const CHAR* pChar);
  35. CWsbStringPtr(const WCHAR* pWchar);
  36. CWsbStringPtr(const CWsbStringPtr& pString);
  37. CWsbStringPtr(REFGUID rguid);
  38. // Destructor
  39. public:
  40. ~CWsbStringPtr();
  41. // Operator Overloading
  42. public:
  43. operator WCHAR*();
  44. WCHAR& operator *();
  45. WCHAR** operator &();
  46. WCHAR& operator [](const int i);
  47. CWsbStringPtr& operator =(const CHAR* pChar);
  48. CWsbStringPtr& operator =(const WCHAR* pWchar);
  49. CWsbStringPtr& operator =(REFGUID rguid);
  50. CWsbStringPtr& operator =(const CWsbStringPtr& pString);
  51. BOOL operator !();
  52. #if 0
  53. BOOL operator==(LPCWSTR s2);
  54. BOOL operator!=(LPCWSTR s2);
  55. BOOL operator==(const CWsbStringPtr& s2);
  56. BOOL operator!=(const CWsbStringPtr& s2);
  57. #else
  58. int Compare( LPCWSTR s2 );
  59. int CompareIgnoreCase( LPCWSTR s2 );
  60. BOOL IsEqual( LPCWSTR s2 );
  61. BOOL IsNotEqual( LPCWSTR s2 );
  62. #endif
  63. // Memory Allocation
  64. public:
  65. HRESULT Alloc(ULONG size);
  66. HRESULT Free(void);
  67. HRESULT GetSize(ULONG* size); // Size of allocated buffer
  68. HRESULT Realloc(ULONG size);
  69. // String Manipulation
  70. public:
  71. HRESULT Append(const CHAR* pChar);
  72. HRESULT Append(const WCHAR* pWchar);
  73. HRESULT Append(const CWsbStringPtr& pString);
  74. HRESULT CopyTo(CHAR** pChar);
  75. HRESULT CopyTo(WCHAR** pWchar);
  76. HRESULT CopyTo(GUID * pguid);
  77. HRESULT CopyToBstr(BSTR* pBstr);
  78. HRESULT CopyTo(CHAR** pChar, ULONG bufferSize);
  79. HRESULT CopyTo(WCHAR** pWchar,ULONG bufferSize);
  80. HRESULT CopyToBstr(BSTR* pBstr,ULONG bufferSize);
  81. HRESULT FindInRsc(ULONG startId, ULONG idsToCheck, ULONG* pMatchId);
  82. HRESULT GetLen(ULONG* size); // Length, in chars, of string
  83. HRESULT GiveTo(WCHAR** ppWchar);
  84. HRESULT LoadFromRsc(HINSTANCE instance, ULONG id);
  85. HRESULT Prepend(const CHAR* pChar);
  86. HRESULT Prepend(const WCHAR* pWchar);
  87. HRESULT Prepend(const CWsbStringPtr& pString);
  88. HRESULT Printf(const WCHAR* fmtString, ...);
  89. HRESULT TakeFrom(WCHAR* pWchar, ULONG bufferSize);
  90. HRESULT VPrintf(const WCHAR* fmtString, va_list vaList);
  91. // Member Data
  92. protected:
  93. WCHAR* m_pString;
  94. ULONG m_givenSize;
  95. static CComPtr<IMalloc> m_pMalloc;
  96. };
  97. inline
  98. HRESULT CWsbStringPtr::GetLen(ULONG* size)
  99. {
  100. HRESULT hr = S_OK;
  101. if (0 == size) {
  102. hr = E_POINTER;
  103. } else if (0 == m_pString) {
  104. *size = 0;
  105. } else {
  106. *size = wcslen(m_pString);
  107. }
  108. return(hr);
  109. }
  110. inline
  111. HRESULT CWsbStringPtr::Printf(const WCHAR* fmtString, ...)
  112. {
  113. HRESULT hr;
  114. va_list vaList;
  115. va_start(vaList, fmtString);
  116. hr = VPrintf(fmtString, vaList);
  117. va_end(vaList);
  118. return(hr);
  119. }
  120. #if 0
  121. // Compare Operators (allow to be compared when on the right)
  122. BOOL operator==(LPCWSTR s1, const CWsbStringPtr& s2);
  123. BOOL operator!=(LPCWSTR s1, const CWsbStringPtr& s2);
  124. inline
  125. BOOL CWsbStringPtr::operator==(LPCWSTR s2)
  126. {
  127. return( wcscmp( m_pString, s2 ) == 0 );
  128. }
  129. inline
  130. BOOL CWsbStringPtr::operator!=(LPCWSTR s2)
  131. {
  132. return( wcscmp( m_pString, s2 ) != 0 );
  133. }
  134. inline
  135. BOOL CWsbStringPtr::operator==(const CWsbStringPtr& s2)
  136. {
  137. return( wcscmp( m_pString, s2.m_pString ) == 0 );
  138. }
  139. inline
  140. BOOL CWsbStringPtr::operator!=(const CWsbStringPtr& s2)
  141. {
  142. return( wcscmp( m_pString, s2.m_pString ) != 0 );
  143. }
  144. inline
  145. BOOL operator==(LPCWSTR s1, const CWsbStringPtr& s2)
  146. {
  147. return( wcscmp( s1, (CWsbStringPtr)s2 ) == 0 );
  148. }
  149. inline
  150. BOOL operator!=(LPCWSTR s1, const CWsbStringPtr& s2)
  151. {
  152. return( wcscmp( s1, (CWsbStringPtr)s2 ) != 0 );
  153. }
  154. #else
  155. inline
  156. int CWsbStringPtr::Compare( LPCWSTR s2 )
  157. {
  158. if( m_pString && s2 ) return( wcscmp( m_pString, s2 ) );
  159. if( !m_pString && s2 ) return( -1 );
  160. if( m_pString && !s2 ) return( 1 );
  161. return( 0 );
  162. }
  163. inline
  164. int CWsbStringPtr::CompareIgnoreCase( LPCWSTR s2 )
  165. {
  166. if( m_pString && s2 ) return( _wcsicmp( m_pString, s2 ) );
  167. if( !m_pString && s2 ) return( -1 );
  168. if( m_pString && !s2 ) return( 1 );
  169. return( 0 );
  170. }
  171. inline
  172. BOOL CWsbStringPtr::IsEqual( LPCWSTR s2 )
  173. {
  174. return( Compare( s2 ) == 0 );
  175. }
  176. inline
  177. BOOL CWsbStringPtr::IsNotEqual( LPCWSTR s2 )
  178. {
  179. return( Compare( s2 ) != 0 );
  180. }
  181. #endif
  182. #endif // _WSBPSTRG