From d78277c063d43e3234fe80a38d272500f3dfce3a Mon Sep 17 00:00:00 2001 From: JosJuice Date: Mon, 28 Dec 2020 13:25:24 +0100 Subject: Android: Add specialized content provider implementation of DoFileSearch --- Source/Core/Common/FileSearch.cpp | 72 ++++++++++++++++++++++++++------------- 1 file changed, 49 insertions(+), 23 deletions(-) (limited to 'Source/Core/Common/FileSearch.cpp') diff --git a/Source/Core/Common/FileSearch.cpp b/Source/Core/Common/FileSearch.cpp index bbcdc12026..b06ef9b3a1 100644 --- a/Source/Core/Common/FileSearch.cpp +++ b/Source/Core/Common/FileSearch.cpp @@ -4,6 +4,7 @@ #include #include +#include #include "Common/CommonPaths.h" #include "Common/FileSearch.h" @@ -15,6 +16,10 @@ namespace fs = std::filesystem; #define HAS_STD_FILESYSTEM #else +#ifdef ANDROID +#include "jni/AndroidCommon/AndroidCommon.h" +#endif + #include #include "Common/CommonFuncs.h" #include "Common/FileUtil.h" @@ -24,36 +29,30 @@ namespace Common { #ifndef HAS_STD_FILESYSTEM -static std::vector -FileSearchWithTest(const std::vector& directories, bool recursive, - std::function callback) +static void FileSearchWithTest(const std::string& directory, bool recursive, + std::vector* result_out, + std::function callback) { - std::vector result; - for (const std::string& directory : directories) - { - File::FSTEntry top = File::ScanDirectoryTree(directory, recursive); - - std::function DoEntry; - DoEntry = [&](File::FSTEntry& entry) { - if (callback(entry)) - result.push_back(entry.physicalName); - for (auto& child : entry.children) - DoEntry(child); - }; - for (auto& child : top.children) + File::FSTEntry top = File::ScanDirectoryTree(directory, recursive); + + const std::function DoEntry = [&](File::FSTEntry& entry) { + if (callback(entry)) + result_out->push_back(entry.physicalName); + for (auto& child : entry.children) DoEntry(child); - } - // remove duplicates - std::sort(result.begin(), result.end()); - result.erase(std::unique(result.begin(), result.end()), result.end()); - return result; + }; + + for (auto& child : top.children) + DoEntry(child); } std::vector DoFileSearch(const std::vector& directories, const std::vector& exts, bool recursive) { + std::vector result; + bool accept_all = exts.empty(); - return FileSearchWithTest(directories, recursive, [&](const File::FSTEntry& entry) { + const auto callback = [&exts, accept_all](const File::FSTEntry& entry) { if (accept_all) return true; if (entry.isDirectory) @@ -63,7 +62,34 @@ std::vector DoFileSearch(const std::vector& directorie return name.length() >= ext.length() && strcasecmp(name.c_str() + name.length() - ext.length(), ext.c_str()) == 0; }); - }); + }; + + 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 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; } #else -- cgit v1.2.3