diff --git a/src/bakatools/file_system/dir_entry.cpp b/src/bakatools/file_system/dir_entry.cpp index 6ef4dbc..278c25e 100644 --- a/src/bakatools/file_system/dir_entry.cpp +++ b/src/bakatools/file_system/dir_entry.cpp @@ -1,84 +1,82 @@ #include "dir_entry.h" namespace Bk { - namespace Tools { - std::string DirEntry::path() - { - return ent.path().string(); - } + std::string DirEntry::path() + { + return ent.path().string(); + } - std::string DirEntry::absolute_path() - { - return std::filesystem::absolute(ent.path()); - } + std::string DirEntry::absolute_path() + { + return std::filesystem::absolute(ent.path()); + } - std::string DirEntry::relative_path() - { - return ent.path().relative_path().string(); - } + std::string DirEntry::relative_path() + { + return ent.path().relative_path().string(); + } - std::string DirEntry::name() - { - return ent.path().filename(); - } + std::string DirEntry::name() + { + return ent.path().filename(); + } - uintmax_t DirEntry::size() - { - return ent.file_size(); - } + uintmax_t DirEntry::size() + { + return ent.file_size(); + } - TimePoint DirEntry::last_write() - { - return TimePoint(ent.last_write_time()); - } + TimePoint DirEntry::last_write() + { + return TimePoint(ent.last_write_time()); + } - bool DirEntry::exists() - { - return ent.exists(); - } + bool DirEntry::exists() + { + return ent.exists(); + } - bool DirEntry::is(FileType type) - { - return this->type() == type; - } + bool DirEntry::is(FileType type) + { + return this->type() == type; + } - FileType DirEntry::type() - { - return ent.status().type(); - } + FileType DirEntry::type() + { + return ent.status().type(); + } - FilePerms DirEntry::perms() - { - return ent.status().permissions(); - } + FilePerms DirEntry::perms() + { + return ent.status().permissions(); + } - uintmax_t DirEntry::hard_links() - { - return ent.hard_link_count(); - } + uintmax_t DirEntry::hard_links() + { + return ent.hard_link_count(); + } - bool DirEntry::move(std::string name) + bool DirEntry::move(std::string name) + { + std::filesystem::directory_entry new_path(ent.path().parent_path()/name); + if (exists() && new_path.exists()) { - std::filesystem::directory_entry new_path(ent.path().parent_path()/name); - if (exists() && new_path.exists()) - { - std::filesystem::rename(ent.path(), new_path.path()/name()); - ent = std::filesystem::directory_entry(new_path.path()); - return true; - } - return false; + std::filesystem::rename(ent.path(), new_path.path()/name()); + ent = std::filesystem::directory_entry(new_path.path()); + return true; } + return false; + } - bool DirEntry::rename(std::string name) + bool DirEntry::rename(std::string name) + { + if (exists()) { - if (exists()) - { - std::filesystem::path new_path = ent.path().parent_path()/name; - std::filesystem::rename(ent.path(), new_path); - ent = std::filesystem::directory_entry(new_path); - return true; - } - return false; + std::filesystem::path new_path = ent.path().parent_path()/name; + std::filesystem::rename(ent.path(), new_path); + ent = std::filesystem::directory_entry(new_path); + return true; } + return false; } } \ No newline at end of file diff --git a/src/bakatools/file_system/dir_entry.h b/src/bakatools/file_system/dir_entry.h index 02b6b56..c65a5d5 100644 --- a/src/bakatools/file_system/dir_entry.h +++ b/src/bakatools/file_system/dir_entry.h @@ -6,50 +6,48 @@ #define BK_DIR_ENTRY_TYPE(type) const char* type_name() override { return BK_STRINGIFY(type); }\ static FileType get_type() { return FileType::type; }\ namespace Bk { - namespace Tools { - using FileType = std::filesystem::file_type; - using FilePerms = std::filesystem::perms; - using FileStatus = std::filesystem::file_status; - using CopyOption = std::filesystem::copy_options; - - class DirEntry - { - public: - DirEntry(std::string path) - { - std::filesystem::path p(path); - ent = std::filesystem::directory_entry(p); - } - - DirEntry(std::filesystem::directory_entry ent) - : ent(ent) {} - - virtual ~DirEntry() {} - - std::string path(); - std::string absolute_path(); - std::string relative_path(); - std::string name(); - - uintmax_t size(); - TimePoint last_write(); - bool exists(); - FilePerms perms(); - uintmax_t hard_links(); - - bool move(std::string path); - bool rename(std::string name); - virtual int remove() { return (int)false; } - virtual void copy(std::string path, CopyOption opt) {} - - - virtual const char* type_name() { return "dir_entry"; } - FileType type(); - bool is(FileType type); - static FileType get_type() { return FileType::unknown; } - - protected: - std::filesystem::directory_entry ent; - }; - } + using FileType = std::filesystem::file_type; + using FilePerms = std::filesystem::perms; + using FileStatus = std::filesystem::file_status; + using CopyOption = std::filesystem::copy_options; + + class DirEntry + { + public: + DirEntry(std::string path) + { + std::filesystem::path p(path); + ent = std::filesystem::directory_entry(p); + } + + DirEntry(std::filesystem::directory_entry ent) + : ent(ent) {} + + virtual ~DirEntry() {} + + std::string path(); + std::string absolute_path(); + std::string relative_path(); + std::string name(); + + uintmax_t size(); + TimePoint last_write(); + bool exists(); + FilePerms perms(); + uintmax_t hard_links(); + + bool move(std::string path); + bool rename(std::string name); + virtual int remove() { return (int)false; } + virtual void copy(std::string path, CopyOption opt) {} + + + virtual const char* type_name() { return "dir_entry"; } + FileType type(); + bool is(FileType type); + static FileType get_type() { return FileType::unknown; } + + protected: + std::filesystem::directory_entry ent; + }; } \ No newline at end of file