aboutsummaryrefslogtreecommitdiff
path: root/c/data.h
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2022-08-17 14:43:52 +0200
committerJohn Ankarström <john@ankarstrom.se>2022-08-17 14:43:52 +0200
commitc60fdf109150312c1ba5e749aff6196f098b2752 (patch)
tree8452b421ff2b4f2e86d4b11b6b906c6fbbe4a656 /c/data.h
parent1d041ad5b123e4ecbe69dfa621349f6be9b933d6 (diff)
downloadEpisodeBrowser-c60fdf109150312c1ba5e749aff6196f098b2752.tar.gz
Add FileView::Initialized constructor.
Diffstat (limited to 'c/data.h')
-rw-r--r--c/data.h18
1 files changed, 17 insertions, 1 deletions
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<T, std::void_t<typename T::version>> = true;
template <typename T>
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);