add in memory read and write for file2

This commit is contained in:
Green Sky 2024-04-03 21:03:04 +02:00
parent 17b4cce69b
commit 659b410caa
No known key found for this signature in database
3 changed files with 93 additions and 0 deletions

View File

@ -28,6 +28,8 @@ add_library(solanaceae_file2
./solanaceae/file/file2.hpp
./solanaceae/file/file2_std.hpp
./solanaceae/file/file2_std.cpp
./solanaceae/file/file2_mem.hpp
./solanaceae/file/file2_mem.cpp
)
target_include_directories(solanaceae_file2 PUBLIC .)

View File

@ -0,0 +1,59 @@
#include "./file2_mem.hpp"
File2MemW::File2MemW(std::vector<uint8_t>& mem) : File2I(true, false), _mem(mem) {
}
File2MemW::~File2MemW(void) {
}
bool File2MemW::isGood(void) {
// TODO: are there any error cases?
return true;
}
bool File2MemW::write(const ByteSpan data, int64_t pos) {
if (pos != -1) { // only support streaming
return false;
}
if (data.empty()) {
return false; // true instead?
}
_mem.insert(_mem.cend(), data.cbegin(), data.cend());
return isGood();
}
std::variant<ByteSpan, std::vector<uint8_t>> File2MemW::read(uint64_t size, int64_t pos) {
return {};
}
File2MemR::File2MemR(ByteSpan mem) : File2I(false, true), _mem(mem) {
}
File2MemR::~File2MemR(void) {
}
bool File2MemR::isGood(void) {
// TODO: return false if eof?
return true;
}
bool File2MemR::write(const ByteSpan data, int64_t pos) {
return false;
}
std::variant<ByteSpan, std::vector<uint8_t>> File2MemR::read(uint64_t size, int64_t pos) {
if (_read_pos >= _mem.size) {
return {};
}
ByteSpan ret {
_mem.ptr + _read_pos,
std::min(size, _mem.size - _read_pos)
};
_read_pos += ret.size;
return ret;
}

View File

@ -0,0 +1,32 @@
#pragma once
#include "./file2.hpp"
// write to vector or read from span
// no seeking for now (could make sense for read)
struct File2MemW : public File2I {
std::vector<uint8_t>& _mem;
File2MemW(std::vector<uint8_t>& mem);
virtual ~File2MemW(void);
bool isGood(void) override;
bool write(const ByteSpan data, int64_t pos = -1) override;
std::variant<ByteSpan, std::vector<uint8_t>> read(uint64_t size, int64_t pos = -1) override;
};
struct File2MemR : public File2I {
ByteSpan _mem;
int64_t _read_pos{0};
File2MemR(ByteSpan mem);
virtual ~File2MemR(void);
bool isGood(void) override;
bool write(const ByteSpan data, int64_t pos = -1) override;
std::variant<ByteSpan, std::vector<uint8_t>> read(uint64_t size, int64_t pos = -1) override;
};