ReactOS  0.4.14-dev-49-gfb4591c
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 
13 BOOL 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 );
35  WIN32_FIND_DATA wfd;
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 }
#define TRUE
Definition: types.h:120
int _tcscmp(const _TCHAR *s1, const _TCHAR *s2)
Definition: tcscmp.h:8
#define INVALID_HANDLE_VALUE
Definition: compat.h:391
_TCHAR * _tcscpy(_TCHAR *to, const _TCHAR *from)
Definition: tcscpy.h:8
int FixLFN(const TCHAR *pBadFileName, TCHAR *pGoodFileName)
Definition: FixLFN.h:12
BOOL EnumFilesInDirectory(const TCHAR *szDirectory_, const TCHAR *szFileSpec, MYENUMFILESPROC pProc, long lParam, BOOL bSubsToo, BOOL bSubsMustMatchFileSpec)
Definition: EnumFilesImpl.h:13
unsigned int BOOL
Definition: ntddk_ex.h:94
size_t __cdecl _tcslen(const _TCHAR *str)
Definition: tcslen.h:9
#define FindFirstFile
Definition: winbase.h:3599
#define FILE_ATTRIBUTE_DIRECTORY
Definition: nt_native.h:705
char TCHAR
Definition: xmlstorage.h:189
#define _T(x)
Definition: vfdio.h:22
#define FindNextFile
Definition: winbase.h:3605
#define MAX_PATH
Definition: compat.h:26
#define _sntprintf
Definition: xmlstorage.h:201
_Check_return_ _Ret_opt_z_ _CRTIMP char *__cdecl getcwd(_Out_writes_opt_(_SizeInBytes) char *_DstBuf, _In_ int _SizeInBytes)
size_t safestrlen(const tchar *string)
Definition: safestr.h:19
_TCHAR * _tcscat(_TCHAR *s, const _TCHAR *append)
Definition: tcscat.h:8
LPARAM lParam
Definition: combotst.c:139
BOOL(* MYENUMFILESPROC)(PWIN32_FIND_DATA, const TCHAR *, long)
Definition: EnumFiles.h:8
BOOL WINAPI FindClose(HANDLE hFindFile)
Definition: find.c:502