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. --- Makefile | 2 +- c/data.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ c/data.h | 4 ++++ c/test.cpp | 26 ++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 c/data.cpp diff --git a/Makefile b/Makefile index 01e08af..cb52303 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ EXE = EpisodeBrowser.exe -OBJ = b/datalistview.obj b/debug.obj b/episodelistview.obj b/wcharptr.obj b/win.obj +OBJ = b/datalistview.obj b/debug.obj b/data.obj b/episodelistview.obj b/wcharptr.obj b/win.obj OBJ += b/layout.obj b/listview.obj b/pl.obj b/test.obj b/resource.obj PL = pl/cfg.pl pl/episode_data.pl pl/local_episodes.pl pl/track_episodes.pl 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; +} diff --git a/c/data.h b/c/data.h index a2b6cac..797271d 100644 --- a/c/data.h +++ b/c/data.h @@ -24,6 +24,10 @@ struct DlvData wchar_t wiki[128] = {0}; }; +constexpr size_t CB_SERIALIZE_ELVDATA = 1+sizeof(ElvData); +unsigned char* Serialize(const ElvData& e, unsigned char* buf); +unsigned char* Unserialize(ElvData& e, unsigned char* buf); + inline int FromWeb(const int iEp, ElvData& e, DlvData& d) noexcept { WcharPtr title, wiki, date, source, hint; diff --git a/c/test.cpp b/c/test.cpp index 67a32c9..1fc7e3b 100644 --- a/c/test.cpp +++ b/c/test.cpp @@ -46,6 +46,31 @@ TESTS if (wcscmp(d.date, L"March 11, 1996") != 0) FAIL("date is not correct"); } + + TEST(Serialization) + { + ElvData e1; + FromProlog(6, e1); + + unsigned char buf[CB_SERIALIZE_ELVDATA]; + Serialize(e1, buf); + + ElvData e2; + if (!Unserialize(e2, buf)) + FAIL("invalid serialization version"); + if (e1.rating != e2.rating) + FAIL("rating is different"); + if (e1.bWatched != e2.bWatched) + FAIL("bWatched is different"); + if (e1.bTVOriginal != e2.bTVOriginal) + FAIL("bTVOriginal is different"); + if (wcscmp(e1.sRating, e2.sRating) != 0) + FAIL("sRating is different"); + if (wcscmp(e1.siEp, e2.siEp) != 0) + FAIL("siEp is different"); + if (wcscmp(e1.title, e2.title) != 0) + FAIL("title is different"); + } }; int RunTests() @@ -54,6 +79,7 @@ int RunTests() StrcpyWithSmallerDestination{}, //EpisodeDataFromWeb{}, EpisodeDataFromProlog{}, + Serialization{}, }; printf("Results (%llu tests):\n", sizeof(tests)/sizeof(*tests)); -- cgit v1.2.3