diff options
author | John Ankarström <john@ankarstrom.se> | 2022-08-17 14:43:52 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2022-08-17 14:43:52 +0200 |
commit | c60fdf109150312c1ba5e749aff6196f098b2752 (patch) | |
tree | 8452b421ff2b4f2e86d4b11b6b906c6fbbe4a656 /c/data.h | |
parent | 1d041ad5b123e4ecbe69dfa621349f6be9b933d6 (diff) | |
download | EpisodeBrowser-c60fdf109150312c1ba5e749aff6196f098b2752.tar.gz |
Add FileView::Initialized constructor.
Diffstat (limited to 'c/data.h')
-rw-r--r-- | c/data.h | 18 |
1 files changed, 17 insertions, 1 deletions
@@ -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); |