Browse Source

Manager: Start working on dotfile class

master
Riyyi 3 years ago
parent
commit
1893f08ea8
  1. 75
      src/dotfile.cpp
  2. 46
      src/dotfile.h

75
src/dotfile.cpp

@ -0,0 +1,75 @@
/*
* Copyright (C) 2021 Riyyi
*
* SPDX-License-Identifier: MIT
*/
#include <cstdio> // fprintf, printf
#include <filesystem>
#include <string>
#include <vector>
#include "dotfile.h"
namespace Util {
std::vector<Dotfile::ExcludePath> 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

46
src/dotfile.h

@ -0,0 +1,46 @@
/*
* Copyright (C) 2021 Riyyi
*
* SPDX-License-Identifier: MIT
*/
#ifndef DOTFILE_H
#define DOTFILE_H
#include <filesystem>
#include <string>
#include <vector>
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<ExcludePath>& excludePaths) { s_excludePaths = excludePaths; }
private:
static bool filter(const std::filesystem::path& path);
static std::vector<ExcludePath> s_excludePaths;
static std::filesystem::path s_workingDirectory;
};
} // namespace Util
#endif // DOTFILE_H
Loading…
Cancel
Save