From bd857d24443b8c8f5d2f58047c3f8ac5f058acea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Wed, 17 Aug 2022 02:33:26 +0200 Subject: Make FileView more type-safe. --- c/data.cpp | 64 -------------------------------------------------------------- 1 file changed, 64 deletions(-) (limited to 'c/data.cpp') diff --git a/c/data.cpp b/c/data.cpp index 60decc9..9dd6ef8 100644 --- a/c/data.cpp +++ b/c/data.cpp @@ -1,65 +1 @@ -#include -#include - #include "data.h" -#include "win.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); -} - -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); - if (hf == INVALID_HANDLE_VALUE) { - if (GetLastError() == ERROR_FILE_NOT_FOUND) { - hf = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, - 0, nullptr, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr); - if (hf == INVALID_HANDLE_VALUE) - throw Win32Error{}; - } else - throw Win32Error{}; - } - - LARGE_INTEGER cbMap; - cbMap.QuadPart = cb; - hm = CreateFileMapping(hf, nullptr, PAGE_READWRITE, - cbMap.HighPart, cbMap.LowPart, nullptr); - if (!hm) - throw Win32Error{}; - - view = reinterpret_cast(MapViewOfFile(hm, FILE_MAP_ALL_ACCESS, 0, 0, 0)); - if (!view) - throw Win32Error{}; -} - -FileView::~FileView() -{ - FlushViewOfFile(view, 0); - UnmapViewOfFile(view); - CloseHandle(hm); - CloseHandle(hf); -} - -void Write(unsigned char* buf, const ElvDataA& e) -{ - memcpy(buf, reinterpret_cast(&e), sizeof(e)); -} - -ElvDataA* Read(unsigned char* const buf) -{ - if (buf[0] != 'a') - return nullptr; - return reinterpret_cast(buf); -} -- cgit v1.2.3