From d56765ac1b1500b801dd8051d213cea3a19b532f Mon Sep 17 00:00:00 2001 From: zv0n Date: Sat, 26 Feb 2022 12:22:38 +0100 Subject: [PATCH] Make the file depth logic a bit better hopefully --- fileobject.hpp | 17 +++++++++-------- functions.cpp | 18 ++++++++++-------- main.cpp | 4 ++-- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/fileobject.hpp b/fileobject.hpp index b4fcd7a..481921e 100644 --- a/fileobject.hpp +++ b/fileobject.hpp @@ -10,11 +10,8 @@ enum FileType { class FileObject { public: - FileObject( const std::string &name ) - : _name( name ) {} - - const std::string &getName() const { - return _name; + const std::string &getPath() const { + return _path; } const int &getDepth() const { @@ -33,16 +30,20 @@ public: _depth = depth; } + void setPath(const std::string &path) { + _path = path; + } + bool operator < (const FileObject &other) const { - return _name < other.getName(); + return _path < other.getPath(); } bool operator > (const FileObject &other) const { - return _name > other.getName(); + return _path > other.getPath(); } private: - std::string _name; + std::string _path = ""; int _depth = -1; FileType _type = TYPE_FILE; }; diff --git a/functions.cpp b/functions.cpp index db6004b..e067a14 100644 --- a/functions.cpp +++ b/functions.cpp @@ -60,26 +60,28 @@ void closeLibraries( std::vector< RenameLibrary > &libraries ) { void addFilesRecursive(const FileObject &parent, std::vector< FileObject > &results, const std::string &filename, const std::string &containing_directory, bool dir_only = false ) { auto path = containing_directory + FSLib::dir_divisor + filename; + FileObject fo{}; if(!dir_only || FSLib::isDirectory(path)) { - if(!parent.getName().empty()) { - results.emplace_back(parent.getName() + FSLib::dir_divisor + filename); + if(!parent.getPath().empty()) { + fo.setPath(parent.getPath() + FSLib::dir_divisor + filename); } else { - results.emplace_back(filename); + fo.setPath(filename); } - results.back().setDepth(parent.getDepth() + 1); + fo.setDepth(parent.getDepth() + 1); } if( FSLib::isDirectory(path) ) { - results.back().setFileType(TYPE_DIRECTORY); + fo.setFileType(TYPE_DIRECTORY); for(const auto &entry : FSLib::Directory(path)) { - addFilesRecursive(results.back(), results, entry, path, dir_only); + addFilesRecursive(fo, results, entry, path, dir_only); } } + results.push_back(std::move(fo)); } std::vector< FileObject > getFilesInSource( const std::string &source_dir ) { std::vector< FileObject > result; for(const auto &entry : FSLib::Directory(source_dir)) { - addFilesRecursive(FileObject(""), result, entry, source_dir); + addFilesRecursive(FileObject(), result, entry, source_dir); } std::sort(result.begin(), result.end()); return result; @@ -88,7 +90,7 @@ std::vector< FileObject > getFilesInSource( const std::string &source_dir ) { std::vector< FileObject > getTargetDirectories( const std::string &target_dir ) { std::vector< FileObject > result; for(const auto &entry : FSLib::Directory(target_dir)) { - addFilesRecursive(FileObject(""), result, entry, target_dir, true); + addFilesRecursive(FileObject(), result, entry, target_dir, true); } std::sort(result.begin(), result.end()); return result; diff --git a/main.cpp b/main.cpp index ca0941f..5833eea 100644 --- a/main.cpp +++ b/main.cpp @@ -235,7 +235,7 @@ std::string getFilesJson() { auto files = getFilesInSource(cfg.getSourcePath()); if(!files.empty()) { for(const auto &file : files) { - res << " {\n \"path\": \"" << safeJson(file.getName()) << "\",\n"; + res << " {\n \"path\": \"" << safeJson(file.getPath()) << "\",\n"; res << " \"depth\": " << file.getDepth() << ",\n"; res << " \"type\": \"" << (file.getFileType() == TYPE_FILE ? "file" : "directory") << "\"\n },\n"; } @@ -294,7 +294,7 @@ std::string getTargetDirectoriesJson(uint64_t id) { auto dirs = getTargetDirectories(cfg.getTargetPaths()[id].first); if(!dirs.empty()) { for(const auto &dir : dirs) { - res << " \"" << safeJson(dir.getName()) << "\",\n"; + res << " \"" << safeJson(dir.getPath()) << "\",\n"; } res.seekp(-2, std::ios_base::end); }