diff options
Diffstat (limited to 'ArchiveEx')
-rw-r--r-- | ArchiveEx/dat.cpp | 30 |
1 files changed, 19 insertions, 11 deletions
diff --git a/ArchiveEx/dat.cpp b/ArchiveEx/dat.cpp index 100f222..88bf973 100644 --- a/ArchiveEx/dat.cpp +++ b/ArchiveEx/dat.cpp @@ -5,6 +5,7 @@ #include <exception> #include <filesystem> #include <fstream> +#include <functional> #include <iostream> #include <memory> #include <ostream> @@ -53,6 +54,8 @@ class ExtractError : std::exception {}; static Options ParseArgs(int argc, char* argv[]); static void PrintUsage(std::ostream& out=std::cerr); static void PrintHelp(std::ostream& out=std::cout); +static auto FilterFile(const std::vector<std::string>& patterns, std::function<void(const char*)> func) -> + std::function<void(const char*)>; static void UpdateFile(ArchiveEx::Archive& archive, const std::string& path, bool warn=true); static void ExtractFile(ArchiveEx::Archive& archive, const char* filepath); @@ -86,17 +89,7 @@ try { UpdateFile(archive, file); break; case Action::EXTRACT: - archive.ForEachEntry([opts, &archive](const char* name){ - if (opts.files.empty()) - return ExtractFile(archive, name); - for (const auto& pattern : opts.files) { - const int match = fnmatch(pattern.c_str(), name, FNM_PATHNAME | FNM_LEADING_DIR); - if (match == 0) { - ExtractFile(archive, name); - break; - } - } - }); + archive.ForEachEntry(FilterFile(opts.files, [&archive](const char* name){ ExtractFile(archive, name); })); break; } } @@ -216,3 +209,18 @@ try { catch (const ExtractError& err) { std::cerr << "could not extract: " << filepath << std::endl; } + + +std::function<void(const char*)> +FilterFile(const std::vector<std::string>& patterns, std::function<void(const char*)> func) +{ + return [&patterns, func](const char* name) -> void { + if (patterns.empty()) + return func(name); + for (const auto& pattern : patterns) { + const int match = fnmatch(pattern.c_str(), name, FNM_PATHNAME | FNM_LEADING_DIR); + if (match == 0) + return func(name); + } + }; +} |