From b52343a428b76ef0fa74c6a939aef7eab33feedb Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Wed, 19 Sep 2018 21:58:33 -0400 Subject: [PATCH] vfs_vector: Add VectorVfsFile Maps a vector into the VFS interface. --- src/core/file_sys/vfs_vector.cpp | 51 ++++++++++++++++++++++++++++++++ src/core/file_sys/vfs_vector.h | 24 +++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/src/core/file_sys/vfs_vector.cpp b/src/core/file_sys/vfs_vector.cpp index ec7f735b5..efca3d4ad 100644 --- a/src/core/file_sys/vfs_vector.cpp +++ b/src/core/file_sys/vfs_vector.cpp @@ -7,6 +7,57 @@ #include "core/file_sys/vfs_vector.h" namespace FileSys { +VectorVfsFile::VectorVfsFile(std::vector initial_data, std::string name, VirtualDir parent) + : data(std::move(initial_data)), name(std::move(name)), parent(std::move(parent)) {} + +std::string VectorVfsFile::GetName() const { + return name; +} + +size_t VectorVfsFile::GetSize() const { + return data.size(); +} + +bool VectorVfsFile::Resize(size_t new_size) { + data.resize(new_size); + return data.size() == new_size; +} + +std::shared_ptr VectorVfsFile::GetContainingDirectory() const { + return parent; +} + +bool VectorVfsFile::IsWritable() const { + return true; +} + +bool VectorVfsFile::IsReadable() const { + return true; +} + +size_t VectorVfsFile::Read(u8* data_, size_t length, size_t offset) const { + const auto read = std::min(length, data.size() - offset); + std::memcpy(data_, data.data() + offset, read); + return read; +} + +size_t VectorVfsFile::Write(const u8* data_, size_t length, size_t offset) { + if (offset + length > data.size()) + data.resize(offset + length); + const auto write = std::min(length, data.size() - offset); + std::memcpy(data.data(), data_, write); + return write; +} + +bool VectorVfsFile::Rename(std::string_view name_) { + name = name_; + return true; +} + +void VectorVfsFile::Assign(std::vector new_data) { + data = new_data; +} + VectorVfsDirectory::VectorVfsDirectory(std::vector files_, std::vector dirs_, std::string name_, VirtualDir parent_) diff --git a/src/core/file_sys/vfs_vector.h b/src/core/file_sys/vfs_vector.h index cba44a7a6..c84e137a9 100644 --- a/src/core/file_sys/vfs_vector.h +++ b/src/core/file_sys/vfs_vector.h @@ -8,6 +8,30 @@ namespace FileSys { +// An implementation of VfsFile that is backed by a vector optionally supplied upon construction +class VectorVfsFile : public VfsFile { +public: + explicit VectorVfsFile(std::vector initial_data = {}, std::string name = "", + VirtualDir parent = nullptr); + + std::string GetName() const override; + size_t GetSize() const override; + bool Resize(size_t new_size) override; + std::shared_ptr GetContainingDirectory() const override; + bool IsWritable() const override; + bool IsReadable() const override; + size_t Read(u8* data, size_t length, size_t offset) const override; + size_t Write(const u8* data, size_t length, size_t offset) override; + bool Rename(std::string_view name) override; + + virtual void Assign(std::vector new_data); + +private: + std::vector data; + VirtualDir parent; + std::string name; +}; + // An implementation of VfsDirectory that maintains two vectors for subdirectories and files. // Vector data is supplied upon construction. class VectorVfsDirectory : public VfsDirectory {