From 03d6d34396237081536ed00b8be99bbcdc5aafc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 2 Oct 2016 13:02:29 +0200 Subject: [PATCH] Fixed #4399 (Exclude directory with absolute path does not work) --- gui/filelist.cpp | 34 ++++++++++++++++++---------------- gui/filelist.h | 7 ------- lib/pathmatch.cpp | 22 +++++++++++----------- lib/pathmatch.h | 6 +++--- 4 files changed, 32 insertions(+), 37 deletions(-) diff --git a/gui/filelist.cpp b/gui/filelist.cpp index f3c0d3bf6..eae24f23f 100644 --- a/gui/filelist.cpp +++ b/gui/filelist.cpp @@ -21,6 +21,8 @@ #include #include #include "filelist.h" +#include "path.h" +#include "pathmatch.h" QStringList FileList::GetDefaultFilters() { @@ -106,28 +108,28 @@ void FileList::AddExcludeList(const QStringList &paths) mExcludedPaths = paths; } +static std::vector toStdStringList(const QStringList &stringList) +{ + std::vector ret; + foreach (const QString &s, stringList) { + ret.push_back(s.toStdString()); + } + return ret; +} + QStringList FileList::ApplyExcludeList() const { +#ifdef _WIN32 + const PathMatch pathMatch(toStdStringList(mExcludedPaths), true); +#else + const PathMatch pathMatch(toStdStringList(mExcludedPaths), false); +#endif + QStringList paths; foreach (QFileInfo item, mFileList) { QString name = QDir::fromNativeSeparators(item.canonicalFilePath()); - if (!Match(name)) + if (!pathMatch.Match(name.toStdString())) paths << name; } return paths; } - -bool FileList::Match(const QString &path) const -{ - for (int i = 0; i < mExcludedPaths.size(); i++) { - if (mExcludedPaths[i].endsWith('/')) { - const QString pathexclude("/" + mExcludedPaths[i]); - if (path.indexOf(pathexclude) != -1) - return true; - } else { - if (path.endsWith(mExcludedPaths[i])) - return true; - } - } - return false; -} diff --git a/gui/filelist.h b/gui/filelist.h index cc6324d6b..dec842d7f 100644 --- a/gui/filelist.h +++ b/gui/filelist.h @@ -91,13 +91,6 @@ protected: */ QStringList ApplyExcludeList() const; - /** - * @brief Test if path matches any of the exclude filters. - * @param path Path to test against exclude filters. - * @return true if any of the filters matches, false otherwise. - */ - bool Match(const QString &path) const; - private: QFileInfoList mFileList; QStringList mExcludedPaths; diff --git a/lib/pathmatch.cpp b/lib/pathmatch.cpp index 3b8e2635d..c981e97ab 100644 --- a/lib/pathmatch.cpp +++ b/lib/pathmatch.cpp @@ -21,11 +21,11 @@ #include #include -PathMatch::PathMatch(const std::vector &masks, bool caseSensitive) - : _masks(masks), _caseSensitive(caseSensitive) +PathMatch::PathMatch(const std::vector &excludedPaths, bool caseSensitive) + : _excludedPaths(excludedPaths), _caseSensitive(caseSensitive) { if (!_caseSensitive) - for (std::vector::iterator i = _masks.begin(); i != _masks.end(); ++i) + for (std::vector::iterator i = _excludedPaths.begin(); i != _excludedPaths.end(); ++i) std::transform(i->begin(), i->end(), i->begin(), ::tolower); } @@ -37,38 +37,38 @@ bool PathMatch::Match(const std::string &path) const std::vector workingDirectory; workingDirectory.push_back(Path::getCurrentPath()); - for (std::vector::const_iterator iterMask = _masks.begin(); iterMask != _masks.end(); ++iterMask) { - const std::string mask((!Path::isAbsolute(path) && Path::isAbsolute(*iterMask)) ? Path::getRelativePath(*iterMask, workingDirectory) : *iterMask); + for (std::vector::const_iterator i = _excludedPaths.begin(); i != _excludedPaths.end(); ++i) { + const std::string excludedPath((!Path::isAbsolute(path) && Path::isAbsolute(*i)) ? Path::getRelativePath(*i, workingDirectory) : *i); std::string findpath = Path::fromNativeSeparators(path); if (!_caseSensitive) std::transform(findpath.begin(), findpath.end(), findpath.begin(), ::tolower); // Filtering directory name - if (mask[mask.length() - 1] == '/') { + if (excludedPath[excludedPath.length() - 1] == '/') { if (findpath[findpath.length() - 1] != '/') findpath = RemoveFilename(findpath); - if (mask.length() > findpath.length()) + if (excludedPath.length() > findpath.length()) continue; // Match relative paths starting with mask // -isrc matches src/foo.cpp - if (findpath.compare(0, mask.size(), mask) == 0) + if (findpath.compare(0, excludedPath.size(), excludedPath) == 0) return true; // Match only full directory name in middle or end of the path // -isrc matches myproject/src/ but does not match // myproject/srcfiles/ or myproject/mysrc/ - if (findpath.find("/" + mask) != std::string::npos) + if (findpath.find("/" + excludedPath) != std::string::npos) return true; } // Filtering filename else { - if (mask.length() > findpath.length()) + if (excludedPath.length() > findpath.length()) continue; // Check if path ends with mask // -ifoo.cpp matches (./)foo.c, src/foo.cpp and proj/src/foo.cpp // -isrc/file.cpp matches src/foo.cpp and proj/src/foo.cpp - if (findpath.compare(findpath.size() - mask.size(), findpath.size(), mask) == 0) + if (findpath.compare(findpath.size() - excludedPath.size(), findpath.size(), excludedPath) == 0) return true; } diff --git a/lib/pathmatch.h b/lib/pathmatch.h index 04b7fca81..6b5607c52 100644 --- a/lib/pathmatch.h +++ b/lib/pathmatch.h @@ -33,11 +33,11 @@ public: /** * The constructor. - * @param masks List of masks. + * @param excludedPaths List of masks. * @param caseSensitive Match the case of the characters when * matching paths? */ - explicit PathMatch(const std::vector &masks, bool caseSensitive = true); + explicit PathMatch(const std::vector &excludedPaths, bool caseSensitive = true); /** * @brief Match path against list of masks. @@ -56,7 +56,7 @@ protected: static std::string RemoveFilename(const std::string &path); private: - std::vector _masks; + std::vector _excludedPaths; bool _caseSensitive; };