124 lines
2.5 KiB
C++

#include "./stacked.hpp"
#include <algorithm>
namespace mijin
{
//
// internal defines
//
//
// internal constants
//
//
// internal types
//
//
// internal variables
//
//
// internal functions
//
//
// public functions
//
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)
{
// try to open existing files first
for (auto& adapter : adapters_)
{
const FileInfo fileInfo = adapter->getFileInfo(path);
if (fileInfo.exists)
{
return adapter->open(path, mode, outStream);
}
}
// if that doesn't work we attempt to write, try creating it
if (mode == FileOpenMode::WRITE || mode == FileOpenMode::READ_WRITE)
{
for (auto& adapter : adapters_)
{
const StreamError error = adapter->open(path, mode, outStream);
if (error == StreamError::SUCCESS)
{
return StreamError::SUCCESS;
}
}
}
return StreamError::IO_ERROR;
}
void StackedFileSystemAdapter::getAllPaths(const fs::path& path, std::vector<PathReference>& outPaths)
{
for (auto& adapter : adapters_)
{
adapter->getAllPaths(path, outPaths);
}
}
} // namespace mijin