OSDN Git Service

fix invalid file permission
[yamy/yamy.git] / registry.cpp
1 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2 // registry.cpp
3
4
5 #include "registry.h"
6 #include "stringtool.h"
7 #include "array.h"
8 #include <malloc.h>
9
10
11 // remove
12 bool Registry::remove(HKEY i_root, const tstring &i_path,
13                       const tstring &i_name)
14 {
15 #ifdef USE_INI
16   return false;
17   if (i_name.empty())
18     return false;
19   return WritePrivateProfileString(_T("yamy"), i_name.c_str(), NULL, i_path.c_str()) == TRUE;
20 #else // !USE_INI
21   if (i_name.empty())
22     return RegDeleteKey(i_root, i_path.c_str()) == ERROR_SUCCESS;
23   HKEY hkey;
24   if (ERROR_SUCCESS !=
25       RegOpenKeyEx(i_root, i_path.c_str(), 0, KEY_SET_VALUE, &hkey))
26     return false;
27   LONG r = RegDeleteValue(hkey, i_name.c_str());
28   RegCloseKey(hkey);
29   return r == ERROR_SUCCESS;
30 #endif // !USE_INI
31 }
32
33
34 // does exist the key ?
35 bool Registry::doesExist(HKEY i_root, const tstring &i_path)
36 {
37 #ifdef USE_INI
38   return true;
39 #else // !USE_INI
40   HKEY hkey;
41   if (ERROR_SUCCESS !=
42       RegOpenKeyEx(i_root, i_path.c_str(), 0, KEY_READ, &hkey))
43     return false;
44   RegCloseKey(hkey);
45   return true;
46 #endif // !USE_INI
47 }
48
49
50 // read DWORD
51 bool Registry::read(HKEY i_root, const tstring &i_path,
52                     const tstring &i_name, int *o_value, int i_defaultValue)
53 {
54 #ifdef USE_INI
55   *o_value =
56     GetPrivateProfileInt(_T("yamy"), i_name.c_str(), i_defaultValue, i_path.c_str());
57   return true;
58 #else // !USE_INI
59   HKEY hkey;
60   if (ERROR_SUCCESS ==
61       RegOpenKeyEx(i_root, i_path.c_str(), 0, KEY_READ, &hkey))
62   {
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);
67     RegCloseKey(hkey);
68     if (r == ERROR_SUCCESS)
69       return true;
70   }
71   *o_value = i_defaultValue;
72   return false;
73 #endif // !USE_INI
74 }
75
76
77 // write DWORD
78 bool Registry::write(HKEY i_root, const tstring &i_path, const tstring &i_name,
79                      int i_value)
80 {
81 #ifdef USE_INI
82   DWORD ret;
83   _TCHAR buf[GANA_MAX_PATH];
84
85   _stprintf(buf, _T("%d"), i_value);
86   ret =  WritePrivateProfileString(_T("yamy"), i_name.c_str(),
87                                    buf, i_path.c_str());
88   return ret != 0;
89 #else // !USE_INI
90   HKEY hkey;
91   DWORD disposition;
92   if (ERROR_SUCCESS !=
93       RegCreateKeyEx(i_root, i_path.c_str(), 0, _T(""),
94                      REG_OPTION_NON_VOLATILE,
95                      KEY_ALL_ACCESS, NULL, &hkey, &disposition))
96     return false;
97   LONG r = RegSetValueEx(hkey, i_name.c_str(), NULL, REG_DWORD,
98                          (BYTE *)&i_value, sizeof(i_value));
99   RegCloseKey(hkey);
100   return r == ERROR_SUCCESS;
101 #endif // !USE_INI
102 }
103
104
105 // read string
106 bool Registry::read(HKEY i_root, const tstring &i_path, const tstring &i_name,
107                     tstring *o_value, const tstring &i_defaultValue)
108 {
109 #ifdef USE_INI
110   _TCHAR buf[GANA_MAX_PATH];
111   DWORD len;
112   len = GetPrivateProfileString(_T("yamy"), i_name.c_str(), _T(""),
113                                 buf, sizeof(buf) / sizeof(buf[0]), i_path.c_str());
114   if (len > 0)
115   {
116     *o_value = buf;
117     return true;
118   }
119   if (!i_defaultValue.empty())
120     *o_value = i_defaultValue;
121   return false;
122 #else // !USE_INI
123   HKEY hkey;
124   if (ERROR_SUCCESS ==
125       RegOpenKeyEx(i_root, i_path.c_str(), 0, KEY_READ, &hkey))
126   {
127     DWORD type = REG_SZ;
128     DWORD size = 0;
129     BYTE dummy;
130     if (ERROR_MORE_DATA ==
131         RegQueryValueEx(hkey, i_name.c_str(), NULL, &type, &dummy, &size))
132     {
133       if (0 < size)
134       {
135         Array<BYTE> buf(size);
136         if (ERROR_SUCCESS == RegQueryValueEx(hkey, i_name.c_str(),
137                                              NULL, &type, buf.get(), &size))
138         {
139           buf.back() = 0;
140           *o_value = reinterpret_cast<_TCHAR *>(buf.get());
141           RegCloseKey(hkey);
142           return true;
143         }
144       }
145     }
146     RegCloseKey(hkey);
147   }
148   if (!i_defaultValue.empty())
149     *o_value = i_defaultValue;
150   return false;
151 #endif // !USE_INI
152 }
153
154
155 // write string
156 bool Registry::write(HKEY i_root, const tstring &i_path,
157                      const tstring &i_name, const tstring &i_value)
158 {
159 #ifdef USE_INI
160   DWORD ret;
161
162   ret =  WritePrivateProfileString(_T("yamy"), i_name.c_str(),
163                                    i_value.c_str(), i_path.c_str());
164   return ret != 0;
165 #else // !USE_INI
166   HKEY hkey;
167   DWORD disposition;
168   if (ERROR_SUCCESS !=
169       RegCreateKeyEx(i_root, i_path.c_str(), 0, _T(""),
170                      REG_OPTION_NON_VOLATILE,
171                      KEY_ALL_ACCESS, NULL, &hkey, &disposition))
172     return false;
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));
176   RegCloseKey(hkey);
177   return true;
178 #endif // !USE_INI
179 }
180
181
182 #ifndef USE_INI
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)
186 {
187   HKEY hkey;
188   if (ERROR_SUCCESS ==
189       RegOpenKeyEx(i_root, i_path.c_str(), 0, KEY_READ, &hkey))
190   {
191     DWORD type = REG_MULTI_SZ;
192     DWORD size = 0;
193     BYTE dummy;
194     if (ERROR_MORE_DATA ==
195         RegQueryValueEx(hkey, i_name.c_str(), NULL, &type, &dummy, &size))
196     {
197       if (0 < size)
198       {
199         Array<BYTE> buf(size);
200         if (ERROR_SUCCESS == RegQueryValueEx(hkey, i_name.c_str(),
201                                              NULL, &type, buf.get(), &size))
202         {
203           buf.back() = 0;
204           o_value->clear();
205           const _TCHAR *p = reinterpret_cast<_TCHAR *>(buf.get());
206           const _TCHAR *end = reinterpret_cast<_TCHAR *>(buf.end());
207           while (p < end && *p)
208           {
209             o_value->push_back(p);
210             p += o_value->back().length() + 1;
211           }
212           RegCloseKey(hkey);
213           return true;
214         }
215       }
216     }
217     RegCloseKey(hkey);
218   }
219   if (!i_defaultValue.empty())
220     *o_value = i_defaultValue;
221   return false;
222 }
223
224
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)
228 {
229   HKEY hkey;
230   DWORD disposition;
231   if (ERROR_SUCCESS !=
232       RegCreateKeyEx(i_root, i_path.c_str(), 0, _T(""),
233                      REG_OPTION_NON_VOLATILE,
234                      KEY_ALL_ACCESS, NULL, &hkey, &disposition))
235     return false;
236   tstring value;
237   for (tstrings::const_iterator i = i_value.begin(); i != i_value.end(); ++ i)
238   {
239     value += *i;
240     value += _T('\0');
241   }
242   RegSetValueEx(hkey, i_name.c_str(), NULL, REG_MULTI_SZ,
243                 (BYTE *)value.c_str(),
244                 (value.size() + 1) * sizeof(tstring::value_type));
245   RegCloseKey(hkey);
246   return true;
247 }
248
249
250 // read binary
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)
254 {
255   if (o_value && 0 < i_valueSize)
256   {
257     HKEY hkey;
258     if (ERROR_SUCCESS ==
259         RegOpenKeyEx(i_root, i_path.c_str(), 0, KEY_READ, &hkey))
260     {
261       DWORD type = REG_BINARY;
262       LONG r = RegQueryValueEx(hkey, i_name.c_str(), NULL, &type,
263                                (BYTE *)o_value, &i_valueSize);
264       RegCloseKey(hkey);
265       if (r == ERROR_SUCCESS)
266         return true;
267     }
268   }
269   if (i_defaultValue)
270     CopyMemory(o_value, i_defaultValue,
271                MIN(i_defaultValueSize, i_valueSize));
272   return false;
273 }
274
275
276 // write binary
277 bool Registry::write(HKEY i_root, const tstring &i_path, const tstring &i_name,
278                      const BYTE *i_value, DWORD i_valueSize)
279 {
280   if (!i_value)
281     return false;
282   HKEY hkey;
283   DWORD disposition;
284   if (ERROR_SUCCESS !=
285       RegCreateKeyEx(i_root, i_path.c_str(), 0, _T(""),
286                      REG_OPTION_NON_VOLATILE,
287                      KEY_ALL_ACCESS, NULL, &hkey, &disposition))
288     return false;
289   RegSetValueEx(hkey, i_name.c_str(), NULL, REG_BINARY, i_value, i_valueSize);
290   RegCloseKey(hkey);
291   return true;
292 }
293 #endif //!USE_INI
294
295
296 //
297 static bool string2logfont(LOGFONT *o_lf, const tstring &i_strlf)
298 {
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+),(.+)$"));
303   tsmatch what;
304
305   if (!boost::regex_match(i_strlf, what, lf))
306     return false;
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));
321   return true;
322 }
323
324
325 // read LOGFONT
326 bool Registry::read(HKEY i_root, const tstring &i_path, const tstring &i_name,
327                     LOGFONT *o_value, const tstring &i_defaultStringValue)
328 {
329   tstring buf;
330   if (!read(i_root, i_path, i_name, &buf) || !string2logfont(o_value, buf))
331   {
332     if (!i_defaultStringValue.empty())
333       string2logfont(o_value, i_defaultStringValue);
334     return false;
335   }
336   return true;
337 }
338
339
340 // write LOGFONT
341 bool Registry::write(HKEY i_root, const tstring &i_path, const tstring &i_name,
342                      const LOGFONT &i_value)
343 {
344   _TCHAR buf[1024];
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,
351              i_value.lfQuality,
352              i_value.lfPitchAndFamily, i_value.lfFaceName);
353   return Registry::write(i_root, i_path, i_name, buf);
354 }