From afd245c205e2787e54cb7e2fd34de617eeceed25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Mon, 15 Aug 2022 22:26:03 +0200 Subject: Add basic serialization functions for ElvData. --- c/data.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 c/data.cpp (limited to 'c/data.cpp') diff --git a/c/data.cpp b/c/data.cpp new file mode 100644 index 0000000..e28378e --- /dev/null +++ b/c/data.cpp @@ -0,0 +1,46 @@ +#include + +#include "data.h" + +template +inline unsigned char* ValToBuf(const T& val, unsigned char* const buf) +{ + memcpy(buf, &val, sizeof(val)); + return buf+sizeof(val); +} + +template +inline unsigned char* BufToVal(unsigned char* const buf, T& val) +{ + memcpy(&val, buf, sizeof(val)); + return buf+sizeof(val); +} + +unsigned char* Serialize(const ElvData& e, unsigned char* buf) +{ + unsigned char version = 'a'; + buf = ValToBuf(version, buf); + buf = ValToBuf(e.rating, buf); + buf = ValToBuf(e.bWatched, buf); + buf = ValToBuf(e.bTVOriginal, buf); + buf = ValToBuf(e.sRating, buf); + buf = ValToBuf(e.siEp, buf); + buf = ValToBuf(e.title, buf); + return buf; +} + +unsigned char* Unserialize(ElvData& e, unsigned char* buf) +{ + unsigned char version; + buf = BufToVal(buf, version); + if (version != 'a') + return nullptr; + + buf = BufToVal(buf, e.rating); + buf = BufToVal(buf, e.bWatched); + buf = BufToVal(buf, e.bTVOriginal); + buf = BufToVal(buf, e.sRating); + buf = BufToVal(buf, e.siEp); + buf = BufToVal(buf, e.title); + return buf; +} -- cgit v1.2.3