diff --git a/openVulkanoCpp/IO/FsUtils.cpp b/openVulkanoCpp/IO/FsUtils.cpp new file mode 100644 index 0000000..802b038 --- /dev/null +++ b/openVulkanoCpp/IO/FsUtils.cpp @@ -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; + } +} \ No newline at end of file diff --git a/openVulkanoCpp/IO/FsUtils.hpp b/openVulkanoCpp/IO/FsUtils.hpp new file mode 100644 index 0000000..575b33e --- /dev/null +++ b/openVulkanoCpp/IO/FsUtils.hpp @@ -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 + +namespace OpenVulkano +{ + class FsUtils + { + static bool DeleteEmptyDirs(const std::filesystem::path& dir, bool recursive = true); + + static bool IsTreeEmpty(const std::filesystem::path& dir); + }; +} \ No newline at end of file