1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDir implementation for Win32
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "dir.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
37 #include "wx/filefn.h" // for wxPathExists()
39 // ----------------------------------------------------------------------------
40 // define the types and functions used for file searching
41 // ----------------------------------------------------------------------------
43 // under Win16 use compiler-specific functions
49 typedef struct _find_t FIND_STRUCT
;
50 #elif defined(__BORLANDC__)
53 typedef struct ffblk FIND_STRUCT
;
55 #error "No directory searching functions for this compiler"
58 typedef FIND_STRUCT
*FIND_DATA
;
59 typedef char FIND_ATTR
;
61 static inline FIND_DATA
InitFindData() { return (FIND_DATA
)NULL
; }
62 static inline bool IsFindDataOk(FIND_DATA fd
) { return fd
!= NULL
; }
63 static inline void FreeFindData(FIND_DATA fd
) { free(fd
); }
65 static inline FIND_DATA
FindFirst(const wxString
& spec
,
66 FIND_STRUCT
* WXUNUSED(finddata
))
68 // attribute to find all files
69 static const FIND_ATTR attr
= 0x3F;
71 FIND_DATA fd
= (FIND_DATA
)malloc(sizeof(FIND_STRUCT
));
75 _dos_findfirst(spec
, attr
, fd
) == 0
77 findfirst(spec
, fd
, attr
) == 0
91 static inline bool FindNext(FIND_DATA fd
, FIND_STRUCT
* WXUNUSED(finddata
))
94 return _dos_findnext(fd
) == 0;
96 return findnext(fd
) == 0;
100 static const wxChar
*GetNameFromFindData(FIND_STRUCT
*finddata
)
103 return finddata
->name
;
105 return finddata
->ff_name
;
109 static const FIND_ATTR
GetAttrFromFindData(FIND_STRUCT
*finddata
)
112 return finddata
->attrib
;
114 return finddata
->ff_attrib
;
118 static inline bool IsDir(FIND_ATTR attr
)
120 return (attr
& _A_SUBDIR
) != 0;
123 static inline bool IsHidden(FIND_ATTR attr
)
125 return (attr
& (_A_SYSTEM
| _A_HIDDEN
)) != 0;
130 typedef WIN32_FIND_DATA FIND_STRUCT
;
131 typedef HANDLE FIND_DATA
;
132 typedef DWORD FIND_ATTR
;
134 static inline FIND_DATA
InitFindData() { return INVALID_HANDLE_VALUE
; }
136 static inline bool IsFindDataOk(FIND_DATA fd
)
138 return fd
!= INVALID_HANDLE_VALUE
;
141 static inline void FreeFindData(FIND_DATA fd
)
143 if ( !::FindClose(fd
) )
145 wxLogLastError(_T("FindClose"));
149 static inline FIND_DATA
FindFirst(const wxString
& spec
,
150 FIND_STRUCT
*finddata
)
152 return ::FindFirstFile(spec
, finddata
);
155 static inline bool FindNext(FIND_DATA fd
, FIND_STRUCT
*finddata
)
157 return ::FindNextFile(fd
, finddata
) != 0;
160 static const wxChar
*GetNameFromFindData(FIND_STRUCT
*finddata
)
162 return finddata
->cFileName
;
165 static const FIND_ATTR
GetAttrFromFindData(FIND_STRUCT
*finddata
)
167 return finddata
->dwFileAttributes
;
170 static inline bool IsDir(FIND_ATTR attr
)
172 return (attr
& FILE_ATTRIBUTE_DIRECTORY
) != 0;
175 static inline bool IsHidden(FIND_ATTR attr
)
177 return (attr
& (FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
)) != 0;
181 // ----------------------------------------------------------------------------
183 // ----------------------------------------------------------------------------
186 #define MAX_PATH 260 // from VC++ headers
189 // ----------------------------------------------------------------------------
191 // ----------------------------------------------------------------------------
193 #define M_DIR ((wxDirData *)m_data)
195 // ----------------------------------------------------------------------------
197 // ----------------------------------------------------------------------------
199 // this class stores everything we need to enumerate the files
203 wxDirData(const wxString
& dirname
);
206 void SetFileSpec(const wxString
& filespec
) { m_filespec
= filespec
; }
207 void SetFlags(int flags
) { m_flags
= flags
; }
211 bool Read(wxString
*filename
);
214 FIND_DATA m_finddata
;
222 // ============================================================================
224 // ============================================================================
226 // ----------------------------------------------------------------------------
228 // ----------------------------------------------------------------------------
230 wxDirData::wxDirData(const wxString
& dirname
)
233 m_finddata
= InitFindData();
236 wxDirData::~wxDirData()
241 void wxDirData::Close()
243 if ( IsFindDataOk(m_finddata
) )
245 FreeFindData(m_finddata
);
247 m_finddata
= InitFindData();
251 void wxDirData::Rewind()
256 bool wxDirData::Read(wxString
*filename
)
261 WIN32_FIND_DATA finddata
;
262 #define PTR_TO_FINDDATA (&finddata)
264 #define PTR_TO_FINDDATA (m_finddata)
267 if ( !IsFindDataOk(m_finddata
) )
270 wxString filespec
= m_dirname
;
271 if ( !wxEndsWithPathSeparator(filespec
) )
273 filespec
+= _T('\\');
275 filespec
+= (!m_filespec
? _T("*.*") : m_filespec
.c_str());
277 m_finddata
= FindFirst(filespec
, PTR_TO_FINDDATA
);
282 if ( !IsFindDataOk(m_finddata
) )
285 DWORD err
= ::GetLastError();
287 if ( err
!= ERROR_FILE_NOT_FOUND
)
289 wxLogSysError(err
, _("Can not enumerate files in directory '%s'"),
293 //else: not an error, just no (such) files
309 if ( !FindNext(m_finddata
, PTR_TO_FINDDATA
) )
312 DWORD err
= ::GetLastError();
314 if ( err
!= ERROR_NO_MORE_FILES
)
316 wxLogLastError(_T("FindNext"));
319 //else: not an error, just no more (such) files
325 name
= GetNameFromFindData(PTR_TO_FINDDATA
);
326 attr
= GetAttrFromFindData(PTR_TO_FINDDATA
);
328 // don't return "." and ".." unless asked for
329 if ( name
[0] == _T('.') &&
330 ((name
[1] == _T('.') && name
[2] == _T('\0')) ||
331 (name
[1] == _T('\0'))) )
333 if ( !(m_flags
& wxDIR_DOTDOT
) )
337 // check the type now
338 if ( !(m_flags
& wxDIR_FILES
) && !IsDir(attr
) )
340 // it's a file, but we don't want them
343 else if ( !(m_flags
& wxDIR_DIRS
) && IsDir(attr
) )
345 // it's a dir, and we don't want it
349 // finally, check whether it's a hidden file
350 if ( !(m_flags
& wxDIR_HIDDEN
) )
352 if ( IsHidden(attr
) )
354 // it's a hidden file, skip it
367 // ----------------------------------------------------------------------------
369 // ----------------------------------------------------------------------------
372 bool wxDir::Exists(const wxString
& dir
)
374 return wxPathExists(dir
);
377 // ----------------------------------------------------------------------------
378 // wxDir construction/destruction
379 // ----------------------------------------------------------------------------
381 wxDir::wxDir(const wxString
& dirname
)
388 bool wxDir::Open(const wxString
& dirname
)
391 m_data
= new wxDirData(dirname
);
396 bool wxDir::IsOpened() const
398 return m_data
!= NULL
;
406 // ----------------------------------------------------------------------------
408 // ----------------------------------------------------------------------------
410 bool wxDir::GetFirst(wxString
*filename
,
411 const wxString
& filespec
,
414 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
418 M_DIR
->SetFileSpec(filespec
);
419 M_DIR
->SetFlags(flags
);
421 return GetNext(filename
);
424 bool wxDir::GetNext(wxString
*filename
) const
426 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
428 wxCHECK_MSG( filename
, FALSE
, _T("bad pointer in wxDir::GetNext()") );
430 return M_DIR
->Read(filename
);