56 lines
1.8 KiB
C++
56 lines
1.8 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<FolderEntry> listFiles(PathView folder) override;
|
|
[[nodiscard]] FileInfo getFileInfo(PathView file) override;
|
|
[[nodiscard]] StreamError open(PathView path, FileOpenMode mode, std::unique_ptr<Stream>& outStream) override;
|
|
|
|
bool addFile(PathView path, std::span<const std::uint8_t> data, Overwrite overwrite = Overwrite::NO, CopyData copyData = CopyData::NO);
|
|
bool addFile(PathView path, std::span<const std::uint8_t> data, CopyData copyData)
|
|
{
|
|
return addFile(path, data, Overwrite::NO, copyData);
|
|
}
|
|
void addFolder(PathView path);
|
|
private:
|
|
detail::MemoryFolder* findFolder(PathView path, bool create = false) MIJIN_NOEXCEPT;
|
|
FileInfo folderInfo(const detail::MemoryFolder& folder) const MIJIN_NOEXCEPT;
|
|
FileInfo fileInfo(const detail::MemoryFile& file) const MIJIN_NOEXCEPT;
|
|
};
|
|
} // namespace mijin
|
|
|
|
#endif // !defined(MIJIN_SOURCE_MIJIN_VIRTUAL_FILESYSTEM_MEMORY_HPP_INCLUDED)
|