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.

282 lines
6.6 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1994.
  5. //
  6. // File: pathpars.hxx
  7. //
  8. // Contents: Parses a given path and gives individual components
  9. //
  10. // Classes: CPathParser
  11. //
  12. // Functions:
  13. //
  14. // History: 2-10-96 srikants Created
  15. //
  16. //----------------------------------------------------------------------------
  17. #pragma once
  18. //+---------------------------------------------------------------------------
  19. //
  20. // Class: CPathParser
  21. //
  22. // Purpose: A class to iterate over the components of a path.
  23. //
  24. // History: 2-11-96 srikants Created
  25. //
  26. // Notes:
  27. //
  28. //----------------------------------------------------------------------------
  29. class CPathParser
  30. {
  31. enum { cMinDriveName = 2, cMinUNC = 5 };
  32. enum EPathType { eRelative = 0, eDrivePath = 1, eUNC = 2 };
  33. public:
  34. enum { wBackSlash = L'\\', wColon = L':' };
  35. CPathParser( WCHAR const * pwszPath, ULONG len = 0 );
  36. CPathParser()
  37. {
  38. _pwszPath = _pCurr = 0;
  39. _len = _iNext = 0;
  40. _type = eRelative;
  41. }
  42. void Init( WCHAR const * pwszPath, ULONG len = 0 );
  43. BOOL IsLastComp() const { return 0 == _pwszPath[_iNext]; }
  44. void Next();
  45. BOOL GetFileName( WCHAR * pwszBuf, ULONG & cc ) const;
  46. BOOL GetFilePath( WCHAR * pwszBuf, ULONG & cc ) const;
  47. BOOL IsUNCName() const { return eUNC == _type; }
  48. BOOL IsDrivePath() const { return eDrivePath == _type; }
  49. BOOL IsRelativePath() const { return eRelative == _type; }
  50. private:
  51. BOOL _IsUNCName();
  52. BOOL _IsDriveName() const;
  53. void _SkipDrive();
  54. void _SkipUNC();
  55. void _SkipToNext();
  56. void _SkipToNextBackSlash();
  57. BOOL _IsAtBackSlash() const
  58. {
  59. return wBackSlash == _pwszPath[_iNext];
  60. }
  61. WCHAR const * _pwszPath;
  62. unsigned _len;
  63. WCHAR const * _pCurr;
  64. unsigned _iNext;
  65. EPathType _type;
  66. };
  67. class CLongPath
  68. {
  69. public:
  70. CLongPath( WCHAR const * pwszPath = 0 );
  71. CLongPath( CLongPath const & rhs );
  72. CLongPath & operator=( CLongPath const & rhs );
  73. void Init( WCHAR const * pwszPath );
  74. void Init( ULONG cwcMax );
  75. ~CLongPath()
  76. {
  77. if ( _pwszPath != _wszBuf )
  78. delete [] _pwszPath;
  79. }
  80. WCHAR const * GetPath() const { return _pwszPath; }
  81. WCHAR * GetWritable() { return _pwszPath; }
  82. ULONG GetMaxChars() const { return _cwcMax; }
  83. ULONG Length() const { return _cwc; }
  84. void MakeWin32LongPath();
  85. BOOL IsWin32LongPath();
  86. static BOOL IsMaxPathExceeded( WCHAR const * pwszPath );
  87. private:
  88. WCHAR _wszBuf[MAX_PATH+1]; // default buffer
  89. WCHAR * _pwszPath; // Current path buffer
  90. ULONG _cwc; // Length of the path
  91. ULONG _cwcMax; // Maximum number of chars in buffer
  92. // including trailing 0
  93. };
  94. //+---------------------------------------------------------------------------
  95. //
  96. // Member: Init
  97. //
  98. // Synopsis: Initializes the CLongPath class to have upto the specified
  99. // number of characters.
  100. //
  101. // Arguments: [cwc] - Maximum chars to be allowed (including 0)
  102. //
  103. // History: 6-26-96 srikants Created
  104. //
  105. //----------------------------------------------------------------------------
  106. inline
  107. void CLongPath::Init( ULONG cwc )
  108. {
  109. if ( cwc > _cwcMax )
  110. {
  111. WCHAR * pwszNew = new WCHAR [cwc];
  112. if ( _pwszPath != _wszBuf )
  113. delete [] _pwszPath;
  114. _pwszPath = pwszNew;
  115. _cwcMax = cwc;
  116. }
  117. _cwc = 0;
  118. _pwszPath[0] = 0;
  119. }
  120. //+---------------------------------------------------------------------------
  121. //
  122. // Member: CLongPath::Init
  123. //
  124. // Synopsis: Initializes by copying the given path buffer into the class
  125. // buffer.
  126. //
  127. // Arguments: [pwszPath] - Path to be copied.
  128. //
  129. // History: 6-26-96 srikants Created
  130. //
  131. // Notes:
  132. //
  133. //----------------------------------------------------------------------------
  134. inline
  135. void CLongPath::Init( WCHAR const * pwszPath )
  136. {
  137. Win4Assert( 0 != _pwszPath );
  138. if ( pwszPath )
  139. {
  140. ULONG cwc = wcslen( pwszPath );
  141. Init( cwc+1 );
  142. RtlCopyMemory( _pwszPath, pwszPath, (cwc+1)*sizeof(WCHAR) );
  143. _cwc = cwc;
  144. }
  145. else
  146. {
  147. _pwszPath[0] = 0;
  148. _cwc = 0;
  149. }
  150. }
  151. //+---------------------------------------------------------------------------
  152. //
  153. // Member: CLongPath::CLongPath
  154. //
  155. // Synopsis: Constructor that takes a path buffer
  156. //
  157. // Arguments: [pwszPath] - Path to be copied to the local buffer.
  158. //
  159. // History: 6-26-96 srikants Created
  160. //
  161. //----------------------------------------------------------------------------
  162. inline
  163. CLongPath::CLongPath( WCHAR const * pwszPath )
  164. {
  165. _pwszPath = _wszBuf;
  166. _cwcMax = sizeof(_wszBuf)/sizeof(WCHAR);
  167. Init( pwszPath );
  168. }
  169. //+---------------------------------------------------------------------------
  170. //
  171. // Member: CLongPath::CLongPath
  172. //
  173. // Synopsis: Copy constructor
  174. //
  175. // Arguments: [rhs] -
  176. //
  177. // History: 6-26-96 srikants Created
  178. //
  179. //----------------------------------------------------------------------------
  180. inline
  181. CLongPath::CLongPath( CLongPath const & rhs )
  182. {
  183. _pwszPath = _wszBuf;
  184. _cwcMax = sizeof(_wszBuf)/sizeof(WCHAR);
  185. Init( rhs.GetPath() );
  186. END_CONSTRUCTION( CLongPath );
  187. }
  188. //+---------------------------------------------------------------------------
  189. //
  190. // Member: CLongPath::operator
  191. //
  192. // Synopsis: Assignment operator for CLongPath
  193. //
  194. // Arguments: [rhs] -
  195. //
  196. // History: 6-26-96 srikants Created
  197. //
  198. // Notes:
  199. //
  200. //----------------------------------------------------------------------------
  201. inline
  202. CLongPath & CLongPath::operator=( CLongPath const & rhs )
  203. {
  204. Init( rhs.GetPath() );
  205. return *this;
  206. }
  207. //+---------------------------------------------------------------------------
  208. //
  209. // Function: IsMaxPathExceeded
  210. //
  211. // Synopsis: Tests if the given path has exceeded the max path
  212. //
  213. // Arguments: [pwszPath] -
  214. //
  215. // Returns: TRUE if pwszPath is >= MAX_PATH; FALSE o/w
  216. //
  217. // History: 6-26-96 srikants Created
  218. //
  219. //----------------------------------------------------------------------------
  220. inline CLongPath::IsMaxPathExceeded( WCHAR const * pwszPath )
  221. {
  222. ULONG cwc = pwszPath ? wcslen( pwszPath ) : 0;
  223. if (cwc >= MAX_PATH)
  224. {
  225. ciDebugOut(( DEB_ERROR,
  226. "Path (%ws), (%d) chars long, is >= than MAX_PATH(%d).\n",
  227. pwszPath, cwc, MAX_PATH ));
  228. return TRUE;
  229. }
  230. else
  231. {
  232. return FALSE;
  233. }
  234. }