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.

368 lines
7.9 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. stistr.h
  5. Abstract:
  6. Lightweight string classes: definition.
  7. Supports both UNICODE and single-byte character strings
  8. Author:
  9. Vlad Sadovsky (vlads) 26-Jan-1997
  10. (Lifted from another C++ project with some modifications and adjustments)
  11. Revision History:
  12. 26-Jan-1997 VladS created
  13. 20-Apr-1999 VladS redesigned to inherit from ATL Cstring class
  14. --*/
  15. #ifndef _STRING_H_
  16. #define _STRING_H_
  17. #ifndef USE_OLD_STI_STRINGS
  18. class StiCString : public CString
  19. {
  20. friend class RegEntry;
  21. public:
  22. StiCString()
  23. {
  24. CString::CString();
  25. }
  26. ~StiCString()
  27. {
  28. }
  29. VOID
  30. CopyString(LPCTSTR lpszT)
  31. {
  32. AssignCopy(lstrlen(lpszT),lpszT);
  33. }
  34. };
  35. class STRArray : public CSimpleArray<StiCString *>
  36. {
  37. public:
  38. STRArray()
  39. {
  40. }
  41. ~STRArray()
  42. {
  43. // Free all allocated strings
  44. for(int i = 0; i < m_nSize; i++)
  45. {
  46. if(m_aT[i] != NULL) {
  47. delete m_aT[i];
  48. m_aT[i] = NULL;
  49. }
  50. }
  51. }
  52. BOOL
  53. Add(
  54. LPCTSTR lpszT
  55. )
  56. {
  57. StiCString *pNew;
  58. pNew = new StiCString;
  59. if (pNew) {
  60. pNew->CopyString(lpszT);
  61. return Add(pNew);
  62. }
  63. return FALSE;
  64. }
  65. BOOL
  66. Add(
  67. StiCString* pstr
  68. )
  69. {
  70. StiCString *pNew;
  71. pNew = new StiCString;
  72. if (pNew) {
  73. *pNew = *pstr;
  74. return CSimpleArray<StiCString *>::Add(pNew);
  75. }
  76. return FALSE;
  77. }
  78. };
  79. VOID
  80. TokenizeIntoStringArray(
  81. STRArray& array,
  82. LPCTSTR lpstrIn,
  83. TCHAR tcSplitter
  84. );
  85. #else
  86. //
  87. //
  88. //
  89. # include <buffer.h>
  90. //
  91. // Maximum number of characters a loadable string resource can be
  92. //
  93. # define STR_MAX_RES_SIZE ( 260)
  94. class STR;
  95. //
  96. // If an application defines STR_MODULE_NAME, it will be used
  97. // as the default module name on string loads
  98. //
  99. #ifndef STR_MODULE_NAME
  100. #define STR_MODULE_NAME NULL
  101. #endif
  102. //
  103. // These are the characters that are considered to be white space
  104. //
  105. #define ISWHITE( ch ) ((ch) == L'\t' || (ch) == L' ' || (ch) == L'\r')
  106. #define ISWHITEA( ch ) ((ch) == '\t' || (ch) == ' ' || (ch) == '\r')
  107. class STR : public BUFFER
  108. {
  109. friend class RegEntry;
  110. public:
  111. STR()
  112. {
  113. _fUnicode = FALSE;
  114. _fValid = TRUE;
  115. }
  116. STR( const CHAR * pchInit );
  117. STR( const WCHAR * pwchInit );
  118. STR( const STR & str );
  119. //STR( UINT dwSize );
  120. BOOL Append( const CHAR * pchInit );
  121. BOOL Append( const WCHAR * pwchInit );
  122. BOOL Append( const STR & str );
  123. BOOL Copy( const CHAR * pchInit );
  124. BOOL Copy( const WCHAR * pwchInit );
  125. BOOL Copy( const STR & str );
  126. BOOL Resize( UINT cbNewReqestedSize );
  127. //
  128. // Loads a string from this module's string resource table
  129. //
  130. BOOL LoadString( IN DWORD dwResID,IN LPCTSTR lpszModuleName = STR_MODULE_NAME);
  131. BOOL LoadString( IN DWORD dwResID,IN HMODULE hModule);
  132. //
  133. // Loads a string with insert params from this module's .mc resource
  134. // table. Pass zero for the resource ID to use *this.
  135. //
  136. BOOL FormatStringV(
  137. IN LPCTSTR lpszModuleName,
  138. ...
  139. );
  140. BOOL FormatString( IN DWORD dwResID,
  141. IN LPCTSTR apszInsertParams[],
  142. IN LPCTSTR lpszModuleName = STR_MODULE_NAME);
  143. //
  144. // Returns the number of bytes in the string excluding the terminating
  145. // NULL
  146. //
  147. UINT QueryCB( VOID ) const
  148. { return IsUnicode() ? ::wcslen((WCHAR *)QueryStrW()) * sizeof(WCHAR) :
  149. ::strlen((CHAR *) QueryStrA()); }
  150. //
  151. // Returns the number of characters in the string excluding the terminating
  152. // NULL
  153. //
  154. UINT QueryCCH( VOID ) const
  155. { return IsUnicode() ? ::wcslen((WCHAR *)QueryStrW()) :
  156. ::strlen((CHAR *) QueryStrA()); }
  157. //
  158. // Makes a Widechar copy of the stored string in given buffer
  159. //
  160. BOOL CopyToBuffer( WCHAR * lpszBuffer, LPDWORD lpcch) const;
  161. //
  162. // Makes a schar copy of the stored string in given buffer
  163. //
  164. BOOL CopyToBufferA( CHAR * lpszBuffer, LPDWORD lpcch) const;
  165. //
  166. // In-place conversion
  167. //
  168. BOOL ConvertToW(VOID);
  169. BOOL ConvertToA(VOID);
  170. //
  171. // If the string buffer is empty, returns the empty string, otherwise
  172. // returns a pointer to the buffer
  173. //
  174. #if 1
  175. CHAR * QueryStrA( VOID ) const;
  176. WCHAR * QueryStrW( VOID ) const;
  177. #else
  178. //
  179. // _pszEmptyString doesn't get imported corectly, results in unresolved
  180. // externals
  181. //
  182. CHAR * QueryStrA( VOID ) const
  183. { return (QueryPtr() ? (CHAR *) QueryPtr() : (CHAR *) _pszEmptyString); }
  184. WCHAR * QueryStrW( VOID ) const
  185. { return (QueryPtr() ? (WCHAR *) QueryPtr() : (WCHAR *) _pszEmptyString); }
  186. #endif //!DBG
  187. #ifdef UNICODE
  188. WCHAR * QueryStr( VOID ) const
  189. { return QueryStrW(); }
  190. #else
  191. CHAR * QueryStr( VOID ) const
  192. { return QueryStrA(); }
  193. #endif
  194. BOOL IsUnicode( VOID ) const
  195. { return _fUnicode; }
  196. VOID SetUnicode( BOOL fUnicode )
  197. { _fUnicode = fUnicode; }
  198. BOOL IsValid( VOID ) const
  199. { return _fValid; }
  200. //
  201. // Checks and returns TRUE if this string has no valid data else FALSE
  202. //
  203. BOOL IsEmpty( VOID) const
  204. { //return ( *QueryStr() == '\0'); }
  205. if (!QuerySize() || !QueryPtr()) {
  206. return TRUE;
  207. }
  208. LPBYTE pb = (BYTE *)QueryPtr();
  209. return (_fUnicode) ?
  210. ((WCHAR)*pb==L'\0') : ((CHAR)*pb=='\0') ;
  211. }
  212. //
  213. // Makes a clone of the current string in the string pointer passed in.
  214. //
  215. BOOL
  216. Clone( OUT STR * pstrClone) const
  217. {
  218. if ( pstrClone == NULL) {
  219. SetLastError( ERROR_INVALID_PARAMETER);
  220. return ( FALSE);
  221. } else {
  222. return ( pstrClone->Copy( *this));
  223. }
  224. } // STR::Clone()
  225. //
  226. // Useful operators
  227. //
  228. operator const TCHAR *() const { return QueryStr(); }
  229. const inline STR& operator =(LPCSTR lpstr) { Copy(lpstr); return *this; }
  230. const inline STR& operator =(LPCWSTR lpwstr) { Copy(lpwstr); return *this; }
  231. const inline STR& operator =(STR& cs) { Copy(cs);return *this; }
  232. const inline STR& operator +=(LPCSTR lpstr) { Append(lpstr);return *this; }
  233. const inline STR& operator +=(LPCWSTR lpwstr) { Append(lpwstr);return *this; }
  234. const inline STR& operator +=(STR& cs) { Append(cs);return *this; }
  235. private:
  236. //
  237. // TRUE if the string has already been mapped to Unicode
  238. // FALSE if the string is in Latin1
  239. //
  240. BOOL _fUnicode;
  241. BOOL _fValid;
  242. //
  243. // Returned when our buffer is empty
  244. //
  245. static WCHAR _pszEmptyString[];
  246. VOID AuxInit( PBYTE pInit, BOOL fUnicode );
  247. BOOL AuxAppend( PBYTE pInit, UINT cbStr, BOOL fAddSlop = TRUE );
  248. };
  249. class STRArray {
  250. STR *m_pcsContents, m_csEmpty;
  251. unsigned m_ucItems, m_ucMax, m_uGrowBy;
  252. void Grow();
  253. public:
  254. STRArray(UINT uGrowBy = 10);
  255. ~STRArray();
  256. UINT Count() const { return m_ucItems; }
  257. void Add(LPCSTR lpstrNew);
  258. void Add(LPCWSTR lpstrNew);
  259. STR& operator[](UINT u) {
  260. return u < m_ucItems ? m_pcsContents[u] : m_csEmpty;
  261. }
  262. void Tokenize(LPCTSTR lpstrIn, TCHAR tcSplitter);
  263. };
  264. #endif
  265. #endif // !_STRING_H_