Fix matching "x*.*" in wxMSW wxDir too

Recent commit corrected handling of "*.*", which previously didn't match
the files without extensions, but still left handling of patterns such
as "x*.*" broken, because they are also supposed to match all files
starting with "x" under Windows, whether they have an extension or not.

Fix this by using PathMatchSpecEx() shell function which should handle
this correctly and update the unit test to check for this case as well.
This commit is contained in:
Vadim Zeitlin 2023-09-29 16:36:47 +02:00
parent f27688367a
commit 22c629d667
2 changed files with 10 additions and 18 deletions

View file

@ -28,6 +28,11 @@
#include "wx/dir.h"
#include "wx/msw/private.h"
#include <shlwapi.h>
#ifdef __VISUALC__
#pragma comment(lib, "shlwapi")
#endif
// ----------------------------------------------------------------------------
// define the types and functions used for file searching
@ -71,23 +76,7 @@ CheckFoundMatch(const FIND_STRUCT* finddata, const wxString& filter)
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(finddata.cFileName);
// 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 )
{
// And maybe even skip the check entirely if we have a filter that we
// know matches everything. This is more than just an optimization, as
// "*.*" it wouldn't match the files without extension according to
// wxString::Matches(), but it should.
return filter == wxS("*.*") || filter == wxS("*") || fn.Matches(filter);
}
return fn.MakeUpper().Matches(filter.Upper());
return ::PathMatchSpecEx(finddata->cFileName, filter, PMSF_NORMAL) == S_OK;
}
inline bool

View file

@ -22,7 +22,7 @@
// We can't use wxFileSelectorDefaultWildcardStr from wxCore here, so define
// our own.
const char WILDCARD_ALL[] =
const wxString WILDCARD_ALL =
#if defined(__WXMSW__)
"*.*"
#else // Unix/Mac
@ -158,6 +158,9 @@ TEST_CASE_METHOD(DirTestCase, "Dir::Traverse", "[dir]")
// enum all files using an explicit wildcard
CHECK(wxDir::GetAllFiles(DIRTEST_FOLDER, &files, WILDCARD_ALL) == 4);
// enum all files using an explicit wildcard different from WILDCARD_ALL
CHECK(wxDir::GetAllFiles(DIRTEST_FOLDER, &files, "d" + WILDCARD_ALL) == 4);
// enum all files according to the filter
CHECK( wxDir::GetAllFiles(DIRTEST_FOLDER, &files, "*.foo") == 1 );