From 2b501a6dcd4209742215c4bbb9756aba5bc4d223 Mon Sep 17 00:00:00 2001 From: Georg Hagen Date: Thu, 10 Oct 2024 18:35:42 +0200 Subject: [PATCH] Add more functions --- openVulkanoCpp/IO/FsUtils.cpp | 53 ++++++++++++++++++++++++++++++++--- openVulkanoCpp/IO/FsUtils.hpp | 18 ++++++++++++ 2 files changed, 67 insertions(+), 4 deletions(-) diff --git a/openVulkanoCpp/IO/FsUtils.cpp b/openVulkanoCpp/IO/FsUtils.cpp index ae38496..3e3edb1 100644 --- a/openVulkanoCpp/IO/FsUtils.cpp +++ b/openVulkanoCpp/IO/FsUtils.cpp @@ -5,6 +5,7 @@ */ #include "FsUtils.hpp" +#include "Base/Logger.hpp" #include #include @@ -12,6 +13,34 @@ namespace fs = std::filesystem; namespace OpenVulkano { + bool FsUtils::DeleteEmptyFilesAndDirs(const std::filesystem::path& dir, const bool recursive) + { + bool deleted = false; + for (auto& p: fs::directory_iterator(dir)) + { + if (fs::is_directory(p)) + { + if (recursive) + { + deleted |= (DeleteEmptyDirs(p), recursive); + } + if (fs::is_empty(p)) + { + fs::remove(p); + Logger::FILESYS->info("Deleted empty directory: {}", p.path().string()); + deleted = true; + } + } + else if (fs::is_empty(p)) + { + fs::remove(p); + Logger::FILESYS->info("Deleted empty file: {}", p.path().string()); + deleted = true; + } + } + return deleted; + } + bool FsUtils::DeleteEmptyDirs(const std::filesystem::path& dir, const bool recursive) { bool deleted = false; @@ -21,14 +50,12 @@ namespace OpenVulkano { if (recursive) { - if (DeleteEmptyDirs(p), recursive) - { - deleted = true; - } + deleted |= (DeleteEmptyDirs(p), recursive); } if (fs::is_empty(p)) { fs::remove(p); + Logger::FILESYS->info("Deleted empty directory: {}", p.path().string()); deleted = true; } } @@ -90,6 +117,24 @@ namespace OpenVulkano return size; } + + FsCount FsUtils::GetDirFileAndDirCount(const std::filesystem::path& dir) + { + FsCount count; + for (auto& p: fs::directory_iterator(dir)) + { + if (fs::is_directory(p)) + { + count += GetDirFileAndDirCount(p); + count.dirCount++; + } + else + { + count.fileCount++; + } + } + return count; + } std::string FsUtils::ToValidFileName(std::string str) { diff --git a/openVulkanoCpp/IO/FsUtils.hpp b/openVulkanoCpp/IO/FsUtils.hpp index e37d2f6..121c72e 100644 --- a/openVulkanoCpp/IO/FsUtils.hpp +++ b/openVulkanoCpp/IO/FsUtils.hpp @@ -11,8 +11,24 @@ namespace OpenVulkano { + struct FsCount + { + size_t fileCount, dirCount; + + size_t GetTotalEntryCount() const { return fileCount + dirCount; } + + FsCount& operator +=(const FsCount& other) + { + fileCount += other.fileCount; + dirCount += other.dirCount; + return *this; + } + }; + struct FsUtils { + static bool DeleteEmptyFilesAndDirs(const std::filesystem::path& dir, bool recursive = true); + static bool DeleteEmptyDirs(const std::filesystem::path& dir, bool recursive = true); static bool DeleteEmptySubTrees(const std::filesystem::path& dir); @@ -20,6 +36,8 @@ namespace OpenVulkano static bool IsTreeEmpty(const std::filesystem::path& dir); static ByteSize GetDirSize(const std::filesystem::path& dir); + + static FsCount GetDirFileAndDirCount(const std::filesystem::path& dir); static std::string ToValidFileName(std::string filename); };