Added Listing directories and filtering filenames by regex

This commit is contained in:
Gliniak 2023-03-12 20:31:31 +01:00
parent 7a1eca7265
commit 6e86eacf5a
2 changed files with 28 additions and 0 deletions

View File

@ -13,6 +13,7 @@
#include <filesystem>
#include <iterator>
#include <memory>
#include <regex>
#include <string>
#include <string_view>
#include <vector>
@ -123,6 +124,9 @@ struct FileInfo {
};
bool GetInfo(const std::filesystem::path& path, FileInfo* out_info);
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);
#if XE_PLATFORM_ANDROID
void AndroidInitialize();

View File

@ -261,5 +261,29 @@ std::vector<FileInfo> ListFiles(const std::filesystem::path& path) {
return result;
}
std::vector<FileInfo> ListDirectories(const std::filesystem::path& path) {
std::vector<FileInfo> files = ListFiles(path);
std::vector<FileInfo> directories = {};
std::copy_if(
files.cbegin(), files.cend(), std::back_inserter(directories),
[](FileInfo file) { return file.type == FileInfo::Type::kDirectory; });
return directories;
}
std::vector<FileInfo> FilterByName(const std::vector<FileInfo>& files,
const std::regex pattern) {
std::vector<FileInfo> filtered_entries = {};
std::copy_if(files.cbegin(), files.cend(),
std::back_inserter(filtered_entries), [pattern](FileInfo file) {
return std::regex_match(file.name.filename().string(),
pattern);
});
return filtered_entries;
}
} // namespace filesystem
} // namespace xe