1 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6 #include "stringtool.h"
12 bool Registry::remove(HKEY i_root, const tstring &i_path,
13 const tstring &i_name)
19 return WritePrivateProfileString(_T("yamy"), i_name.c_str(), NULL, i_path.c_str()) == TRUE;
22 return RegDeleteKey(i_root, i_path.c_str()) == ERROR_SUCCESS;
25 RegOpenKeyEx(i_root, i_path.c_str(), 0, KEY_SET_VALUE, &hkey))
27 LONG r = RegDeleteValue(hkey, i_name.c_str());
29 return r == ERROR_SUCCESS;
34 // does exist the key ?
35 bool Registry::doesExist(HKEY i_root, const tstring &i_path)
42 RegOpenKeyEx(i_root, i_path.c_str(), 0, KEY_READ, &hkey))
51 bool Registry::read(HKEY i_root, const tstring &i_path,
52 const tstring &i_name, int *o_value, int i_defaultValue)
56 GetPrivateProfileInt(_T("yamy"), i_name.c_str(), i_defaultValue, i_path.c_str());
61 RegOpenKeyEx(i_root, i_path.c_str(), 0, KEY_READ, &hkey))
63 DWORD type = REG_DWORD;
64 DWORD size = sizeof(*o_value);
65 LONG r = RegQueryValueEx(hkey, i_name.c_str(), NULL,
66 &type, (BYTE *)o_value, &size);
68 if (r == ERROR_SUCCESS)
71 *o_value = i_defaultValue;
78 bool Registry::write(HKEY i_root, const tstring &i_path, const tstring &i_name,
83 _TCHAR buf[GANA_MAX_PATH];
85 _stprintf(buf, _T("%d"), i_value);
86 ret = WritePrivateProfileString(_T("yamy"), i_name.c_str(),
93 RegCreateKeyEx(i_root, i_path.c_str(), 0, _T(""),
94 REG_OPTION_NON_VOLATILE,
95 KEY_ALL_ACCESS, NULL, &hkey, &disposition))
97 LONG r = RegSetValueEx(hkey, i_name.c_str(), NULL, REG_DWORD,
98 (BYTE *)&i_value, sizeof(i_value));
100 return r == ERROR_SUCCESS;
106 bool Registry::read(HKEY i_root, const tstring &i_path, const tstring &i_name,
107 tstring *o_value, const tstring &i_defaultValue)
110 _TCHAR buf[GANA_MAX_PATH];
112 len = GetPrivateProfileString(_T("yamy"), i_name.c_str(), _T(""),
113 buf, sizeof(buf) / sizeof(buf[0]), i_path.c_str());
119 if (!i_defaultValue.empty())
120 *o_value = i_defaultValue;
125 RegOpenKeyEx(i_root, i_path.c_str(), 0, KEY_READ, &hkey))
130 if (ERROR_MORE_DATA ==
131 RegQueryValueEx(hkey, i_name.c_str(), NULL, &type, &dummy, &size))
135 Array<BYTE> buf(size);
136 if (ERROR_SUCCESS == RegQueryValueEx(hkey, i_name.c_str(),
137 NULL, &type, buf.get(), &size))
140 *o_value = reinterpret_cast<_TCHAR *>(buf.get());
148 if (!i_defaultValue.empty())
149 *o_value = i_defaultValue;
156 bool Registry::write(HKEY i_root, const tstring &i_path,
157 const tstring &i_name, const tstring &i_value)
162 ret = WritePrivateProfileString(_T("yamy"), i_name.c_str(),
163 i_value.c_str(), i_path.c_str());
169 RegCreateKeyEx(i_root, i_path.c_str(), 0, _T(""),
170 REG_OPTION_NON_VOLATILE,
171 KEY_ALL_ACCESS, NULL, &hkey, &disposition))
173 RegSetValueEx(hkey, i_name.c_str(), NULL, REG_SZ,
174 (BYTE *)i_value.c_str(),
175 (i_value.size() + 1) * sizeof(tstring::value_type));
183 // read list of string
184 bool Registry::read(HKEY i_root, const tstring &i_path, const tstring &i_name,
185 tstrings *o_value, const tstrings &i_defaultValue)
189 RegOpenKeyEx(i_root, i_path.c_str(), 0, KEY_READ, &hkey))
191 DWORD type = REG_MULTI_SZ;
194 if (ERROR_MORE_DATA ==
195 RegQueryValueEx(hkey, i_name.c_str(), NULL, &type, &dummy, &size))
199 Array<BYTE> buf(size);
200 if (ERROR_SUCCESS == RegQueryValueEx(hkey, i_name.c_str(),
201 NULL, &type, buf.get(), &size))
205 const _TCHAR *p = reinterpret_cast<_TCHAR *>(buf.get());
206 const _TCHAR *end = reinterpret_cast<_TCHAR *>(buf.end());
207 while (p < end && *p)
209 o_value->push_back(p);
210 p += o_value->back().length() + 1;
219 if (!i_defaultValue.empty())
220 *o_value = i_defaultValue;
225 // write list of string
226 bool Registry::write(HKEY i_root, const tstring &i_path,
227 const tstring &i_name, const tstrings &i_value)
232 RegCreateKeyEx(i_root, i_path.c_str(), 0, _T(""),
233 REG_OPTION_NON_VOLATILE,
234 KEY_ALL_ACCESS, NULL, &hkey, &disposition))
237 for (tstrings::const_iterator i = i_value.begin(); i != i_value.end(); ++ i)
242 RegSetValueEx(hkey, i_name.c_str(), NULL, REG_MULTI_SZ,
243 (BYTE *)value.c_str(),
244 (value.size() + 1) * sizeof(tstring::value_type));
251 bool Registry::read(HKEY i_root, const tstring &i_path,
252 const tstring &i_name, BYTE *o_value, DWORD i_valueSize,
253 const BYTE *i_defaultValue, DWORD i_defaultValueSize)
255 if (o_value && 0 < i_valueSize)
259 RegOpenKeyEx(i_root, i_path.c_str(), 0, KEY_READ, &hkey))
261 DWORD type = REG_BINARY;
262 LONG r = RegQueryValueEx(hkey, i_name.c_str(), NULL, &type,
263 (BYTE *)o_value, &i_valueSize);
265 if (r == ERROR_SUCCESS)
270 CopyMemory(o_value, i_defaultValue,
271 MIN(i_defaultValueSize, i_valueSize));
277 bool Registry::write(HKEY i_root, const tstring &i_path, const tstring &i_name,
278 const BYTE *i_value, DWORD i_valueSize)
285 RegCreateKeyEx(i_root, i_path.c_str(), 0, _T(""),
286 REG_OPTION_NON_VOLATILE,
287 KEY_ALL_ACCESS, NULL, &hkey, &disposition))
289 RegSetValueEx(hkey, i_name.c_str(), NULL, REG_BINARY, i_value, i_valueSize);
297 static bool string2logfont(LOGFONT *o_lf, const tstring &i_strlf)
299 // -13,0,0,0,400,0,0,0,128,1,2,1,1,Terminal
300 tregex lf(_T("^(-?\\d+),(-?\\d+),(-?\\d+),(-?\\d+),(-?\\d+),")
301 _T("(-?\\d+),(-?\\d+),(-?\\d+),(-?\\d+),(-?\\d+),")
302 _T("(-?\\d+),(-?\\d+),(-?\\d+),(.+)$"));
305 if (!boost::regex_match(i_strlf, what, lf))
307 o_lf->lfHeight = _ttoi(what.str(1).c_str());
308 o_lf->lfWidth = _ttoi(what.str(2).c_str());
309 o_lf->lfEscapement = _ttoi(what.str(3).c_str());
310 o_lf->lfOrientation = _ttoi(what.str(4).c_str());
311 o_lf->lfWeight = _ttoi(what.str(5).c_str());
312 o_lf->lfItalic = (BYTE)_ttoi(what.str(6).c_str());
313 o_lf->lfUnderline = (BYTE)_ttoi(what.str(7).c_str());
314 o_lf->lfStrikeOut = (BYTE)_ttoi(what.str(8).c_str());
315 o_lf->lfCharSet = (BYTE)_ttoi(what.str(9).c_str());
316 o_lf->lfOutPrecision = (BYTE)_ttoi(what.str(10).c_str());
317 o_lf->lfClipPrecision = (BYTE)_ttoi(what.str(11).c_str());
318 o_lf->lfQuality = (BYTE)_ttoi(what.str(12).c_str());
319 o_lf->lfPitchAndFamily = (BYTE)_ttoi(what.str(13).c_str());
320 tcslcpy(o_lf->lfFaceName, what.str(14).c_str(), NUMBER_OF(o_lf->lfFaceName));
326 bool Registry::read(HKEY i_root, const tstring &i_path, const tstring &i_name,
327 LOGFONT *o_value, const tstring &i_defaultStringValue)
330 if (!read(i_root, i_path, i_name, &buf) || !string2logfont(o_value, buf))
332 if (!i_defaultStringValue.empty())
333 string2logfont(o_value, i_defaultStringValue);
341 bool Registry::write(HKEY i_root, const tstring &i_path, const tstring &i_name,
342 const LOGFONT &i_value)
345 _sntprintf(buf, NUMBER_OF(buf),
346 _T("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%s"),
347 i_value.lfHeight, i_value.lfWidth, i_value.lfEscapement,
348 i_value.lfOrientation, i_value.lfWeight, i_value.lfItalic,
349 i_value.lfUnderline, i_value.lfStrikeOut, i_value.lfCharSet,
350 i_value.lfOutPrecision, i_value.lfClipPrecision,
352 i_value.lfPitchAndFamily, i_value.lfFaceName);
353 return Registry::write(i_root, i_path, i_name, buf);