From e9e732686644ee342a37386473fc516ac6b8c83e Mon Sep 17 00:00:00 2001 From: Georg Hagen Date: Fri, 9 May 2025 20:29:11 +0200 Subject: [PATCH] Add FilesWithExtension function --- openVulkanoCpp/IO/FsUtils.cpp | 34 ++++++++++++++++++++++++++++++++++ openVulkanoCpp/IO/FsUtils.hpp | 2 ++ 2 files changed, 36 insertions(+) diff --git a/openVulkanoCpp/IO/FsUtils.cpp b/openVulkanoCpp/IO/FsUtils.cpp index 55f6f92..2f8be50 100644 --- a/openVulkanoCpp/IO/FsUtils.cpp +++ b/openVulkanoCpp/IO/FsUtils.cpp @@ -6,6 +6,7 @@ #include "FsUtils.hpp" #include "Base/Logger.hpp" +#include "Base/Utils.hpp" #include #include @@ -192,4 +193,37 @@ namespace OpenVulkano return str; } + + std::vector FsUtils::FilesWithExtension(const std::filesystem::path& dir, std::string ext) + { + std::vector files; + + if (!fs::exists(dir) || !fs::is_directory(dir)) + { + Logger::FILESYS->warn("Directory does not exist or is not a directory: {}", dir.string()); + return files; + } + + if (!ext.empty() && ext[0] != '.') ext = "." + ext; + Utils::ToLower(ext); + + try + { + for (const auto& entry : fs::directory_iterator(dir)) + { + std::string fExt = entry.path().extension().string(); + Utils::ToLower(fExt); + if (fs::is_regular_file(entry) && fExt == ext) + { + files.push_back(entry.path()); + } + } + } + catch (const fs::filesystem_error& e) + { + Logger::FILESYS->error("Filesystem error when searching for files: {}", e.what()); + } + + return files; + } } diff --git a/openVulkanoCpp/IO/FsUtils.hpp b/openVulkanoCpp/IO/FsUtils.hpp index f9a2bf5..74952ce 100644 --- a/openVulkanoCpp/IO/FsUtils.hpp +++ b/openVulkanoCpp/IO/FsUtils.hpp @@ -40,5 +40,7 @@ namespace OpenVulkano static FsCount GetDirFileAndDirCount(const std::filesystem::path& dir); static std::string ToValidFileName(std::string filename); + + static std::vector FilesWithExtension(const std::filesystem::path& dir, std::string ext); }; }