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"
31 // For _A_SUBDIR, etc.
32 #if defined(__BORLANDC__) && defined(__WIN16__)
42 #include "wx/filefn.h" // for wxPathExists()
44 // ----------------------------------------------------------------------------
45 // define the types and functions used for file searching
46 // ----------------------------------------------------------------------------
48 // under Win16 use compiler-specific functions
54 typedef struct _find_t FIND_STRUCT
;
55 #elif defined(__BORLANDC__)
58 typedef struct ffblk FIND_STRUCT
;
60 #error "No directory searching functions for this compiler"
63 typedef FIND_STRUCT
*FIND_DATA
;
64 typedef char FIND_ATTR
;
66 static inline FIND_DATA
InitFindData() { return (FIND_DATA
)NULL
; }
67 static inline bool IsFindDataOk(FIND_DATA fd
) { return fd
!= NULL
; }
68 static inline void FreeFindData(FIND_DATA fd
) { free(fd
); }
70 static inline FIND_DATA
FindFirst(const wxString
& spec
,
71 FIND_STRUCT
* WXUNUSED(finddata
))
73 // attribute to find all files
74 static const FIND_ATTR attr
= 0x3F;
76 FIND_DATA fd
= (FIND_DATA
)malloc(sizeof(FIND_STRUCT
));
80 _dos_findfirst(spec
, attr
, fd
) == 0
82 findfirst(spec
, fd
, attr
) == 0
96 static inline bool FindNext(FIND_DATA fd
, FIND_STRUCT
* WXUNUSED(finddata
))
99 return _dos_findnext(fd
) == 0;
101 return findnext(fd
) == 0;
105 static const wxChar
*GetNameFromFindData(FIND_STRUCT
*finddata
)
108 return finddata
->name
;
110 return finddata
->ff_name
;
114 static const FIND_ATTR
GetAttrFromFindData(FIND_STRUCT
*finddata
)
117 return finddata
->attrib
;
119 return finddata
->ff_attrib
;
123 static inline bool IsDir(FIND_ATTR attr
)
125 return (attr
& _A_SUBDIR
) != 0;
128 static inline bool IsHidden(FIND_ATTR attr
)
130 return (attr
& (_A_SYSTEM
| _A_HIDDEN
)) != 0;
135 typedef WIN32_FIND_DATA FIND_STRUCT
;
136 typedef HANDLE FIND_DATA
;
137 typedef DWORD FIND_ATTR
;
139 static inline FIND_DATA
InitFindData() { return INVALID_HANDLE_VALUE
; }
141 static inline bool IsFindDataOk(FIND_DATA fd
)
143 return fd
!= INVALID_HANDLE_VALUE
;
146 static inline void FreeFindData(FIND_DATA fd
)
148 if ( !::FindClose(fd
) )
150 wxLogLastError(_T("FindClose"));
154 static inline FIND_DATA
FindFirst(const wxString
& spec
,
155 FIND_STRUCT
*finddata
)
157 return ::FindFirstFile(spec
, finddata
);
160 static inline bool FindNext(FIND_DATA fd
, FIND_STRUCT
*finddata
)
162 return ::FindNextFile(fd
, finddata
) != 0;
165 static const wxChar
*GetNameFromFindData(FIND_STRUCT
*finddata
)
167 return finddata
->cFileName
;
170 static const FIND_ATTR
GetAttrFromFindData(FIND_STRUCT
*finddata
)
172 return finddata
->dwFileAttributes
;
175 static inline bool IsDir(FIND_ATTR attr
)
177 return (attr
& FILE_ATTRIBUTE_DIRECTORY
) != 0;
180 static inline bool IsHidden(FIND_ATTR attr
)
182 return (attr
& (FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
)) != 0;
186 // ----------------------------------------------------------------------------
188 // ----------------------------------------------------------------------------
191 #define MAX_PATH 260 // from VC++ headers
194 // ----------------------------------------------------------------------------
196 // ----------------------------------------------------------------------------
198 #define M_DIR ((wxDirData *)m_data)
200 // ----------------------------------------------------------------------------
202 // ----------------------------------------------------------------------------
204 // this class stores everything we need to enumerate the files
208 wxDirData(const wxString
& dirname
);
211 void SetFileSpec(const wxString
& filespec
) { m_filespec
= filespec
; }
212 void SetFlags(int flags
) { m_flags
= flags
; }
216 bool Read(wxString
*filename
);
218 const wxString
& GetName() const { return m_dirname
; }
221 FIND_DATA m_finddata
;
229 // ============================================================================
231 // ============================================================================
233 // ----------------------------------------------------------------------------
235 // ----------------------------------------------------------------------------
237 wxDirData::wxDirData(const wxString
& dirname
)
240 m_finddata
= InitFindData();
243 wxDirData::~wxDirData()
248 void wxDirData::Close()
250 if ( IsFindDataOk(m_finddata
) )
252 FreeFindData(m_finddata
);
254 m_finddata
= InitFindData();
258 void wxDirData::Rewind()
263 bool wxDirData::Read(wxString
*filename
)
268 WIN32_FIND_DATA finddata
;
269 #define PTR_TO_FINDDATA (&finddata)
271 #define PTR_TO_FINDDATA (m_finddata)
274 if ( !IsFindDataOk(m_finddata
) )
277 wxString filespec
= m_dirname
;
278 if ( !wxEndsWithPathSeparator(filespec
) )
280 filespec
+= _T('\\');
282 filespec
+= (!m_filespec
? _T("*.*") : m_filespec
.c_str());
284 m_finddata
= FindFirst(filespec
, PTR_TO_FINDDATA
);
289 if ( !IsFindDataOk(m_finddata
) )
292 DWORD err
= ::GetLastError();
294 if ( err
!= ERROR_FILE_NOT_FOUND
)
296 wxLogSysError(err
, _("Can not enumerate files in directory '%s'"),
300 //else: not an error, just no (such) files
316 if ( !FindNext(m_finddata
, PTR_TO_FINDDATA
) )
319 DWORD err
= ::GetLastError();
321 if ( err
!= ERROR_NO_MORE_FILES
)
323 wxLogLastError(_T("FindNext"));
326 //else: not an error, just no more (such) files
332 name
= GetNameFromFindData(PTR_TO_FINDDATA
);
333 attr
= GetAttrFromFindData(PTR_TO_FINDDATA
);
335 // don't return "." and ".." unless asked for
336 if ( name
[0] == _T('.') &&
337 ((name
[1] == _T('.') && name
[2] == _T('\0')) ||
338 (name
[1] == _T('\0'))) )
340 if ( !(m_flags
& wxDIR_DOTDOT
) )
344 // check the type now
345 if ( !(m_flags
& wxDIR_FILES
) && !IsDir(attr
) )
347 // it's a file, but we don't want them
350 else if ( !(m_flags
& wxDIR_DIRS
) && IsDir(attr
) )
352 // it's a dir, and we don't want it
356 // finally, check whether it's a hidden file
357 if ( !(m_flags
& wxDIR_HIDDEN
) )
359 if ( IsHidden(attr
) )
361 // it's a hidden file, skip it
374 // ----------------------------------------------------------------------------
376 // ----------------------------------------------------------------------------
379 bool wxDir::Exists(const wxString
& dir
)
381 return wxPathExists(dir
);
384 // ----------------------------------------------------------------------------
385 // wxDir construction/destruction
386 // ----------------------------------------------------------------------------
388 wxDir::wxDir(const wxString
& dirname
)
395 bool wxDir::Open(const wxString
& dirname
)
398 m_data
= new wxDirData(dirname
);
403 bool wxDir::IsOpened() const
405 return m_data
!= NULL
;
408 wxString
wxDir::GetName() const
413 name
= M_DIR
->GetName();
416 // bring to canonical Windows form
417 name
.Replace(_T("/"), _T("\\"));
419 if ( name
.Last() == _T('\\') )
421 // chop off the last (back)slash
422 name
.Truncate(name
.length() - 1);
435 // ----------------------------------------------------------------------------
437 // ----------------------------------------------------------------------------
439 bool wxDir::GetFirst(wxString
*filename
,
440 const wxString
& filespec
,
443 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
447 M_DIR
->SetFileSpec(filespec
);
448 M_DIR
->SetFlags(flags
);
450 return GetNext(filename
);
453 bool wxDir::GetNext(wxString
*filename
) const
455 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
457 wxCHECK_MSG( filename
, FALSE
, _T("bad pointer in wxDir::GetNext()") );
459 return M_DIR
->Read(filename
);
462 // ----------------------------------------------------------------------------
463 // wxGetDirectoryTimes: used by wxFileName::GetTimes()
464 // ----------------------------------------------------------------------------
469 wxGetDirectoryTimes(const wxString
& dirname
,
470 FILETIME
*ftAccess
, FILETIME
*ftCreate
, FILETIME
*ftMod
)
472 // FindFirst() is going to fail
473 wxASSERT_MSG( !dirname
.empty() && dirname
.Last() != _T('\\'),
474 _T("incorrect directory name format in wxGetDirectoryTimes") );
477 FIND_DATA fd
= FindFirst(dirname
, &fs
);
478 if ( !IsFindDataOk(fd
) )
483 *ftAccess
= fs
.ftLastAccessTime
;
484 *ftCreate
= fs
.ftCreationTime
;
485 *ftMod
= fs
.ftLastWriteTime
;