ReactOS 0.4.15-dev-7834-g00c4b3d
EnumFilesImpl.h
Go to the documentation of this file.
1//
2// EnumFilesImpl.h
3//
4
5#include "EnumFiles.h"
6#include "FixLFN.h"
7#include "safestr.h"
8#ifndef UNDER_CE
9#include <direct.h>
10#endif//UNDER_CE
11#include <stdio.h>
12
13BOOL EnumFilesInDirectory ( const TCHAR* szDirectory_, const TCHAR* szFileSpec, MYENUMFILESPROC pProc, long lParam, BOOL bSubsToo, BOOL bSubsMustMatchFileSpec )
14{
15 TCHAR szDirectory[MAX_PATH+1];
16 TCHAR szSearchPath[MAX_PATH+1];
17 TCHAR szTemp[MAX_PATH+1];
18
19 if ( safestrlen(szDirectory_) > MAX_PATH || !szFileSpec || !pProc )
20 return FALSE;
21 if ( szDirectory_ )
22 _tcscpy ( szDirectory, szDirectory_ );
23 else
24#ifdef UNDER_CE
25 _tcscpy ( szDirectory, _T("") );
26#else//UNDER_CE
27 getcwd ( szDirectory, sizeof(szDirectory)-1 );
28#endif//UNDER_CE
29 int dirlen = _tcslen(szDirectory);
30 if ( dirlen > 0 && szDirectory[dirlen-1] != '\\' )
31 _tcscat ( szDirectory, _T("\\") );
32
33 // first search for all files in directory that match szFileSpec...
34 _sntprintf ( szSearchPath, sizeof(szSearchPath)-1, _T("%s%s"), szDirectory, szFileSpec );
36 HANDLE hfind = FindFirstFile ( szSearchPath, &wfd );
37 if ( hfind != INVALID_HANDLE_VALUE )
38 {
39 do
40 {
41 if ( !_tcscmp ( wfd.cFileName, _T(".") ) || !_tcscmp ( wfd.cFileName, _T("..") ) )
42 continue;
43 if ( wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
44 {
45 if ( !bSubsMustMatchFileSpec )
46 continue;
47 _sntprintf ( szTemp, sizeof(szTemp)-1, _T("%s%s"), szDirectory, wfd.cFileName );
48 if ( bSubsToo )
49 {
50 if ( !EnumFilesInDirectory ( szTemp, szFileSpec, pProc, lParam, bSubsToo, bSubsMustMatchFileSpec ) )
51 {
52 FindClose ( hfind );
53 return FALSE;
54 }
55 }
56 }
57 _sntprintf ( szTemp, sizeof(szTemp)-1, _T("%s%s"), szDirectory, wfd.cFileName );
58 FixLFN(szTemp,szTemp);
59 if ( !pProc ( &wfd, szTemp, lParam ) )
60 {
61 FindClose ( hfind );
62 return FALSE;
63 }
64 } while ( FindNextFile ( hfind, &wfd ) );
65 FindClose ( hfind );
66 }
67 if ( !bSubsToo || bSubsMustMatchFileSpec )
68 return TRUE;
69
70 // now search for all subdirectories...
71 _sntprintf ( szSearchPath, sizeof(szSearchPath)-1, _T("%s*.*"), szDirectory );
72 hfind = FindFirstFile ( szSearchPath, &wfd );
73 if ( hfind != INVALID_HANDLE_VALUE )
74 {
75 do
76 {
77 if ( !_tcscmp ( wfd.cFileName, _T(".") ) || !_tcscmp ( wfd.cFileName, _T("..") ) )
78 continue;
79 if ( !(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
80 continue;
81 _sntprintf ( szTemp, sizeof(szTemp)-1, _T("%s%s"), szDirectory, wfd.cFileName );
82 if ( FALSE == EnumFilesInDirectory ( szTemp, szFileSpec, pProc, lParam, bSubsToo, bSubsMustMatchFileSpec ) )
83 {
84 FindClose ( hfind );
85 return FALSE;
86 }
87 } while ( FindNextFile ( hfind, &wfd ) );
88 FindClose ( hfind );
89 }
90
91 return TRUE;
92}
BOOL EnumFilesInDirectory(const TCHAR *szDirectory_, const TCHAR *szFileSpec, MYENUMFILESPROC pProc, long lParam, BOOL bSubsToo, BOOL bSubsMustMatchFileSpec)
Definition: EnumFilesImpl.h:13
BOOL(* MYENUMFILESPROC)(PWIN32_FIND_DATA, const TCHAR *, long)
Definition: EnumFiles.h:8
int FixLFN(const TCHAR *pBadFileName, TCHAR *pGoodFileName)
Definition: FixLFN.h:12
LPARAM lParam
Definition: combotst.c:139
_Check_return_ _Ret_opt_z_ _CRTIMP char *__cdecl getcwd(_Out_writes_opt_(_SizeInBytes) char *_DstBuf, _In_ int _SizeInBytes)
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define MAX_PATH
Definition: compat.h:34
BOOL WINAPI FindClose(HANDLE hFindFile)
Definition: find.c:502
unsigned int BOOL
Definition: ntddk_ex.h:94
#define _tcscmp
Definition: tchar.h:1424
#define _tcscat
Definition: tchar.h:622
#define _tcscpy
Definition: tchar.h:623
#define FILE_ATTRIBUTE_DIRECTORY
Definition: nt_native.h:705
size_t safestrlen(const tchar *string)
Definition: safestr.h:19
#define _T(x)
Definition: vfdio.h:22
#define FindNextFile
Definition: winbase.h:3723
#define FindFirstFile
Definition: winbase.h:3717
char TCHAR
Definition: xmlstorage.h:189
#define _sntprintf
Definition: xmlstorage.h:201
#define _tcslen
Definition: xmlstorage.h:198