1 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\r
5 #include "registry.h"
\r
6 #include "stringtool.h"
\r
12 bool Registry::remove(HKEY i_root, const tstring &i_path,
\r
13 const tstring &i_name)
\r
17 return RegDeleteKey(i_root, i_path.c_str()) == ERROR_SUCCESS;
\r
19 if (ERROR_SUCCESS != RegOpenKeyEx(i_root, i_path.c_str(), 0, KEY_SET_VALUE, &hkey))
\r
21 LONG r = RegDeleteValue(hkey, i_name.c_str());
\r
23 return r == ERROR_SUCCESS;
\r
28 return WritePrivateProfileString(_T("yamy"), i_name.c_str(), NULL, i_path.c_str()) == TRUE;
\r
33 // does exist the key ?
\r
34 bool Registry::doesExist(HKEY i_root, const tstring &i_path)
\r
38 if (ERROR_SUCCESS != RegOpenKeyEx(i_root, i_path.c_str(), 0, KEY_READ, &hkey))
\r
49 bool Registry::read(HKEY i_root, const tstring &i_path,
\r
50 const tstring &i_name, int *o_value, int i_defaultValue)
\r
54 if (ERROR_SUCCESS == RegOpenKeyEx(i_root, i_path.c_str(), 0, KEY_READ, &hkey)) {
\r
55 DWORD type = REG_DWORD;
\r
56 DWORD size = sizeof(*o_value);
\r
57 LONG r = RegQueryValueEx(hkey, i_name.c_str(), NULL, &type, (BYTE *)o_value, &size);
\r
59 if (r == ERROR_SUCCESS)
\r
62 *o_value = i_defaultValue;
\r
65 *o_value = GetPrivateProfileInt(_T("yamy"), i_name.c_str(), i_defaultValue, i_path.c_str());
\r
72 bool Registry::write(HKEY i_root, const tstring &i_path, const tstring &i_name,
\r
78 if (ERROR_SUCCESS !=
\r
79 RegCreateKeyEx(i_root, i_path.c_str(), 0, _T(""),
\r
80 REG_OPTION_NON_VOLATILE,
\r
81 KEY_ALL_ACCESS, NULL, &hkey, &disposition))
\r
83 LONG r = RegSetValueEx(hkey, i_name.c_str(), NULL, REG_DWORD,
\r
84 (BYTE *)&i_value, sizeof(i_value));
\r
86 return r == ERROR_SUCCESS;
\r
89 _TCHAR buf[GANA_MAX_PATH];
\r
91 _stprintf(buf, _T("%d"), i_value);
\r
92 ret = WritePrivateProfileString(_T("yamy"), i_name.c_str(),
\r
93 buf, i_path.c_str());
\r
100 bool Registry::read(HKEY i_root, const tstring &i_path, const tstring &i_name,
\r
101 tstring *o_value, const tstring &i_defaultValue)
\r
105 if (ERROR_SUCCESS ==
\r
106 RegOpenKeyEx(i_root, i_path.c_str(), 0, KEY_READ, &hkey)) {
\r
107 DWORD type = REG_SZ;
\r
110 if (ERROR_MORE_DATA ==
\r
111 RegQueryValueEx(hkey, i_name.c_str(), NULL, &type, &dummy, &size)) {
\r
113 Array<BYTE> buf(size);
\r
114 if (ERROR_SUCCESS == RegQueryValueEx(hkey, i_name.c_str(),
\r
115 NULL, &type, buf.get(), &size)) {
\r
117 *o_value = reinterpret_cast<_TCHAR *>(buf.get());
\r
125 if (!i_defaultValue.empty())
\r
126 *o_value = i_defaultValue;
\r
129 _TCHAR buf[GANA_MAX_PATH];
\r
131 len = GetPrivateProfileString(_T("yamy"), i_name.c_str(), _T(""),
\r
132 buf, sizeof(buf) / sizeof(buf[0]), i_path.c_str());
\r
137 if (!i_defaultValue.empty())
\r
138 *o_value = i_defaultValue;
\r
145 bool Registry::write(HKEY i_root, const tstring &i_path,
\r
146 const tstring &i_name, const tstring &i_value)
\r
151 if (ERROR_SUCCESS !=
\r
152 RegCreateKeyEx(i_root, i_path.c_str(), 0, _T(""),
\r
153 REG_OPTION_NON_VOLATILE,
\r
154 KEY_ALL_ACCESS, NULL, &hkey, &disposition))
\r
156 RegSetValueEx(hkey, i_name.c_str(), NULL, REG_SZ,
\r
157 (BYTE *)i_value.c_str(),
\r
158 (i_value.size() + 1) * sizeof(tstring::value_type));
\r
164 ret = WritePrivateProfileString(_T("yamy"), i_name.c_str(),
\r
165 i_value.c_str(), i_path.c_str());
\r
172 // read list of string
\r
173 bool Registry::read(HKEY i_root, const tstring &i_path, const tstring &i_name,
\r
174 tstrings *o_value, const tstrings &i_defaultValue)
\r
177 if (ERROR_SUCCESS ==
\r
178 RegOpenKeyEx(i_root, i_path.c_str(), 0, KEY_READ, &hkey)) {
\r
179 DWORD type = REG_MULTI_SZ;
\r
182 if (ERROR_MORE_DATA ==
\r
183 RegQueryValueEx(hkey, i_name.c_str(), NULL, &type, &dummy, &size)) {
\r
185 Array<BYTE> buf(size);
\r
186 if (ERROR_SUCCESS == RegQueryValueEx(hkey, i_name.c_str(),
\r
187 NULL, &type, buf.get(), &size)) {
\r
190 const _TCHAR *p = reinterpret_cast<_TCHAR *>(buf.get());
\r
191 const _TCHAR *end = reinterpret_cast<_TCHAR *>(buf.end());
\r
192 while (p < end && *p) {
\r
193 o_value->push_back(p);
\r
194 p += o_value->back().length() + 1;
\r
203 if (!i_defaultValue.empty())
\r
204 *o_value = i_defaultValue;
\r
209 // write list of string
\r
210 bool Registry::write(HKEY i_root, const tstring &i_path,
\r
211 const tstring &i_name, const tstrings &i_value)
\r
215 if (ERROR_SUCCESS !=
\r
216 RegCreateKeyEx(i_root, i_path.c_str(), 0, _T(""),
\r
217 REG_OPTION_NON_VOLATILE,
\r
218 KEY_ALL_ACCESS, NULL, &hkey, &disposition))
\r
221 for (tstrings::const_iterator i = i_value.begin(); i != i_value.end(); ++ i) {
\r
225 RegSetValueEx(hkey, i_name.c_str(), NULL, REG_MULTI_SZ,
\r
226 (BYTE *)value.c_str(),
\r
227 (value.size() + 1) * sizeof(tstring::value_type));
\r
235 bool Registry::read(HKEY i_root, const tstring &i_path,
\r
236 const tstring &i_name, BYTE *o_value, DWORD *i_valueSize,
\r
237 const BYTE *i_defaultValue, DWORD i_defaultValueSize)
\r
242 if (ERROR_SUCCESS ==
\r
243 RegOpenKeyEx(i_root, i_path.c_str(), 0, KEY_READ, &hkey)) {
\r
244 DWORD type = REG_BINARY;
\r
245 LONG r = RegQueryValueEx(hkey, i_name.c_str(), NULL, &type,
\r
246 (BYTE *)o_value, i_valueSize);
\r
248 if (r == ERROR_SUCCESS)
\r
252 if (i_defaultValue)
\r
253 CopyMemory(o_value, i_defaultValue,
\r
254 MIN(i_defaultValueSize, *i_valueSize));
\r
263 bool Registry::write(HKEY i_root, const tstring &i_path, const tstring &i_name,
\r
264 const BYTE *i_value, DWORD i_valueSize)
\r
271 if (ERROR_SUCCESS !=
\r
272 RegCreateKeyEx(i_root, i_path.c_str(), 0, _T(""),
\r
273 REG_OPTION_NON_VOLATILE,
\r
274 KEY_ALL_ACCESS, NULL, &hkey, &disposition))
\r
276 RegSetValueEx(hkey, i_name.c_str(), NULL, REG_BINARY, i_value, i_valueSize);
\r
286 static bool string2logfont(LOGFONT *o_lf, const tstring &i_strlf)
\r
288 // -13,0,0,0,400,0,0,0,128,1,2,1,1,Terminal
\r
289 tregex lf(_T("^(-?\\d+),(-?\\d+),(-?\\d+),(-?\\d+),(-?\\d+),")
\r
290 _T("(-?\\d+),(-?\\d+),(-?\\d+),(-?\\d+),(-?\\d+),")
\r
291 _T("(-?\\d+),(-?\\d+),(-?\\d+),(.+)$"));
\r
294 if (!boost::regex_match(i_strlf, what, lf))
\r
296 o_lf->lfHeight = _ttoi(what.str(1).c_str());
\r
297 o_lf->lfWidth = _ttoi(what.str(2).c_str());
\r
298 o_lf->lfEscapement = _ttoi(what.str(3).c_str());
\r
299 o_lf->lfOrientation = _ttoi(what.str(4).c_str());
\r
300 o_lf->lfWeight = _ttoi(what.str(5).c_str());
\r
301 o_lf->lfItalic = (BYTE)_ttoi(what.str(6).c_str());
\r
302 o_lf->lfUnderline = (BYTE)_ttoi(what.str(7).c_str());
\r
303 o_lf->lfStrikeOut = (BYTE)_ttoi(what.str(8).c_str());
\r
304 o_lf->lfCharSet = (BYTE)_ttoi(what.str(9).c_str());
\r
305 o_lf->lfOutPrecision = (BYTE)_ttoi(what.str(10).c_str());
\r
306 o_lf->lfClipPrecision = (BYTE)_ttoi(what.str(11).c_str());
\r
307 o_lf->lfQuality = (BYTE)_ttoi(what.str(12).c_str());
\r
308 o_lf->lfPitchAndFamily = (BYTE)_ttoi(what.str(13).c_str());
\r
309 tcslcpy(o_lf->lfFaceName, what.str(14).c_str(), NUMBER_OF(o_lf->lfFaceName));
\r
315 bool Registry::read(HKEY i_root, const tstring &i_path, const tstring &i_name,
\r
316 LOGFONT *o_value, const tstring &i_defaultStringValue)
\r
319 if (!read(i_root, i_path, i_name, &buf) || !string2logfont(o_value, buf)) {
\r
320 if (!i_defaultStringValue.empty())
\r
321 string2logfont(o_value, i_defaultStringValue);
\r
329 bool Registry::write(HKEY i_root, const tstring &i_path, const tstring &i_name,
\r
330 const LOGFONT &i_value)
\r
333 _sntprintf(buf, NUMBER_OF(buf),
\r
334 _T("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%s"),
\r
335 i_value.lfHeight, i_value.lfWidth, i_value.lfEscapement,
\r
336 i_value.lfOrientation, i_value.lfWeight, i_value.lfItalic,
\r
337 i_value.lfUnderline, i_value.lfStrikeOut, i_value.lfCharSet,
\r
338 i_value.lfOutPrecision, i_value.lfClipPrecision,
\r
340 i_value.lfPitchAndFamily, i_value.lfFaceName);
\r
341 return Registry::write(i_root, i_path, i_name, buf);
\r