From b9c64fcffbd860c44696c62bf480cbf9e4b8248e Mon Sep 17 00:00:00 2001 From: jyelon Date: Tue, 25 Jul 2023 16:22:51 -0400 Subject: [PATCH] More tweaks to base-writer --- luprex/Makefile | 8 ++-- luprex/cpp/core/streambuffer.cpp | 1 - luprex/cpp/core/streambuffer.hpp | 12 ++--- luprex/{cpp/core => ext}/base-writer.hpp | 57 ++++++++++++------------ 4 files changed, 40 insertions(+), 38 deletions(-) rename luprex/{cpp/core => ext}/base-writer.hpp (81%) diff --git a/luprex/Makefile b/luprex/Makefile index e29e91f5..8deead6f 100644 --- a/luprex/Makefile +++ b/luprex/Makefile @@ -197,12 +197,12 @@ build/$(OS)/eris/%.obj: ext/eris-master/src/%.c build/$(OS)/DIRECTORY $(COMPILE) $@ $(LUA_FLAGS) $< build/$(OS)/core/%.obj: cpp/core/%.cpp build/$(OS)/DIRECTORY - $(MAKEDEPS) $@d -I./ext/eris-master/src -I./cpp/wrap -I./cpp/core $< - $(COMPILE) $@ -I./ext/eris-master/src -I./cpp/wrap -I./cpp/core $< + $(MAKEDEPS) $@d -I./ext/eris-master/src -I./cpp/wrap -I./cpp/core -I./ext $< + $(COMPILE) $@ -I./ext/eris-master/src -I./cpp/wrap -I./cpp/core -I./ext $< build/$(OS)/drv/%.obj: cpp/drv/%.cpp build/$(OS)/DIRECTORY - $(MAKEDEPS) $@d $(OPENSSL_INCLUDE) -I./src/drv $< - $(COMPILE) $@ $(OPENSSL_INCLUDE) -I./src/drv $< + $(MAKEDEPS) $@d $(OPENSSL_INCLUDE) -I./src/drv -I./ext $< + $(COMPILE) $@ $(OPENSSL_INCLUDE) -I./src/drv -I./ext $< clean: rm -f luprex* luprex*.* *.pdb diff --git a/luprex/cpp/core/streambuffer.cpp b/luprex/cpp/core/streambuffer.cpp index 58449d07..7e92471c 100644 --- a/luprex/cpp/core/streambuffer.cpp +++ b/luprex/cpp/core/streambuffer.cpp @@ -169,7 +169,6 @@ void StreamBuffer::read_bytes_into(char *data, int64_t size) { read_cursor_ += size; } - void StreamBuffer::write_xyz(const util::XYZ &xyz) { make_space(12); memcpy(write_cursor_, &xyz.x, 4); diff --git a/luprex/cpp/core/streambuffer.hpp b/luprex/cpp/core/streambuffer.hpp index 6db4ecb0..5355512c 100644 --- a/luprex/cpp/core/streambuffer.hpp +++ b/luprex/cpp/core/streambuffer.hpp @@ -241,8 +241,10 @@ public: virtual char const *what() const { return "Stream contained invalid data"; } }; -class StreamBuffer : public eng::nevernew, public BaseReader, public BaseWriter { +class StreamBuffer : public eng::nevernew, public BaseReader, public BaseWriter { public: + using read_string_type = eng::string; + // Construct an empty buffer. StreamBuffer(); @@ -290,10 +292,6 @@ public: void write_bytes(const char *bytes, int64_t len); void write_bytes(std::string_view s); - // Copy bytes from the StreamBuffer into an external buffer. - // - void read_bytes_into(char *target, int64_t len); - // Read a block of bytes from the buffer. // // Caution: the pointer returned is a pointer to the stream's buffer. It is @@ -302,6 +300,10 @@ public: // const char *read_bytes(int64_t bytes); + // Copy bytes from the StreamBuffer into an external buffer. + // + void read_bytes_into(char *target, int64_t len); + // Read and write larger types. // // Throws StreamEof if the specified number of bytes aren't present. diff --git a/luprex/cpp/core/base-writer.hpp b/luprex/ext/base-writer.hpp similarity index 81% rename from luprex/cpp/core/base-writer.hpp rename to luprex/ext/base-writer.hpp index 8ba7fa55..f44c9462 100644 --- a/luprex/cpp/core/base-writer.hpp +++ b/luprex/ext/base-writer.hpp @@ -28,8 +28,8 @@ // void write_char(char c) // void write_float(float data) // void write_double(double data) -// void write_bytes(std::string_view bytes) -// void write_string(std::string_view bytes) +// void write_length(size_t data) +// void write_string(std::string_view data) // // In order to derive from BaseWriter, you must use the CRTP pattern: // @@ -74,16 +74,19 @@ public: void write_float(float arg) { write_value_core(arg); } void write_double(double arg) { write_value_core(arg); } - void write_string(std::string_view s) { - if (s.size() >= 255) { + void write_length(size_t len) { + if (len >= 255) { write_uint8(0xFF); - write_uint64(s.size()); - static_cast(this)->write_bytes(s.data(), s.size()); + write_uint64(len); } else { - write_uint8(s.size()); - static_cast(this)->write_bytes(s.data(), s.size()); + write_uint8(len); } } + + void write_string(std::string_view s) { + write_length(s.size()); + static_cast(this)->write_bytes(s.data(), s.size()); + } }; /////////////////////////////////////////////////////////////// @@ -104,8 +107,9 @@ public: // char read_char(); // float read_float(); // double read_double(); -// string read_string_limit(int64_t size); -// string read_string(); +// size_t read_length(); +// String read_string_limit(uint64_t size); +// String read_string(); // // In order to derive from BaseReader, you must use the CRTP pattern: // @@ -113,18 +117,15 @@ public: // // The derived class must provide: // -// using string = std::string (or similar) -// +// using read_string_type = std::string; // or similar // void read_bytes_into(char *n, size_t size) -// // void raise_string_too_long(); // /////////////////////////////////////////////////////////////// -template +template class BaseReader { protected: - template T read_value_core() { T result; @@ -148,22 +149,22 @@ public: float read_float() { return read_value_core(); } double read_double() { return read_value_core(); } - String read_string_limit(int64_t limit) { - uint64_t size = read_uint8(); - if (size == 255) { - size = read_uint64(); + size_t read_length() { + uint64_t len = read_uint8(); + if (len == 255) { + len = read_uint64(); } - if ((limit < 0)||(size > uint64_t(limit))) { - static_cast(this)->raise_string_too_long(); - } - String result; - result.resize(size); - static_cast(this)->read_bytes_into(&result[0], size); + return len; + } + + auto read_string_limit(uint64_t limit) { + size_t len = read_length(); + if (len > limit) static_cast(this)->raise_string_too_long(); + typename Derived::read_string_type result(len, ' '); + static_cast(this)->read_bytes_into(&(result[0]), len); return result; } - String read_string() { - return read_string_limit(0xFFFFFFF); - } + auto read_string() { return read_string_limit(0x1000000); } // 16MB limit default };