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.

439 lines
11 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows NT **/
  3. /** Copyright(c) Microsoft Corp., 1991 **/
  4. /**********************************************************************/
  5. /*
  6. FSEnum.hxx
  7. This file contains the definition of FS_ENUM, DOS_FS_ENUM and
  8. LFN_FS_ENUM, OS2_FS_ENUM and W32_FS_ENUM. These are used for
  9. traversing files and directories on a volume.
  10. FILE HISTORY:
  11. Johnl 02-Oct-1991 Created
  12. */
  13. #ifndef _FSENUM_HXX_
  14. #define _FSENUM_HXX_
  15. #include "base.hxx"
  16. #include "slist.hxx"
  17. #include "string.hxx"
  18. #include "strlst.hxx"
  19. #include <dos.h> // For file attribute masks
  20. enum FILE_TYPE { FILTYP_ALL_FILES, /* Files and subdirectories */
  21. FILTYP_DIRS, /* Directories only */
  22. FILTYP_FILES /* Files only */
  23. } ;
  24. /*************************************************************************
  25. NAME: DIR_BLOCK
  26. SYNOPSIS: Storage unit representing a single progressing directory enumeration
  27. INTERFACE: QueryFileName returns the current filename in the find buffer
  28. QueryAttr returns the current attribute in the find buffer
  29. PARENT: BASE
  30. USES: NLS_STR, BUFFER
  31. CAVEATS:
  32. NOTES: Clients should derive their own DIR_BLOCKs that contains the
  33. specific find buffer they are interested in using.
  34. HISTORY:
  35. Johnl 15-Oct-1991 Created
  36. **************************************************************************/
  37. DLL_CLASS DIR_BLOCK : public BASE
  38. {
  39. private:
  40. BOOL _fHasFindFirstBeenCalled ;
  41. /* This flag gets set to TRUE after we've traversed the directory and
  42. * now we need to dive into the directories stored in
  43. * _strlistBreadFirstDirs.
  44. */
  45. BOOL _fBreadthFirstDoDirs ;
  46. /* This strlist keeps track of the directories we encounter when we
  47. * do a breadth first search
  48. */
  49. STRLIST _strlistBreadthFirstDirs ;
  50. ITER_STRLIST _strlistIter ;
  51. public:
  52. DIR_BLOCK( ) ;
  53. virtual ~DIR_BLOCK() ;
  54. virtual UINT QueryAttr( void ) = 0 ;
  55. virtual const TCHAR * QueryFileName( void ) = 0 ;
  56. BOOL HasFindFirstBeenCalled( void )
  57. { return _fHasFindFirstBeenCalled ; }
  58. void SetFindFirstFlag( BOOL fFindFirstDone )
  59. { _fHasFindFirstBeenCalled = fFindFirstDone ; }
  60. BOOL IsDir( void )
  61. { return !!( QueryAttr() & _A_SUBDIR ) ; }
  62. BOOL DoBreadthFirstDirs( void ) const
  63. { return _fBreadthFirstDoDirs ; }
  64. void SetDoBreadthFirstDirs( BOOL fFlag )
  65. { _fBreadthFirstDoDirs = fFlag ; }
  66. STRLIST * QueryDirs( void )
  67. { return &_strlistBreadthFirstDirs ; }
  68. ITER_STRLIST * QueryDirsIter( void )
  69. { return &_strlistIter ; }
  70. } ;
  71. DECL_SLIST_OF( DIR_BLOCK, DLL_BASED )
  72. /*************************************************************************
  73. NAME: FS_ENUM
  74. SYNOPSIS: Enumerates files/directories on a volume
  75. This abstract super class provides a template for deriving your
  76. own file/directory traversers. It can search for all files and
  77. directories, directories only, files only, or by some mask
  78. (e.g., "*.bak"). The "." and ".." entries are automatically
  79. filtered out.
  80. INTERFACE:
  81. Next()
  82. Gets the next file/directory from the enumeration. Returns
  83. FALSE when no more files can be retrieved (either due to
  84. ERROR_NO_MORE_FILES or some other error). To determine if
  85. the enumeration was successful, QueryLastError() should be
  86. called. If the error is ERROR_NO_MORE_FILES, then all files
  87. should have been enumerated, otherwise an error occurred.
  88. QueryAttr()
  89. Returns the attribute mask of the current file
  90. QueryName()
  91. Returns the qualified file/directory name
  92. QueryLastError()
  93. Returns the error code of the last error that occurred (check
  94. this after Next() returns FALSE).
  95. PARENT: BASE
  96. USES: NLS_STR, ALIAS_STR, SLIST
  97. CAVEATS:
  98. NOTES: The cMaxDepth parameter indicates the deepest directory to
  99. traverse. A cMaxDepth of 0 means only traverse the items
  100. in the current directory. 1 means this directory and the
  101. contents of any subdirectories.
  102. HISTORY:
  103. Johnl 16-Oct-1991 Created
  104. Johnl 26-Oct-1992 Added depth parameter
  105. **************************************************************************/
  106. DLL_CLASS FS_ENUM : public BASE
  107. {
  108. public:
  109. virtual ~FS_ENUM() ;
  110. BOOL Next( void ) ;
  111. APIERR QueryName( NLS_STR * pnlsQualifiedName ) const ;
  112. UINT QueryAttr( void ) const
  113. { return QueryCurrentDirBlock()->QueryAttr() ; }
  114. APIERR QueryLastError( void ) const
  115. { return _errLastError ; }
  116. DIR_BLOCK * QueryCurrentDirBlock( void ) const
  117. { return _pCurrentDirBlock ; }
  118. UINT QueryTotalFiles( void )
  119. { return _cTotalFiles ; }
  120. UINT QueryMaxDepth( void )
  121. { return _nMaxDepth ; }
  122. UINT QueryCurrentDepth( void )
  123. { return _nCurrentDepth ; }
  124. static const TCHAR * _pszAllFilesMask ; // SZ("(*.*)"
  125. #define FS_ENUM_ALL_FILES FS_ENUM::_pszAllFilesMask
  126. protected:
  127. FS_ENUM( const TCHAR * pszOrgPath,
  128. const TCHAR * pszMask,
  129. enum FILE_TYPE filtypeInclude,
  130. BOOL fDepthFirst = TRUE,
  131. UINT cMaxDepth = 0xffffffff ) ;
  132. /* nlsSearchPath = "X:\A\B\*.*", i.e., _nlsPath + _nlsMask
  133. */
  134. virtual APIERR FindFirst( DIR_BLOCK * pDirBlock,
  135. const NLS_STR & nlsSearchPath,
  136. UINT uiSearchAttr ) = 0 ;
  137. virtual APIERR FindNext( DIR_BLOCK * pDirBlock, UINT uiSearchAttr ) = 0 ;
  138. BOOL NextDepthFirst( void ) ;
  139. BOOL NextBreadthFirst( void ) ;
  140. /* We rely on the deriver to create a DIR_BLOCK of the appropriate
  141. * type. The caller should wait until FindFirst to fill in most
  142. * of the information (such as search path etc.). The expected
  143. * derivation of this method looks like:
  144. *
  145. * XXX_FS_ENUM::CreateDirBlock( void )
  146. * { return new XXX_DIR_BLOCK( xxx ) ; }
  147. *
  148. * FS_ENUM will check for NULL *and* does a QueryError(), reporting the
  149. * error if necessary.
  150. */
  151. virtual DIR_BLOCK * CreateDirBlock( void ) = 0 ;
  152. /* We know the current file is a directory name, so push the current
  153. * DIR_BLOCK, create a new one and append the current file name as the
  154. * base for the directory. If _pCurrentDirBlock is NULL, then we just
  155. * create a Dirblock and assign to _pCurrentDirBlock.
  156. */
  157. APIERR PushDir( const TCHAR * pszNewDir = NULL ) ;
  158. /* Delete the current DIR_BLOCK and grab the previous one. Returns
  159. * ERROR_NO_MORE_FILES if we ran out of DIR_BLOCKS.
  160. */
  161. APIERR PopDir( void ) ;
  162. void ReportLastError( APIERR err )
  163. { _errLastError = err ; }
  164. BOOL ShouldThisFileBeIncluded( UINT uiAttr ) const ;
  165. UINT QuerySearchAttr( void ) const ;
  166. void SetCurrentDirBlock( DIR_BLOCK * pDirBlock )
  167. { _pCurrentDirBlock = pDirBlock ; }
  168. private:
  169. INT _cTotalFiles ;
  170. enum FILE_TYPE _filtypInclude ;
  171. const ALIAS_STR _nlsMask ;
  172. const ALIAS_STR _nlsDot ; // "."
  173. const ALIAS_STR _nlsDotDot ; // ".."
  174. /* In the form of "X:\A\B\C\D\" (notice terminating "\").
  175. */
  176. NLS_STR _nlsPath ;
  177. APIERR _errLastError ;
  178. DIR_BLOCK * _pCurrentDirBlock ;
  179. SLIST_OF( DIR_BLOCK ) _slStackOfDirBlocks ;
  180. BOOL _fDepthFirst ;
  181. UINT _nMaxDepth ; // How deep we can go
  182. UINT _nCurrentDepth ; // How deep we are currently
  183. } ;
  184. #ifndef WIN32
  185. /*************************************************************************
  186. NAME: OS2_FS_ENUM
  187. SYNOPSIS: Enumerates files using the DosFindFirst/Next API
  188. INTERFACE: See FS_ENUM
  189. PARENT: FS_ENUM
  190. NOTES: This only exists in the OS/2 version of the uimisc library
  191. HISTORY:
  192. Johnl 16-Oct-1991 Created
  193. **************************************************************************/
  194. DLL_CLASS OS2_FS_ENUM : public FS_ENUM
  195. {
  196. public:
  197. OS2_FS_ENUM ( const TCHAR * pszPath,
  198. const TCHAR * pszMask = FS_ENUM_ALL_FILES,
  199. enum FILE_TYPE filtypeInclude = FILTYP_ALL_FILES,
  200. BOOL fDepthFirst = TRUE ) ;
  201. virtual ~OS2_FS_ENUM() ;
  202. protected:
  203. virtual APIERR FindFirst( DIR_BLOCK * pDirBlock,
  204. const NLS_STR & nlsSearchPath,
  205. UINT uiSearchAttr ) ;
  206. virtual APIERR FindNext( DIR_BLOCK * pDirBlock, UINT uiSearchAttr ) ;
  207. virtual DIR_BLOCK * CreateDirBlock( void ) ;
  208. };
  209. /*************************************************************************
  210. NAME: DOS_FS_ENUM
  211. SYNOPSIS: Enumerates files using the dos _dos_findfirst/_dos_findnext API
  212. INTERFACE:
  213. PARENT: FS_ENUM
  214. NOTES: This exists in both the OS/2 and windows uimisc libraries
  215. HISTORY:
  216. Johnl 16-Oct-1991 Created
  217. **************************************************************************/
  218. DLL_CLASS DOS_FS_ENUM : public FS_ENUM
  219. {
  220. public:
  221. DOS_FS_ENUM ( const TCHAR * pszPath,
  222. const TCHAR * pszMask = FS_ENUM_ALL_FILES,
  223. enum FILE_TYPE filtypeInclude = FILTYP_ALL_FILES,
  224. BOOL fDepthFirst = TRUE ) ;
  225. virtual ~DOS_FS_ENUM() ;
  226. protected:
  227. virtual APIERR FindFirst( DIR_BLOCK * pDirBlock,
  228. const NLS_STR & nlsSearchPath,
  229. UINT uiSearchAttr ) ;
  230. virtual APIERR FindNext( DIR_BLOCK * pDirBlock, UINT uiSearchAttr ) ;
  231. virtual DIR_BLOCK * CreateDirBlock( void ) ;
  232. private:
  233. APIERR MapDOSError( unsigned uDosError ) const ;
  234. };
  235. /*************************************************************************
  236. NAME: WINNET_LFN_FS_ENUM
  237. SYNOPSIS: Enumerates files using the Winnets LFNFindFirst/LFNFindNext
  238. API.
  239. INTERFACE:
  240. PARENT: FS_ENUM
  241. NOTES: The LFN APIs exist only in the shell.
  242. *** The LFN APIs should *only* be called on servers that
  243. *** support Long Filenames (use LFNVolumeType to determine).
  244. HISTORY:
  245. Johnl 16-Oct-1991 Created
  246. **************************************************************************/
  247. DLL_CLASS WINNET_LFN_FS_ENUM : public FS_ENUM
  248. {
  249. public:
  250. WINNET_LFN_FS_ENUM ( const TCHAR * pszPath,
  251. const TCHAR * pszMask = FS_ENUM_ALL_FILES,
  252. enum FILE_TYPE filtypeInclude = FILTYP_ALL_FILES,
  253. BOOL fDepthFirst = TRUE ) ;
  254. virtual ~WINNET_LFN_FS_ENUM() ;
  255. protected:
  256. virtual APIERR FindFirst( DIR_BLOCK * pDirBlock,
  257. const NLS_STR & nlsSearchPath,
  258. UINT uiSearchAttr ) ;
  259. virtual APIERR FindNext( DIR_BLOCK * pDirBlock, UINT uiSearchAttr ) ;
  260. virtual DIR_BLOCK * CreateDirBlock( void ) ;
  261. };
  262. #endif //WIN32
  263. /*************************************************************************
  264. NAME: W32_FS_ENUM
  265. SYNOPSIS: Enumerates files using the Win32 APIs
  266. INTERFACE:
  267. PARENT: FS_ENUM
  268. NOTES:
  269. HISTORY:
  270. Johnl 16-Oct-1991 Created
  271. Johnl 10-Mar-1992 Implemented
  272. **************************************************************************/
  273. DLL_CLASS W32_FS_ENUM : public FS_ENUM
  274. {
  275. public:
  276. W32_FS_ENUM ( const TCHAR * pszPath,
  277. const TCHAR * pszMask = FS_ENUM_ALL_FILES,
  278. enum FILE_TYPE filtypeInclude = FILTYP_ALL_FILES,
  279. BOOL fDepthFirst = TRUE,
  280. UINT nMaxDepth = 0xffffffff ) ;
  281. virtual ~W32_FS_ENUM() ;
  282. protected:
  283. virtual APIERR FindFirst( DIR_BLOCK * pDirBlock,
  284. const NLS_STR & nlsSearchPath,
  285. UINT uiSearchAttr ) ;
  286. virtual APIERR FindNext( DIR_BLOCK * pDirBlock, UINT uiSearchAttr ) ;
  287. virtual DIR_BLOCK * CreateDirBlock( void ) ;
  288. };
  289. #endif // _FSENUM_HXX_