From 22c629d6672f49793c650f4d4840eb4fa0728185 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 29 Sep 2023 16:36:47 +0200 Subject: [PATCH] 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. --- src/msw/dir.cpp | 23 ++++++----------------- tests/file/dir.cpp | 5 ++++- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/src/msw/dir.cpp b/src/msw/dir.cpp index 7d175d3467..7254b6553c 100644 --- a/src/msw/dir.cpp +++ b/src/msw/dir.cpp @@ -28,6 +28,11 @@ #include "wx/dir.h" #include "wx/msw/private.h" +#include + +#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 diff --git a/tests/file/dir.cpp b/tests/file/dir.cpp index 5d11f80a52..b15fed7f89 100644 --- a/tests/file/dir.cpp +++ b/tests/file/dir.cpp @@ -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 );