From 3addc57bfc2da3e87b30d130d5eb8c72c8bf2e28 Mon Sep 17 00:00:00 2001 From: Erica Fischer Date: Mon, 23 Jan 2023 15:47:38 -0800 Subject: [PATCH] Add to-memory versions of the serialization functions --- serial.cpp | 63 +++++++++++++++++++++++++++++++++++++++++++++++------- serial.hpp | 7 +++++- 2 files changed, 61 insertions(+), 9 deletions(-) diff --git a/serial.cpp b/serial.cpp index c50360cf..ee02e5b8 100644 --- a/serial.cpp +++ b/serial.cpp @@ -27,6 +27,8 @@ #define SHIFT_RIGHT(a) ((((a) + COORD_OFFSET) >> geometry_scale) - (COORD_OFFSET >> geometry_scale)) #define SHIFT_LEFT(a) ((((a) + (COORD_OFFSET >> geometry_scale)) << geometry_scale) - COORD_OFFSET) +// write to file + size_t fwrite_check(const void *ptr, size_t size, size_t nitems, FILE *stream, const char *fname) { size_t w = fwrite(ptr, size, nitems, stream); if (w != nitems) { @@ -74,10 +76,53 @@ void serialize_byte(FILE *out, signed char n, std::atomic *fpos, cons } void serialize_uint(FILE *out, unsigned n, std::atomic *fpos, const char *fname) { - fwrite_check(&n, sizeof(unsigned), 1, out, fname); - *fpos += sizeof(unsigned); + serialize_ulong_long(out, n, fpos, fname); } +// write to memory + +size_t fwrite_check(const void *ptr, size_t size, size_t nitems, std::string &stream) { + stream += std::string((char *) ptr, size * nitems); + return nitems; +} + +void serialize_ulong_long(std::string &out, unsigned long long zigzag, std::atomic *fpos) { + while (1) { + unsigned char b = zigzag & 0x7F; + if ((zigzag >> 7) != 0) { + b |= 0x80; + out += b; + *fpos += 1; + zigzag >>= 7; + } else { + out += b; + *fpos += 1; + break; + } + } +} + +void serialize_long_long(std::string &out, long long n, std::atomic *fpos) { + unsigned long long zigzag = protozero::encode_zigzag64(n); + + serialize_ulong_long(out, zigzag, fpos); +} + +void serialize_int(std::string &out, int n, std::atomic *fpos) { + serialize_long_long(out, n, fpos); +} + +void serialize_byte(std::string &out, signed char n, std::atomic *fpos) { + out += n; + *fpos += sizeof(signed char); +} + +void serialize_uint(std::string &out, unsigned n, std::atomic *fpos) { + serialize_ulong_long(out, n, fpos); +} + +// read from memory + void deserialize_int(char **f, int *n) { long long ll; deserialize_long_long(f, &ll); @@ -109,8 +154,9 @@ void deserialize_ulong_long(char **f, unsigned long long *zigzag) { } void deserialize_uint(char **f, unsigned *n) { - memcpy(n, *f, sizeof(unsigned)); - *f += sizeof(unsigned); + unsigned long long v; + deserialize_ulong_long(f, &v); + *n = v; } void deserialize_byte(char **f, signed char *n) { @@ -118,6 +164,8 @@ void deserialize_byte(char **f, signed char *n) { *f += sizeof(signed char); } +// read from file + int deserialize_long_long_io(FILE *f, long long *n, std::atomic *geompos) { unsigned long long zigzag = 0; int ret = deserialize_ulong_long_io(f, &zigzag, geompos); @@ -157,10 +205,9 @@ int deserialize_int_io(FILE *f, int *n, std::atomic *geompos) { } int deserialize_uint_io(FILE *f, unsigned *n, std::atomic *geompos) { - if (fread(n, sizeof(unsigned), 1, f) != 1) { - return 0; - } - *geompos += sizeof(unsigned); + unsigned long long v; + deserialize_ulong_long_io(f, &v, geompos); + *n = v; return 1; } diff --git a/serial.hpp b/serial.hpp index 711d3fb5..ad9d5890 100644 --- a/serial.hpp +++ b/serial.hpp @@ -18,7 +18,12 @@ void serialize_long_long(FILE *out, long long n, std::atomic *fpos, c void serialize_ulong_long(FILE *out, unsigned long long n, std::atomic *fpos, const char *fname); void serialize_byte(FILE *out, signed char n, std::atomic *fpos, const char *fname); void serialize_uint(FILE *out, unsigned n, std::atomic *fpos, const char *fname); -void serialize_string(FILE *out, const char *s, std::atomic *fpos, const char *fname); + +void serialize_int(std::string &out, int n, std::atomic *fpos); +void serialize_long_long(std::string &out, long long n, std::atomic *fpos); +void serialize_ulong_long(std::string &out, unsigned long long n, std::atomic *fpos); +void serialize_byte(std::string &out, signed char n, std::atomic *fpos); +void serialize_uint(std::string &out, unsigned n, std::atomic *fpos); void deserialize_int(char **f, int *n); void deserialize_long_long(char **f, long long *n);