diff options
| author | hovertank3d <[email protected]> | 2025-11-15 18:31:32 +0100 |
|---|---|---|
| committer | hovertank3d <[email protected]> | 2025-11-15 18:31:32 +0100 |
| commit | c2b53f27bad24003867afa5f5f7aedcf6fa7b26d (patch) | |
| tree | b1cbc5ae52957df6c6aeba60c7be5ae35872e709 /include/util/io.hpp | |
| parent | fa318825ef0404816728f733484caa1bc23afa53 (diff) | |
| download | ogurec-c2b53f27bad24003867afa5f5f7aedcf6fa7b26d.tar.gz ogurec-c2b53f27bad24003867afa5f5f7aedcf6fa7b26d.zip | |
minor fixes
Diffstat (limited to 'include/util/io.hpp')
| -rw-r--r-- | include/util/io.hpp | 22 |
1 files changed, 16 insertions, 6 deletions
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<char*>(&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<char*>(&x), sizeof(T)); + return io.write_data(reinterpret_cast<const char*>(&x), sizeof(T)); } template <std::floating_point T> ssize_t write(const T f) { // FIXME: endianness - return io.write_data(reinterpret_cast<char*>(&f), sizeof(T)); + return io.write_data(reinterpret_cast<const char*>(&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; } |
