remove old file stuff from message

This commit is contained in:
Green Sky 2024-07-20 18:28:06 +02:00
parent f03c4b1210
commit 1c77a403be
No known key found for this signature in database
5 changed files with 0 additions and 231 deletions

View File

@ -1,20 +0,0 @@
#pragma once
#include <cstdint>
#include <vector>
struct FileI {
uint64_t _file_size {0};
// TODO: remove?
uint64_t _bytes_read {0};
uint64_t _bytes_written {0};
virtual ~FileI(void) {}
virtual bool isGood(void) = 0;
// TODO: move to owning/nonowning pointers
virtual std::vector<uint8_t> read(uint64_t pos, uint64_t size) = 0;
virtual bool write(uint64_t pos, const std::vector<uint8_t>& data) = 0;
};

View File

@ -1,56 +0,0 @@
#pragma once
#include "./file.hpp"
#include <fstream>
#include <ios>
#include <limits>
struct FileRFile : public FileI {
std::ifstream _file;
FileRFile(std::string_view file_path) : _file(static_cast<std::string>(file_path), std::ios::binary) {
if (!_file.is_open()) {
return; // TODO: error
}
// figure out size
_file.seekg(0, _file.end);
_file_size = _file.tellg();
_file.seekg(0, _file.beg);
}
//FileRFile(std::ifstream&& other_file) : _file(std::move(other_file)) {}
//FileRFile(std::ifstream&& other_file, size_t file_size) : _file(std::move(other_file)), _file_size(file_size) {}
virtual ~FileRFile(void) {}
bool isGood(void) override {
return _file.is_open() && _file.good();
}
std::vector<uint8_t> read(uint64_t pos, uint64_t size) override {
if (_file_size > 0 && pos >= _file_size) {
return {};
}
// TODO: error check
_file.seekg(pos);
std::vector<uint8_t> chunk(size);
const auto nread = _file.read(reinterpret_cast<char*>(chunk.data()), chunk.size()).gcount();
if (nread != std::numeric_limits<std::streamsize>::max()) {
chunk.resize(nread); // usually a noop
} else {
chunk.clear();
}
_bytes_read += chunk.size();
return chunk;
}
// read only
bool write(uint64_t, const std::vector<uint8_t>&) override { return false; }
};

View File

@ -1,38 +0,0 @@
#pragma once
#include "./file.hpp"
struct FileRMem : public FileI {
std::vector<uint8_t> _data;
FileRMem(void) = delete;
FileRMem(const std::vector<uint8_t>& data) : _data(data) {
_file_size = _data.size();
}
virtual ~FileRMem(void) {}
bool isGood(void) override {
return true;
}
std::vector<uint8_t> read(uint64_t pos, uint64_t size) override {
if (_file_size > 0 && pos >= _data.size()) {
return {};
}
// TODO: optimize
std::vector<uint8_t> chunk;
for (size_t i = 0; i < size && i+pos < _data.size(); i++) {
chunk.push_back(_data[pos+i]);
}
//chunk.insert(chunk.begin(), _data.cbegin()+pos, _data.cbegin()+pos+size);
_bytes_read += chunk.size();
return chunk;
}
// read only
bool write(uint64_t, const std::vector<uint8_t>&) override { return false; }
};

View File

@ -1,71 +0,0 @@
#pragma once
#include "./file.hpp"
#include <fstream>
#include <limits>
struct FileRWFile : public FileI {
std::fstream _file;
// dont truncate by default
FileRWFile(std::string_view file_path, uint64_t file_size, bool trunc = false)
: _file(
static_cast<std::string>(file_path),
std::ios::in |
std::ios::out |
(trunc ? std::ios::trunc | std::ios::binary : std::ios::binary) // hacky but type safe
) {
_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, uint64_t size) override {
if (pos >= _file_size) {
return {};
}
// TODO: error check
_file.seekg(pos, std::ios::beg);
std::vector<uint8_t> chunk(size);
const auto nread = _file.read(reinterpret_cast<char*>(chunk.data()), chunk.size()).gcount();
if (nread != std::numeric_limits<std::streamsize>::max()) {
chunk.resize(nread); // usually a noop
} else {
chunk.clear();
}
_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
if (_file.seekp(pos, std::ios::beg).fail()) {
return false;
}
}
_file.write(reinterpret_cast<const char*>(data.data()), data.size());
return _file.good();
}
};

View File

@ -1,46 +0,0 @@
#pragma once
#include "./file.hpp"
#include <string_view>
#include <fstream>
struct FileWFile : public FileI {
std::ofstream _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 ~FileWFile(void) {}
bool isGood(void) override {
return _file.is_open() && _file.good();
}
// write only
std::vector<uint8_t> read(uint64_t, uint64_t) override { return {}; }
bool write(uint64_t pos, const std::vector<uint8_t>& data) override {
if (_file_size > 0 && 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());
_bytes_written += data.size();
return _file.good();
}
};