Add FilesWithExtension function

This commit is contained in:
Georg Hagen
2025-05-09 20:29:11 +02:00
parent f79d84b708
commit e9e7326866
2 changed files with 36 additions and 0 deletions

View File

@@ -6,6 +6,7 @@
#include "FsUtils.hpp" #include "FsUtils.hpp"
#include "Base/Logger.hpp" #include "Base/Logger.hpp"
#include "Base/Utils.hpp"
#include <unordered_map> #include <unordered_map>
#include <regex> #include <regex>
@@ -192,4 +193,37 @@ namespace OpenVulkano
return str; return str;
} }
std::vector<std::filesystem::path> FsUtils::FilesWithExtension(const std::filesystem::path& dir, std::string ext)
{
std::vector<std::filesystem::path> 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;
}
} }

View File

@@ -40,5 +40,7 @@ namespace OpenVulkano
static FsCount GetDirFileAndDirCount(const std::filesystem::path& dir); static FsCount GetDirFileAndDirCount(const std::filesystem::path& dir);
static std::string ToValidFileName(std::string filename); static std::string ToValidFileName(std::string filename);
static std::vector<std::filesystem::path> FilesWithExtension(const std::filesystem::path& dir, std::string ext);
}; };
} }