Files
OpenVulkano/openVulkanoCpp/IO/FsUtils.cpp
2023-10-11 09:54:44 +02:00

72 lines
1.2 KiB
C++

/*
* 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, const bool recursive)
{
bool deleted = false;
for (auto& p: fs::directory_iterator(dir))
{
if (fs::is_directory(p))
{
if (recursive)
{
if (DeleteEmptyDirs(p), recursive)
{
deleted = true;
}
}
if (fs::is_empty(p))
{
fs::remove(p);
deleted = true;
}
}
}
return deleted;
}
bool FsUtils::DeleteEmptySubTrees(const std::filesystem::path& dir)
{
bool deleted = false;
for (auto& p: fs::directory_iterator(dir))
{
if (fs::is_directory(p))
{
if (IsTreeEmpty(p))
{
fs::remove_all(p);
deleted = true;
}
}
}
return deleted;
}
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;
}
}
return true;
}
}