2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 03:09:55 +00:00
|
|
|
// Refer to the license.txt file included.
|
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>
|
2014-02-17 10:18:15 +00:00
|
|
|
|
|
|
|
#include "Common/CommonPaths.h"
|
|
|
|
#include "Common/FileSearch.h"
|
2017-06-21 04:24:26 +00:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <experimental/filesystem>
|
|
|
|
namespace fs = std::experimental::filesystem;
|
|
|
|
#define HAS_STD_FILESYSTEM
|
|
|
|
#else
|
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
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
static std::vector<std::string>
|
|
|
|
FileSearchWithTest(const std::vector<std::string>& directories, bool recursive,
|
|
|
|
std::function<bool(const File::FSTEntry&)> callback)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
std::vector<std::string> result;
|
|
|
|
for (const std::string& directory : directories)
|
|
|
|
{
|
|
|
|
File::FSTEntry top = File::ScanDirectoryTree(directory, recursive);
|
2014-11-15 20:46:40 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
std::function<void(File::FSTEntry&)> 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)
|
|
|
|
DoEntry(child);
|
|
|
|
}
|
|
|
|
// 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-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
|
|
|
{
|
2017-06-21 04:24:26 +00:00
|
|
|
bool accept_all = exts.empty();
|
2016-06-24 08:43:46 +00:00
|
|
|
return FileSearchWithTest(directories, recursive, [&](const File::FSTEntry& entry) {
|
|
|
|
if (accept_all)
|
|
|
|
return true;
|
2017-06-26 09:44:23 +00:00
|
|
|
if (entry.isDirectory)
|
|
|
|
return false;
|
2016-06-24 08:43:46 +00:00
|
|
|
std::string name = entry.virtualName;
|
|
|
|
std::transform(name.begin(), name.end(), name.begin(), ::tolower);
|
|
|
|
return std::any_of(exts.begin(), exts.end(), [&](const std::string& ext) {
|
|
|
|
return name.length() >= ext.length() &&
|
|
|
|
name.compare(name.length() - ext.length(), ext.length(), ext) == 0;
|
|
|
|
});
|
|
|
|
});
|
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)
|
|
|
|
native_exts.push_back(ext);
|
|
|
|
|
|
|
|
// 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
|
|
|
|
return native_path.length() >= ext.native().length() &&
|
|
|
|
_wcsicmp(&native_path.c_str()[native_path.length() - ext.native().length()],
|
|
|
|
ext.c_str()) == 0;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
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)))
|
|
|
|
result.emplace_back(path.u8string());
|
|
|
|
};
|
|
|
|
for (const auto& directory : directories)
|
|
|
|
{
|
|
|
|
if (recursive)
|
|
|
|
{
|
|
|
|
// TODO use fs::directory_options::follow_directory_symlink ?
|
|
|
|
for (auto& entry : fs::recursive_directory_iterator(fs::path(directory.c_str())))
|
|
|
|
add_filtered(entry);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (auto& entry : fs::directory_iterator(fs::path(directory.c_str())))
|
|
|
|
add_filtered(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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());
|
|
|
|
|
|
|
|
// Dolphin expects to be able to use "/" (DIR_SEP) everywhere. std::filesystem uses the OS
|
|
|
|
// separator.
|
|
|
|
if (fs::path::preferred_separator != DIR_SEP_CHR)
|
|
|
|
for (auto& path : result)
|
|
|
|
std::replace(path.begin(), path.end(), '\\', DIR_SEP_CHR);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2017-04-07 04:55:10 +00:00
|
|
|
} // namespace Common
|