diff options
author | John Ankarström <john@ankarstrom.se> | 2022-08-17 02:33:26 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2022-08-17 02:33:26 +0200 |
commit | bd857d24443b8c8f5d2f58047c3f8ac5f058acea (patch) | |
tree | 0ef5f3c9abf5ccd6ee6a9e376ceac985de2409fd /c/data.cpp | |
parent | 8351d4c42c76584415e02a40d41f497a8f042c72 (diff) | |
download | EpisodeBrowser-bd857d24443b8c8f5d2f58047c3f8ac5f058acea.tar.gz |
Make FileView more type-safe.
Diffstat (limited to 'c/data.cpp')
-rw-r--r-- | c/data.cpp | 64 |
1 files changed, 0 insertions, 64 deletions
@@ -1,65 +1 @@ -#include <memory> -#include <vector> - #include "data.h" -#include "win.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); -} - -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<unsigned char*>(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<const unsigned char*>(&e), sizeof(e)); -} - -ElvDataA* Read(unsigned char* const buf) -{ - if (buf[0] != 'a') - return nullptr; - return reinterpret_cast<ElvDataA*>(buf); -} |