aboutsummaryrefslogtreecommitdiff
path: root/c/data.cpp
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2022-08-15 22:26:03 +0200
committerJohn Ankarström <john@ankarstrom.se>2022-08-15 22:26:03 +0200
commitafd245c205e2787e54cb7e2fd34de617eeceed25 (patch)
treecdce14213a376a0e44c8a4906a35b009e65eef7a /c/data.cpp
parent246fd1369dce903dad48730478bebbe9733ac88e (diff)
downloadEpisodeBrowser-afd245c205e2787e54cb7e2fd34de617eeceed25.tar.gz
Add basic serialization functions for ElvData.
Diffstat (limited to 'c/data.cpp')
-rw-r--r--c/data.cpp46
1 files changed, 46 insertions, 0 deletions
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 <memory>
+
+#include "data.h"
+
+template <typename T>
+inline unsigned char* ValToBuf(const T& val, unsigned char* const buf)
+{
+ memcpy(buf, &val, sizeof(val));
+ return buf+sizeof(val);
+}
+
+template <typename T>
+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;
+}