From 913976015adfd128bd80c8d7930514e35ffe3344 Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Thu, 19 Dec 2019 02:42:56 +0100 Subject: [PATCH 1/4] Fixed typos --- flint.yaml | 3 +-- test.bin | Bin 0 -> 4 bytes test/src/test.cpp | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) create mode 100644 test.bin diff --git a/flint.yaml b/flint.yaml index 48840cc..d922775 100644 --- a/flint.yaml +++ b/flint.yaml @@ -10,7 +10,6 @@ test: - logging - test_framework - fmt: type: lib git: @@ -31,4 +30,4 @@ test_framework: url: https://git.mtgames.nl/Dreaded_X/test_framework revision: master dependency: - - logger + - logging diff --git a/test.bin b/test.bin new file mode 100644 index 0000000000000000000000000000000000000000..3b6fc84f4eecaa6f1b4fc34beba7e3a80f68e98d GIT binary patch literal 4 LcmZQzU| #include From ccf0707a0e7d87771038ddd7fba2d29bebedb4c8 Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Wed, 6 May 2020 18:24:39 +0200 Subject: [PATCH 2/4] Updated flint file to new format --- flint.yaml | 49 ++++++++++++++++++------------------------------- 1 file changed, 18 insertions(+), 31 deletions(-) diff --git a/flint.yaml b/flint.yaml index d922775..7a17da8 100644 --- a/flint.yaml +++ b/flint.yaml @@ -1,33 +1,20 @@ -io: - type: lib - path: . - -test: - type: exe - path: test - dependency: - - io - - logging - - test_framework - -fmt: - type: lib - git: - url: https://github.com/fmtlib/fmt - revision: 6.1.2 - -logging: - type: lib - git: - url: https://git.mtgames.nl/Dreaded_X/logging - revision: master - dependency: - - fmt - -test_framework: - type: lib - git: +git: + test_framework: url: https://git.mtgames.nl/Dreaded_X/test_framework revision: master - dependency: - - logging + +include: + - .build/git/test_framework/flint.yaml + +targets: + io: + type: lib + path: . + + test: + type: exe + path: test + dependency: + - io + - logging + - test_framework From 7201035a207fcdefc59179707b887d4b1ac59353 Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Wed, 6 May 2020 18:50:48 +0200 Subject: [PATCH 3/4] Added read/write for float (kind of hacky, assumes IEE) --- include/io/read.h | 12 ++++++++++++ include/io/write.h | 11 +++++++++++ test/src/test.cpp | 18 ++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/include/io/read.h b/include/io/read.h index 29b607f..a83a71a 100644 --- a/include/io/read.h +++ b/include/io/read.h @@ -35,6 +35,18 @@ namespace io { return value; } + template <> + float read(std::istream& is) { + union { + uint32_t a; + float b; + } u; + + u.a = read_bytes(is, sizeof(uint32_t)); + + return u.b; + } + template<> size_t read(std::istream& is) { size_t value = 0; diff --git a/include/io/write.h b/include/io/write.h index aa96804..3ed511b 100644 --- a/include/io/write.h +++ b/include/io/write.h @@ -31,6 +31,17 @@ namespace io { write_bytes(os, value, sizeof(T)); } + template <> + void write(std::ostream& os, float value) { + union { + uint32_t a; + float b; + } u; + + u.b = value; + write_bytes(os, u.a, sizeof(uint32_t)); + } + // Special implementation for size_t (also uint64_t, so maybe this is not that smart) template <> void write(std::ostream& os, size_t value) { diff --git a/test/src/test.cpp b/test/src/test.cpp index bb67435..dce3c62 100644 --- a/test/src/test.cpp +++ b/test/src/test.cpp @@ -74,6 +74,24 @@ int main() { return io::read(f) == 80085; }()); + Test("Write for float", [] { + std::fstream f("test.bin", std::ios::trunc | std::ios::in | std::ios::out); + io::write(f, 123.654f); + + bool succes = 4 == f.tellg(); f.seekg(0); char c; + f.read(&c,1); succes &= (c & 0xff) == 0x42; + f.read(&c,1); succes &= (c & 0xff) == 0xf7; + f.read(&c,1); succes &= (c & 0xff) == 0x4e; + f.read(&c,1); succes &= (c & 0xff) == 0xd9; + return succes; + }()); + + Test("Read for float", [] { + std::ifstream f("test.bin"); + return io::read(f) == 123.654f; + }()); + + Test("Write for short size_t", [] { std::fstream f("test.bin", std::ios::trunc | std::ios::in | std::ios::out); io::write(f, 12); From d3c63370684ea7b659bb805cf0d00eb6cded654b Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Wed, 6 May 2020 21:23:59 +0200 Subject: [PATCH 4/4] Fixed issies with multiple definitions --- include/io/read.h | 24 ++---------------------- include/io/write.h | 28 ++-------------------------- src/read.cpp | 27 +++++++++++++++++++++++++++ src/write.cpp | 33 ++++++++++++++++++++++++++++++++- 4 files changed, 63 insertions(+), 49 deletions(-) create mode 100644 src/read.cpp diff --git a/include/io/read.h b/include/io/read.h index a83a71a..7a0447e 100644 --- a/include/io/read.h +++ b/include/io/read.h @@ -36,30 +36,10 @@ namespace io { } template <> - float read(std::istream& is) { - union { - uint32_t a; - float b; - } u; - - u.a = read_bytes(is, sizeof(uint32_t)); - - return u.b; - } + float read(std::istream& is); template<> - size_t read(std::istream& is) { - size_t value = 0; - uint8_t length = io::read(is); - if (length & 0b10000000) { - value = read_bytes(is, length & 0b01111111); - } else { - value = length; - } - return value; - } - - + size_t read(std::istream& is); template requires std::is_convertible_v T read(std::istream& is, size_t length) { diff --git a/include/io/write.h b/include/io/write.h index 3ed511b..2d83b19 100644 --- a/include/io/write.h +++ b/include/io/write.h @@ -32,35 +32,11 @@ namespace io { } template <> - void write(std::ostream& os, float value) { - union { - uint32_t a; - float b; - } u; - - u.b = value; - write_bytes(os, u.a, sizeof(uint32_t)); - } + void write(std::ostream& os, float value); // Special implementation for size_t (also uint64_t, so maybe this is not that smart) template <> - void write(std::ostream& os, size_t value) { - // Check if we need more then one byte - if (value > 0b01111111) { - // Calculate how many bytes we need to store the number - uint8_t length = 0; - auto x = value; - while (x != 0) { - x >>= 8; - length++; - } - - write(os, length | 0b10000000); - write_bytes(os, value, length); - } else { - write(os, value); - } - } + void write(std::ostream& os, size_t value); template requires std::is_convertible_v void write(std::ostream& os, T value, bool store_length = true) { diff --git a/src/read.cpp b/src/read.cpp new file mode 100644 index 0000000..1b22f4d --- /dev/null +++ b/src/read.cpp @@ -0,0 +1,27 @@ +#include "io/read.h" + +namespace io { + template<> + float read(std::istream& is) { + union { + uint32_t a; + float b; + } u; + + u.a = read_bytes(is, sizeof(uint32_t)); + + return u.b; + } + + template<> + size_t read(std::istream& is) { + size_t value = 0; + uint8_t length = io::read(is); + if (length & 0b10000000) { + value = read_bytes(is, length & 0b01111111); + } else { + value = length; + } + return value; + } +} diff --git a/src/write.cpp b/src/write.cpp index 4b37887..555fc8b 100644 --- a/src/write.cpp +++ b/src/write.cpp @@ -1,3 +1,34 @@ #include "io/write.h" -// We do not implement anything without templates +namespace io { + template<> + void write(std::ostream& os, float value) { + union { + uint32_t a; + float b; + } u; + + u.b = value; + write_bytes(os, u.a, sizeof(uint32_t)); + } + + // Special implementation for size_t (also uint64_t, so maybe this is not that smart) + template<> + void write(std::ostream& os, size_t value) { + // Check if we need more then one byte + if (value > 0b01111111) { + // Calculate how many bytes we need to store the number + uint8_t length = 0; + auto x = value; + while (x != 0) { + x >>= 8; + length++; + } + + write(os, length | 0b10000000); + write_bytes(os, value, length); + } else { + write(os, value); + } + } +}