137 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			137 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| 
 | |
| #include "./stacked.hpp"
 | |
| 
 | |
| #include <algorithm>
 | |
| #include "../detect.hpp"
 | |
| 
 | |
| namespace mijin
 | |
| {
 | |
| 
 | |
| //
 | |
| // internal defines
 | |
| //
 | |
| 
 | |
| //
 | |
| // internal constants
 | |
| //
 | |
| 
 | |
| //
 | |
| // internal types
 | |
| //
 | |
| 
 | |
| //
 | |
| // internal variables
 | |
| //
 | |
| 
 | |
| //
 | |
| // internal functions
 | |
| //
 | |
| 
 | |
| //
 | |
| // public functions
 | |
| //
 | |
| 
 | |
| fs::path StackedFileSystemAdapter::getHomeFolder()
 | |
| {
 | |
|     if (adapters_.empty()) {
 | |
|         return fs::path();
 | |
|     }
 | |
| #if MIJIN_COMPILER == MIJIN_COMPILER_MSVC
 | |
| #pragma warning(push)
 | |
| #pragma warning(disable : 4996) // yeah, we're using a deprecated function here, in order to implement another deprecated function ¯\_(ツ)_/¯
 | |
| #elif MIJIN_COMPILER == MIJIN_COMPILER_GCC || MIJIN_COMPILER == MIJIN_COMPILER_CLANG
 | |
| #pragma GCC diagnostic push
 | |
| #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
 | |
| #endif
 | |
|     return adapters_.front()->getHomeFolder();
 | |
| #if MIJIN_COMPILER == MIJIN_COMPILER_MSVC
 | |
| #pragma warning(pop)
 | |
| #elif MIJIN_COMPILER == MIJIN_COMPILER_GCC || MIJIN_COMPILER == MIJIN_COMPILER_CLANG
 | |
| #pragma GCC diagnostic pop
 | |
| #endif
 | |
| }
 | |
| 
 | |
| 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
 |