-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.cpp
342 lines (311 loc) · 9.56 KB
/
registry.cpp
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// registry.cpp
#include "registry.h"
#include "stringtool.h"
#include "array.h"
#include <malloc.h>
// remove
bool Registry::remove(HKEY i_root, const tstring &i_path,
const tstring &i_name)
{
if (i_root) {
if (i_name.empty())
return RegDeleteKey(i_root, i_path.c_str()) == ERROR_SUCCESS;
HKEY hkey;
if (ERROR_SUCCESS != RegOpenKeyEx(i_root, i_path.c_str(), 0, KEY_SET_VALUE, &hkey))
return false;
LONG r = RegDeleteValue(hkey, i_name.c_str());
RegCloseKey(hkey);
return r == ERROR_SUCCESS;
} else {
return false;
if (i_name.empty())
return false;
return WritePrivateProfileString(_T("yamy"), i_name.c_str(), NULL, i_path.c_str()) == TRUE;
}
}
// does exist the key ?
bool Registry::doesExist(HKEY i_root, const tstring &i_path)
{
if (i_root) {
HKEY hkey;
if (ERROR_SUCCESS != RegOpenKeyEx(i_root, i_path.c_str(), 0, KEY_READ, &hkey))
return false;
RegCloseKey(hkey);
return true;
} else {
return true;
}
}
// read DWORD
bool Registry::read(HKEY i_root, const tstring &i_path,
const tstring &i_name, int *o_value, int i_defaultValue)
{
if (i_root) {
HKEY hkey;
if (ERROR_SUCCESS == RegOpenKeyEx(i_root, i_path.c_str(), 0, KEY_READ, &hkey)) {
DWORD type = REG_DWORD;
DWORD size = sizeof(*o_value);
LONG r = RegQueryValueEx(hkey, i_name.c_str(), NULL, &type, (BYTE *)o_value, &size);
RegCloseKey(hkey);
if (r == ERROR_SUCCESS)
return true;
}
*o_value = i_defaultValue;
return false;
} else {
*o_value = GetPrivateProfileInt(_T("yamy"), i_name.c_str(), i_defaultValue, i_path.c_str());
return true;
}
}
// write DWORD
bool Registry::write(HKEY i_root, const tstring &i_path, const tstring &i_name,
int i_value)
{
if (i_root) {
HKEY hkey;
DWORD disposition;
if (ERROR_SUCCESS !=
RegCreateKeyEx(i_root, i_path.c_str(), 0, _T(""),
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS, NULL, &hkey, &disposition))
return false;
LONG r = RegSetValueEx(hkey, i_name.c_str(), NULL, REG_DWORD,
(BYTE *)&i_value, sizeof(i_value));
RegCloseKey(hkey);
return r == ERROR_SUCCESS;
} else {
DWORD ret;
_TCHAR buf[GANA_MAX_PATH];
_stprintf(buf, _T("%d"), i_value);
ret = WritePrivateProfileString(_T("yamy"), i_name.c_str(),
buf, i_path.c_str());
return ret != 0;
}
}
// read string
bool Registry::read(HKEY i_root, const tstring &i_path, const tstring &i_name,
tstring *o_value, const tstring &i_defaultValue)
{
if (i_root) {
HKEY hkey;
if (ERROR_SUCCESS ==
RegOpenKeyEx(i_root, i_path.c_str(), 0, KEY_READ, &hkey)) {
DWORD type = REG_SZ;
DWORD size = 0;
BYTE dummy;
if (ERROR_MORE_DATA ==
RegQueryValueEx(hkey, i_name.c_str(), NULL, &type, &dummy, &size)) {
if (0 < size) {
Array<BYTE> buf(size);
if (ERROR_SUCCESS == RegQueryValueEx(hkey, i_name.c_str(),
NULL, &type, buf.get(), &size)) {
buf.back() = 0;
*o_value = reinterpret_cast<_TCHAR *>(buf.get());
RegCloseKey(hkey);
return true;
}
}
}
RegCloseKey(hkey);
}
if (!i_defaultValue.empty())
*o_value = i_defaultValue;
return false;
} else {
_TCHAR buf[GANA_MAX_PATH];
DWORD len;
len = GetPrivateProfileString(_T("yamy"), i_name.c_str(), _T(""),
buf, sizeof(buf) / sizeof(buf[0]), i_path.c_str());
if (len > 0) {
*o_value = buf;
return true;
}
if (!i_defaultValue.empty())
*o_value = i_defaultValue;
return false;
}
}
// write string
bool Registry::write(HKEY i_root, const tstring &i_path,
const tstring &i_name, const tstring &i_value)
{
if (i_root) {
HKEY hkey;
DWORD disposition;
if (ERROR_SUCCESS !=
RegCreateKeyEx(i_root, i_path.c_str(), 0, _T(""),
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS, NULL, &hkey, &disposition))
return false;
RegSetValueEx(hkey, i_name.c_str(), NULL, REG_SZ,
(BYTE *)i_value.c_str(),
(i_value.size() + 1) * sizeof(tstring::value_type));
RegCloseKey(hkey);
return true;
} else {
DWORD ret;
ret = WritePrivateProfileString(_T("yamy"), i_name.c_str(),
i_value.c_str(), i_path.c_str());
return ret != 0;
}
}
#ifndef USE_INI
// read list of string
bool Registry::read(HKEY i_root, const tstring &i_path, const tstring &i_name,
tstrings *o_value, const tstrings &i_defaultValue)
{
HKEY hkey;
if (ERROR_SUCCESS ==
RegOpenKeyEx(i_root, i_path.c_str(), 0, KEY_READ, &hkey)) {
DWORD type = REG_MULTI_SZ;
DWORD size = 0;
BYTE dummy;
if (ERROR_MORE_DATA ==
RegQueryValueEx(hkey, i_name.c_str(), NULL, &type, &dummy, &size)) {
if (0 < size) {
Array<BYTE> buf(size);
if (ERROR_SUCCESS == RegQueryValueEx(hkey, i_name.c_str(),
NULL, &type, buf.get(), &size)) {
buf.back() = 0;
o_value->clear();
const _TCHAR *p = reinterpret_cast<_TCHAR *>(buf.get());
const _TCHAR *end = reinterpret_cast<_TCHAR *>(buf.end());
while (p < end && *p) {
o_value->push_back(p);
p += o_value->back().length() + 1;
}
RegCloseKey(hkey);
return true;
}
}
}
RegCloseKey(hkey);
}
if (!i_defaultValue.empty())
*o_value = i_defaultValue;
return false;
}
// write list of string
bool Registry::write(HKEY i_root, const tstring &i_path,
const tstring &i_name, const tstrings &i_value)
{
HKEY hkey;
DWORD disposition;
if (ERROR_SUCCESS !=
RegCreateKeyEx(i_root, i_path.c_str(), 0, _T(""),
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS, NULL, &hkey, &disposition))
return false;
tstring value;
for (tstrings::const_iterator i = i_value.begin(); i != i_value.end(); ++ i) {
value += *i;
value += _T('\0');
}
RegSetValueEx(hkey, i_name.c_str(), NULL, REG_MULTI_SZ,
(BYTE *)value.c_str(),
(value.size() + 1) * sizeof(tstring::value_type));
RegCloseKey(hkey);
return true;
}
#endif //!USE_INI
// read binary
bool Registry::read(HKEY i_root, const tstring &i_path,
const tstring &i_name, BYTE *o_value, DWORD *i_valueSize,
const BYTE *i_defaultValue, DWORD i_defaultValueSize)
{
if (i_root) {
if (i_valueSize) {
HKEY hkey;
if (ERROR_SUCCESS ==
RegOpenKeyEx(i_root, i_path.c_str(), 0, KEY_READ, &hkey)) {
DWORD type = REG_BINARY;
LONG r = RegQueryValueEx(hkey, i_name.c_str(), NULL, &type,
(BYTE *)o_value, i_valueSize);
RegCloseKey(hkey);
if (r == ERROR_SUCCESS)
return true;
}
}
if (i_defaultValue)
CopyMemory(o_value, i_defaultValue,
MIN(i_defaultValueSize, *i_valueSize));
return false;
} else {
return false;
}
}
// write binary
bool Registry::write(HKEY i_root, const tstring &i_path, const tstring &i_name,
const BYTE *i_value, DWORD i_valueSize)
{
if (i_root) {
if (!i_value)
return false;
HKEY hkey;
DWORD disposition;
if (ERROR_SUCCESS !=
RegCreateKeyEx(i_root, i_path.c_str(), 0, _T(""),
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS, NULL, &hkey, &disposition))
return false;
RegSetValueEx(hkey, i_name.c_str(), NULL, REG_BINARY, i_value, i_valueSize);
RegCloseKey(hkey);
return true;
} else {
return false;
}
}
//
static bool string2logfont(LOGFONT *o_lf, const tstring &i_strlf)
{
// -13,0,0,0,400,0,0,0,128,1,2,1,1,Terminal
tregex lf(_T("^(-?\\d+),(-?\\d+),(-?\\d+),(-?\\d+),(-?\\d+),")
_T("(-?\\d+),(-?\\d+),(-?\\d+),(-?\\d+),(-?\\d+),")
_T("(-?\\d+),(-?\\d+),(-?\\d+),(.+)$"));
tsmatch what;
if (!boost::regex_match(i_strlf, what, lf))
return false;
o_lf->lfHeight = _ttoi(what.str(1).c_str());
o_lf->lfWidth = _ttoi(what.str(2).c_str());
o_lf->lfEscapement = _ttoi(what.str(3).c_str());
o_lf->lfOrientation = _ttoi(what.str(4).c_str());
o_lf->lfWeight = _ttoi(what.str(5).c_str());
o_lf->lfItalic = (BYTE)_ttoi(what.str(6).c_str());
o_lf->lfUnderline = (BYTE)_ttoi(what.str(7).c_str());
o_lf->lfStrikeOut = (BYTE)_ttoi(what.str(8).c_str());
o_lf->lfCharSet = (BYTE)_ttoi(what.str(9).c_str());
o_lf->lfOutPrecision = (BYTE)_ttoi(what.str(10).c_str());
o_lf->lfClipPrecision = (BYTE)_ttoi(what.str(11).c_str());
o_lf->lfQuality = (BYTE)_ttoi(what.str(12).c_str());
o_lf->lfPitchAndFamily = (BYTE)_ttoi(what.str(13).c_str());
tcslcpy(o_lf->lfFaceName, what.str(14).c_str(), NUMBER_OF(o_lf->lfFaceName));
return true;
}
// read LOGFONT
bool Registry::read(HKEY i_root, const tstring &i_path, const tstring &i_name,
LOGFONT *o_value, const tstring &i_defaultStringValue)
{
tstring buf;
if (!read(i_root, i_path, i_name, &buf) || !string2logfont(o_value, buf)) {
if (!i_defaultStringValue.empty())
string2logfont(o_value, i_defaultStringValue);
return false;
}
return true;
}
// write LOGFONT
bool Registry::write(HKEY i_root, const tstring &i_path, const tstring &i_name,
const LOGFONT &i_value)
{
_TCHAR buf[1024];
_sntprintf(buf, NUMBER_OF(buf),
_T("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%s"),
i_value.lfHeight, i_value.lfWidth, i_value.lfEscapement,
i_value.lfOrientation, i_value.lfWeight, i_value.lfItalic,
i_value.lfUnderline, i_value.lfStrikeOut, i_value.lfCharSet,
i_value.lfOutPrecision, i_value.lfClipPrecision,
i_value.lfQuality,
i_value.lfPitchAndFamily, i_value.lfFaceName);
return Registry::write(i_root, i_path, i_name, buf);
}