X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/9a83f860948059b0273b5cc6d9e43fadad3ebfca..9ed99f82da680a40becbb4e72d03a30d4acb335e:/src/msw/dir.cpp diff --git a/src/msw/dir.cpp b/src/msw/dir.cpp index 5404038be8..0867811c70 100644 --- a/src/msw/dir.cpp +++ b/src/msw/dir.cpp @@ -1,10 +1,9 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: msw/dir.cpp +// Name: src/msw/dir.cpp // Purpose: wxDir implementation for Win32 // Author: Vadim Zeitlin // Modified by: // Created: 08.12.99 -// RCS-ID: $Id$ // Copyright: (c) 1999 Vadim Zeitlin // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// @@ -30,7 +29,6 @@ #endif // PCH #include "wx/dir.h" -#include "wx/filefn.h" // for wxDirExists() #ifdef __WINDOWS__ #include "wx/msw/private.h" @@ -65,20 +63,74 @@ inline void FreeFindData(FIND_DATA fd) } } -inline FIND_DATA FindFirst(const wxString& spec, - FIND_STRUCT *finddata) +const wxChar *GetNameFromFindData(const FIND_STRUCT *finddata) { - return ::FindFirstFile(spec.fn_str(), finddata); + return finddata->cFileName; } -inline bool FindNext(FIND_DATA fd, FIND_STRUCT *finddata) +// Helper function checking that the contents of the given FIND_STRUCT really +// match our filter. We need to do it ourselves as native Windows functions +// apply the filter to both the long and the short names of the file, so +// something like "*.bar" matches "foo.bar.baz" too and not only "foo.bar", so +// we have to double check that we have a real match. +inline bool +CheckFoundMatch(const FIND_STRUCT* finddata, const wxString& filter) { - return ::FindNextFile(fd, finddata) != 0; + // If there is no filter, the found file must be the one we really are + // looking for. + if ( filter.empty() ) + return true; + + // Otherwise do check the match validity. Notice that we must do it + // case-insensitively because the case of the file names is not supposed to + // matter under Windows. + wxString fn(GetNameFromFindData(finddata)); + + // However if the filter contains only special characters (which is a + // common case), we can skip the case conversion. + if ( filter.find_first_not_of(wxS("*?.")) == wxString::npos ) + return fn.Matches(filter); + + return fn.MakeUpper().Matches(filter.Upper()); } -const wxChar *GetNameFromFindData(FIND_STRUCT *finddata) +inline bool +FindNext(FIND_DATA fd, const wxString& filter, FIND_STRUCT *finddata) { - return finddata->cFileName; + for ( ;; ) + { + if ( !::FindNextFile(fd, finddata) ) + return false; + + // If we did find something, check that it really matches. + if ( CheckFoundMatch(finddata, filter) ) + return true; + } +} + +inline FIND_DATA +FindFirst(const wxString& spec, + const wxString& filter, + FIND_STRUCT *finddata) +{ + FIND_DATA fd = ::FindFirstFile(spec.t_str(), finddata); + + // As in FindNext() above, we need to check that the file name we found + // really matches our filter and look for the next match if it doesn't. + if ( IsFindDataOk(fd) && !CheckFoundMatch(finddata, filter) ) + { + if ( !FindNext(fd, filter, finddata) ) + { + // As we return the invalid handle from here to indicate that we + // didn't find anything, close the one we initially received + // ourselves. + FreeFindData(fd); + + return INVALID_HANDLE_VALUE; + } + } + + return fd; } inline FIND_ATTR GetAttrFromFindData(FIND_STRUCT *finddata) @@ -197,7 +249,7 @@ bool wxDirData::Read(wxString *filename) else filespec += m_filespec; - m_finddata = FindFirst(filespec, PTR_TO_FINDDATA); + m_finddata = FindFirst(filespec, m_filespec, PTR_TO_FINDDATA); first = true; } @@ -209,7 +261,7 @@ bool wxDirData::Read(wxString *filename) if ( err != ERROR_FILE_NOT_FOUND && err != ERROR_NO_MORE_FILES ) { - wxLogSysError(err, _("Can not enumerate files in directory '%s'"), + wxLogSysError(err, _("Cannot enumerate files in directory '%s'"), m_dirname.c_str()); } #endif // __WIN32__ @@ -229,7 +281,7 @@ bool wxDirData::Read(wxString *filename) } else { - if ( !FindNext(m_finddata, PTR_TO_FINDDATA) ) + if ( !FindNext(m_finddata, m_filespec, PTR_TO_FINDDATA) ) { #ifdef __WIN32__ DWORD err = ::GetLastError(); @@ -287,16 +339,6 @@ bool wxDirData::Read(wxString *filename) return true; } -// ---------------------------------------------------------------------------- -// wxDir helpers -// ---------------------------------------------------------------------------- - -/* static */ -bool wxDir::Exists(const wxString& dir) -{ - return wxDirExists(dir); -} - // ---------------------------------------------------------------------------- // wxDir construction/destruction // ---------------------------------------------------------------------------- @@ -311,7 +353,7 @@ wxDir::wxDir(const wxString& dirname) bool wxDir::Open(const wxString& dirname) { delete M_DIR; - + // The Unix code does a similar test if (wxDirExists(dirname)) { @@ -322,7 +364,7 @@ bool wxDir::Open(const wxString& dirname) else { m_data = NULL; - + return false; } } @@ -354,9 +396,13 @@ wxString wxDir::GetName() const return name; } -wxDir::~wxDir() +void wxDir::Close() { - delete M_DIR; + if ( m_data ) + { + delete m_data; + m_data = NULL; + } } // ---------------------------------------------------------------------------- @@ -407,7 +453,7 @@ wxGetDirectoryTimes(const wxString& dirname, #endif FIND_STRUCT fs; - FIND_DATA fd = FindFirst(dirname, &fs); + FIND_DATA fd = FindFirst(dirname, wxEmptyString, &fs); if ( !IsFindDataOk(fd) ) { return false;