2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include <algorithm>
|
2014-11-15 20:46:40 +00:00
|
|
|
#include <functional>
|
2020-12-28 12:25:24 +00:00
|
|
|
#include <iterator>
|
2014-02-17 10:18:15 +00:00
|
|
|
|
|
|
|
#include "Common/CommonPaths.h"
|
|
|
|
#include "Common/FileSearch.h"
|
2019-06-21 16:07:59 +00:00
|
|
|
#include "Common/StringUtil.h"
|
2017-06-21 04:24:26 +00:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
2017-06-25 23:46:59 +00:00
|
|
|
#include <Windows.h>
|
2019-06-20 08:39:56 +00:00
|
|
|
#include <filesystem>
|
|
|
|
namespace fs = std::filesystem;
|
2017-06-21 04:24:26 +00:00
|
|
|
#define HAS_STD_FILESYSTEM
|
|
|
|
#else
|
2020-12-28 12:25:24 +00:00
|
|
|
#ifdef ANDROID
|
|
|
|
#include "jni/AndroidCommon/AndroidCommon.h"
|
|
|
|
#endif
|
|
|
|
|
2017-06-27 10:45:02 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include "Common/CommonFuncs.h"
|
2014-11-15 20:46:40 +00:00
|
|
|
#include "Common/FileUtil.h"
|
2017-06-21 04:24:26 +00:00
|
|
|
#endif
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2017-04-07 04:55:10 +00:00
|
|
|
namespace Common
|
|
|
|
{
|
2017-06-21 04:24:26 +00:00
|
|
|
#ifndef HAS_STD_FILESYSTEM
|
|
|
|
|
2020-12-28 12:25:24 +00:00
|
|
|
static void FileSearchWithTest(const std::string& directory, bool recursive,
|
|
|
|
std::vector<std::string>* result_out,
|
|
|
|
std::function<bool(const File::FSTEntry&)> callback)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2020-12-28 12:25:24 +00:00
|
|
|
File::FSTEntry top = File::ScanDirectoryTree(directory, recursive);
|
|
|
|
|
|
|
|
const std::function<void(File::FSTEntry&)> DoEntry = [&](File::FSTEntry& entry) {
|
|
|
|
if (callback(entry))
|
|
|
|
result_out->push_back(entry.physicalName);
|
|
|
|
for (auto& child : entry.children)
|
2015-06-21 20:03:32 +00:00
|
|
|
DoEntry(child);
|
2020-12-28 12:25:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for (auto& child : top.children)
|
|
|
|
DoEntry(child);
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2017-06-25 09:59:28 +00:00
|
|
|
std::vector<std::string> DoFileSearch(const std::vector<std::string>& directories,
|
|
|
|
const std::vector<std::string>& exts, bool recursive)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2020-12-28 12:25:24 +00:00
|
|
|
std::vector<std::string> result;
|
|
|
|
|
2017-06-21 04:24:26 +00:00
|
|
|
bool accept_all = exts.empty();
|
2020-12-28 12:25:24 +00:00
|
|
|
const auto callback = [&exts, accept_all](const File::FSTEntry& entry) {
|
2015-09-22 00:01:08 +00:00
|
|
|
if (accept_all)
|
|
|
|
return true;
|
2017-06-26 09:44:23 +00:00
|
|
|
if (entry.isDirectory)
|
|
|
|
return false;
|
2015-09-22 00:01:08 +00:00
|
|
|
return std::any_of(exts.begin(), exts.end(), [&](const std::string& ext) {
|
2017-06-27 10:45:02 +00:00
|
|
|
const std::string& name = entry.virtualName;
|
2015-09-22 00:01:08 +00:00
|
|
|
return name.length() >= ext.length() &&
|
2017-06-27 10:45:02 +00:00
|
|
|
strcasecmp(name.c_str() + name.length() - ext.length(), ext.c_str()) == 0;
|
2015-09-22 00:01:08 +00:00
|
|
|
});
|
2020-12-28 12:25:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for (const std::string& directory : directories)
|
|
|
|
{
|
|
|
|
#ifdef ANDROID
|
|
|
|
// While File::ScanDirectoryTree (which is called in FileSearchWithTest) does handle Android
|
|
|
|
// content correctly, having a specialized implementation of DoFileSearch for Android content
|
|
|
|
// provides a much needed performance boost. Also, this specialized implementation will be
|
|
|
|
// required if we in the future replace the use of File::ScanDirectoryTree with std::filesystem.
|
|
|
|
if (IsPathAndroidContent(directory))
|
|
|
|
{
|
|
|
|
const std::vector<std::string> partial_result =
|
|
|
|
DoFileSearchAndroidContent(directory, exts, recursive);
|
|
|
|
|
|
|
|
result.insert(result.end(), std::make_move_iterator(partial_result.begin()),
|
|
|
|
std::make_move_iterator(partial_result.end()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
FileSearchWithTest(directory, recursive, &result, callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove duplicates
|
|
|
|
std::sort(result.begin(), result.end());
|
|
|
|
result.erase(std::unique(result.begin(), result.end()), result.end());
|
|
|
|
return result;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2017-06-21 04:24:26 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
std::vector<std::string> DoFileSearch(const std::vector<std::string>& directories,
|
|
|
|
const std::vector<std::string>& exts, bool recursive)
|
|
|
|
{
|
|
|
|
bool accept_all = exts.empty();
|
|
|
|
|
|
|
|
std::vector<fs::path> native_exts;
|
|
|
|
for (const auto& ext : exts)
|
2019-06-21 16:07:59 +00:00
|
|
|
native_exts.push_back(StringToPath(ext));
|
2017-06-21 04:24:26 +00:00
|
|
|
|
|
|
|
// N.B. This avoids doing any copies
|
|
|
|
auto ext_matches = [&native_exts](const fs::path& path) {
|
|
|
|
const auto& native_path = path.native();
|
|
|
|
return std::any_of(native_exts.cbegin(), native_exts.cend(), [&native_path](const auto& ext) {
|
|
|
|
// TODO provide cross-platform compat for the comparison function, once more platforms
|
|
|
|
// support std::filesystem
|
2017-06-25 23:46:59 +00:00
|
|
|
int compare_len = static_cast<int>(ext.native().length());
|
|
|
|
return native_path.length() >= compare_len &&
|
|
|
|
CompareStringOrdinal(&native_path.c_str()[native_path.length() - compare_len],
|
|
|
|
compare_len, ext.c_str(), compare_len, TRUE) == CSTR_EQUAL;
|
2017-06-21 04:24:26 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<std::string> result;
|
|
|
|
auto add_filtered = [&](const fs::directory_entry& entry) {
|
|
|
|
auto& path = entry.path();
|
|
|
|
if (accept_all || (ext_matches(path) && !fs::is_directory(path)))
|
2019-06-21 16:07:59 +00:00
|
|
|
result.emplace_back(PathToString(path));
|
2017-06-21 04:24:26 +00:00
|
|
|
};
|
|
|
|
for (const auto& directory : directories)
|
|
|
|
{
|
2020-08-18 00:23:28 +00:00
|
|
|
fs::path directory_path = StringToPath(directory);
|
2019-06-20 16:29:29 +00:00
|
|
|
if (fs::is_directory(directory_path)) // Can't create iterators for non-existant directories
|
2017-06-21 04:24:26 +00:00
|
|
|
{
|
2019-06-20 16:29:29 +00:00
|
|
|
if (recursive)
|
|
|
|
{
|
|
|
|
// TODO use fs::directory_options::follow_directory_symlink ?
|
|
|
|
for (auto& entry : fs::recursive_directory_iterator(std::move(directory_path)))
|
|
|
|
add_filtered(entry);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (auto& entry : fs::directory_iterator(std::move(directory_path)))
|
|
|
|
add_filtered(entry);
|
|
|
|
}
|
2017-06-21 04:24:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove duplicates (occurring because caller gave e.g. duplicate or overlapping directories -
|
|
|
|
// not because std::filesystem returns duplicates). Also note that this pathname-based uniqueness
|
|
|
|
// isn't as thorough as std::filesystem::equivalent.
|
|
|
|
std::sort(result.begin(), result.end());
|
|
|
|
result.erase(std::unique(result.begin(), result.end()), result.end());
|
|
|
|
|
2017-06-26 09:50:10 +00:00
|
|
|
// Dolphin expects to be able to use "/" (DIR_SEP) everywhere.
|
|
|
|
// std::filesystem uses the OS separator.
|
|
|
|
constexpr fs::path::value_type os_separator = fs::path::preferred_separator;
|
|
|
|
static_assert(os_separator == DIR_SEP_CHR || os_separator == '\\', "Unsupported path separator");
|
2020-08-22 01:04:50 +00:00
|
|
|
if constexpr (os_separator != DIR_SEP_CHR)
|
|
|
|
{
|
2017-06-21 04:24:26 +00:00
|
|
|
for (auto& path : result)
|
|
|
|
std::replace(path.begin(), path.end(), '\\', DIR_SEP_CHR);
|
2020-08-22 01:04:50 +00:00
|
|
|
}
|
2017-06-21 04:24:26 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2017-04-07 04:55:10 +00:00
|
|
|
} // namespace Common
|