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 // If there is no filter, the found file must be the one we really are
85 // Otherwise do check the match validity. Notice that we must do it
86 // case-insensitively because the case of the file names is not supposed to
87 // matter under Windows.
88 wxString
fn(GetNameFromFindData(finddata
));
90 // However if the filter contains only special characters (which is a
91 // common case), we can skip the case conversion.
92 if ( filter
.find_first_not_of(wxS("*?.")) == wxString::npos
)
93 return fn
.Matches(filter
);
95 return fn
.MakeUpper().Matches(filter
.Upper());
99 FindNext(FIND_DATA fd
, const wxString
& filter
, FIND_STRUCT
*finddata
)
103 if ( !::FindNextFile(fd
, finddata
) )
106 // If we did find something, check that it really matches.
107 if ( CheckFoundMatch(finddata
, filter
) )
113 FindFirst(const wxString
& spec
,
114 const wxString
& filter
,
115 FIND_STRUCT
*finddata
)
117 FIND_DATA fd
= ::FindFirstFile(spec
.t_str(), finddata
);
119 // As in FindNext() above, we need to check that the file name we found
120 // really matches our filter and look for the next match if it doesn't.
121 if ( IsFindDataOk(fd
) && !CheckFoundMatch(finddata
, filter
) )
123 if ( !FindNext(fd
, filter
, finddata
) )
125 // As we return the invalid handle from here to indicate that we
126 // didn't find anything, close the one we initially received
130 return INVALID_HANDLE_VALUE
;
137 inline FIND_ATTR
GetAttrFromFindData(FIND_STRUCT
*finddata
)
139 return finddata
->dwFileAttributes
;
142 inline bool IsDir(FIND_ATTR attr
)
144 return (attr
& FILE_ATTRIBUTE_DIRECTORY
) != 0;
147 inline bool IsHidden(FIND_ATTR attr
)
149 return (attr
& (FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
)) != 0;
152 } // anonymous namespace
154 // ----------------------------------------------------------------------------
156 // ----------------------------------------------------------------------------
159 #define MAX_PATH 260 // from VC++ headers
162 // ----------------------------------------------------------------------------
164 // ----------------------------------------------------------------------------
166 #define M_DIR ((wxDirData *)m_data)
168 // ----------------------------------------------------------------------------
170 // ----------------------------------------------------------------------------
172 // this class stores everything we need to enumerate the files
176 wxDirData(const wxString
& dirname
);
179 void SetFileSpec(const wxString
& filespec
) { m_filespec
= filespec
; }
180 void SetFlags(int flags
) { m_flags
= flags
; }
184 bool Read(wxString
*filename
);
186 const wxString
& GetName() const { return m_dirname
; }
189 FIND_DATA m_finddata
;
196 wxDECLARE_NO_COPY_CLASS(wxDirData
);
199 // ============================================================================
201 // ============================================================================
203 // ----------------------------------------------------------------------------
205 // ----------------------------------------------------------------------------
207 wxDirData::wxDirData(const wxString
& dirname
)
210 m_finddata
= InitFindData();
213 wxDirData::~wxDirData()
218 void wxDirData::Close()
220 if ( IsFindDataOk(m_finddata
) )
222 FreeFindData(m_finddata
);
224 m_finddata
= InitFindData();
228 void wxDirData::Rewind()
233 bool wxDirData::Read(wxString
*filename
)
237 WIN32_FIND_DATA finddata
;
238 #define PTR_TO_FINDDATA (&finddata)
240 if ( !IsFindDataOk(m_finddata
) )
243 wxString filespec
= m_dirname
;
244 if ( !wxEndsWithPathSeparator(filespec
) )
246 filespec
+= wxT('\\');
249 filespec
+= wxT("*.*");
251 filespec
+= m_filespec
;
253 m_finddata
= FindFirst(filespec
, m_filespec
, PTR_TO_FINDDATA
);
258 if ( !IsFindDataOk(m_finddata
) )
261 DWORD err
= ::GetLastError();
263 if ( err
!= ERROR_FILE_NOT_FOUND
&& err
!= ERROR_NO_MORE_FILES
)
265 wxLogSysError(err
, _("Cannot enumerate files in directory '%s'"),
269 //else: not an error, just no (such) files
285 if ( !FindNext(m_finddata
, m_filespec
, PTR_TO_FINDDATA
) )
288 DWORD err
= ::GetLastError();
290 if ( err
!= ERROR_NO_MORE_FILES
)
292 wxLogLastError(wxT("FindNext"));
295 //else: not an error, just no more (such) files
301 name
= GetNameFromFindData(PTR_TO_FINDDATA
);
302 attr
= GetAttrFromFindData(PTR_TO_FINDDATA
);
304 // don't return "." and ".." unless asked for
305 if ( name
[0] == wxT('.') &&
306 ((name
[1] == wxT('.') && name
[2] == wxT('\0')) ||
307 (name
[1] == wxT('\0'))) )
309 if ( !(m_flags
& wxDIR_DOTDOT
) )
313 // check the type now
314 if ( !(m_flags
& wxDIR_FILES
) && !IsDir(attr
) )
316 // it's a file, but we don't want them
319 else if ( !(m_flags
& wxDIR_DIRS
) && IsDir(attr
) )
321 // it's a dir, and we don't want it
325 // finally, check whether it's a hidden file
326 if ( !(m_flags
& wxDIR_HIDDEN
) )
328 if ( IsHidden(attr
) )
330 // it's a hidden file, skip it
343 // ----------------------------------------------------------------------------
344 // wxDir construction/destruction
345 // ----------------------------------------------------------------------------
347 wxDir::wxDir(const wxString
& dirname
)
354 bool wxDir::Open(const wxString
& dirname
)
358 // The Unix code does a similar test
359 if (wxDirExists(dirname
))
361 m_data
= new wxDirData(dirname
);
373 bool wxDir::IsOpened() const
375 return m_data
!= NULL
;
378 wxString
wxDir::GetName() const
383 name
= M_DIR
->GetName();
386 // bring to canonical Windows form
387 name
.Replace(wxT("/"), wxT("\\"));
389 if ( name
.Last() == wxT('\\') )
391 // chop off the last (back)slash
392 name
.Truncate(name
.length() - 1);
409 // ----------------------------------------------------------------------------
411 // ----------------------------------------------------------------------------
413 bool wxDir::GetFirst(wxString
*filename
,
414 const wxString
& filespec
,
417 wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") );
421 M_DIR
->SetFileSpec(filespec
);
422 M_DIR
->SetFlags(flags
);
424 return GetNext(filename
);
427 bool wxDir::GetNext(wxString
*filename
) const
429 wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") );
431 wxCHECK_MSG( filename
, false, wxT("bad pointer in wxDir::GetNext()") );
433 return M_DIR
->Read(filename
);
436 // ----------------------------------------------------------------------------
437 // wxGetDirectoryTimes: used by wxFileName::GetTimes()
438 // ----------------------------------------------------------------------------
443 wxGetDirectoryTimes(const wxString
& dirname
,
444 FILETIME
*ftAccess
, FILETIME
*ftCreate
, FILETIME
*ftMod
)
447 // FindFirst() is going to fail
448 wxASSERT_MSG( !dirname
.empty(),
449 wxT("incorrect directory name format in wxGetDirectoryTimes") );
451 // FindFirst() is going to fail
452 wxASSERT_MSG( !dirname
.empty() && dirname
.Last() != wxT('\\'),
453 wxT("incorrect directory name format in wxGetDirectoryTimes") );
457 FIND_DATA fd
= FindFirst(dirname
, wxEmptyString
, &fs
);
458 if ( !IsFindDataOk(fd
) )
463 *ftAccess
= fs
.ftLastAccessTime
;
464 *ftCreate
= fs
.ftCreationTime
;
465 *ftMod
= fs
.ftLastWriteTime
;