diff --git a/src/file_deserialising.h b/src/file_deserialising.h index cba7e1d..b07f57e 100644 --- a/src/file_deserialising.h +++ b/src/file_deserialising.h @@ -29,13 +29,14 @@ namespace file_reader { + template struct parsing_buffer { - parsing_buffer(buffer &b) + parsing_buffer(buffer &b) :buf(b) {} - buffer &buf; - char *point; + buffer &buf; + T *point; int consumed_size; }; @@ -61,6 +62,28 @@ namespace file_reader } } + template + static void read_type(T& val, unsigned char *data) + { + std::memcpy(&val, data, sizeof(T)); + switch (sizeof(T)) + { + case 1: + break; + case 2: + val = be16toh(val); + break; + case 4: + val = be32toh(val); + break; + case 8: + val = be64toh(val); + break; + default: + std::println("This integer is not supported!"); + } + } + template <> inline void read_type(float &val, char *data) { @@ -71,6 +94,16 @@ namespace file_reader memcpy(&val, &num_as_uint32, sizeof(float)); } + template <> + inline void read_type(float &val, unsigned char *data) + { + uint32_t num_as_uint32; + + memcpy(&num_as_uint32, data, sizeof(float)); + num_as_uint32 = be32toh(num_as_uint32); + memcpy(&val, &num_as_uint32, sizeof(float)); + } + template <> inline void read_type(double &val, char *data) { @@ -81,6 +114,16 @@ namespace file_reader memcpy(&val, &num_as_uint64, sizeof(double)); } + template <> + inline void read_type(double &val, unsigned char *data) + { + uint64_t num_as_uint64; + + memcpy(&num_as_uint64, data, sizeof(double)); + num_as_uint64 = be64toh(num_as_uint64); + memcpy(&val, &num_as_uint64, sizeof(double)); + } + template<> inline void read_type>(buffer &val, char *data) { @@ -92,7 +135,7 @@ namespace file_reader template struct read_var { - static void call(T& val, parsing_buffer* v) + static void call(T& val, parsing_buffer * v) { int size = 0; if constexpr (typeid(T) == typeid(buffer)) @@ -112,6 +155,26 @@ namespace file_reader v->point += size; v->consumed_size += size; } + static void call(T& val, parsing_buffer * v) + { + int size = 0; + if constexpr (typeid(T) == typeid(buffer)) + size = val.size; + else + size = sizeof(T); + if (size + v->consumed_size > v->buf.size) + { + if constexpr (typeid(T) == typeid(buffer)) + { + val.size = v->buf.size; + } + else + return ; + } + read_type(val, v->point); + v->point += size; + v->consumed_size += size; + } };