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