diff --git a/src/dotfile.cpp b/src/dotfile.cpp new file mode 100644 index 0000000..cd048cb --- /dev/null +++ b/src/dotfile.cpp @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2021 Riyyi + * + * SPDX-License-Identifier: MIT + */ + +#include // fprintf, printf +#include +#include +#include + +#include "dotfile.h" + +namespace Util { + +std::vector Dotfile::s_excludePaths; +std::filesystem::path Dotfile::s_workingDirectory; + +Dotfile::Dotfile() +{ +} + +Dotfile::~Dotfile() +{ +} + +// ----------------------------------------- + +void Dotfile::list() +{ + if (s_workingDirectory.empty()) { + fprintf(stderr, "\033[31;1mDotfile:\033[0m working directory is unset\n"); + return; + } + + if (!std::filesystem::is_directory(s_workingDirectory)) { + fprintf(stderr, "\033[31;1mDotfile:\033[0m working directory is not a directory\n"); + return; + } + + size_t workingDirectory = s_workingDirectory.string().size() + 1; + for (const auto& path : std::filesystem::recursive_directory_iterator { s_workingDirectory }) { + if (path.is_directory() || filter(path)) { + continue; + } + printf("%s\n", path.path().c_str() + workingDirectory); + } +} + +// ----------------------------------------- + +bool Dotfile::filter(const std::filesystem::path& path) +{ + for (auto& excludePath : s_excludePaths) { + if (excludePath.type == ExcludeType::File) { + if (path.string() == s_workingDirectory / excludePath.path) { + return true; + } + } + else if (excludePath.type == ExcludeType::Directory) { + if (path.string().find(s_workingDirectory / excludePath.path) == 0) { + return true; + } + } + else if (excludePath.type == ExcludeType::EndsWith) { + if (path.string().find(excludePath.path) == path.string().size() - excludePath.path.size()) { + return true; + } + } + } + + return false; +} + +} // namespace Util diff --git a/src/dotfile.h b/src/dotfile.h new file mode 100644 index 0000000..ae12d37 --- /dev/null +++ b/src/dotfile.h @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2021 Riyyi + * + * SPDX-License-Identifier: MIT + */ + +#ifndef DOTFILE_H +#define DOTFILE_H + +#include +#include +#include + +namespace Util { + +class Dotfile { +public: + Dotfile(); + virtual ~Dotfile(); + + enum class ExcludeType { + File, + Directory, + EndsWith, + }; + + struct ExcludePath { + ExcludeType type; + std::string path; + }; + + static void list(); + + static void setWorkingDirectory(std::filesystem::path directory) { s_workingDirectory = directory; } + static void setExcludePaths(const std::vector& excludePaths) { s_excludePaths = excludePaths; } + +private: + static bool filter(const std::filesystem::path& path); + + static std::vector s_excludePaths; + static std::filesystem::path s_workingDirectory; +}; + +} // namespace Util + +#endif // DOTFILE_H