2011-05-05 17:00:00 +00:00
|
|
|
#include "multidircollection.hpp"
|
2022-07-02 22:02:29 +00:00
|
|
|
#include "conversion.hpp"
|
2011-05-05 17:00:00 +00:00
|
|
|
|
2022-06-08 21:25:50 +00:00
|
|
|
#include <filesystem>
|
2011-05-05 17:00:00 +00:00
|
|
|
|
2018-08-14 15:42:41 +00:00
|
|
|
#include <components/debug/debuglog.hpp>
|
|
|
|
|
2011-05-05 17:00:00 +00:00
|
|
|
namespace Files
|
|
|
|
{
|
|
|
|
struct NameEqual
|
|
|
|
{
|
|
|
|
bool mStrict;
|
|
|
|
|
|
|
|
NameEqual(bool strict)
|
|
|
|
: mStrict(strict)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator()(const std::string& left, const std::string& right) const
|
|
|
|
{
|
|
|
|
if (mStrict)
|
|
|
|
return left == right;
|
2022-06-01 20:09:13 +00:00
|
|
|
return Misc::StringUtils::ciEqual(left, right);
|
2011-05-05 17:00:00 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-09-02 20:45:21 +00:00
|
|
|
MultiDirCollection::MultiDirCollection(
|
2011-05-05 17:00:00 +00:00
|
|
|
const Files::PathContainer& directories, const std::string& extension, bool foldCase)
|
2011-05-05 19:39:52 +00:00
|
|
|
: mFiles(NameLess(!foldCase))
|
2011-05-05 17:00:00 +00:00
|
|
|
{
|
2011-05-05 19:39:52 +00:00
|
|
|
NameEqual equal(!foldCase);
|
2011-05-05 17:00:00 +00:00
|
|
|
|
2022-06-08 21:25:50 +00:00
|
|
|
for (const auto& directory : directories)
|
2011-05-05 17:00:00 +00:00
|
|
|
{
|
2022-06-08 21:25:50 +00:00
|
|
|
if (!std::filesystem::is_directory(directory))
|
2011-09-02 20:45:21 +00:00
|
|
|
{
|
2022-07-02 22:02:29 +00:00
|
|
|
Log(Debug::Info) << "Skipping invalid directory: " << directory;
|
2011-09-02 20:45:21 +00:00
|
|
|
continue;
|
|
|
|
}
|
2011-05-05 17:00:00 +00:00
|
|
|
|
2022-07-02 22:02:29 +00:00
|
|
|
for (const auto& dirIter : std::filesystem::directory_iterator(directory))
|
2011-05-05 17:00:00 +00:00
|
|
|
{
|
2022-07-02 22:02:29 +00:00
|
|
|
const auto& path = dirIter.path();
|
2011-05-05 17:00:00 +00:00
|
|
|
|
2022-07-02 22:02:29 +00:00
|
|
|
if (!equal(extension, Files::pathToUnicodeString(path.extension())))
|
2011-05-05 17:00:00 +00:00
|
|
|
continue;
|
|
|
|
|
2022-07-02 22:02:29 +00:00
|
|
|
const auto filename = Files::pathToUnicodeString(path.filename());
|
2011-05-05 17:00:00 +00:00
|
|
|
|
|
|
|
TIter result = mFiles.find(filename);
|
|
|
|
|
|
|
|
if (result == mFiles.end())
|
|
|
|
{
|
|
|
|
mFiles.insert(std::make_pair(filename, path));
|
|
|
|
}
|
|
|
|
else if (result->first == filename)
|
|
|
|
{
|
|
|
|
mFiles[filename] = path;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// handle case folding
|
|
|
|
mFiles.erase(result->first);
|
|
|
|
mFiles.insert(std::make_pair(filename, path));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-08 21:25:50 +00:00
|
|
|
std::filesystem::path MultiDirCollection::getPath(const std::string& file) const
|
2011-05-05 17:00:00 +00:00
|
|
|
{
|
|
|
|
TIter iter = mFiles.find(file);
|
|
|
|
|
|
|
|
if (iter == mFiles.end())
|
|
|
|
throw std::runtime_error("file " + file + " not found");
|
|
|
|
|
|
|
|
return iter->second;
|
|
|
|
}
|
|
|
|
|
2012-12-26 16:15:53 +00:00
|
|
|
bool MultiDirCollection::doesExist(const std::string& file) const
|
|
|
|
{
|
|
|
|
return mFiles.find(file) != mFiles.end();
|
|
|
|
}
|
|
|
|
|
2011-05-05 17:00:00 +00:00
|
|
|
MultiDirCollection::TIter MultiDirCollection::begin() const
|
|
|
|
{
|
|
|
|
return mFiles.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
MultiDirCollection::TIter MultiDirCollection::end() const
|
|
|
|
{
|
|
|
|
return mFiles.end();
|
|
|
|
}
|
|
|
|
}
|