diff options
| author | Jules Blok <jules.blok@gmail.com> | 2015-06-10 00:00:12 +0200 |
|---|---|---|
| committer | Jules Blok <jules.blok@gmail.com> | 2015-06-10 00:00:12 +0200 |
| commit | 7dfced21a2e5aac7195e0e1ad76468e30306766b (patch) | |
| tree | bd671b639d3d911460b767caabad8fc8a759a6bf /Source/Core/Common/FileSearch.cpp | |
| parent | c25be031fc1031d795157d8344b87b7841145197 (diff) | |
| parent | 7b0a65e295c784f9d573f2ef52d4e2a7b9bb2889 (diff) | |
Merge branch 'master' into stable
Diffstat (limited to 'Source/Core/Common/FileSearch.cpp')
| -rw-r--r-- | Source/Core/Common/FileSearch.cpp | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/Source/Core/Common/FileSearch.cpp b/Source/Core/Common/FileSearch.cpp new file mode 100644 index 0000000000..13e99fe88a --- /dev/null +++ b/Source/Core/Common/FileSearch.cpp @@ -0,0 +1,61 @@ +// Copyright 2008 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#if !__clang__ && __GNUC__ == 4 && __GNUC_MINOR__ < 9 +#error <regex> is broken in GCC < 4.9; please upgrade +#endif + +#include <algorithm> +#include <functional> +#include <regex> + +#include "Common/CommonPaths.h" +#include "Common/FileSearch.h" +#include "Common/FileUtil.h" + +static std::vector<std::string> FileSearchWithTest(const std::vector<std::string>& directories, bool recursive, std::function<bool(const File::FSTEntry &)> callback) +{ + std::vector<std::string> result; + for (const std::string& directory : directories) + { + File::FSTEntry top = File::ScanDirectoryTree(directory, recursive); + + std::function<void(File::FSTEntry&)> DoEntry; + DoEntry = [&](File::FSTEntry& entry) { + if (callback(entry)) + result.push_back(entry.physicalName); + for (auto& child : entry.children) + DoEntry(child); + }; + DoEntry(top); + } + // remove duplicates + std::sort(result.begin(), result.end()); + result.erase(std::unique(result.begin(), result.end()), result.end()); + return result; +} + +std::vector<std::string> DoFileSearch(const std::vector<std::string>& globs, const std::vector<std::string>& directories, bool recursive) +{ + std::string regex_str = "^("; + for (const auto& str : globs) + { + if (regex_str.size() != 2) + regex_str += "|"; + // convert glob to regex + regex_str += std::regex_replace(std::regex_replace(str, std::regex("\\."), "\\."), std::regex("\\*"), ".*"); + } + regex_str += ")$"; + std::regex regex(regex_str, std::regex_constants::icase); + return FileSearchWithTest(directories, recursive, [&](const File::FSTEntry& entry) { + return std::regex_match(entry.virtualName, regex); + }); +} + +std::vector<std::string> FindSubdirectories(const std::vector<std::string>& directories, bool recursive) +{ + return FileSearchWithTest(directories, true, [&](const File::FSTEntry& entry) { + return entry.isDirectory; + }); +} |
