forked from coddec/Classic-Shell
- Notifications
You must be signed in to change notification settings - Fork 452
/
Copy pathModernSettings.h
141 lines (114 loc) · 2.59 KB
/
ModernSettings.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Modern settings helper
#pragma once
#include<functional>
#include<map>
#include<memory>
#include<string>
#include<vector>
structBlob
{
constvoid* data = nullptr;
size_t size = 0;
};
classFile
{
public:
File(const WCHAR* fileName, DWORD desiredAccess, DWORD shareMode, DWORD creationDisposition = OPEN_EXISTING, DWORD flagsAndAttributes = FILE_ATTRIBUTE_NORMAL)
{
m_handle = ::CreateFile(fileName, desiredAccess, shareMode, nullptr, creationDisposition, flagsAndAttributes, nullptr);
}
~File()
{
if (m_handle != INVALID_HANDLE_VALUE)
::CloseHandle(m_handle);
}
File(const File&) = delete;
File& operator=(const File&) = delete;
explicitoperatorbool() const
{
return (m_handle != INVALID_HANDLE_VALUE);
}
operatorHANDLE() const
{
return m_handle;
}
uint64_tsize() const
{
LARGE_INTEGER li = {};
return ::GetFileSizeEx(m_handle, &li) ? li.QuadPart : (uint64_t)-1;
}
private:
HANDLE m_handle;
};
classMappedFile
{
public:
MappedFile(const WCHAR* fileName) : m_file(fileName, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_DELETE)
{
if (m_file)
{
auto mapping = ::CreateFileMapping(m_file, nullptr, PAGE_READONLY, 0, 0, nullptr);
if (mapping)
{
m_view.data = ::MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
if (m_view.data)
m_view.size = (size_t)m_file.size();
::CloseHandle(mapping);
}
}
}
~MappedFile()
{
if (m_view.data)
::UnmapViewOfFile(m_view.data);
}
MappedFile(const MappedFile&) = delete;
MappedFile& operator=(const MappedFile&) = delete;
explicitoperatorbool() const
{
return (m_view.data != nullptr);
}
Blob get() const
{
return m_view;
}
private:
File m_file;
Blob m_view;
};
classModernSettings
{
public:
ModernSettings(constwchar_t* fname);
size_tsize() const
{
return m_settings.size();
}
structSetting
{
std::wstring_view fileName;
std::wstring_view deepLink;
std::wstring_view icon;
std::wstring_view glyph;
std::wstring_view pageId;
std::wstring_view hostId;
std::wstring_view groupId;
std::wstring_view settingId;
std::wstring_view description;
std::wstring_view keywords;
Setting() = default;
Setting(const Blob& blob);
Setting(const std::vector<uint8_t>& blob) : Setting(Blob{ blob.data(), blob.size() }) {}
explicitoperatorbool() const
{
return !fileName.empty();
}
};
std::vector<std::wstring_view> enumerate() const;
Setting get(const std::wstring_view& name) const;
private:
MappedFile m_storage;
std::map<std::wstring_view, Blob> m_settings;
};
// retrieve actual instance of ModernSettings
std::shared_ptr<ModernSettings> GetModernSettings();