From 9cc6ceddf25153919f5d0aa5f5fc020ac0402dd7 Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 11 Sep 2022 16:14:08 +0200 Subject: Extracted fnmatch filtering to a higher-order func --- ArchiveEx/dat.cpp | 30 +++++++++++++++++++----------- 1 file 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 #include #include +#include #include #include #include @@ -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& patterns, std::function func) -> + std::function; 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 +FilterFile(const std::vector& patterns, std::function 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); + } + }; +} -- cgit v1.1