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.

337 lines
9.1 KiB

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