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
.fn_str(), 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 wxDECLARE_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('\\');
188 filespec
+= _T("*.*");
190 filespec
+= m_filespec
;
192 m_finddata
= FindFirst(filespec
, PTR_TO_FINDDATA
);
197 if ( !IsFindDataOk(m_finddata
) )
200 DWORD err
= ::GetLastError();
202 if ( err
!= ERROR_FILE_NOT_FOUND
&& err
!= ERROR_NO_MORE_FILES
)
204 wxLogSysError(err
, _("Can not enumerate files in directory '%s'"),
208 //else: not an error, just no (such) files
224 if ( !FindNext(m_finddata
, PTR_TO_FINDDATA
) )
227 DWORD err
= ::GetLastError();
229 if ( err
!= ERROR_NO_MORE_FILES
)
231 wxLogLastError(_T("FindNext"));
234 //else: not an error, just no more (such) files
240 name
= GetNameFromFindData(PTR_TO_FINDDATA
);
241 attr
= GetAttrFromFindData(PTR_TO_FINDDATA
);
243 // don't return "." and ".." unless asked for
244 if ( name
[0] == _T('.') &&
245 ((name
[1] == _T('.') && name
[2] == _T('\0')) ||
246 (name
[1] == _T('\0'))) )
248 if ( !(m_flags
& wxDIR_DOTDOT
) )
252 // check the type now
253 if ( !(m_flags
& wxDIR_FILES
) && !IsDir(attr
) )
255 // it's a file, but we don't want them
258 else if ( !(m_flags
& wxDIR_DIRS
) && IsDir(attr
) )
260 // it's a dir, and we don't want it
264 // finally, check whether it's a hidden file
265 if ( !(m_flags
& wxDIR_HIDDEN
) )
267 if ( IsHidden(attr
) )
269 // it's a hidden file, skip it
282 // ----------------------------------------------------------------------------
284 // ----------------------------------------------------------------------------
287 bool wxDir::Exists(const wxString
& dir
)
289 return wxDirExists(dir
);
292 // ----------------------------------------------------------------------------
293 // wxDir construction/destruction
294 // ----------------------------------------------------------------------------
296 wxDir::wxDir(const wxString
& dirname
)
303 bool wxDir::Open(const wxString
& dirname
)
307 // The Unix code does a similar test
308 if (wxDirExists(dirname
))
310 m_data
= new wxDirData(dirname
);
322 bool wxDir::IsOpened() const
324 return m_data
!= NULL
;
327 wxString
wxDir::GetName() const
332 name
= M_DIR
->GetName();
335 // bring to canonical Windows form
336 name
.Replace(_T("/"), _T("\\"));
338 if ( name
.Last() == _T('\\') )
340 // chop off the last (back)slash
341 name
.Truncate(name
.length() - 1);
354 // ----------------------------------------------------------------------------
356 // ----------------------------------------------------------------------------
358 bool wxDir::GetFirst(wxString
*filename
,
359 const wxString
& filespec
,
362 wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") );
366 M_DIR
->SetFileSpec(filespec
);
367 M_DIR
->SetFlags(flags
);
369 return GetNext(filename
);
372 bool wxDir::GetNext(wxString
*filename
) const
374 wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") );
376 wxCHECK_MSG( filename
, false, _T("bad pointer in wxDir::GetNext()") );
378 return M_DIR
->Read(filename
);
381 // ----------------------------------------------------------------------------
382 // wxGetDirectoryTimes: used by wxFileName::GetTimes()
383 // ----------------------------------------------------------------------------
388 wxGetDirectoryTimes(const wxString
& dirname
,
389 FILETIME
*ftAccess
, FILETIME
*ftCreate
, FILETIME
*ftMod
)
392 // FindFirst() is going to fail
393 wxASSERT_MSG( !dirname
.empty(),
394 _T("incorrect directory name format in wxGetDirectoryTimes") );
396 // FindFirst() is going to fail
397 wxASSERT_MSG( !dirname
.empty() && dirname
.Last() != _T('\\'),
398 _T("incorrect directory name format in wxGetDirectoryTimes") );
402 FIND_DATA fd
= FindFirst(dirname
, &fs
);
403 if ( !IsFindDataOk(fd
) )
408 *ftAccess
= fs
.ftLastAccessTime
;
409 *ftCreate
= fs
.ftCreationTime
;
410 *ftMod
= fs
.ftLastWriteTime
;