119 lines
2.2 KiB
C++
119 lines
2.2 KiB
C++
|
|
#include "./stacked.hpp"
|
|
|
|
#include <algorithm>
|
|
|
|
namespace mijin
|
|
{
|
|
|
|
//
|
|
// internal defines
|
|
//
|
|
|
|
//
|
|
// internal constants
|
|
//
|
|
|
|
//
|
|
// internal types
|
|
//
|
|
|
|
//
|
|
// internal variables
|
|
//
|
|
|
|
//
|
|
// internal functions
|
|
//
|
|
|
|
//
|
|
// public functions
|
|
//
|
|
|
|
std::vector<fs::path> StackedFileSystemAdapter::getRoots()
|
|
{
|
|
std::vector<fs::path> roots;
|
|
|
|
for (auto& adapter : adapters_)
|
|
{
|
|
for (const fs::path& root : adapter->getRoots())
|
|
{
|
|
auto it = std::find(roots.begin(), roots.end(), root);
|
|
if (it == roots.end()) {
|
|
roots.push_back(root);
|
|
}
|
|
}
|
|
}
|
|
|
|
return roots;
|
|
}
|
|
|
|
fs::path StackedFileSystemAdapter::getHomeFolder()
|
|
{
|
|
if (adapters_.empty()) {
|
|
return fs::path();
|
|
}
|
|
return adapters_.front()->getHomeFolder();
|
|
}
|
|
|
|
std::vector<FileInfo> StackedFileSystemAdapter::listFiles(const fs::path& folder)
|
|
{
|
|
std::vector<FileInfo> files;
|
|
|
|
for (auto& adapter : adapters_)
|
|
{
|
|
for (const FileInfo& file : adapter->listFiles(folder))
|
|
{
|
|
auto it = std::find_if(files.begin(), files.end(), [&](const FileInfo& existing)
|
|
{
|
|
return existing.path == file.path;
|
|
});
|
|
if (it == files.end()) {
|
|
files.push_back(file);
|
|
}
|
|
}
|
|
}
|
|
|
|
return files;
|
|
}
|
|
|
|
FileInfo StackedFileSystemAdapter::getFileInfo(const fs::path& file)
|
|
{
|
|
for (auto& adapter : adapters_)
|
|
{
|
|
FileInfo fileInfo = adapter->getFileInfo(file);
|
|
if (fileInfo.exists) {
|
|
return fileInfo;
|
|
}
|
|
}
|
|
|
|
return {};
|
|
}
|
|
|
|
Optional<fs::path> StackedFileSystemAdapter::getNativePath(const fs::path& file)
|
|
{
|
|
for (auto& adapter : adapters_)
|
|
{
|
|
Optional<fs::path> result = adapter->getNativePath(file);
|
|
if (!result.empty())
|
|
{
|
|
return result;
|
|
}
|
|
}
|
|
return NULL_OPTIONAL;
|
|
}
|
|
|
|
StreamError StackedFileSystemAdapter::open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream)
|
|
{
|
|
for (auto& adapter : adapters_)
|
|
{
|
|
const FileInfo fileInfo = adapter->getFileInfo(path);
|
|
if (fileInfo.exists) {
|
|
return adapter->open(path, mode, outStream);
|
|
}
|
|
}
|
|
return StreamError::IO_ERROR;
|
|
}
|
|
|
|
} // namespace mijin
|