56 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			56 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
 | 
						|
#pragma once
 | 
						|
 | 
						|
#if !defined(MIJIN_SOURCE_MIJIN_VIRTUAL_FILESYSTEM_MEMORY_HPP_INCLUDED)
 | 
						|
#define MIJIN_SOURCE_MIJIN_VIRTUAL_FILESYSTEM_MEMORY_HPP_INCLUDED 1
 | 
						|
 | 
						|
#include <variant>
 | 
						|
#include <vector>
 | 
						|
 | 
						|
#include "../container/vector_map.hpp"
 | 
						|
#include "../util/flag.hpp"
 | 
						|
#include "../virtual_filesystem/filesystem.hpp"
 | 
						|
 | 
						|
namespace mijin
 | 
						|
{
 | 
						|
namespace detail
 | 
						|
{
 | 
						|
struct MemoryFile
 | 
						|
{
 | 
						|
    std::span<const std::uint8_t> data;
 | 
						|
};
 | 
						|
struct MemoryFolder
 | 
						|
{
 | 
						|
    VectorMap<std::string, MemoryFile, std::allocator<std::string>, std::allocator<MemoryFile>> files; // TODO: make the FS library allocator aware
 | 
						|
    VectorMap<std::string, MemoryFolder, std::allocator<std::string>, std::allocator<MemoryFolder>> folders;
 | 
						|
};
 | 
						|
}
 | 
						|
 | 
						|
class MemoryFileSystemAdapter : public FileSystemAdapter
 | 
						|
{
 | 
						|
public:
 | 
						|
    MIJIN_DEFINE_FLAG(Overwrite);
 | 
						|
    MIJIN_DEFINE_FLAG(CopyData);
 | 
						|
private:
 | 
						|
    detail::MemoryFolder root_;
 | 
						|
    std::vector<std::vector<std::uint8_t>> fileData_;
 | 
						|
public:
 | 
						|
    [[nodiscard]] std::vector<FileInfo> listFiles(const fs::path& folder) override;
 | 
						|
    [[nodiscard]] FileInfo getFileInfo(const fs::path& file) override;
 | 
						|
    [[nodiscard]] StreamError open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream) override;
 | 
						|
 | 
						|
    bool addFile(const fs::path& path, std::span<const std::uint8_t> data, Overwrite overwrite = Overwrite::NO, CopyData copyData = CopyData::NO);
 | 
						|
    bool addFile(const fs::path& path, std::span<const std::uint8_t> data, CopyData copyData)
 | 
						|
    {
 | 
						|
        return addFile(path, data, Overwrite::NO, copyData);
 | 
						|
    }
 | 
						|
    void addFolder(const fs::path& path);
 | 
						|
private:
 | 
						|
    detail::MemoryFolder* findFolder(const fs::path& path, bool create = false) MIJIN_NOEXCEPT;
 | 
						|
    FileInfo folderInfo(const fs::path& path, const detail::MemoryFolder& folder) const MIJIN_NOEXCEPT;
 | 
						|
    FileInfo fileInfo(const fs::path& path, const detail::MemoryFile& file) const MIJIN_NOEXCEPT;
 | 
						|
};
 | 
						|
} // namespace mijin
 | 
						|
 | 
						|
#endif // !defined(MIJIN_SOURCE_MIJIN_VIRTUAL_FILESYSTEM_MEMORY_HPP_INCLUDED)
 |