From c60fdf109150312c1ba5e749aff6196f098b2752 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Wed, 17 Aug 2022 14:43:52 +0200 Subject: Add FileView::Initialized constructor. --- c/data.h | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'c/data.h') diff --git a/c/data.h b/c/data.h index c799e52..6b9a986 100644 --- a/c/data.h +++ b/c/data.h @@ -70,7 +70,23 @@ constexpr inline bool HasVersion> = true; template struct FileView { - FileView(const wchar_t* filename, size_t c_) : c(c_) + static FileView Initialized(const wchar_t* filename, size_t c, size_t cInit = 0) + { + bool bExisting = GetFileAttributes(filename) != INVALID_FILE_ATTRIBUTES; + FileView fv{filename, c}; + + /* If file didn't exist, initialize it with defaults. */ + if (!bExisting) { + T t; + cInit = cInit? cInit: c; + for (size_t i = 0; i < cInit; i++) + memcpy(fv+i, &t, sizeof(T)); + } + + return fv; + } + + FileView(const wchar_t* filename, size_t c) : c(c) { hf = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); -- cgit v1.2.3