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.

236 lines
7.4 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name :
  4. MyString.h
  5. Abstract:
  6. A lightweight string class which supports UNICODE/MCBS.
  7. Author:
  8. Neil Allain ( a-neilal ) August-1997
  9. Revision History:
  10. --*/
  11. #pragma once
  12. #ifndef _MYSTRING_H_
  13. #define _MYSTRING_H_
  14. //==========================================================================================
  15. // Dependencies
  16. //==========================================================================================
  17. #include <string.h>
  18. #include "RefPtr.h"
  19. #include "RefCount.h"
  20. //==========================================================================================
  21. // Classes
  22. //==========================================================================================
  23. class BaseStringBuffer
  24. {
  25. // interface
  26. public:
  27. typedef size_t size_type;
  28. enum {
  29. npos = -1
  30. };
  31. BaseStringBuffer( LPCTSTR inString );
  32. BaseStringBuffer( LPCTSTR s1, LPCTSTR s2 );
  33. BaseStringBuffer( size_t bufferSize );
  34. ~BaseStringBuffer();
  35. LPTSTR c_str()
  36. { _ASSERT( m_pString ); return m_pString; }
  37. LPCTSTR c_str() const
  38. { _ASSERT( m_pString ); return m_pString; }
  39. size_t length() const
  40. { return m_length; }
  41. size_t bufferSize() const
  42. { return m_bufferSize; }
  43. HRESULT copy( LPCTSTR );
  44. HRESULT concatenate( LPCTSTR );
  45. HRESULT concatenate( _TCHAR );
  46. size_type find_last_of(_TCHAR c) const;
  47. size_type find_first_of(_TCHAR c) const;
  48. LPTSTR substr( size_type b, size_type e ) const;
  49. // implementation
  50. protected:
  51. HRESULT growBuffer( size_t inMinSize );
  52. size_t m_bufferSize;
  53. size_t m_length;
  54. LPTSTR m_pString;
  55. };
  56. class StringBuffer : public BaseStringBuffer, public CRefCounter
  57. {
  58. public:
  59. StringBuffer( LPCTSTR inString ) : BaseStringBuffer( inString ){};
  60. StringBuffer( size_t bufferSize ) : BaseStringBuffer( bufferSize ){};
  61. StringBuffer( LPCTSTR s1, LPCTSTR s2 ) : BaseStringBuffer( s1, s2 ){};
  62. ~StringBuffer(){};
  63. };
  64. class String : public TRefPtr< StringBuffer >
  65. {
  66. public:
  67. typedef BaseStringBuffer::size_type size_type;
  68. enum {
  69. npos = BaseStringBuffer::npos
  70. };
  71. String(bool fCaseSensitive = true);
  72. String( const String&, bool fCaseSensitive = true );
  73. String( LPCTSTR, bool fCaseSensitive = true );
  74. String( StringBuffer* pT, bool fCaseSensitive = true )
  75. { m_fCaseSensitive = fCaseSensitive;
  76. Set( pT );
  77. }
  78. String& operator=( StringBuffer* );
  79. String& operator=( const String& );
  80. String& operator=( LPCTSTR );
  81. String& operator+=( const String& );
  82. String& operator+=( LPCTSTR );
  83. String operator+( const String& ) const;
  84. String operator+( LPCTSTR ) const;
  85. String operator+( _TCHAR ) const;
  86. bool operator==( const String& ) const;
  87. bool operator==( LPCTSTR ) const;
  88. bool operator!=( const String& s ) const { return !( *this == s ); }
  89. bool operator!=( LPCTSTR s ) const { return !( *this == s ); }
  90. bool operator<( const String& ) const;
  91. bool operator<( LPCTSTR ) const;
  92. int compare( const String& s) const { return _tcscmp( c_str(), s.c_str() ); }
  93. int compare( LPCTSTR s ) const { return _tcscmp( c_str(), s ); }
  94. int compare( size_t, size_t, const String& ) const;
  95. size_t find( _TCHAR ) const;
  96. LPCTSTR c_str() const { return m_pT->c_str(); };
  97. LPTSTR c_str(){ return m_pT->c_str(); }
  98. size_t length() const { return m_pT->length(); }
  99. size_t size() const { return length(); }
  100. size_t bufferSize() const { return m_pT->bufferSize(); }
  101. _TCHAR operator[](size_t s) const { return c_str()[s]; }
  102. _TCHAR charAt( size_t s ) const { return c_str()[ s ]; }
  103. SHORT toInt16() const { return (SHORT)_ttoi(c_str()); }
  104. LONG toInt32() const { return _ttol(c_str()); }
  105. ULONG toUInt32() const { return (ULONG)_ttol(c_str()); }
  106. float toFloat() const { USES_CONVERSION; return (float)atof(T2CA(c_str())); }
  107. double toDouble() const { USES_CONVERSION; return atof(T2CA(c_str())); }
  108. size_type find_last_of(_TCHAR c) const
  109. {
  110. return m_pT->find_last_of(c);
  111. }
  112. size_type find_first_of(_TCHAR c) const
  113. {
  114. return m_pT->find_first_of(c);
  115. }
  116. String substr( size_type b, size_type e ) const
  117. {
  118. LPTSTR pStr = m_pT->substr(b,e);
  119. String s( pStr );
  120. delete[] pStr;
  121. return s;
  122. }
  123. static String fromInt16( SHORT );
  124. static String fromInt32( LONG );
  125. static String fromFloat( float );
  126. static String fromDouble( double );
  127. private:
  128. StringBuffer& operator*(){ return *m_pT; }
  129. StringBuffer* operator->(){ return m_pT; }
  130. bool m_fCaseSensitive;
  131. };
  132. String operator+( LPCTSTR lhs, const String& rhs );
  133. /*
  134. * A simple class to convert Multibyte to Widechar. Uses object memory, if sufficient,
  135. * else allocates memory from the heap. Intended to be used on the stack.
  136. */
  137. class CMBCSToWChar
  138. {
  139. private:
  140. LPWSTR m_pszResult;
  141. WCHAR m_resMemory[1024];
  142. INT m_cchResult;
  143. public:
  144. CMBCSToWChar() { m_pszResult = m_resMemory; m_cchResult = 0; }
  145. ~CMBCSToWChar();
  146. // Init(): converts the MBCS string at pSrc to a Wide string in memory
  147. // managed by CMBCSToWChar
  148. HRESULT Init(LPCSTR pSrc, UINT lCodePage = CP_ACP, int cch = -1);
  149. // GetString(): returns a pointer to the converted string. Passing TRUE
  150. // gives the ownership of the memory to the caller. Passing TRUE has the
  151. // side effect of clearing the object's contents with respect to the
  152. // converted string. Subsequent calls to GetString(). after which a TRUE
  153. // value was passed, will result in a pointer to an empty string being
  154. // returned.
  155. LPWSTR GetString(BOOL fTakeOwnerShip = FALSE);
  156. // returns the number of bytes in the converted string - NOT including the
  157. // NULL terminating byte. Note that this is the number of bytes in the
  158. // string and not the number of characters.
  159. INT GetStringLen() { return (m_cchResult ? m_cchResult - 1 : 0); }
  160. };
  161. /*
  162. * A simple class to convert WideChar to Multibyte. Uses object memory, if sufficient,
  163. * else allocates memory from the heap. Intended to be used on the stack.
  164. */
  165. class CWCharToMBCS
  166. {
  167. private:
  168. LPSTR m_pszResult;
  169. char m_resMemory[1024];
  170. INT m_cbResult;
  171. public:
  172. CWCharToMBCS() { m_pszResult = m_resMemory; m_cbResult = 0; }
  173. ~CWCharToMBCS();
  174. // Init(): converts the widechar string at pWSrc to an MBCS string in memory
  175. // managed by CWCharToMBCS
  176. HRESULT Init(LPCWSTR pWSrc, UINT lCodePage = CP_ACP, int cch = -1);
  177. // GetString(): returns a pointer to the converted string. Passing TRUE
  178. // gives the ownership of the memory to the caller. Passing TRUE has the
  179. // side effect of clearing the object's contents with respect to the
  180. // converted string. Subsequent calls to GetString(). after which a TRUE
  181. // value was passed, will result in a pointer to an empty string being
  182. // returned.
  183. LPSTR GetString(BOOL fTakeOwnerShip = FALSE);
  184. // returns the number of bytes in the converted string - NOT including the
  185. // NULL terminating byte. Note that this is the number of bytes in the
  186. // string and not the number of characters.
  187. INT GetStringLen() { return (m_cbResult ? m_cbResult - 1 : 0); }
  188. };
  189. #endif // !_MYSTRING_H_