Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Base] Added Listing directories and filtering filenames by regex #2140

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/xenia/base/filesystem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,30 @@ bool CreateParentFolder(const std::filesystem::path& path) {
return true;
}

std::vector<FileInfo> ListDirectories(const std::filesystem::path& path) {
const std::vector<FileInfo> files = ListFiles(path);
std::vector<FileInfo> directories = {};

std::copy_if(files.cbegin(), files.cend(), std::back_inserter(directories),
[](const 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](const FileInfo& file) {
return std::regex_match(file.name.filename().string(), pattern);
});

return filtered_entries;
}

} // namespace filesystem
} // namespace xe
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did the files where these functions are called accidentally slip under the radar of git add?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, these will be used in the future and in different PRs.
It will simplify loading patches and simplify loading profile directories in the future

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may end up being removed as dead code at some point between the merging of this pull request and when the patches/profile changes arrive, or it may just deteriorate as nothing tests this code in practice, I think it'd be safer to submit these functions alongside the first use of them.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with that, but it's quite annoying if that change would appear in many PRs.
Maybe instead it would be better to add note about future usage?

Especially when we consider how long resolving all quirks in bigger PRs might take

std::vector<FileInfo> FilterByName(const std::vector<FileInfo>& files,
const std::regex pattern);

#if XE_PLATFORM_ANDROID
void AndroidInitialize();
Expand Down