Add more functions

This commit is contained in:
Georg Hagen
2024-10-10 18:35:42 +02:00
parent e1313f33de
commit 2b501a6dcd
2 changed files with 67 additions and 4 deletions

View File

@@ -5,6 +5,7 @@
*/
#include "FsUtils.hpp"
#include "Base/Logger.hpp"
#include <unordered_map>
#include <regex>
@@ -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)
{

View File

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