Add FsUtils

This commit is contained in:
2023-10-10 09:37:11 +02:00
parent 488d1ebfc8
commit 2a49949e0c
2 changed files with 71 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "FsUtils.hpp"
namespace fs = std::filesystem;
namespace OpenVulkano
{
bool FsUtils::DeleteEmptyDirs(const std::filesystem::path& dir, bool recursive)
{
for (auto& p: fs::directory_iterator(dir))
{
if (fs::is_directory(p))
{
if (recursive)
{
if (IsTreeEmpty(p))
{
fs::remove_all(p);
}
}
else if (fs::is_empty(p))
{
fs::remove(p);
}
}
}
}
bool FsUtils::IsTreeEmpty(const std::filesystem::path& dir)
{
for (auto& p: fs::directory_iterator(dir))
{
if (fs::is_directory(p))
{
if (!IsTreeEmpty(p))
{
return false;
}
}
else
{
return false; // found a file, so the directory tree is not empty
}
}
return true;
}
}

View File

@@ -0,0 +1,19 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#pragma once
#include <filesystem>
namespace OpenVulkano
{
class FsUtils
{
static bool DeleteEmptyDirs(const std::filesystem::path& dir, bool recursive = true);
static bool IsTreeEmpty(const std::filesystem::path& dir);
};
}