2023-05-29 14:51:44 +02:00

106 lines
1.9 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 {};
}
StreamError StackedFileSystemAdapter::open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream)
{
for (auto& adapter : adapters_)
{
FileInfo fileInfo = adapter->getFileInfo(path);
if (fileInfo.exists) {
return adapter->open(path, mode, outStream);
}
}
return StreamError::IO_ERROR;
}
} // namespace mijin