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.

335 lines
9.4 KiB

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2000-2002 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // CStr.h
  7. //
  8. // Description:
  9. // Header file for CStr class.
  10. //
  11. // CStr is a class the provides the functionality of a string of
  12. // characters.
  13. //
  14. // This class is intended to be used instead of std::string since the
  15. // use of STL is prohibited in our project.
  16. //
  17. // Implementation File:
  18. // CStr.cpp
  19. //
  20. // Maintained By:
  21. // Vij Vasu (Vvasu) 24-APR-2000
  22. //
  23. //////////////////////////////////////////////////////////////////////////////
  24. // Make sure that this file is included only once per compile path.
  25. #pragma once
  26. //////////////////////////////////////////////////////////////////////////////
  27. // Include files
  28. //////////////////////////////////////////////////////////////////////////////
  29. // For a few platform SDK functions
  30. #include <windows.h>
  31. //////////////////////////////////////////////////////////////////////////////
  32. //++
  33. //
  34. // class CStr
  35. //
  36. // Description:
  37. // CStr is a class the provides the functionality of a string of
  38. // characters.
  39. //
  40. //--
  41. //////////////////////////////////////////////////////////////////////////////
  42. class CStr
  43. {
  44. public:
  45. //////////////////////////////////////////////////////////////////////////
  46. // Constructors and destructors
  47. //////////////////////////////////////////////////////////////////////////
  48. // Default constructor
  49. CStr( void ) throw()
  50. : m_pszData( const_cast< WCHAR * >( &ms_chNull ) )
  51. , m_nLen( 0 )
  52. , m_cchBufferSize( 0 )
  53. {
  54. } //*** CStr()
  55. // Copy constructor
  56. CStr( const CStr & rcstrSrcIn )
  57. : m_pszData( const_cast< WCHAR * >( &ms_chNull ) )
  58. , m_nLen( 0 )
  59. , m_cchBufferSize( 0 )
  60. {
  61. Assign( rcstrSrcIn );
  62. } //*** CStr( const CStr & )
  63. // Construct using string ID
  64. CStr( HINSTANCE hInstanceIn, UINT nStringIdIn )
  65. : m_pszData( const_cast< WCHAR * >( &ms_chNull ) )
  66. , m_nLen( 0 )
  67. , m_cchBufferSize( 0 )
  68. {
  69. LoadString( hInstanceIn, nStringIdIn );
  70. } //*** CStr( HINSTANCE, UINT )
  71. // Construct using string
  72. CStr( const WCHAR * pcszSrcIn )
  73. : m_pszData( const_cast< WCHAR * >( &ms_chNull ) )
  74. , m_nLen( 0 )
  75. , m_cchBufferSize( 0 )
  76. {
  77. Assign( pcszSrcIn );
  78. } //*** CStr( const WCHAR * )
  79. // Construct using buffer size
  80. CStr( UINT cchBufferSize, WCHAR chInitialChar = ms_chNull )
  81. : m_pszData( const_cast< WCHAR * >( &ms_chNull ) )
  82. , m_nLen( 0 )
  83. , m_cchBufferSize( 0 )
  84. {
  85. if ( cchBufferSize > 0 )
  86. {
  87. AllocateBuffer( cchBufferSize );
  88. _wcsnset( m_pszData, chInitialChar, cchBufferSize );
  89. m_pszData[ cchBufferSize - 1 ] = ms_chNull;
  90. m_nLen = (UINT) wcslen( m_pszData );
  91. }
  92. } //*** CStr( UINT cchBufferSize, WCHAR chInitialChar )
  93. // Destructor
  94. ~CStr( void ) throw()
  95. {
  96. Free();
  97. } //*** ~CStr()
  98. //////////////////////////////////////////////////////////////////////////
  99. // Public member functions
  100. //////////////////////////////////////////////////////////////////////////
  101. // Assign another CStr to this one.
  102. void Assign( const CStr & rcstrSrcIn )
  103. {
  104. UINT nSrcLen = rcstrSrcIn.m_nLen;
  105. if ( nSrcLen != 0 )
  106. {
  107. AllocateBuffer( nSrcLen + 1 );
  108. m_nLen = nSrcLen;
  109. THR( StringCchCopyNW( m_pszData, m_cchBufferSize, rcstrSrcIn.m_pszData, nSrcLen ) );
  110. } // if: the source string is not empty
  111. else
  112. {
  113. // Clean up existing string.
  114. Empty();
  115. } // if: the source string is empty
  116. } //*** Assign( const CStr & )
  117. // Assign a character string to this one.
  118. void Assign( const WCHAR * pcszSrcIn )
  119. {
  120. if ( ( pcszSrcIn != NULL ) && ( *pcszSrcIn != ms_chNull ) )
  121. {
  122. UINT nSrcLen = (UINT) wcslen( pcszSrcIn );
  123. AllocateBuffer( nSrcLen + 1 );
  124. m_nLen = nSrcLen;
  125. THR( StringCchCopyNW( m_pszData, m_cchBufferSize, pcszSrcIn, nSrcLen ) );
  126. } // if: the source string is not NULL
  127. else
  128. {
  129. // Clean up existing string.
  130. Empty();
  131. } // else: the source string is NULL
  132. } //*** Assign( const WCHAR * )
  133. // Free the buffer for this string
  134. void Free( void ) throw()
  135. {
  136. if ( m_pszData != &ms_chNull )
  137. {
  138. delete m_pszData;
  139. } // if: the pointer was dynamically allocated
  140. m_pszData = const_cast< WCHAR * >( &ms_chNull );
  141. m_nLen = 0;
  142. m_cchBufferSize = 0;
  143. } //*** Free()
  144. // Empty this string
  145. void Empty( void ) throw()
  146. {
  147. if ( m_nLen != 0 )
  148. {
  149. *m_pszData = ms_chNull;
  150. m_nLen = 0;
  151. } // if: the string is not already empty
  152. } //*** Empty()
  153. // Load a string from the resource table and assign it to this string.
  154. void LoadString( HINSTANCE hInstIn, UINT nStringIdIn );
  155. //////////////////////////////////////////////////////////////////////////
  156. // Public accessors
  157. //////////////////////////////////////////////////////////////////////////
  158. // Get a pointer to the underlying string
  159. const WCHAR * PszData( void ) const throw()
  160. {
  161. return m_pszData;
  162. } //*** PszData()
  163. // Get the length of the string.
  164. UINT NGetLen( void ) const throw()
  165. {
  166. return m_nLen;
  167. } //*** NGetLen()
  168. // Get the size of the string buffer.
  169. UINT NGetSize( void ) const throw()
  170. {
  171. return m_cchBufferSize;
  172. } //*** NGetSize()
  173. // Is this string empty?
  174. bool FIsEmpty( void ) const throw()
  175. {
  176. return ( m_nLen == 0 );
  177. } //*** FIsEmpty()
  178. //////////////////////////////////////////////////////////////////////////
  179. // Public operators
  180. //////////////////////////////////////////////////////////////////////////
  181. // Assignment operator ( const CStr & )
  182. const CStr & operator=( const CStr & rcstrSrcIn )
  183. {
  184. Assign( rcstrSrcIn );
  185. return *this;
  186. } //*** operator=( const CStr & )
  187. // Assignment operator ( const WCHAR * )
  188. const CStr & operator=( const WCHAR * pcszSrcIn )
  189. {
  190. Assign( pcszSrcIn );
  191. return *this;
  192. } //*** operator=( const WCHAR * )
  193. // Concatenation operator ( const CStr & )
  194. CStr operator+( const CStr & rcstrSrcIn ) const
  195. {
  196. CStr strReturn( m_nLen + rcstrSrcIn.m_nLen + 1 );
  197. strReturn.Assign( *this );
  198. strReturn.Concatenate( rcstrSrcIn.m_pszData, rcstrSrcIn.m_nLen );
  199. return strReturn;
  200. } //*** operator+( const CStr & )
  201. // Concatenation operator ( const WCHAR * )
  202. CStr operator+( const WCHAR * pcszSrcIn ) const
  203. {
  204. if ( ( pcszSrcIn != NULL ) && ( *pcszSrcIn != ms_chNull ) )
  205. {
  206. UINT nSrcLen = (UINT) wcslen( pcszSrcIn );
  207. CStr strReturn( m_nLen + nSrcLen + 1);
  208. strReturn.Assign( *this );
  209. strReturn.Concatenate( pcszSrcIn, nSrcLen );
  210. return strReturn;
  211. } // if: the string to be concatenated is not empty
  212. else
  213. {
  214. return *this;
  215. } // else: the string to be concatenated is empty
  216. } //*** operator+( const WCHAR * )
  217. // Append operator ( const CStr & )
  218. const CStr & operator+=( const CStr & rcstrSrcIn )
  219. {
  220. Concatenate( rcstrSrcIn.m_pszData, rcstrSrcIn.m_nLen );
  221. return *this;
  222. } //*** operator+( const CStr & )
  223. // Append operator ( const WCHAR * )
  224. const CStr & operator+=( const WCHAR * pcszSrcIn )
  225. {
  226. if ( ( pcszSrcIn != NULL ) && ( *pcszSrcIn != ms_chNull ) )
  227. {
  228. UINT nSrcLen = (UINT) wcslen( pcszSrcIn );
  229. Concatenate( pcszSrcIn, nSrcLen );
  230. } // if: the string to be appended is not empty
  231. return *this;
  232. } //*** operator+( const WCHAR * )
  233. private:
  234. //////////////////////////////////////////////////////////////////////////
  235. // Private member functions
  236. //////////////////////////////////////////////////////////////////////////
  237. // Allocate a buffer of the required size.
  238. void AllocateBuffer( UINT cchBufferSizeIn );
  239. // Concatenation function.
  240. void Concatenate(
  241. const WCHAR * pcszStr2In
  242. , UINT nStr2LenIn
  243. )
  244. {
  245. AllocateBuffer( m_nLen + nStr2LenIn + 1);
  246. // Copy the strings to the destination.
  247. THR( StringCchCopyNW( m_pszData + m_nLen, m_cchBufferSize - m_nLen, pcszStr2In, nStr2LenIn ) );
  248. m_nLen += nStr2LenIn;
  249. } //*** Concatenate()
  250. //////////////////////////////////////////////////////////////////////////
  251. // Private class data
  252. //////////////////////////////////////////////////////////////////////////
  253. // The NULL character. All empty CStrs point here.
  254. static const WCHAR ms_chNull = L'\0';
  255. //////////////////////////////////////////////////////////////////////////
  256. // Private member data
  257. //////////////////////////////////////////////////////////////////////////
  258. WCHAR * m_pszData;
  259. UINT m_nLen;
  260. UINT m_cchBufferSize;
  261. }; //*** class CStr