Make the file depth logic a bit better hopefully

This commit is contained in:
zv0n 2022-02-26 12:22:38 +01:00
parent 15047bd623
commit d56765ac1b
3 changed files with 21 additions and 18 deletions

View File

@ -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;
};

View File

@ -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;

View File

@ -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);
}