1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-25 06:35:30 +00:00
OpenMW/components/files/multidircollection.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

75 lines
2.0 KiB
C++
Raw Normal View History

2011-05-05 19:00:00 +02:00
#include "multidircollection.hpp"
#include "conversion.hpp"
2011-05-05 19:00:00 +02:00
#include <filesystem>
2011-05-05 19:00:00 +02:00
2018-08-14 19:42:41 +04:00
#include <components/debug/debuglog.hpp>
2011-05-05 19:00:00 +02:00
namespace Files
{
2023-05-31 23:11:03 +02:00
MultiDirCollection::MultiDirCollection(const Files::PathContainer& directories, const std::string& extension)
2011-05-05 19:00:00 +02:00
{
for (const auto& directory : directories)
2011-05-05 19:00:00 +02:00
{
if (!std::filesystem::is_directory(directory))
{
Log(Debug::Info) << "Skipping invalid directory: " << directory;
continue;
}
2011-05-05 19:00:00 +02:00
for (const auto& dirIter : std::filesystem::directory_iterator(directory))
2011-05-05 19:00:00 +02:00
{
const auto& path = dirIter.path();
2011-05-05 19:00:00 +02:00
2023-05-31 23:11:03 +02:00
if (!Misc::StringUtils::ciEqual(extension, Files::pathToUnicodeString(path.extension())))
2011-05-05 19:00:00 +02:00
continue;
const auto filename = Files::pathToUnicodeString(path.filename());
2011-05-05 19:00:00 +02: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));
}
}
}
}
std::filesystem::path MultiDirCollection::getPath(const std::string& file) const
2011-05-05 19:00:00 +02:00
{
TIter iter = mFiles.find(file);
if (iter == mFiles.end())
throw std::runtime_error("file " + file + " not found");
return iter->second;
}
2012-12-26 17:15:53 +01:00
bool MultiDirCollection::doesExist(const std::string& file) const
{
return mFiles.find(file) != mFiles.end();
}
2011-05-05 19:00:00 +02:00
MultiDirCollection::TIter MultiDirCollection::begin() const
{
return mFiles.begin();
}
MultiDirCollection::TIter MultiDirCollection::end() const
{
return mFiles.end();
}
}