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 licence
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()
45 #include "wx/msw/private.h"
48 // ----------------------------------------------------------------------------
49 // define the types and functions used for file searching
50 // ----------------------------------------------------------------------------
52 // under Win16 use compiler-specific functions
58 typedef struct _find_t FIND_STRUCT
;
59 #elif defined(__BORLANDC__)
62 typedef struct ffblk FIND_STRUCT
;
64 #error "No directory searching functions for this compiler"
67 typedef FIND_STRUCT
*FIND_DATA
;
68 typedef char FIND_ATTR
;
70 static inline FIND_DATA
InitFindData() { return (FIND_DATA
)NULL
; }
71 static inline bool IsFindDataOk(FIND_DATA fd
) { return fd
!= NULL
; }
72 static inline void FreeFindData(FIND_DATA fd
) { free(fd
); }
74 static inline FIND_DATA
FindFirst(const wxString
& spec
,
75 FIND_STRUCT
* WXUNUSED(finddata
))
77 // attribute to find all files
78 static const FIND_ATTR attr
= 0x3F;
80 FIND_DATA fd
= (FIND_DATA
)malloc(sizeof(FIND_STRUCT
));
84 _dos_findfirst(spec
, attr
, fd
) == 0
86 findfirst(spec
, fd
, attr
) == 0
100 static inline bool FindNext(FIND_DATA fd
, FIND_STRUCT
* WXUNUSED(finddata
))
103 return _dos_findnext(fd
) == 0;
105 return findnext(fd
) == 0;
109 static const wxChar
*GetNameFromFindData(FIND_STRUCT
*finddata
)
112 return finddata
->name
;
114 return finddata
->ff_name
;
118 static const FIND_ATTR
GetAttrFromFindData(FIND_STRUCT
*finddata
)
121 return finddata
->attrib
;
123 return finddata
->ff_attrib
;
127 static inline bool IsDir(FIND_ATTR attr
)
129 return (attr
& _A_SUBDIR
) != 0;
132 static inline bool IsHidden(FIND_ATTR attr
)
134 return (attr
& (_A_SYSTEM
| _A_HIDDEN
)) != 0;
137 typedef WIN32_FIND_DATA FIND_STRUCT
;
138 typedef HANDLE FIND_DATA
;
139 typedef DWORD FIND_ATTR
;
141 static inline FIND_DATA
InitFindData() { return INVALID_HANDLE_VALUE
; }
143 static inline bool IsFindDataOk(FIND_DATA fd
)
145 return fd
!= INVALID_HANDLE_VALUE
;
148 static inline void FreeFindData(FIND_DATA fd
)
150 if ( !::FindClose(fd
) )
152 wxLogLastError(_T("FindClose"));
156 static inline FIND_DATA
FindFirst(const wxString
& spec
,
157 FIND_STRUCT
*finddata
)
159 return ::FindFirstFile(spec
, finddata
);
162 static inline bool FindNext(FIND_DATA fd
, FIND_STRUCT
*finddata
)
164 return ::FindNextFile(fd
, finddata
) != 0;
167 static const wxChar
*GetNameFromFindData(FIND_STRUCT
*finddata
)
169 return finddata
->cFileName
;
172 static const FIND_ATTR
GetAttrFromFindData(FIND_STRUCT
*finddata
)
174 return finddata
->dwFileAttributes
;
177 static inline bool IsDir(FIND_ATTR attr
)
179 return (attr
& FILE_ATTRIBUTE_DIRECTORY
) != 0;
182 static inline bool IsHidden(FIND_ATTR attr
)
184 return (attr
& (FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
)) != 0;
188 // ----------------------------------------------------------------------------
190 // ----------------------------------------------------------------------------
193 #define MAX_PATH 260 // from VC++ headers
196 // ----------------------------------------------------------------------------
198 // ----------------------------------------------------------------------------
200 #define M_DIR ((wxDirData *)m_data)
202 // ----------------------------------------------------------------------------
204 // ----------------------------------------------------------------------------
206 // this class stores everything we need to enumerate the files
210 wxDirData(const wxString
& dirname
);
213 void SetFileSpec(const wxString
& filespec
) { m_filespec
= filespec
; }
214 void SetFlags(int flags
) { m_flags
= flags
; }
218 bool Read(wxString
*filename
);
220 const wxString
& GetName() const { return m_dirname
; }
223 FIND_DATA m_finddata
;
230 DECLARE_NO_COPY_CLASS(wxDirData
)
233 // ============================================================================
235 // ============================================================================
237 // ----------------------------------------------------------------------------
239 // ----------------------------------------------------------------------------
241 wxDirData::wxDirData(const wxString
& dirname
)
244 m_finddata
= InitFindData();
247 wxDirData::~wxDirData()
252 void wxDirData::Close()
254 if ( IsFindDataOk(m_finddata
) )
256 FreeFindData(m_finddata
);
258 m_finddata
= InitFindData();
262 void wxDirData::Rewind()
267 bool wxDirData::Read(wxString
*filename
)
272 WIN32_FIND_DATA finddata
;
273 #define PTR_TO_FINDDATA (&finddata)
275 #define PTR_TO_FINDDATA (m_finddata)
278 if ( !IsFindDataOk(m_finddata
) )
281 wxString filespec
= m_dirname
;
282 if ( !wxEndsWithPathSeparator(filespec
) )
284 filespec
+= _T('\\');
286 filespec
+= (!m_filespec
? _T("*.*") : m_filespec
.c_str());
288 m_finddata
= FindFirst(filespec
, PTR_TO_FINDDATA
);
293 if ( !IsFindDataOk(m_finddata
) )
296 DWORD err
= ::GetLastError();
298 if ( err
!= ERROR_FILE_NOT_FOUND
)
300 wxLogSysError(err
, _("Can not enumerate files in directory '%s'"),
304 //else: not an error, just no (such) files
320 if ( !FindNext(m_finddata
, PTR_TO_FINDDATA
) )
323 DWORD err
= ::GetLastError();
325 if ( err
!= ERROR_NO_MORE_FILES
)
327 wxLogLastError(_T("FindNext"));
330 //else: not an error, just no more (such) files
336 name
= GetNameFromFindData(PTR_TO_FINDDATA
);
337 attr
= GetAttrFromFindData(PTR_TO_FINDDATA
);
339 // don't return "." and ".." unless asked for
340 if ( name
[0] == _T('.') &&
341 ((name
[1] == _T('.') && name
[2] == _T('\0')) ||
342 (name
[1] == _T('\0'))) )
344 if ( !(m_flags
& wxDIR_DOTDOT
) )
348 // check the type now
349 if ( !(m_flags
& wxDIR_FILES
) && !IsDir(attr
) )
351 // it's a file, but we don't want them
354 else if ( !(m_flags
& wxDIR_DIRS
) && IsDir(attr
) )
356 // it's a dir, and we don't want it
360 // finally, check whether it's a hidden file
361 if ( !(m_flags
& wxDIR_HIDDEN
) )
363 if ( IsHidden(attr
) )
365 // it's a hidden file, skip it
378 // ----------------------------------------------------------------------------
380 // ----------------------------------------------------------------------------
383 bool wxDir::Exists(const wxString
& dir
)
385 return wxPathExists(dir
);
388 // ----------------------------------------------------------------------------
389 // wxDir construction/destruction
390 // ----------------------------------------------------------------------------
392 wxDir::wxDir(const wxString
& dirname
)
399 bool wxDir::Open(const wxString
& dirname
)
402 m_data
= new wxDirData(dirname
);
407 bool wxDir::IsOpened() const
409 return m_data
!= NULL
;
412 wxString
wxDir::GetName() const
417 name
= M_DIR
->GetName();
420 // bring to canonical Windows form
421 name
.Replace(_T("/"), _T("\\"));
423 if ( name
.Last() == _T('\\') )
425 // chop off the last (back)slash
426 name
.Truncate(name
.length() - 1);
439 // ----------------------------------------------------------------------------
441 // ----------------------------------------------------------------------------
443 bool wxDir::GetFirst(wxString
*filename
,
444 const wxString
& filespec
,
447 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
451 M_DIR
->SetFileSpec(filespec
);
452 M_DIR
->SetFlags(flags
);
454 return GetNext(filename
);
457 bool wxDir::GetNext(wxString
*filename
) const
459 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
461 wxCHECK_MSG( filename
, FALSE
, _T("bad pointer in wxDir::GetNext()") );
463 return M_DIR
->Read(filename
);
466 // ----------------------------------------------------------------------------
467 // wxGetDirectoryTimes: used by wxFileName::GetTimes()
468 // ----------------------------------------------------------------------------
473 wxGetDirectoryTimes(const wxString
& dirname
,
474 FILETIME
*ftAccess
, FILETIME
*ftCreate
, FILETIME
*ftMod
)
476 // FindFirst() is going to fail
477 wxASSERT_MSG( !dirname
.empty() && dirname
.Last() != _T('\\'),
478 _T("incorrect directory name format in wxGetDirectoryTimes") );
481 FIND_DATA fd
= FindFirst(dirname
, &fs
);
482 if ( !IsFindDataOk(fd
) )
487 *ftAccess
= fs
.ftLastAccessTime
;
488 *ftCreate
= fs
.ftCreationTime
;
489 *ftMod
= fs
.ftLastWriteTime
;