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"
|
2014-11-15 20:46:40 +00:00
|
|
|
#include "Common/FileUtil.h"
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2014-11-15 20:46:40 +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
|
|
|
{
|
2014-11-15 20:46:40 +00:00
|
|
|
std::vector<std::string> result;
|
|
|
|
for (const std::string& directory : directories)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2014-11-15 20:46:40 +00:00
|
|
|
File::FSTEntry top = File::ScanDirectoryTree(directory, recursive);
|
|
|
|
|
|
|
|
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);
|
|
|
|
};
|
2015-06-21 20:03:32 +00:00
|
|
|
for (auto& child : top.children)
|
|
|
|
DoEntry(child);
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
2014-11-15 20:46:40 +00:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2015-09-22 00:01:08 +00:00
|
|
|
std::vector<std::string> DoFileSearch(const std::vector<std::string>& exts, const std::vector<std::string>& directories, bool recursive)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2015-09-22 00:01:08 +00:00
|
|
|
bool accept_all = std::find(exts.begin(), exts.end(), "") != exts.end();
|
2014-11-15 20:46:40 +00:00
|
|
|
return FileSearchWithTest(directories, recursive, [&](const File::FSTEntry& entry) {
|
2015-09-22 00:01:08 +00:00
|
|
|
if (accept_all)
|
|
|
|
return true;
|
|
|
|
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;
|
|
|
|
});
|
2014-11-15 20:46:40 +00:00
|
|
|
});
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2015-06-21 20:03:32 +00:00
|
|
|
// Result includes the passed directories themselves as well as their subdirectories.
|
2014-11-15 20:46:40 +00:00
|
|
|
std::vector<std::string> FindSubdirectories(const std::vector<std::string>& directories, bool recursive)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2014-11-15 20:46:40 +00:00
|
|
|
return FileSearchWithTest(directories, true, [&](const File::FSTEntry& entry) {
|
|
|
|
return entry.isDirectory;
|
|
|
|
});
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|