From 7920b7280e0b865ba69b9ceef4f7f35e05107506 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Wed, 22 Sep 2021 23:43:37 +0200 Subject: [PATCH] Manager: Add generic dotfile looping --- src/dotfile.cpp | 20 ++++++++++++++------ src/dotfile.h | 3 +++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/dotfile.cpp b/src/dotfile.cpp index 272db8f..baaa643 100644 --- a/src/dotfile.cpp +++ b/src/dotfile.cpp @@ -8,7 +8,8 @@ #include // size_t #include // fprintf, printf, stderr #include -#include // getpwnam +#include // function +#include // getpwnam #include #include // error_code #include // geteuid, getlogin, setegid, seteuid @@ -114,7 +115,7 @@ void Dotfile::add(const std::vector& targets) std::error_code error; if (std::filesystem::is_regular_file(from)) { auto directory = to.relative_path().parent_path(); - if (!directory.empty() && !std::filesystem::exists(directory) ) { + if (!directory.empty() && !std::filesystem::exists(directory)) { printf("created directory '%s'\n", directory.c_str()); std::filesystem::create_directories(directory, error); printError(to.relative_path().parent_path(), error); @@ -160,7 +161,16 @@ void Dotfile::list(const std::vector& targets) return; } - size_t workingDirectory = s_workingDirectory.string().size() + 1; + forEachDotfile(targets, [](std::filesystem::directory_entry path, size_t workingDirectory) { + printf("%s\n", path.path().c_str() + workingDirectory + 1); + }); +} + +// ----------------------------------------- + +void Dotfile::forEachDotfile(const std::vector& targets, const std::function& callback) +{ + size_t workingDirectory = s_workingDirectory.string().size(); for (const auto& path : std::filesystem::recursive_directory_iterator { s_workingDirectory }) { if (path.is_directory() || filter(path)) { continue; @@ -168,12 +178,10 @@ void Dotfile::list(const std::vector& targets) if (!targets.empty() && !include(path.path().string(), targets)) { continue; } - printf("%s\n", path.path().c_str() + workingDirectory); + callback(path, workingDirectory); } } -// ----------------------------------------- - bool Dotfile::filter(const std::filesystem::path& path) { for (auto& excludePath : s_excludePaths) { diff --git a/src/dotfile.h b/src/dotfile.h index 609f269..eedab9e 100644 --- a/src/dotfile.h +++ b/src/dotfile.h @@ -7,7 +7,9 @@ #ifndef DOTFILE_H #define DOTFILE_H +#include // size_t #include +#include // function #include #include @@ -37,6 +39,7 @@ public: static void setExcludePaths(const std::vector& excludePaths) { s_excludePaths = excludePaths; } private: + static void forEachDotfile(const std::vector& targets, const std::function& callback); static bool filter(const std::filesystem::path& path); static bool include(const std::filesystem::path& path, const std::vector& targets);