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.

261 lines
6.7 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996 - 2000.
  5. //
  6. // File: lcase.hxx
  7. //
  8. // Contents: Lowercases path
  9. //
  10. // History: 01-Jan-1996 KyleP Moved from cicat.cxx
  11. //
  12. //----------------------------------------------------------------------------
  13. #pragma once
  14. inline void TerminateWithBackSlash( WCHAR * pwszStr, ULONG & cc )
  15. {
  16. if ( 0 != pwszStr && 0 != cc )
  17. {
  18. if ( L'\\' != pwszStr[cc-1] )
  19. {
  20. pwszStr[cc++] = L'\\';
  21. pwszStr[cc] = 0;
  22. }
  23. }
  24. }
  25. #if CIDBG == 1
  26. inline void _AssertLowerCase( const WCHAR * pwszStr, ULONG cchLen = 0 )
  27. {
  28. if ( 0 == cchLen )
  29. {
  30. cchLen = wcslen( pwszStr );
  31. }
  32. XGrowable<WCHAR> xTmp( cchLen + 1 );
  33. ULONG cchLen2 = LCMapStringW ( LOCALE_NEUTRAL,
  34. LCMAP_LOWERCASE,
  35. pwszStr,
  36. cchLen,
  37. xTmp.Get(),
  38. xTmp.Count() );
  39. Win4Assert( cchLen == cchLen2 );
  40. Win4Assert( RtlCompareMemory( xTmp.Get(), pwszStr, cchLen * sizeof WCHAR ) );
  41. }
  42. #define AssertLowerCase(pwszStr, cchLen) _AssertLowerCase(pwszStr, cchLen)
  43. #else
  44. #define AssertLowerCase(pwszStr, cchLen)
  45. #endif
  46. //+---------------------------------------------------------------------------
  47. //
  48. // Class: CLowcaseBuf
  49. //
  50. // Purpose: Buffer to create lower case copy of string
  51. //
  52. // History: 10-Mar-92 BartoszM Created
  53. //
  54. //----------------------------------------------------------------------------
  55. class CLowcaseBuf
  56. {
  57. public:
  58. inline CLowcaseBuf ( WCHAR const * str );
  59. inline CLowcaseBuf ( WCHAR const * str, BOOL fIsLower );
  60. inline ~CLowcaseBuf();
  61. WCHAR const * Get() const { return _pBuf; }
  62. WCHAR * GetWriteable() { return _pBuf; }
  63. unsigned Length() const { return _cchLen; }
  64. void AppendBackSlash()
  65. {
  66. if ( 0 == _pBuf || 0 == _cchLen || L'\\' == _pBuf[_cchLen-1] )
  67. {
  68. return;
  69. }
  70. if ( _fOwned && _cchLen + 2 > _cchSize )
  71. {
  72. // Need more space
  73. WCHAR* pTmp = new WCHAR[++_cchSize];
  74. RtlCopyMemory( pTmp, _pBuf, _cchLen * sizeof( WCHAR ) );
  75. if ( _pBuf != _buf )
  76. delete [] _pBuf;
  77. _pBuf = pTmp;
  78. }
  79. TerminateWithBackSlash( _pBuf, _cchLen );
  80. }
  81. void RemoveBackSlash()
  82. {
  83. if ( 0 == _pBuf || 0 == _cchLen || L'\\' != _pBuf[_cchLen-1] )
  84. {
  85. return;
  86. }
  87. _pBuf[--_cchLen] = 0;
  88. }
  89. BOOL AreEqual( CLowcaseBuf const & rhs ) const
  90. {
  91. return _cchLen == rhs._cchLen &&
  92. RtlEqualMemory( _pBuf, rhs._pBuf, _cchLen*sizeof(WCHAR) );
  93. }
  94. inline void ForwardToBackSlash();
  95. private:
  96. WCHAR *_pBuf;
  97. ULONG _cchLen;
  98. ULONG _cchSize;
  99. BOOL _fOwned;
  100. enum { CAT_BUF_SIZE = (MAX_PATH + 1) };
  101. WCHAR _buf[CAT_BUF_SIZE];
  102. };
  103. //+-------------------------------------------------------------------------
  104. //
  105. // Member: CLowcaseBuf::CLowcaseBuf, public
  106. //
  107. // Synopsis: Constructor
  108. //
  109. // Arguments: [str] - string to make a lower case copy of.
  110. //
  111. // History: 10-Mar-92 BartoszM Created
  112. //
  113. //--------------------------------------------------------------------------
  114. inline CLowcaseBuf::CLowcaseBuf ( WCHAR const * str ) :
  115. _cchLen( 0 ),
  116. _fOwned( TRUE ),
  117. _cchSize( CAT_BUF_SIZE )
  118. {
  119. ULONG cwcIn = wcslen( str );
  120. ULONG cwcOut = sizeof( _buf ) / sizeof( _buf[0] );
  121. _pBuf = _buf;
  122. if ( 0 == cwcIn )
  123. {
  124. _cchLen = 0;
  125. }
  126. else
  127. {
  128. while ( TRUE )
  129. {
  130. ULONG cchLen = LCMapStringW ( LOCALE_NEUTRAL,
  131. LCMAP_LOWERCASE,
  132. str,
  133. cwcIn,
  134. _pBuf,
  135. cwcOut - 1 );
  136. if ( 0 == cchLen )
  137. {
  138. if ( GetLastError() == ERROR_INSUFFICIENT_BUFFER )
  139. {
  140. if ( _pBuf != _buf )
  141. delete [] _pBuf;
  142. cwcOut *= 2;
  143. _pBuf = new WCHAR[ _cchSize = cwcOut ];
  144. }
  145. else
  146. {
  147. ciDebugOut(( DEB_ERROR, "Error 0x%x lowercasing path\n", GetLastError() ));
  148. Win4Assert(( !"neutral lowercase failed" ));
  149. THROW( CException() );
  150. }
  151. }
  152. else
  153. {
  154. _cchLen = cchLen;
  155. break;
  156. }
  157. }
  158. }
  159. _pBuf[_cchLen] = 0;
  160. }
  161. //+-------------------------------------------------------------------------
  162. //
  163. // Member: CLowcaseBuf::CLowcaseBuf, public
  164. //
  165. // Synopsis: Constructor, when you need a CLowcaseBuf object and the
  166. // string is already lowercase.
  167. //
  168. // Arguments: [str] -- string to make a lower case copy of.
  169. // [fLower] -- must be TRUE
  170. //
  171. // History: 10-Mar-92 BartoszM Created (really?)
  172. //
  173. //--------------------------------------------------------------------------
  174. inline CLowcaseBuf::CLowcaseBuf (
  175. WCHAR const * str,
  176. BOOL fLower ) :
  177. _cchLen( wcslen( str ) ),
  178. _pBuf( (WCHAR *) str ),
  179. _fOwned( FALSE ),
  180. _cchSize( CAT_BUF_SIZE )
  181. {
  182. Win4Assert( fLower );
  183. AssertLowerCase( str, _cchLen );
  184. } //CLowcaseBuf
  185. //+-------------------------------------------------------------------------
  186. //
  187. // Member: CLowcaseBuf::~CLowcaseBuf, public
  188. //
  189. // Synopsis: Destructor (may delete heap buffer)
  190. //
  191. // History: 31-May-96 KyleP Created
  192. //
  193. //--------------------------------------------------------------------------
  194. CLowcaseBuf::~CLowcaseBuf()
  195. {
  196. if ( _fOwned && _pBuf != _buf )
  197. delete [] _pBuf;
  198. }
  199. //+---------------------------------------------------------------------------
  200. //
  201. // Member: CLowcaseBuf::ForwardToBackSlash
  202. //
  203. // Synopsis: Converts the forward slashes to back slashes in the string.
  204. //
  205. // History: 7-12-96 srikants Created
  206. //
  207. //----------------------------------------------------------------------------
  208. void CLowcaseBuf::ForwardToBackSlash()
  209. {
  210. for ( ULONG i = 0; i < _cchLen; i++ )
  211. {
  212. if ( L'/' == _pBuf[i] )
  213. {
  214. _pBuf[i] = L'\\';
  215. }
  216. }
  217. }