[Base] Added option to recursively search for file

This commit is contained in:
Gliniak
2026-05-24 22:02:33 +02:00
parent c48e0f29e5
commit 02a0d697cb
2 changed files with 37 additions and 2 deletions

View File

@@ -9,6 +9,8 @@
#include "xenia/base/filesystem.h"
#include "xenia/base/string_util.h"
namespace xe {
namespace filesystem {
@@ -44,7 +46,7 @@ std::vector<FileInfo> ListDirectories(const std::filesystem::path& path) {
return file.type == FileInfo::Type::kDirectory;
});
return std::move(directories);
return directories;
}
std::vector<FileInfo> FilterByName(const std::vector<FileInfo>& files,
@@ -56,7 +58,37 @@ std::vector<FileInfo> FilterByName(const std::vector<FileInfo>& files,
[pattern](const FileInfo& file) {
return std::regex_match(file.name.filename().string(), pattern);
});
return std::move(filtered_entries);
return filtered_entries;
}
std::vector<FileInfo> FindFileWithName(const std::filesystem::path& path,
std::string_view name, bool recursive) {
if (!std::filesystem::exists(path)) {
return {};
}
if (!std::filesystem::is_directory(path)) {
return {};
}
if (!recursive) {
return FilterByName(ListFiles(path), std::regex(std::string(name)));
}
const std::string file_name = xe::utf8::lower_ascii(name);
std::vector<FileInfo> filtered_entries = {};
for (const auto& entry :
std::filesystem::recursive_directory_iterator(path)) {
if (entry.is_regular_file() && xe::utf8::lower_ascii(xe::path_to_utf8(
entry.path().filename())) == file_name) {
auto file_info = GetInfo(entry.path());
if (file_info) {
filtered_entries.push_back(std::move(file_info.value()));
}
}
}
return filtered_entries;
}
} // namespace filesystem

View File

@@ -133,6 +133,9 @@ std::vector<FileInfo> ListFiles(const std::filesystem::path& path);
std::vector<FileInfo> ListDirectories(const std::filesystem::path& path);
std::vector<FileInfo> FilterByName(const std::vector<FileInfo>& files,
const std::regex pattern);
std::vector<FileInfo> FindFileWithName(const std::filesystem::path& path,
std::string_view name,
bool recursive = false);
bool SetAttributes(const std::filesystem::path& path, uint64_t attributes);