1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/dir.cpp
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"
35 #include "wx/msw/private.h"
38 // ----------------------------------------------------------------------------
39 // define the types and functions used for file searching
40 // ----------------------------------------------------------------------------
45 typedef WIN32_FIND_DATA FIND_STRUCT
;
46 typedef HANDLE FIND_DATA
;
47 typedef DWORD FIND_ATTR
;
49 inline FIND_DATA
InitFindData()
51 return INVALID_HANDLE_VALUE
;
54 inline bool IsFindDataOk(FIND_DATA fd
)
56 return fd
!= INVALID_HANDLE_VALUE
;
59 inline void FreeFindData(FIND_DATA fd
)
61 if ( !::FindClose(fd
) )
63 wxLogLastError(wxT("FindClose"));
67 const wxChar
*GetNameFromFindData(const FIND_STRUCT
*finddata
)
69 return finddata
->cFileName
;
72 // Helper function checking that the contents of the given FIND_STRUCT really
73 // match our filter. We need to do it ourselves as native Windows functions
74 // apply the filter to both the long and the short names of the file, so
75 // something like "*.bar" matches "foo.bar.baz" too and not only "foo.bar", so
76 // we have to double check that we have a real match.
78 CheckFoundMatch(const FIND_STRUCT
* finddata
, const wxString
& filter
)
80 return filter
.empty() ||
81 wxString(GetNameFromFindData(finddata
)).Matches(filter
);
85 FindNext(FIND_DATA fd
, const wxString
& filter
, FIND_STRUCT
*finddata
)
89 if ( !::FindNextFile(fd
, finddata
) )
92 // If we did find something, check that it really matches.
93 if ( CheckFoundMatch(finddata
, filter
) )
99 FindFirst(const wxString
& spec
,
100 const wxString
& filter
,
101 FIND_STRUCT
*finddata
)
103 FIND_DATA fd
= ::FindFirstFile(spec
.t_str(), finddata
);
105 // As in FindNext() above, we need to check that the file name we found
106 // really matches our filter and look for the next match if it doesn't.
107 if ( IsFindDataOk(fd
) && !CheckFoundMatch(finddata
, filter
) )
109 if ( !FindNext(fd
, filter
, finddata
) )
111 // As we return the invalid handle from here to indicate that we
112 // didn't find anything, close the one we initially received
116 return INVALID_HANDLE_VALUE
;
123 inline FIND_ATTR
GetAttrFromFindData(FIND_STRUCT
*finddata
)
125 return finddata
->dwFileAttributes
;
128 inline bool IsDir(FIND_ATTR attr
)
130 return (attr
& FILE_ATTRIBUTE_DIRECTORY
) != 0;
133 inline bool IsHidden(FIND_ATTR attr
)
135 return (attr
& (FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
)) != 0;
138 } // anonymous namespace
140 // ----------------------------------------------------------------------------
142 // ----------------------------------------------------------------------------
145 #define MAX_PATH 260 // from VC++ headers
148 // ----------------------------------------------------------------------------
150 // ----------------------------------------------------------------------------
152 #define M_DIR ((wxDirData *)m_data)
154 // ----------------------------------------------------------------------------
156 // ----------------------------------------------------------------------------
158 // this class stores everything we need to enumerate the files
162 wxDirData(const wxString
& dirname
);
165 void SetFileSpec(const wxString
& filespec
) { m_filespec
= filespec
; }
166 void SetFlags(int flags
) { m_flags
= flags
; }
170 bool Read(wxString
*filename
);
172 const wxString
& GetName() const { return m_dirname
; }
175 FIND_DATA m_finddata
;
182 wxDECLARE_NO_COPY_CLASS(wxDirData
);
185 // ============================================================================
187 // ============================================================================
189 // ----------------------------------------------------------------------------
191 // ----------------------------------------------------------------------------
193 wxDirData::wxDirData(const wxString
& dirname
)
196 m_finddata
= InitFindData();
199 wxDirData::~wxDirData()
204 void wxDirData::Close()
206 if ( IsFindDataOk(m_finddata
) )
208 FreeFindData(m_finddata
);
210 m_finddata
= InitFindData();
214 void wxDirData::Rewind()
219 bool wxDirData::Read(wxString
*filename
)
223 WIN32_FIND_DATA finddata
;
224 #define PTR_TO_FINDDATA (&finddata)
226 if ( !IsFindDataOk(m_finddata
) )
229 wxString filespec
= m_dirname
;
230 if ( !wxEndsWithPathSeparator(filespec
) )
232 filespec
+= wxT('\\');
235 filespec
+= wxT("*.*");
237 filespec
+= m_filespec
;
239 m_finddata
= FindFirst(filespec
, m_filespec
, PTR_TO_FINDDATA
);
244 if ( !IsFindDataOk(m_finddata
) )
247 DWORD err
= ::GetLastError();
249 if ( err
!= ERROR_FILE_NOT_FOUND
&& err
!= ERROR_NO_MORE_FILES
)
251 wxLogSysError(err
, _("Cannot enumerate files in directory '%s'"),
255 //else: not an error, just no (such) files
271 if ( !FindNext(m_finddata
, m_filespec
, PTR_TO_FINDDATA
) )
274 DWORD err
= ::GetLastError();
276 if ( err
!= ERROR_NO_MORE_FILES
)
278 wxLogLastError(wxT("FindNext"));
281 //else: not an error, just no more (such) files
287 name
= GetNameFromFindData(PTR_TO_FINDDATA
);
288 attr
= GetAttrFromFindData(PTR_TO_FINDDATA
);
290 // don't return "." and ".." unless asked for
291 if ( name
[0] == wxT('.') &&
292 ((name
[1] == wxT('.') && name
[2] == wxT('\0')) ||
293 (name
[1] == wxT('\0'))) )
295 if ( !(m_flags
& wxDIR_DOTDOT
) )
299 // check the type now
300 if ( !(m_flags
& wxDIR_FILES
) && !IsDir(attr
) )
302 // it's a file, but we don't want them
305 else if ( !(m_flags
& wxDIR_DIRS
) && IsDir(attr
) )
307 // it's a dir, and we don't want it
311 // finally, check whether it's a hidden file
312 if ( !(m_flags
& wxDIR_HIDDEN
) )
314 if ( IsHidden(attr
) )
316 // it's a hidden file, skip it
329 // ----------------------------------------------------------------------------
330 // wxDir construction/destruction
331 // ----------------------------------------------------------------------------
333 wxDir::wxDir(const wxString
& dirname
)
340 bool wxDir::Open(const wxString
& dirname
)
344 // The Unix code does a similar test
345 if (wxDirExists(dirname
))
347 m_data
= new wxDirData(dirname
);
359 bool wxDir::IsOpened() const
361 return m_data
!= NULL
;
364 wxString
wxDir::GetName() const
369 name
= M_DIR
->GetName();
372 // bring to canonical Windows form
373 name
.Replace(wxT("/"), wxT("\\"));
375 if ( name
.Last() == wxT('\\') )
377 // chop off the last (back)slash
378 name
.Truncate(name
.length() - 1);
395 // ----------------------------------------------------------------------------
397 // ----------------------------------------------------------------------------
399 bool wxDir::GetFirst(wxString
*filename
,
400 const wxString
& filespec
,
403 wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") );
407 M_DIR
->SetFileSpec(filespec
);
408 M_DIR
->SetFlags(flags
);
410 return GetNext(filename
);
413 bool wxDir::GetNext(wxString
*filename
) const
415 wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") );
417 wxCHECK_MSG( filename
, false, wxT("bad pointer in wxDir::GetNext()") );
419 return M_DIR
->Read(filename
);
422 // ----------------------------------------------------------------------------
423 // wxGetDirectoryTimes: used by wxFileName::GetTimes()
424 // ----------------------------------------------------------------------------
429 wxGetDirectoryTimes(const wxString
& dirname
,
430 FILETIME
*ftAccess
, FILETIME
*ftCreate
, FILETIME
*ftMod
)
433 // FindFirst() is going to fail
434 wxASSERT_MSG( !dirname
.empty(),
435 wxT("incorrect directory name format in wxGetDirectoryTimes") );
437 // FindFirst() is going to fail
438 wxASSERT_MSG( !dirname
.empty() && dirname
.Last() != wxT('\\'),
439 wxT("incorrect directory name format in wxGetDirectoryTimes") );
443 FIND_DATA fd
= FindFirst(dirname
, wxEmptyString
, &fs
);
444 if ( !IsFindDataOk(fd
) )
449 *ftAccess
= fs
.ftLastAccessTime
;
450 *ftCreate
= fs
.ftCreationTime
;
451 *ftMod
= fs
.ftLastWriteTime
;