From 1cb00589065fd05b8d7cf0030eed84c488e9634d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Tue, 16 Aug 2022 13:42:11 +0200 Subject: Avoid serialization. A great benefit of this is that the program doesn't need to COPY the data from the file view to the struct. --- c/data.cpp | 33 ++++++++------------------------- 1 file changed, 8 insertions(+), 25 deletions(-) (limited to 'c/data.cpp') diff --git a/c/data.cpp b/c/data.cpp index 1a78ef2..fe348e7 100644 --- a/c/data.cpp +++ b/c/data.cpp @@ -18,7 +18,7 @@ inline unsigned char* BufToVal(unsigned char* const buf, T& val) return buf+sizeof(val); } -DatView::DatView(const wchar_t* const filename, const size_t cb) +FileView::FileView(const wchar_t* const filename, const size_t cb) { hf = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); @@ -39,12 +39,12 @@ DatView::DatView(const wchar_t* const filename, const size_t cb) if (!hm) throw Win32Error{}; - view = MapViewOfFile(hm, FILE_MAP_ALL_ACCESS, 0, 0, 0); + view = reinterpret_cast(MapViewOfFile(hm, FILE_MAP_ALL_ACCESS, 0, 0, 0)); if (!view) throw Win32Error{}; } -DatView::~DatView() +FileView::~FileView() { FlushViewOfFile(view, 0); UnmapViewOfFile(view); @@ -52,31 +52,14 @@ DatView::~DatView() CloseHandle(hf); } -unsigned char* Serialize(const ElvData& e, unsigned char* buf) +void Write(unsigned char* buf, const ElvData& e) { - 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; + memcpy(buf, reinterpret_cast(&e), sizeof(e)); } -unsigned char* Unserialize(ElvData& e, unsigned char* buf) +ElvData* Read(unsigned char* const buf) { - unsigned char version; - buf = BufToVal(buf, version); - if (version != 'a') + if (buf[0] != '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; + return reinterpret_cast(buf); } -- cgit v1.2.3