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(filespec
, &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
) )
271 filespec
<< m_dirname
<< _T('\\')
272 << (!m_filespec
? _T("*.*") : m_filespec
.c_str());
274 m_finddata
= FindFirst(filespec
, PTR_TO_FINDDATA
);
279 if ( !IsFindDataOk(m_finddata
) )
282 DWORD err
= ::GetLastError();
284 if ( err
!= ERROR_FILE_NOT_FOUND
)
286 wxLogSysError(err
, _("Can not enumerate files in directory '%s'"),
290 //else: not an error, just no (such) files
306 if ( !FindNext(m_finddata
, PTR_TO_FINDDATA
) )
309 DWORD err
= ::GetLastError();
311 if ( err
!= ERROR_NO_MORE_FILES
)
313 wxLogLastError(_T("FindNext"));
316 //else: not an error, just no more (such) files
322 name
= GetNameFromFindData(PTR_TO_FINDDATA
);
323 attr
= GetAttrFromFindData(PTR_TO_FINDDATA
);
325 // don't return "." and ".." unless asked for
326 if ( name
[0] == _T('.') &&
327 ((name
[1] == _T('.') && name
[2] == _T('\0')) ||
328 (name
[1] == _T('\0'))) )
330 if ( !(m_flags
& wxDIR_DOTDOT
) )
334 // check the type now
335 if ( !(m_flags
& wxDIR_FILES
) && !IsDir(attr
) )
337 // it's a file, but we don't want them
340 else if ( !(m_flags
& wxDIR_DIRS
) && IsDir(attr
) )
342 // it's a dir, and we don't want it
346 // finally, check whether it's a hidden file
347 if ( !(m_flags
& wxDIR_HIDDEN
) )
349 if ( IsHidden(attr
) )
351 // it's a hidden file, skip it
364 // ----------------------------------------------------------------------------
366 // ----------------------------------------------------------------------------
369 bool wxDir::Exists(const wxString
& dir
)
371 return wxPathExists(dir
);
374 // ----------------------------------------------------------------------------
375 // wxDir construction/destruction
376 // ----------------------------------------------------------------------------
378 wxDir::wxDir(const wxString
& dirname
)
385 bool wxDir::Open(const wxString
& dirname
)
388 m_data
= new wxDirData(dirname
);
393 bool wxDir::IsOpened() const
395 return m_data
!= NULL
;
403 // ----------------------------------------------------------------------------
405 // ----------------------------------------------------------------------------
407 bool wxDir::GetFirst(wxString
*filename
,
408 const wxString
& filespec
,
411 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
415 M_DIR
->SetFileSpec(filespec
);
416 M_DIR
->SetFlags(flags
);
418 return GetNext(filename
);
421 bool wxDir::GetNext(wxString
*filename
) const
423 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
425 wxCHECK_MSG( filename
, FALSE
, _T("bad pointer in wxDir::GetNext()") );
427 return M_DIR
->Read(filename
);