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 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
33 #include "wx/filefn.h" // for wxDirExists()
36 #include "wx/msw/private.h"
39 // ----------------------------------------------------------------------------
40 // define the types and functions used for file searching
41 // ----------------------------------------------------------------------------
43 typedef WIN32_FIND_DATA FIND_STRUCT
;
44 typedef HANDLE FIND_DATA
;
45 typedef DWORD FIND_ATTR
;
47 static inline FIND_DATA
InitFindData() { return INVALID_HANDLE_VALUE
; }
49 static inline bool IsFindDataOk(FIND_DATA fd
)
51 return fd
!= INVALID_HANDLE_VALUE
;
54 static inline void FreeFindData(FIND_DATA fd
)
56 if ( !::FindClose(fd
) )
58 wxLogLastError(_T("FindClose"));
62 static inline FIND_DATA
FindFirst(const wxString
& spec
,
63 FIND_STRUCT
*finddata
)
65 return ::FindFirstFile(spec
, finddata
);
68 static inline bool FindNext(FIND_DATA fd
, FIND_STRUCT
*finddata
)
70 return ::FindNextFile(fd
, finddata
) != 0;
73 static const wxChar
*GetNameFromFindData(FIND_STRUCT
*finddata
)
75 return finddata
->cFileName
;
78 static const FIND_ATTR
GetAttrFromFindData(FIND_STRUCT
*finddata
)
80 return finddata
->dwFileAttributes
;
83 static inline bool IsDir(FIND_ATTR attr
)
85 return (attr
& FILE_ATTRIBUTE_DIRECTORY
) != 0;
88 static inline bool IsHidden(FIND_ATTR attr
)
90 return (attr
& (FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
)) != 0;
93 // ----------------------------------------------------------------------------
95 // ----------------------------------------------------------------------------
98 #define MAX_PATH 260 // from VC++ headers
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 #define M_DIR ((wxDirData *)m_data)
107 // ----------------------------------------------------------------------------
109 // ----------------------------------------------------------------------------
111 // this class stores everything we need to enumerate the files
115 wxDirData(const wxString
& dirname
);
118 void SetFileSpec(const wxString
& filespec
) { m_filespec
= filespec
; }
119 void SetFlags(int flags
) { m_flags
= flags
; }
123 bool Read(wxString
*filename
);
125 const wxString
& GetName() const { return m_dirname
; }
128 FIND_DATA m_finddata
;
135 DECLARE_NO_COPY_CLASS(wxDirData
)
138 // ============================================================================
140 // ============================================================================
142 // ----------------------------------------------------------------------------
144 // ----------------------------------------------------------------------------
146 wxDirData::wxDirData(const wxString
& dirname
)
149 m_finddata
= InitFindData();
152 wxDirData::~wxDirData()
157 void wxDirData::Close()
159 if ( IsFindDataOk(m_finddata
) )
161 FreeFindData(m_finddata
);
163 m_finddata
= InitFindData();
167 void wxDirData::Rewind()
172 bool wxDirData::Read(wxString
*filename
)
176 WIN32_FIND_DATA finddata
;
177 #define PTR_TO_FINDDATA (&finddata)
179 if ( !IsFindDataOk(m_finddata
) )
182 wxString filespec
= m_dirname
;
183 if ( !wxEndsWithPathSeparator(filespec
) )
185 filespec
+= _T('\\');
187 filespec
+= (!m_filespec
? _T("*.*") : m_filespec
.c_str());
189 m_finddata
= FindFirst(filespec
, PTR_TO_FINDDATA
);
194 if ( !IsFindDataOk(m_finddata
) )
197 DWORD err
= ::GetLastError();
199 if ( err
!= ERROR_FILE_NOT_FOUND
&& err
!= ERROR_NO_MORE_FILES
)
201 wxLogSysError(err
, _("Can not enumerate files in directory '%s'"),
205 //else: not an error, just no (such) files
221 if ( !FindNext(m_finddata
, PTR_TO_FINDDATA
) )
224 DWORD err
= ::GetLastError();
226 if ( err
!= ERROR_NO_MORE_FILES
)
228 wxLogLastError(_T("FindNext"));
231 //else: not an error, just no more (such) files
237 name
= GetNameFromFindData(PTR_TO_FINDDATA
);
238 attr
= GetAttrFromFindData(PTR_TO_FINDDATA
);
240 // don't return "." and ".." unless asked for
241 if ( name
[0] == _T('.') &&
242 ((name
[1] == _T('.') && name
[2] == _T('\0')) ||
243 (name
[1] == _T('\0'))) )
245 if ( !(m_flags
& wxDIR_DOTDOT
) )
249 // check the type now
250 if ( !(m_flags
& wxDIR_FILES
) && !IsDir(attr
) )
252 // it's a file, but we don't want them
255 else if ( !(m_flags
& wxDIR_DIRS
) && IsDir(attr
) )
257 // it's a dir, and we don't want it
261 // finally, check whether it's a hidden file
262 if ( !(m_flags
& wxDIR_HIDDEN
) )
264 if ( IsHidden(attr
) )
266 // it's a hidden file, skip it
279 // ----------------------------------------------------------------------------
281 // ----------------------------------------------------------------------------
284 bool wxDir::Exists(const wxString
& dir
)
286 return wxDirExists(dir
);
289 // ----------------------------------------------------------------------------
290 // wxDir construction/destruction
291 // ----------------------------------------------------------------------------
293 wxDir::wxDir(const wxString
& dirname
)
300 bool wxDir::Open(const wxString
& dirname
)
303 m_data
= new wxDirData(dirname
);
308 bool wxDir::IsOpened() const
310 return m_data
!= NULL
;
313 wxString
wxDir::GetName() const
318 name
= M_DIR
->GetName();
321 // bring to canonical Windows form
322 name
.Replace(_T("/"), _T("\\"));
324 if ( name
.Last() == _T('\\') )
326 // chop off the last (back)slash
327 name
.Truncate(name
.length() - 1);
340 // ----------------------------------------------------------------------------
342 // ----------------------------------------------------------------------------
344 bool wxDir::GetFirst(wxString
*filename
,
345 const wxString
& filespec
,
348 wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") );
352 M_DIR
->SetFileSpec(filespec
);
353 M_DIR
->SetFlags(flags
);
355 return GetNext(filename
);
358 bool wxDir::GetNext(wxString
*filename
) const
360 wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") );
362 wxCHECK_MSG( filename
, false, _T("bad pointer in wxDir::GetNext()") );
364 return M_DIR
->Read(filename
);
367 // ----------------------------------------------------------------------------
368 // wxGetDirectoryTimes: used by wxFileName::GetTimes()
369 // ----------------------------------------------------------------------------
374 wxGetDirectoryTimes(const wxString
& dirname
,
375 FILETIME
*ftAccess
, FILETIME
*ftCreate
, FILETIME
*ftMod
)
377 // FindFirst() is going to fail
378 wxASSERT_MSG( !dirname
.empty() && dirname
.Last() != _T('\\'),
379 _T("incorrect directory name format in wxGetDirectoryTimes") );
382 FIND_DATA fd
= FindFirst(dirname
, &fs
);
383 if ( !IsFindDataOk(fd
) )
388 *ftAccess
= fs
.ftLastAccessTime
;
389 *ftCreate
= fs
.ftCreationTime
;
390 *ftMod
= fs
.ftLastWriteTime
;