Add FsUtils
This commit is contained in:
52
openVulkanoCpp/IO/FsUtils.cpp
Normal file
52
openVulkanoCpp/IO/FsUtils.cpp
Normal 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;
|
||||
}
|
||||
}
|
||||
19
openVulkanoCpp/IO/FsUtils.hpp
Normal file
19
openVulkanoCpp/IO/FsUtils.hpp
Normal 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);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user