[Base] Added option to recursively search for file
This commit is contained in:
@@ -9,6 +9,8 @@
|
|||||||
|
|
||||||
#include "xenia/base/filesystem.h"
|
#include "xenia/base/filesystem.h"
|
||||||
|
|
||||||
|
#include "xenia/base/string_util.h"
|
||||||
|
|
||||||
namespace xe {
|
namespace xe {
|
||||||
namespace filesystem {
|
namespace filesystem {
|
||||||
|
|
||||||
@@ -44,7 +46,7 @@ std::vector<FileInfo> ListDirectories(const std::filesystem::path& path) {
|
|||||||
return file.type == FileInfo::Type::kDirectory;
|
return file.type == FileInfo::Type::kDirectory;
|
||||||
});
|
});
|
||||||
|
|
||||||
return std::move(directories);
|
return directories;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<FileInfo> FilterByName(const std::vector<FileInfo>& files,
|
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) {
|
[pattern](const FileInfo& file) {
|
||||||
return std::regex_match(file.name.filename().string(), pattern);
|
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
|
} // namespace filesystem
|
||||||
|
|||||||
@@ -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> ListDirectories(const std::filesystem::path& path);
|
||||||
std::vector<FileInfo> FilterByName(const std::vector<FileInfo>& files,
|
std::vector<FileInfo> FilterByName(const std::vector<FileInfo>& files,
|
||||||
const std::regex pattern);
|
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);
|
bool SetAttributes(const std::filesystem::path& path, uint64_t attributes);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user