Browse Source

Manager: Add generic dotfile looping

master
Riyyi 3 years ago
parent
commit
7920b7280e
  1. 16
      src/dotfile.cpp
  2. 3
      src/dotfile.h

16
src/dotfile.cpp

@ -8,6 +8,7 @@
#include <cstddef> // size_t #include <cstddef> // size_t
#include <cstdio> // fprintf, printf, stderr #include <cstdio> // fprintf, printf, stderr
#include <filesystem> #include <filesystem>
#include <functional> // function
#include <pwd.h> // getpwnam #include <pwd.h> // getpwnam
#include <string> #include <string>
#include <system_error> // error_code #include <system_error> // error_code
@ -160,7 +161,16 @@ void Dotfile::list(const std::vector<std::string>& targets)
return; 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<std::string>& targets, const std::function<void(std::filesystem::directory_entry, size_t)>& callback)
{
size_t workingDirectory = s_workingDirectory.string().size();
for (const auto& path : std::filesystem::recursive_directory_iterator { s_workingDirectory }) { for (const auto& path : std::filesystem::recursive_directory_iterator { s_workingDirectory }) {
if (path.is_directory() || filter(path)) { if (path.is_directory() || filter(path)) {
continue; continue;
@ -168,12 +178,10 @@ void Dotfile::list(const std::vector<std::string>& targets)
if (!targets.empty() && !include(path.path().string(), targets)) { if (!targets.empty() && !include(path.path().string(), targets)) {
continue; continue;
} }
printf("%s\n", path.path().c_str() + workingDirectory); callback(path, workingDirectory);
} }
} }
// -----------------------------------------
bool Dotfile::filter(const std::filesystem::path& path) bool Dotfile::filter(const std::filesystem::path& path)
{ {
for (auto& excludePath : s_excludePaths) { for (auto& excludePath : s_excludePaths) {

3
src/dotfile.h

@ -7,7 +7,9 @@
#ifndef DOTFILE_H #ifndef DOTFILE_H
#define DOTFILE_H #define DOTFILE_H
#include <cstddef> // size_t
#include <filesystem> #include <filesystem>
#include <functional> // function
#include <string> #include <string>
#include <vector> #include <vector>
@ -37,6 +39,7 @@ public:
static void setExcludePaths(const std::vector<ExcludePath>& excludePaths) { s_excludePaths = excludePaths; } static void setExcludePaths(const std::vector<ExcludePath>& excludePaths) { s_excludePaths = excludePaths; }
private: private:
static void forEachDotfile(const std::vector<std::string>& targets, const std::function<void(std::filesystem::directory_entry, size_t)>& callback);
static bool filter(const std::filesystem::path& path); static bool filter(const std::filesystem::path& path);
static bool include(const std::filesystem::path& path, const std::vector<std::string>& targets); static bool include(const std::filesystem::path& path, const std::vector<std::string>& targets);

Loading…
Cancel
Save