Skip to content

Commit

Permalink
[Base] Added Listing directories and filtering filenames by regex
Browse files Browse the repository at this point in the history
  • Loading branch information
Gliniak committed Mar 12, 2023
1 parent f357f26 commit c7f82ed
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/xenia/base/filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <filesystem>
#include <iterator>
#include <memory>
#include <regex>
#include <string>
#include <string_view>
#include <vector>
Expand Down Expand Up @@ -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();
Expand Down
24 changes: 24 additions & 0 deletions src/xenia/base/filesystem_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c7f82ed

Please sign in to comment.