Make the file depth logic a bit better hopefully
This commit is contained in:
parent
15047bd623
commit
d56765ac1b
@ -10,11 +10,8 @@ enum FileType {
|
|||||||
|
|
||||||
class FileObject {
|
class FileObject {
|
||||||
public:
|
public:
|
||||||
FileObject( const std::string &name )
|
const std::string &getPath() const {
|
||||||
: _name( name ) {}
|
return _path;
|
||||||
|
|
||||||
const std::string &getName() const {
|
|
||||||
return _name;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const int &getDepth() const {
|
const int &getDepth() const {
|
||||||
@ -33,16 +30,20 @@ public:
|
|||||||
_depth = depth;
|
_depth = depth;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setPath(const std::string &path) {
|
||||||
|
_path = path;
|
||||||
|
}
|
||||||
|
|
||||||
bool operator < (const FileObject &other) const {
|
bool operator < (const FileObject &other) const {
|
||||||
return _name < other.getName();
|
return _path < other.getPath();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator > (const FileObject &other) const {
|
bool operator > (const FileObject &other) const {
|
||||||
return _name > other.getName();
|
return _path > other.getPath();
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string _name;
|
std::string _path = "";
|
||||||
int _depth = -1;
|
int _depth = -1;
|
||||||
FileType _type = TYPE_FILE;
|
FileType _type = TYPE_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 ) {
|
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;
|
auto path = containing_directory + FSLib::dir_divisor + filename;
|
||||||
|
FileObject fo{};
|
||||||
if(!dir_only || FSLib::isDirectory(path)) {
|
if(!dir_only || FSLib::isDirectory(path)) {
|
||||||
if(!parent.getName().empty()) {
|
if(!parent.getPath().empty()) {
|
||||||
results.emplace_back(parent.getName() + FSLib::dir_divisor + filename);
|
fo.setPath(parent.getPath() + FSLib::dir_divisor + filename);
|
||||||
} else {
|
} else {
|
||||||
results.emplace_back(filename);
|
fo.setPath(filename);
|
||||||
}
|
}
|
||||||
results.back().setDepth(parent.getDepth() + 1);
|
fo.setDepth(parent.getDepth() + 1);
|
||||||
}
|
}
|
||||||
if( FSLib::isDirectory(path) ) {
|
if( FSLib::isDirectory(path) ) {
|
||||||
results.back().setFileType(TYPE_DIRECTORY);
|
fo.setFileType(TYPE_DIRECTORY);
|
||||||
for(const auto &entry : FSLib::Directory(path)) {
|
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 > getFilesInSource( const std::string &source_dir ) {
|
||||||
std::vector< FileObject > result;
|
std::vector< FileObject > result;
|
||||||
for(const auto &entry : FSLib::Directory(source_dir)) {
|
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());
|
std::sort(result.begin(), result.end());
|
||||||
return result;
|
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 > getTargetDirectories( const std::string &target_dir ) {
|
||||||
std::vector< FileObject > result;
|
std::vector< FileObject > result;
|
||||||
for(const auto &entry : FSLib::Directory(target_dir)) {
|
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());
|
std::sort(result.begin(), result.end());
|
||||||
return result;
|
return result;
|
||||||
|
4
main.cpp
4
main.cpp
@ -235,7 +235,7 @@ std::string getFilesJson() {
|
|||||||
auto files = getFilesInSource(cfg.getSourcePath());
|
auto files = getFilesInSource(cfg.getSourcePath());
|
||||||
if(!files.empty()) {
|
if(!files.empty()) {
|
||||||
for(const auto &file : files) {
|
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 << " \"depth\": " << file.getDepth() << ",\n";
|
||||||
res << " \"type\": \"" << (file.getFileType() == TYPE_FILE ? "file" : "directory") << "\"\n },\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);
|
auto dirs = getTargetDirectories(cfg.getTargetPaths()[id].first);
|
||||||
if(!dirs.empty()) {
|
if(!dirs.empty()) {
|
||||||
for(const auto &dir : dirs) {
|
for(const auto &dir : dirs) {
|
||||||
res << " \"" << safeJson(dir.getName()) << "\",\n";
|
res << " \"" << safeJson(dir.getPath()) << "\",\n";
|
||||||
}
|
}
|
||||||
res.seekp(-2, std::ios_base::end);
|
res.seekp(-2, std::ios_base::end);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user