OSDN Git Service

use boost_1_56_0 and build by VS2013
[yamy/yamy.git] / registry.cpp
1 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r
2 // registry.cpp\r
3 \r
4 \r
5 #include "registry.h"\r
6 #include "stringtool.h"\r
7 #include "array.h"\r
8 #include <malloc.h>\r
9 \r
10 \r
11 // remove\r
12 bool Registry::remove(HKEY i_root, const tstring &i_path,\r
13                                           const tstring &i_name)\r
14 {\r
15         if (i_root) {\r
16                 if (i_name.empty())\r
17                         return RegDeleteKey(i_root, i_path.c_str()) == ERROR_SUCCESS;\r
18                 HKEY hkey;\r
19                 if (ERROR_SUCCESS != RegOpenKeyEx(i_root, i_path.c_str(), 0, KEY_SET_VALUE, &hkey))\r
20                         return false;\r
21                 LONG r = RegDeleteValue(hkey, i_name.c_str());\r
22                 RegCloseKey(hkey);\r
23                 return r == ERROR_SUCCESS;\r
24         } else {\r
25                 return false;\r
26                 if (i_name.empty())\r
27                         return false;\r
28                 return WritePrivateProfileString(_T("yamy"), i_name.c_str(), NULL, i_path.c_str()) == TRUE;\r
29         }\r
30 }\r
31 \r
32 \r
33 // does exist the key ?\r
34 bool Registry::doesExist(HKEY i_root, const tstring &i_path)\r
35 {\r
36         if (i_root) {\r
37                 HKEY hkey;\r
38                 if (ERROR_SUCCESS != RegOpenKeyEx(i_root, i_path.c_str(), 0, KEY_READ, &hkey))\r
39                         return false;\r
40                 RegCloseKey(hkey);\r
41                 return true;\r
42         } else {\r
43                 return true;\r
44         }\r
45 }\r
46 \r
47 \r
48 // read DWORD\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
51 {\r
52         if (i_root) {\r
53                 HKEY hkey;\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
58                         RegCloseKey(hkey);\r
59                         if (r == ERROR_SUCCESS)\r
60                                 return true;\r
61                 }\r
62                 *o_value = i_defaultValue;\r
63                 return false;\r
64         } else {\r
65                 *o_value = GetPrivateProfileInt(_T("yamy"), i_name.c_str(), i_defaultValue, i_path.c_str());\r
66                 return true;\r
67         }\r
68 }\r
69 \r
70 \r
71 // write DWORD\r
72 bool Registry::write(HKEY i_root, const tstring &i_path, const tstring &i_name,\r
73                                          int i_value)\r
74 {\r
75         if (i_root) {\r
76                 HKEY hkey;\r
77                 DWORD disposition;\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
82                         return false;\r
83                 LONG r = RegSetValueEx(hkey, i_name.c_str(), NULL, REG_DWORD,\r
84                                                            (BYTE *)&i_value, sizeof(i_value));\r
85                 RegCloseKey(hkey);\r
86                 return r == ERROR_SUCCESS;\r
87         } else {\r
88                 DWORD ret;\r
89                 _TCHAR buf[GANA_MAX_PATH];\r
90 \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
94                 return ret != 0;\r
95         }\r
96 }\r
97 \r
98 \r
99 // read string\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
102 {\r
103         if (i_root) {\r
104                 HKEY hkey;\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
108                         DWORD size = 0;\r
109                         BYTE dummy;\r
110                         if (ERROR_MORE_DATA ==\r
111                                         RegQueryValueEx(hkey, i_name.c_str(), NULL, &type, &dummy, &size)) {\r
112                                 if (0 < 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
116                                                 buf.back() = 0;\r
117                                                 *o_value = reinterpret_cast<_TCHAR *>(buf.get());\r
118                                                 RegCloseKey(hkey);\r
119                                                 return true;\r
120                                         }\r
121                                 }\r
122                         }\r
123                         RegCloseKey(hkey);\r
124                 }\r
125                 if (!i_defaultValue.empty())\r
126                         *o_value = i_defaultValue;\r
127                 return false;\r
128         } else {\r
129                 _TCHAR buf[GANA_MAX_PATH];\r
130                 DWORD len;\r
131                 len = GetPrivateProfileString(_T("yamy"), i_name.c_str(), _T(""),\r
132                                                                           buf, sizeof(buf) / sizeof(buf[0]), i_path.c_str());\r
133                 if (len > 0) {\r
134                         *o_value = buf;\r
135                         return true;\r
136                 }\r
137                 if (!i_defaultValue.empty())\r
138                         *o_value = i_defaultValue;\r
139                 return false;\r
140         }\r
141 }\r
142 \r
143 \r
144 // write string\r
145 bool Registry::write(HKEY i_root, const tstring &i_path,\r
146                                          const tstring &i_name, const tstring &i_value)\r
147 {\r
148         if (i_root) {\r
149                 HKEY hkey;\r
150                 DWORD disposition;\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
155                         return false;\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
159                 RegCloseKey(hkey);\r
160                 return true;\r
161         } else {\r
162                 DWORD ret;\r
163 \r
164                 ret =  WritePrivateProfileString(_T("yamy"), i_name.c_str(),\r
165                                                                                  i_value.c_str(), i_path.c_str());\r
166                 return ret != 0;\r
167         }\r
168 }\r
169 \r
170 \r
171 #ifndef USE_INI\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
175 {\r
176         HKEY hkey;\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
180                 DWORD size = 0;\r
181                 BYTE dummy;\r
182                 if (ERROR_MORE_DATA ==\r
183                                 RegQueryValueEx(hkey, i_name.c_str(), NULL, &type, &dummy, &size)) {\r
184                         if (0 < 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
188                                         buf.back() = 0;\r
189                                         o_value->clear();\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
195                                         }\r
196                                         RegCloseKey(hkey);\r
197                                         return true;\r
198                                 }\r
199                         }\r
200                 }\r
201                 RegCloseKey(hkey);\r
202         }\r
203         if (!i_defaultValue.empty())\r
204                 *o_value = i_defaultValue;\r
205         return false;\r
206 }\r
207 \r
208 \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
212 {\r
213         HKEY hkey;\r
214         DWORD disposition;\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
219                 return false;\r
220         tstring value;\r
221         for (tstrings::const_iterator i = i_value.begin(); i != i_value.end(); ++ i) {\r
222                 value += *i;\r
223                 value += _T('\0');\r
224         }\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
228         RegCloseKey(hkey);\r
229         return true;\r
230 }\r
231 #endif //!USE_INI\r
232 \r
233 \r
234 // read binary\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
238 {\r
239         if (i_root) {\r
240                 if (i_valueSize) {\r
241                         HKEY hkey;\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
247                                 RegCloseKey(hkey);\r
248                                 if (r == ERROR_SUCCESS)\r
249                                         return true;\r
250                         }\r
251                 }\r
252                 if (i_defaultValue)\r
253                         CopyMemory(o_value, i_defaultValue,\r
254                                            MIN(i_defaultValueSize, *i_valueSize));\r
255                 return false;\r
256         } else {\r
257                 return false;\r
258         }\r
259 }\r
260 \r
261 \r
262 // write binary\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
265 {\r
266         if (i_root) {\r
267                 if (!i_value)\r
268                         return false;\r
269                 HKEY hkey;\r
270                 DWORD disposition;\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
275                         return false;\r
276                 RegSetValueEx(hkey, i_name.c_str(), NULL, REG_BINARY, i_value, i_valueSize);\r
277                 RegCloseKey(hkey);\r
278                 return true;\r
279         } else {\r
280                 return false;\r
281         }\r
282 }\r
283 \r
284 \r
285 //\r
286 static bool string2logfont(LOGFONT *o_lf, const tstring &i_strlf)\r
287 {\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
292         tsmatch what;\r
293 \r
294         if (!boost::regex_match(i_strlf, what, lf))\r
295                 return false;\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
310         return true;\r
311 }\r
312 \r
313 \r
314 // read LOGFONT\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
317 {\r
318         tstring buf;\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
322                 return false;\r
323         }\r
324         return true;\r
325 }\r
326 \r
327 \r
328 // write LOGFONT\r
329 bool Registry::write(HKEY i_root, const tstring &i_path, const tstring &i_name,\r
330                                          const LOGFONT &i_value)\r
331 {\r
332         _TCHAR buf[1024];\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
339                            i_value.lfQuality,\r
340                            i_value.lfPitchAndFamily, i_value.lfFaceName);\r
341         return Registry::write(i_root, i_path, i_name, buf);\r
342 }\r