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.

312 lines
8.6 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows NT **/
  3. /** Copyright(c) Microsoft Corp., 1994 **/
  4. /**********************************************************************/
  5. /*
  6. string.hxx
  7. This module contains a light weight string class
  8. FILE HISTORY:
  9. Johnl 15-Aug-1994 Created
  10. MuraliK 09-July-1996 Rewrote for efficiency with no unicode support
  11. */
  12. #ifndef _STRING_HXX_
  13. #define _STRING_HXX_
  14. # include <buffer.hxx>
  15. //
  16. // Maximum number of characters a loadable string resource can be
  17. //
  18. # define STR_MAX_RES_SIZE ( 320)
  19. class dllexp STR;
  20. //
  21. // If an application defines STR_MODULE_NAME, it will be used
  22. // as the default module name on string loads
  23. //
  24. #ifndef STR_MODULE_NAME
  25. #define STR_MODULE_NAME NULL
  26. #endif
  27. //
  28. // These are the characters that are considered to be white space
  29. //
  30. #define ISWHITE( ch ) ((ch) == L'\t' || (ch) == L' ' || (ch) == L'\r')
  31. #define ISWHITEA( ch ) ((ch) == '\t' || (ch) == ' ' || (ch) == '\r')
  32. //
  33. // Removes useless segments from the URL and makes sure it doesn't go
  34. // past the root of the tree (i.e., "/foo/../..")
  35. //
  36. INT CanonURL( CHAR * pszPath, BOOL fIsDBCS = FALSE );
  37. //
  38. // Map these string functions to appropriate RT call depending on the code
  39. // page of the system. Thus we only use DBCS operations when necessary.
  40. //
  41. DWORD InitializeStringFunctions( VOID );
  42. UCHAR * IISstrupr( UCHAR * pszString );
  43. UCHAR * IISstrlwr( UCHAR * pszString );
  44. INT IISstrnicmp( UCHAR * pszString1, UCHAR * pszString2, size_t iSize );
  45. size_t IISstrlen( UCHAR * pszString1 );
  46. char * IISstrncpy (char * dest, const char * source, size_t count);
  47. INT IISstricmp( UCHAR * pszString1, UCHAR * pszString2);
  48. UCHAR *IISstrrchr(const UCHAR * pszString, UINT c);
  49. /*++
  50. class STR:
  51. Intention:
  52. A light-weight string class supporting encapsulated string class.
  53. This object is derived from BUFFER class.
  54. It maintains following state:
  55. m_fValid - whether this object is valid -
  56. used only by STR() init functions
  57. * NYI: I need to kill this someday *
  58. m_cchLen - string length cached when we update the string.
  59. Member Functions:
  60. There are two categories of functions:
  61. 1) Safe Functions - which do integrity checking of state
  62. 2) UnSafe Functions - which do not do integrity checking, but
  63. enable writing to the data stream freely.
  64. (someday this will be enabled as Safe versions without
  65. problem for users)
  66. --*/
  67. class dllexp STR : public BUFFER
  68. {
  69. public:
  70. STR()
  71. : BUFFER (),
  72. m_cchLen ( 0)
  73. {}
  74. // creates a stack version of the STR object - uses passed in stack buffer
  75. // STR does not free this pbInit on its own.
  76. STR( CHAR * pbInit, DWORD cbInit)
  77. : BUFFER( (BYTE *) pbInit, cbInit),
  78. m_cchLen (0)
  79. {}
  80. STR( DWORD cbInit)
  81. : BUFFER( cbInit),
  82. m_cchLen (0)
  83. {}
  84. STR( const CHAR * pchInit )
  85. : BUFFER (),
  86. m_cchLen ( 0)
  87. { AuxInit( (const BYTE * ) pchInit); }
  88. STR( const STR & str )
  89. : BUFFER (),
  90. m_cchLen ( 0)
  91. { AuxInit( (const BYTE * ) str.QueryStr()); }
  92. BOOL SetLen( IN DWORD cchLen)
  93. {
  94. return ( ( cchLen >= QuerySize())? FALSE :
  95. ( *((CHAR *) QueryPtr() + cchLen) = '\0', // null terminate
  96. m_cchLen = cchLen, // set the length
  97. TRUE
  98. )
  99. );
  100. }
  101. BOOL IsValid(VOID) const { return ( BUFFER::IsValid()) ; }
  102. //
  103. // Checks and returns TRUE if this string has no valid data else FALSE
  104. //
  105. BOOL IsEmpty( VOID) const { return ( *QueryStr() == '\0'); }
  106. BOOL Append( const CHAR * pchInit ) {
  107. return ((pchInit != NULL) ? (AuxAppend( (const BYTE * ) pchInit,
  108. ::strlen(pchInit))) :
  109. TRUE);
  110. }
  111. BOOL Append( const CHAR * pchInit, DWORD cchLen ) {
  112. return ((pchInit != NULL) ? (AuxAppend( (const BYTE * ) pchInit,
  113. cchLen)) :
  114. TRUE);
  115. }
  116. BOOL Append( const STR & str )
  117. { return AuxAppend( (const BYTE * ) str.QueryStr(), str.QueryCCH()); }
  118. // Resets the internal string to be NULL string. Buffer remains cached.
  119. VOID Reset( VOID)
  120. { DBG_ASSERT( QueryPtr() != NULL);
  121. *(QueryStr()) = '\0'; m_cchLen = 0;
  122. }
  123. BOOL Copy( const CHAR * pchInit ) {
  124. if ( QueryPtr() ) { *(QueryStr()) = '\0'; m_cchLen = 0; }
  125. return ( (pchInit != NULL) ?
  126. AuxAppend( (const BYTE *) pchInit, ::strlen( pchInit ), FALSE ):
  127. TRUE);
  128. }
  129. BOOL SafeCopy( const CHAR * pchInit );
  130. BOOL Copy( const CHAR * pchInit, IN DWORD cchLen ) {
  131. if ( QueryPtr() ) { *(QueryStr()) = '\0'; m_cchLen = 0; }
  132. return ( (pchInit != NULL) ?
  133. AuxAppend( (const BYTE *) pchInit, cchLen, FALSE ):
  134. TRUE);
  135. }
  136. BOOL Copy( const STR & str )
  137. { return ( Copy(str.QueryStr(), str.QueryCCH())); }
  138. //
  139. // Loads a string from this module's string resource table
  140. //
  141. BOOL LoadString( IN DWORD dwResID,
  142. IN LPCTSTR lpszModuleName = STR_MODULE_NAME,
  143. IN DWORD dwLangID = 0);
  144. BOOL LoadString( IN DWORD dwResID,
  145. IN HMODULE hModule);
  146. //
  147. // Loads a string with insert params from this module's .mc resource
  148. // table. Pass zero for the resource ID to use *this.
  149. //
  150. BOOL
  151. FormatString( IN DWORD dwResID,
  152. IN LPCTSTR apszInsertParams[],
  153. IN LPCTSTR lpszModuleName = STR_MODULE_NAME OPTIONAL,
  154. IN DWORD cbMaxMsg = 1024 OPTIONAL );
  155. //
  156. // Inserts and removes any odd ranged Latin-1 characters with the
  157. // escaped hexadecimal equivalent (%xx)
  158. //
  159. BOOL Escape();
  160. BOOL EscapeSpaces();
  161. BOOL Unescape();
  162. //
  163. // Returns the number of bytes in the string excluding the terminating
  164. // NULL
  165. //
  166. UINT QueryCB( VOID ) const
  167. { return ( m_cchLen * sizeof(CHAR)); }
  168. //
  169. // Returns # of characters in the string excluding the terminating NULL
  170. //
  171. UINT QueryCCH( VOID ) const { return (m_cchLen); }
  172. //
  173. // Makes a copy of the stored string in given buffer
  174. //
  175. BOOL CopyToBuffer( WCHAR * lpszBuffer, LPDWORD lpcch) const;
  176. BOOL CopyToBuffer( CHAR * lpszBuffer, LPDWORD lpcch) const;
  177. //
  178. // Return the string buffer
  179. //
  180. CHAR * QueryStrA( VOID ) const { return ( QueryStr()); }
  181. CHAR * QueryStr( VOID ) const { return ((CHAR *) QueryPtr()); }
  182. //
  183. // Makes a clone of the current string in the string pointer passed in.
  184. //
  185. BOOL
  186. Clone( OUT STR * pstrClone) const
  187. {
  188. return ((pstrClone == NULL) ?
  189. (SetLastError(ERROR_INVALID_PARAMETER), FALSE) :
  190. (pstrClone->Copy( *this))
  191. );
  192. } // STR::Clone()
  193. // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  194. // UNSAFE FUNCTIONS - make sure you have enough space here
  195. // Use these only if you have been dumping bytes to string
  196. // object by considering it as a buffer object
  197. // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  198. // Append a single character
  199. VOID Append( CHAR ch)
  200. {
  201. register CHAR * pch = ((CHAR *) QueryPtr()) + (m_cchLen++);
  202. *pch = ch; *(pch+1) = '\0';
  203. }
  204. // Append two characters
  205. VOID Append( CHAR ch1, CHAR ch2)
  206. {
  207. register CHAR * pch = ((CHAR *) QueryPtr()) + (m_cchLen += 2);
  208. *(pch-2) = ch1; *(pch-1) = ch2; *pch = '\0';
  209. }
  210. // Append CRLF pattern \r\n --> use two character append function
  211. VOID AppendCRLF(VOID) { Append( '\r', '\n'); }
  212. private:
  213. DWORD m_cchLen;
  214. VOID AuxInit( const BYTE * pInit );
  215. BOOL AuxAppend( const BYTE * pInit,
  216. UINT cbStr, BOOL fAddSlop = TRUE );
  217. };
  218. //
  219. // Quick macro for declaring a STR that will use stack memory of <size>
  220. // bytes. If the buffer overflows then a heap buffer will be allocated
  221. //
  222. #define STACK_STR( name, size ) \
  223. CHAR __ach##name[size]; \
  224. STR name( __ach##name, sizeof( __ach##name ))
  225. //
  226. // Unlike the STACK_STR macro, this template can be used for member
  227. // variables in classes.
  228. //
  229. template <size_t N>
  230. class dllexp Str : public STR
  231. {
  232. private:
  233. CHAR m_achData[N]; // Actual data
  234. public:
  235. Str()
  236. : STR(m_achData, N)
  237. {}
  238. }; // class Str<N>
  239. #endif // !_STRING_HXX_