diff options
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); |