inital commit (message v3)
This commit is contained in:
60
solanaceae/message3/file_rw_file.hpp
Normal file
60
solanaceae/message3/file_rw_file.hpp
Normal file
@ -0,0 +1,60 @@
|
||||
#pragma once
|
||||
|
||||
#include "./file.hpp"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
struct FileRWFile : public FileI {
|
||||
std::fstream _file;
|
||||
|
||||
//FileWFile(std::string_view file_path, uint64_t file_size) : _file(static_cast<std::string>(file_path), std::ios::binary) {
|
||||
//_file_size = file_size;
|
||||
|
||||
//if (!_file.is_open()) {
|
||||
//return; // TODO: error
|
||||
//}
|
||||
//}
|
||||
|
||||
virtual ~FileRWFile(void) {}
|
||||
|
||||
bool isGood(void) override {
|
||||
return _file.is_open() && _file.good();
|
||||
}
|
||||
|
||||
std::vector<uint8_t> read(uint64_t pos, uint32_t size) override {
|
||||
if (pos >= _file_size) {
|
||||
return {};
|
||||
}
|
||||
|
||||
// TODO: error check
|
||||
_file.seekg(pos);
|
||||
|
||||
// TODO: optimize
|
||||
std::vector<uint8_t> chunk;
|
||||
int read_char;
|
||||
for (size_t i = 0; i < size && (_file_size == 0 || i+pos < _file_size) && (read_char = _file.get()) != std::ifstream::traits_type::eof(); i++) {
|
||||
chunk.push_back(read_char);
|
||||
}
|
||||
|
||||
_bytes_read += chunk.size();
|
||||
|
||||
return chunk;
|
||||
}
|
||||
|
||||
bool write(uint64_t pos, const std::vector<uint8_t>& data) override {
|
||||
if (pos >= _file_size) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// if out-of-order, seek
|
||||
if (_file.tellp() != int64_t(pos)) {
|
||||
// TODO: error check
|
||||
_file.seekp(pos);
|
||||
}
|
||||
|
||||
_file.write(reinterpret_cast<const char*>(data.data()), data.size());
|
||||
|
||||
return _file.good();
|
||||
}
|
||||
};
|
||||
|
Reference in New Issue
Block a user