From c2b53f27bad24003867afa5f5f7aedcf6fa7b26d Mon Sep 17 00:00:00 2001 From: hovertank3d Date: Sat, 15 Nov 2025 18:31:32 +0100 Subject: minor fixes --- include/util/io.hpp | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'include/util') diff --git a/include/util/io.hpp b/include/util/io.hpp index 8cfaf4c..e99c86a 100644 --- a/include/util/io.hpp +++ b/include/util/io.hpp @@ -41,6 +41,8 @@ public: { } + struct eof {}; + file_io(std::string filename) : fd(open(filename.c_str(), O_RDWR)) { @@ -56,13 +58,21 @@ public: ssize_t read_data(char* buf, size_t nbytes) { auto bytes = ::read(fd, buf, nbytes); + if (bytes == EOF) { + throw eof{}; + } + assert(bytes >= 0); return bytes; } ssize_t write_data(const char* buf, size_t nbytes) { - auto bytes = ::write(fd, buf, nbytes); + auto bytes = ::write(fd, buf, nbytes); + if (bytes == EOF) { + throw eof{}; + } + assert(bytes >= 0); return bytes; } @@ -117,7 +127,7 @@ public: ssize_t read(T& f) { auto bytes = io.read_data(reinterpret_cast(&f), sizeof(T)); - f = to_little(f); + //f = to_little(f); return bytes; } @@ -158,21 +168,21 @@ public: ssize_t write(const T f) { auto x = to_little(f); - return io.write_data(reinterpret_cast(&x), sizeof(T)); + return io.write_data(reinterpret_cast(&x), sizeof(T)); } template ssize_t write(const T f) { // FIXME: endianness - return io.write_data(reinterpret_cast(&f), sizeof(T)); + return io.write_data(reinterpret_cast(&f), sizeof(T)); } ssize_t write(const std::string f) { // FIXME: varint - auto bytes = write(f.size()); - bytes += io.write_data(f.data(), f.size()); + auto bytes = write(uint8_t(f.size())); + bytes += io.write_data(f.c_str(), f.size()); return bytes; } -- cgit v1.2.3