From 53d65a06855129135e5884f50d9983676290ac24 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Thu, 17 Aug 2023 22:40:46 +0200 Subject: [PATCH] get file rw working --- solanaceae/message3/file_rw_file.hpp | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/solanaceae/message3/file_rw_file.hpp b/solanaceae/message3/file_rw_file.hpp index 43604a7..8644cf7 100644 --- a/solanaceae/message3/file_rw_file.hpp +++ b/solanaceae/message3/file_rw_file.hpp @@ -8,13 +8,20 @@ struct FileRWFile : public FileI { std::fstream _file; - //FileWFile(std::string_view file_path, uint64_t file_size) : _file(static_cast(file_path), std::ios::binary) { - //_file_size = file_size; + // dont truncate by default + FileRWFile(std::string_view file_path, uint64_t file_size, bool trunc = false) + : _file( + static_cast(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 - //} - //} + if (!_file.is_open()) { + return; // TODO: error + } + } virtual ~FileRWFile(void) {} @@ -28,7 +35,7 @@ struct FileRWFile : public FileI { } // TODO: error check - _file.seekg(pos); + _file.seekg(pos, std::ios::beg); #if 0 std::vector chunk; @@ -59,7 +66,7 @@ struct FileRWFile : public FileI { // if out-of-order, seek if (_file.tellp() != int64_t(pos)) { // TODO: error check - _file.seekp(pos); + _file.seekp(pos, std::ios::beg); } _file.write(reinterpret_cast(data.data()), data.size());