2011-05-05 19:00:00 +02:00
|
|
|
#include "collections.hpp"
|
2022-07-03 00:02:29 +02:00
|
|
|
#include "conversion.hpp"
|
2011-05-05 19:00:00 +02:00
|
|
|
|
2022-08-03 00:00:54 +02:00
|
|
|
#include <components/misc/strings/algorithm.hpp>
|
|
|
|
#include <components/misc/strings/lower.hpp>
|
2014-05-26 17:34:36 +02:00
|
|
|
|
2011-05-05 19:00:00 +02:00
|
|
|
namespace Files
|
|
|
|
{
|
2011-08-19 21:06:09 +02:00
|
|
|
Collections::Collections()
|
|
|
|
: mDirectories()
|
|
|
|
, mCollections()
|
|
|
|
{
|
|
|
|
}
|
2011-05-05 19:00:00 +02:00
|
|
|
|
2023-05-31 23:11:03 +02:00
|
|
|
Collections::Collections(const Files::PathContainer& directories)
|
2011-08-19 21:06:09 +02:00
|
|
|
: mDirectories(directories)
|
|
|
|
, mCollections()
|
2011-05-05 19:00:00 +02:00
|
|
|
{
|
2011-08-19 21:06:09 +02:00
|
|
|
}
|
2011-05-05 19:00:00 +02:00
|
|
|
|
2011-08-19 21:06:09 +02:00
|
|
|
const MultiDirCollection& Collections::getCollection(const std::string& extension) const
|
|
|
|
{
|
2022-07-14 17:02:35 +03:00
|
|
|
std::string ext = Misc::StringUtils::lowerCase(extension);
|
2022-07-14 16:50:25 +00:00
|
|
|
auto iter = mCollections.find(ext);
|
2011-05-05 19:00:00 +02:00
|
|
|
if (iter == mCollections.end())
|
|
|
|
{
|
2011-08-19 21:06:09 +02:00
|
|
|
std::pair<MultiDirCollectionContainer::iterator, bool> result
|
2023-05-31 23:11:03 +02:00
|
|
|
= mCollections.emplace(ext, MultiDirCollection(mDirectories, ext));
|
2011-05-05 19:00:00 +02:00
|
|
|
|
|
|
|
iter = result.first;
|
|
|
|
}
|
|
|
|
|
|
|
|
return iter->second;
|
|
|
|
}
|
2012-03-30 14:45:32 +02:00
|
|
|
|
2022-06-08 23:25:50 +02:00
|
|
|
std::filesystem::path Collections::getPath(const std::string& file) const
|
2013-03-09 21:08:08 +01:00
|
|
|
{
|
2023-02-11 01:25:00 +03:00
|
|
|
for (auto iter = mDirectories.rbegin(); iter != mDirectories.rend(); iter++)
|
2013-03-09 21:08:08 +01:00
|
|
|
{
|
2023-02-11 01:25:00 +03:00
|
|
|
for (const auto& iter2 : std::filesystem::directory_iterator(*iter))
|
2014-05-26 17:34:36 +02:00
|
|
|
{
|
2022-06-19 13:28:33 +02:00
|
|
|
const auto& path = iter2.path();
|
2022-07-03 00:02:29 +02:00
|
|
|
const auto str = Files::pathToUnicodeString(path.filename());
|
|
|
|
|
2023-05-31 23:11:03 +02:00
|
|
|
if (Misc::StringUtils::ciEqual(file, str))
|
2022-06-19 13:28:33 +02:00
|
|
|
return path;
|
2014-05-26 17:34:36 +02:00
|
|
|
}
|
2013-03-09 21:08:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
throw std::runtime_error("file " + file + " not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Collections::doesExist(const std::string& file) const
|
|
|
|
{
|
2023-02-11 01:25:00 +03:00
|
|
|
for (auto iter = mDirectories.rbegin(); iter != mDirectories.rend(); iter++)
|
2013-03-09 21:08:08 +01:00
|
|
|
{
|
2023-02-11 01:25:00 +03:00
|
|
|
for (const auto& iter2 : std::filesystem::directory_iterator(*iter))
|
2014-05-26 17:34:36 +02:00
|
|
|
{
|
2022-06-19 13:28:33 +02:00
|
|
|
const auto& path = iter2.path();
|
2022-07-03 00:02:29 +02:00
|
|
|
const auto str = Files::pathToUnicodeString(path.filename());
|
2014-05-26 17:34:36 +02:00
|
|
|
|
2023-05-31 23:11:03 +02:00
|
|
|
if (Misc::StringUtils::ciEqual(file, str))
|
2014-05-26 17:34:36 +02:00
|
|
|
return true;
|
|
|
|
}
|
2013-03-09 21:08:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-03-30 14:45:32 +02:00
|
|
|
const Files::PathContainer& Collections::getPaths() const
|
|
|
|
{
|
|
|
|
return mDirectories;
|
|
|
|
}
|
2011-05-05 19:00:00 +02:00
|
|
|
}
|