]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/msw/registry.cpp | |
3 | // Purpose: implementation of registry classes and functions | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 03.04.98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
9 | // Licence: wxWindows licence | |
10 | // TODO: - parsing of registry key names | |
11 | // - support of other (than REG_SZ/REG_DWORD) registry types | |
12 | // - add high level functions (RegisterOleServer, ...) | |
13 | /////////////////////////////////////////////////////////////////////////////// | |
14 | ||
15 | // for compilers that support precompilation, includes "wx.h". | |
16 | #include "wx/wxprec.h" | |
17 | ||
18 | #ifdef __BORLANDC__ | |
19 | #pragma hdrstop | |
20 | #endif | |
21 | ||
22 | #ifndef WX_PRECOMP | |
23 | #include "wx/msw/wrapwin.h" | |
24 | #include "wx/string.h" | |
25 | #include "wx/intl.h" | |
26 | #include "wx/log.h" | |
27 | #endif | |
28 | ||
29 | #include "wx/file.h" | |
30 | #include "wx/wfstream.h" | |
31 | ||
32 | // Windows headers | |
33 | #ifdef __WXWINCE__ | |
34 | #include "wx/msw/private.h" | |
35 | #include <winbase.h> | |
36 | #include <winreg.h> | |
37 | #endif | |
38 | ||
39 | // other std headers | |
40 | #include <stdlib.h> // for _MAX_PATH | |
41 | ||
42 | #ifndef _MAX_PATH | |
43 | #define _MAX_PATH 512 | |
44 | #endif | |
45 | ||
46 | // our header | |
47 | #define HKEY_DEFINED // already defined in windows.h | |
48 | #include "wx/msw/registry.h" | |
49 | ||
50 | // some registry functions don't like signed chars | |
51 | typedef unsigned char *RegString; | |
52 | typedef BYTE* RegBinary; | |
53 | ||
54 | // ---------------------------------------------------------------------------- | |
55 | // constants | |
56 | // ---------------------------------------------------------------------------- | |
57 | ||
58 | // the standard key names, short names and handles all bundled together for | |
59 | // convenient access | |
60 | static struct | |
61 | { | |
62 | HKEY hkey; | |
63 | const wxChar *szName; | |
64 | const wxChar *szShortName; | |
65 | } | |
66 | aStdKeys[] = | |
67 | { | |
68 | { HKEY_CLASSES_ROOT, wxT("HKEY_CLASSES_ROOT"), wxT("HKCR") }, | |
69 | { HKEY_CURRENT_USER, wxT("HKEY_CURRENT_USER"), wxT("HKCU") }, | |
70 | { HKEY_LOCAL_MACHINE, wxT("HKEY_LOCAL_MACHINE"), wxT("HKLM") }, | |
71 | { HKEY_USERS, wxT("HKEY_USERS"), wxT("HKU") }, // short name? | |
72 | #ifndef __WXWINCE__ | |
73 | { HKEY_PERFORMANCE_DATA, wxT("HKEY_PERFORMANCE_DATA"), wxT("HKPD") }, | |
74 | #endif | |
75 | #ifdef HKEY_CURRENT_CONFIG | |
76 | { HKEY_CURRENT_CONFIG, wxT("HKEY_CURRENT_CONFIG"), wxT("HKCC") }, | |
77 | #endif | |
78 | #ifdef HKEY_DYN_DATA | |
79 | { HKEY_DYN_DATA, wxT("HKEY_DYN_DATA"), wxT("HKDD") }, // short name? | |
80 | #endif | |
81 | }; | |
82 | ||
83 | // the registry name separator (perhaps one day MS will change it to '/' ;-) | |
84 | #define REG_SEPARATOR wxT('\\') | |
85 | ||
86 | // useful for Windows programmers: makes somewhat more clear all these zeroes | |
87 | // being passed to Windows APIs | |
88 | #define RESERVED (0) | |
89 | ||
90 | // ---------------------------------------------------------------------------- | |
91 | // macros | |
92 | // ---------------------------------------------------------------------------- | |
93 | ||
94 | // const_cast<> is not yet supported by all compilers | |
95 | #define CONST_CAST ((wxRegKey *)this)-> | |
96 | ||
97 | // and neither is mutable which m_dwLastError should be | |
98 | #define m_dwLastError CONST_CAST m_dwLastError | |
99 | ||
100 | // ---------------------------------------------------------------------------- | |
101 | // non member functions | |
102 | // ---------------------------------------------------------------------------- | |
103 | ||
104 | // removes the trailing backslash from the string if it has one | |
105 | static inline void RemoveTrailingSeparator(wxString& str); | |
106 | ||
107 | // returns true if given registry key exists | |
108 | static bool KeyExists(WXHKEY hRootKey, const wxChar *szKey); | |
109 | ||
110 | // combines value and key name (uses static buffer!) | |
111 | static const wxChar *GetFullName(const wxRegKey *pKey, | |
112 | const wxChar *szValue = NULL); | |
113 | ||
114 | // ============================================================================ | |
115 | // implementation of wxRegKey class | |
116 | // ============================================================================ | |
117 | ||
118 | // ---------------------------------------------------------------------------- | |
119 | // static functions and variables | |
120 | // ---------------------------------------------------------------------------- | |
121 | ||
122 | const size_t wxRegKey::nStdKeys = WXSIZEOF(aStdKeys); | |
123 | ||
124 | // @@ should take a `StdKey key', but as it's often going to be used in loops | |
125 | // it would require casts in user code. | |
126 | const wxChar *wxRegKey::GetStdKeyName(size_t key) | |
127 | { | |
128 | // return empty string if key is invalid | |
129 | wxCHECK_MSG( key < nStdKeys, wxEmptyString, wxT("invalid key in wxRegKey::GetStdKeyName") ); | |
130 | ||
131 | return aStdKeys[key].szName; | |
132 | } | |
133 | ||
134 | const wxChar *wxRegKey::GetStdKeyShortName(size_t key) | |
135 | { | |
136 | // return empty string if key is invalid | |
137 | wxCHECK( key < nStdKeys, wxEmptyString ); | |
138 | ||
139 | return aStdKeys[key].szShortName; | |
140 | } | |
141 | ||
142 | wxRegKey::StdKey wxRegKey::ExtractKeyName(wxString& strKey) | |
143 | { | |
144 | wxString strRoot = strKey.BeforeFirst(REG_SEPARATOR); | |
145 | ||
146 | size_t ui; | |
147 | for ( ui = 0; ui < nStdKeys; ui++ ) { | |
148 | if ( strRoot.CmpNoCase(aStdKeys[ui].szName) == 0 || | |
149 | strRoot.CmpNoCase(aStdKeys[ui].szShortName) == 0 ) { | |
150 | break; | |
151 | } | |
152 | } | |
153 | ||
154 | if ( ui == nStdKeys ) { | |
155 | wxFAIL_MSG(wxT("invalid key prefix in wxRegKey::ExtractKeyName.")); | |
156 | ||
157 | ui = HKCR; | |
158 | } | |
159 | else { | |
160 | strKey = strKey.After(REG_SEPARATOR); | |
161 | if ( !strKey.empty() && strKey.Last() == REG_SEPARATOR ) | |
162 | strKey.Truncate(strKey.Len() - 1); | |
163 | } | |
164 | ||
165 | return (StdKey)ui; | |
166 | } | |
167 | ||
168 | wxRegKey::StdKey wxRegKey::GetStdKeyFromHkey(WXHKEY hkey) | |
169 | { | |
170 | for ( size_t ui = 0; ui < nStdKeys; ui++ ) { | |
171 | if ( aStdKeys[ui].hkey == (HKEY)hkey ) | |
172 | return (StdKey)ui; | |
173 | } | |
174 | ||
175 | wxFAIL_MSG(wxT("non root hkey passed to wxRegKey::GetStdKeyFromHkey.")); | |
176 | ||
177 | return HKCR; | |
178 | } | |
179 | ||
180 | // ---------------------------------------------------------------------------- | |
181 | // ctors and dtor | |
182 | // ---------------------------------------------------------------------------- | |
183 | ||
184 | wxRegKey::wxRegKey() | |
185 | { | |
186 | m_hRootKey = (WXHKEY) aStdKeys[HKCR].hkey; | |
187 | ||
188 | Init(); | |
189 | } | |
190 | ||
191 | wxRegKey::wxRegKey(const wxString& strKey) : m_strKey(strKey) | |
192 | { | |
193 | m_hRootKey = (WXHKEY) aStdKeys[ExtractKeyName(m_strKey)].hkey; | |
194 | ||
195 | Init(); | |
196 | } | |
197 | ||
198 | // parent is a predefined (and preopened) key | |
199 | wxRegKey::wxRegKey(StdKey keyParent, const wxString& strKey) : m_strKey(strKey) | |
200 | { | |
201 | RemoveTrailingSeparator(m_strKey); | |
202 | m_hRootKey = (WXHKEY) aStdKeys[keyParent].hkey; | |
203 | ||
204 | Init(); | |
205 | } | |
206 | ||
207 | // parent is a normal regkey | |
208 | wxRegKey::wxRegKey(const wxRegKey& keyParent, const wxString& strKey) | |
209 | : m_strKey(keyParent.m_strKey) | |
210 | { | |
211 | // combine our name with parent's to get the full name | |
212 | if ( !m_strKey.empty() && | |
213 | (strKey.empty() || strKey[0] != REG_SEPARATOR) ) { | |
214 | m_strKey += REG_SEPARATOR; | |
215 | } | |
216 | ||
217 | m_strKey += strKey; | |
218 | RemoveTrailingSeparator(m_strKey); | |
219 | ||
220 | m_hRootKey = keyParent.m_hRootKey; | |
221 | ||
222 | Init(); | |
223 | } | |
224 | ||
225 | // dtor closes the key releasing system resource | |
226 | wxRegKey::~wxRegKey() | |
227 | { | |
228 | Close(); | |
229 | } | |
230 | ||
231 | // ---------------------------------------------------------------------------- | |
232 | // change the key name/hkey | |
233 | // ---------------------------------------------------------------------------- | |
234 | ||
235 | // set the full key name | |
236 | void wxRegKey::SetName(const wxString& strKey) | |
237 | { | |
238 | Close(); | |
239 | ||
240 | m_strKey = strKey; | |
241 | m_hRootKey = (WXHKEY) aStdKeys[ExtractKeyName(m_strKey)].hkey; | |
242 | } | |
243 | ||
244 | // the name is relative to the parent key | |
245 | void wxRegKey::SetName(StdKey keyParent, const wxString& strKey) | |
246 | { | |
247 | Close(); | |
248 | ||
249 | m_strKey = strKey; | |
250 | RemoveTrailingSeparator(m_strKey); | |
251 | m_hRootKey = (WXHKEY) aStdKeys[keyParent].hkey; | |
252 | } | |
253 | ||
254 | // the name is relative to the parent key | |
255 | void wxRegKey::SetName(const wxRegKey& keyParent, const wxString& strKey) | |
256 | { | |
257 | Close(); | |
258 | ||
259 | // combine our name with parent's to get the full name | |
260 | ||
261 | // NB: this method is called by wxRegConfig::SetPath() which is a performance | |
262 | // critical function and so it preallocates space for our m_strKey to | |
263 | // gain some speed - this is why we only use += here and not = which | |
264 | // would just free the prealloc'd buffer and would have to realloc it the | |
265 | // next line! | |
266 | m_strKey.clear(); | |
267 | m_strKey += keyParent.m_strKey; | |
268 | if ( !strKey.empty() && strKey[0] != REG_SEPARATOR ) | |
269 | m_strKey += REG_SEPARATOR; | |
270 | m_strKey += strKey; | |
271 | ||
272 | RemoveTrailingSeparator(m_strKey); | |
273 | ||
274 | m_hRootKey = keyParent.m_hRootKey; | |
275 | } | |
276 | ||
277 | // hKey should be opened and will be closed in wxRegKey dtor | |
278 | void wxRegKey::SetHkey(WXHKEY hKey) | |
279 | { | |
280 | Close(); | |
281 | ||
282 | m_hKey = hKey; | |
283 | } | |
284 | ||
285 | // ---------------------------------------------------------------------------- | |
286 | // info about the key | |
287 | // ---------------------------------------------------------------------------- | |
288 | ||
289 | // returns true if the key exists | |
290 | bool wxRegKey::Exists() const | |
291 | { | |
292 | // opened key has to exist, try to open it if not done yet | |
293 | return IsOpened() ? true : KeyExists(m_hRootKey, m_strKey); | |
294 | } | |
295 | ||
296 | // returns the full name of the key (prefix is abbreviated if bShortPrefix) | |
297 | wxString wxRegKey::GetName(bool bShortPrefix) const | |
298 | { | |
299 | StdKey key = GetStdKeyFromHkey((WXHKEY) m_hRootKey); | |
300 | wxString str = bShortPrefix ? aStdKeys[key].szShortName | |
301 | : aStdKeys[key].szName; | |
302 | if ( !m_strKey.empty() ) | |
303 | str << _T("\\") << m_strKey; | |
304 | ||
305 | return str; | |
306 | } | |
307 | ||
308 | bool wxRegKey::GetKeyInfo(size_t *pnSubKeys, | |
309 | size_t *pnMaxKeyLen, | |
310 | size_t *pnValues, | |
311 | size_t *pnMaxValueLen) const | |
312 | { | |
313 | // old gcc headers incorrectly prototype RegQueryInfoKey() | |
314 | #if defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__) | |
315 | #define REG_PARAM (size_t *) | |
316 | #else | |
317 | #define REG_PARAM (LPDWORD) | |
318 | #endif | |
319 | ||
320 | // it might be unexpected to some that this function doesn't open the key | |
321 | wxASSERT_MSG( IsOpened(), _T("key should be opened in GetKeyInfo") ); | |
322 | ||
323 | m_dwLastError = ::RegQueryInfoKey | |
324 | ( | |
325 | (HKEY) m_hKey, | |
326 | NULL, // class name | |
327 | NULL, // (ptr to) size of class name buffer | |
328 | RESERVED, | |
329 | REG_PARAM | |
330 | pnSubKeys, // [out] number of subkeys | |
331 | REG_PARAM | |
332 | pnMaxKeyLen, // [out] max length of a subkey name | |
333 | NULL, // longest subkey class name | |
334 | REG_PARAM | |
335 | pnValues, // [out] number of values | |
336 | REG_PARAM | |
337 | pnMaxValueLen, // [out] max length of a value name | |
338 | NULL, // longest value data | |
339 | NULL, // security descriptor | |
340 | NULL // time of last modification | |
341 | ); | |
342 | ||
343 | #undef REG_PARAM | |
344 | ||
345 | if ( m_dwLastError != ERROR_SUCCESS ) { | |
346 | wxLogSysError(m_dwLastError, _("Can't get info about registry key '%s'"), | |
347 | GetName().c_str()); | |
348 | return false; | |
349 | } | |
350 | ||
351 | return true; | |
352 | } | |
353 | ||
354 | // ---------------------------------------------------------------------------- | |
355 | // operations | |
356 | // ---------------------------------------------------------------------------- | |
357 | ||
358 | // opens key (it's not an error to call Open() on an already opened key) | |
359 | bool wxRegKey::Open(AccessMode mode) | |
360 | { | |
361 | if ( IsOpened() ) | |
362 | { | |
363 | if ( mode <= m_mode ) | |
364 | return true; | |
365 | ||
366 | // we had been opened in read mode but now must be reopened in write | |
367 | Close(); | |
368 | } | |
369 | ||
370 | HKEY tmpKey; | |
371 | m_dwLastError = ::RegOpenKeyEx | |
372 | ( | |
373 | (HKEY) m_hRootKey, | |
374 | m_strKey, | |
375 | RESERVED, | |
376 | mode == Read ? KEY_READ : KEY_ALL_ACCESS, | |
377 | &tmpKey | |
378 | ); | |
379 | ||
380 | if ( m_dwLastError != ERROR_SUCCESS ) | |
381 | { | |
382 | wxLogSysError(m_dwLastError, _("Can't open registry key '%s'"), | |
383 | GetName().c_str()); | |
384 | return false; | |
385 | } | |
386 | ||
387 | m_hKey = (WXHKEY) tmpKey; | |
388 | m_mode = mode; | |
389 | ||
390 | return true; | |
391 | } | |
392 | ||
393 | // creates key, failing if it exists and !bOkIfExists | |
394 | bool wxRegKey::Create(bool bOkIfExists) | |
395 | { | |
396 | // check for existence only if asked (i.e. order is important!) | |
397 | if ( !bOkIfExists && Exists() ) | |
398 | return false; | |
399 | ||
400 | if ( IsOpened() ) | |
401 | return true; | |
402 | ||
403 | HKEY tmpKey; | |
404 | #ifdef __WXWINCE__ | |
405 | DWORD disposition; | |
406 | m_dwLastError = RegCreateKeyEx((HKEY) m_hRootKey, m_strKey, | |
407 | NULL, // reserved | |
408 | NULL, // class string | |
409 | 0, | |
410 | 0, | |
411 | NULL, | |
412 | &tmpKey, | |
413 | &disposition); | |
414 | #else | |
415 | m_dwLastError = RegCreateKey((HKEY) m_hRootKey, m_strKey, &tmpKey); | |
416 | #endif | |
417 | if ( m_dwLastError != ERROR_SUCCESS ) { | |
418 | wxLogSysError(m_dwLastError, _("Can't create registry key '%s'"), | |
419 | GetName().c_str()); | |
420 | return false; | |
421 | } | |
422 | else | |
423 | { | |
424 | m_hKey = (WXHKEY) tmpKey; | |
425 | return true; | |
426 | } | |
427 | } | |
428 | ||
429 | // close the key, it's not an error to call it when not opened | |
430 | bool wxRegKey::Close() | |
431 | { | |
432 | if ( IsOpened() ) { | |
433 | m_dwLastError = RegCloseKey((HKEY) m_hKey); | |
434 | m_hKey = 0; | |
435 | ||
436 | if ( m_dwLastError != ERROR_SUCCESS ) { | |
437 | wxLogSysError(m_dwLastError, _("Can't close registry key '%s'"), | |
438 | GetName().c_str()); | |
439 | ||
440 | return false; | |
441 | } | |
442 | } | |
443 | ||
444 | return true; | |
445 | } | |
446 | ||
447 | bool wxRegKey::RenameValue(const wxChar *szValueOld, const wxChar *szValueNew) | |
448 | { | |
449 | bool ok = true; | |
450 | if ( HasValue(szValueNew) ) { | |
451 | wxLogError(_("Registry value '%s' already exists."), szValueNew); | |
452 | ||
453 | ok = false; | |
454 | } | |
455 | ||
456 | if ( !ok || | |
457 | !CopyValue(szValueOld, *this, szValueNew) || | |
458 | !DeleteValue(szValueOld) ) { | |
459 | wxLogError(_("Failed to rename registry value '%s' to '%s'."), | |
460 | szValueOld, szValueNew); | |
461 | ||
462 | return false; | |
463 | } | |
464 | ||
465 | return true; | |
466 | } | |
467 | ||
468 | bool wxRegKey::CopyValue(const wxChar *szValue, | |
469 | wxRegKey& keyDst, | |
470 | const wxChar *szValueNew) | |
471 | { | |
472 | if ( !szValueNew ) { | |
473 | // by default, use the same name | |
474 | szValueNew = szValue; | |
475 | } | |
476 | ||
477 | switch ( GetValueType(szValue) ) { | |
478 | case Type_String: | |
479 | { | |
480 | wxString strVal; | |
481 | return QueryValue(szValue, strVal) && | |
482 | keyDst.SetValue(szValueNew, strVal); | |
483 | } | |
484 | ||
485 | case Type_Dword: | |
486 | /* case Type_Dword_little_endian: == Type_Dword */ | |
487 | { | |
488 | long dwVal; | |
489 | return QueryValue(szValue, &dwVal) && | |
490 | keyDst.SetValue(szValueNew, dwVal); | |
491 | } | |
492 | ||
493 | case Type_Binary: | |
494 | { | |
495 | wxMemoryBuffer buf; | |
496 | return QueryValue(szValue,buf) && | |
497 | keyDst.SetValue(szValueNew,buf); | |
498 | } | |
499 | ||
500 | // these types are unsupported because I am not sure about how | |
501 | // exactly they should be copied and because they shouldn't | |
502 | // occur among the application keys (supposedly created with | |
503 | // this class) | |
504 | case Type_None: | |
505 | case Type_Expand_String: | |
506 | case Type_Dword_big_endian: | |
507 | case Type_Link: | |
508 | case Type_Multi_String: | |
509 | case Type_Resource_list: | |
510 | case Type_Full_resource_descriptor: | |
511 | case Type_Resource_requirements_list: | |
512 | default: | |
513 | wxLogError(_("Can't copy values of unsupported type %d."), | |
514 | GetValueType(szValue)); | |
515 | return false; | |
516 | } | |
517 | } | |
518 | ||
519 | bool wxRegKey::Rename(const wxChar *szNewName) | |
520 | { | |
521 | wxCHECK_MSG( !m_strKey.empty(), false, _T("registry hives can't be renamed") ); | |
522 | ||
523 | if ( !Exists() ) { | |
524 | wxLogError(_("Registry key '%s' does not exist, cannot rename it."), | |
525 | GetFullName(this)); | |
526 | ||
527 | return false; | |
528 | } | |
529 | ||
530 | // do we stay in the same hive? | |
531 | bool inSameHive = !wxStrchr(szNewName, REG_SEPARATOR); | |
532 | ||
533 | // construct the full new name of the key | |
534 | wxRegKey keyDst; | |
535 | ||
536 | if ( inSameHive ) { | |
537 | // rename the key to the new name under the same parent | |
538 | wxString strKey = m_strKey.BeforeLast(REG_SEPARATOR); | |
539 | if ( !strKey.empty() ) { | |
540 | // don't add '\\' in the start if strFullNewName is empty | |
541 | strKey += REG_SEPARATOR; | |
542 | } | |
543 | ||
544 | strKey += szNewName; | |
545 | ||
546 | keyDst.SetName(GetStdKeyFromHkey(m_hRootKey), strKey); | |
547 | } | |
548 | else { | |
549 | // this is the full name already | |
550 | keyDst.SetName(szNewName); | |
551 | } | |
552 | ||
553 | bool ok = keyDst.Create(false /* fail if alredy exists */); | |
554 | if ( !ok ) { | |
555 | wxLogError(_("Registry key '%s' already exists."), | |
556 | GetFullName(&keyDst)); | |
557 | } | |
558 | else { | |
559 | ok = Copy(keyDst) && DeleteSelf(); | |
560 | } | |
561 | ||
562 | if ( !ok ) { | |
563 | wxLogError(_("Failed to rename the registry key '%s' to '%s'."), | |
564 | GetFullName(this), GetFullName(&keyDst)); | |
565 | } | |
566 | else { | |
567 | m_hRootKey = keyDst.m_hRootKey; | |
568 | m_strKey = keyDst.m_strKey; | |
569 | } | |
570 | ||
571 | return ok; | |
572 | } | |
573 | ||
574 | bool wxRegKey::Copy(const wxChar *szNewName) | |
575 | { | |
576 | // create the new key first | |
577 | wxRegKey keyDst(szNewName); | |
578 | bool ok = keyDst.Create(false /* fail if alredy exists */); | |
579 | if ( ok ) { | |
580 | ok = Copy(keyDst); | |
581 | ||
582 | // we created the dest key but copying to it failed - delete it | |
583 | if ( !ok ) { | |
584 | (void)keyDst.DeleteSelf(); | |
585 | } | |
586 | } | |
587 | ||
588 | return ok; | |
589 | } | |
590 | ||
591 | bool wxRegKey::Copy(wxRegKey& keyDst) | |
592 | { | |
593 | bool ok = true; | |
594 | ||
595 | // copy all sub keys to the new location | |
596 | wxString strKey; | |
597 | long lIndex; | |
598 | bool bCont = GetFirstKey(strKey, lIndex); | |
599 | while ( ok && bCont ) { | |
600 | wxRegKey key(*this, strKey); | |
601 | wxString keyName; | |
602 | keyName << GetFullName(&keyDst) << REG_SEPARATOR << strKey; | |
603 | ok = key.Copy((const wxChar*) keyName); | |
604 | ||
605 | if ( ok ) | |
606 | bCont = GetNextKey(strKey, lIndex); | |
607 | else | |
608 | wxLogError(_("Failed to copy the registry subkey '%s' to '%s'."), | |
609 | GetFullName(&key), keyName.c_str()); | |
610 | ||
611 | } | |
612 | ||
613 | // copy all values | |
614 | wxString strVal; | |
615 | bCont = GetFirstValue(strVal, lIndex); | |
616 | while ( ok && bCont ) { | |
617 | ok = CopyValue(strVal, keyDst); | |
618 | ||
619 | if ( !ok ) { | |
620 | wxLogSysError(m_dwLastError, | |
621 | _("Failed to copy registry value '%s'"), | |
622 | strVal.c_str()); | |
623 | } | |
624 | else { | |
625 | bCont = GetNextValue(strVal, lIndex); | |
626 | } | |
627 | } | |
628 | ||
629 | if ( !ok ) { | |
630 | wxLogError(_("Failed to copy the contents of registry key '%s' to '%s'."), | |
631 | GetFullName(this), GetFullName(&keyDst)); | |
632 | } | |
633 | ||
634 | return ok; | |
635 | } | |
636 | ||
637 | // ---------------------------------------------------------------------------- | |
638 | // delete keys/values | |
639 | // ---------------------------------------------------------------------------- | |
640 | bool wxRegKey::DeleteSelf() | |
641 | { | |
642 | { | |
643 | wxLogNull nolog; | |
644 | if ( !Open() ) { | |
645 | // it already doesn't exist - ok! | |
646 | return true; | |
647 | } | |
648 | } | |
649 | ||
650 | // prevent a buggy program from erasing one of the root registry keys or an | |
651 | // immediate subkey (i.e. one which doesn't have '\\' inside) of any other | |
652 | // key except HKCR (HKCR has some "deleteable" subkeys) | |
653 | if ( m_strKey.empty() || | |
654 | ((m_hRootKey != (WXHKEY) aStdKeys[HKCR].hkey) && | |
655 | (m_strKey.Find(REG_SEPARATOR) == wxNOT_FOUND)) ) { | |
656 | wxLogError(_("Registry key '%s' is needed for normal system operation,\ndeleting it will leave your system in unusable state:\noperation aborted."), | |
657 | GetFullName(this)); | |
658 | ||
659 | return false; | |
660 | } | |
661 | ||
662 | // we can't delete keys while enumerating because it confuses GetNextKey, so | |
663 | // we first save the key names and then delete them all | |
664 | wxArrayString astrSubkeys; | |
665 | ||
666 | wxString strKey; | |
667 | long lIndex; | |
668 | bool bCont = GetFirstKey(strKey, lIndex); | |
669 | while ( bCont ) { | |
670 | astrSubkeys.Add(strKey); | |
671 | ||
672 | bCont = GetNextKey(strKey, lIndex); | |
673 | } | |
674 | ||
675 | size_t nKeyCount = astrSubkeys.Count(); | |
676 | for ( size_t nKey = 0; nKey < nKeyCount; nKey++ ) { | |
677 | wxRegKey key(*this, astrSubkeys[nKey]); | |
678 | if ( !key.DeleteSelf() ) | |
679 | return false; | |
680 | } | |
681 | ||
682 | // now delete this key itself | |
683 | Close(); | |
684 | ||
685 | m_dwLastError = RegDeleteKey((HKEY) m_hRootKey, m_strKey); | |
686 | // deleting a key which doesn't exist is not considered an error | |
687 | if ( m_dwLastError != ERROR_SUCCESS && | |
688 | m_dwLastError != ERROR_FILE_NOT_FOUND ) { | |
689 | wxLogSysError(m_dwLastError, _("Can't delete key '%s'"), | |
690 | GetName().c_str()); | |
691 | return false; | |
692 | } | |
693 | ||
694 | return true; | |
695 | } | |
696 | ||
697 | bool wxRegKey::DeleteKey(const wxChar *szKey) | |
698 | { | |
699 | if ( !Open() ) | |
700 | return false; | |
701 | ||
702 | wxRegKey key(*this, szKey); | |
703 | return key.DeleteSelf(); | |
704 | } | |
705 | ||
706 | bool wxRegKey::DeleteValue(const wxChar *szValue) | |
707 | { | |
708 | if ( !Open() ) | |
709 | return false; | |
710 | ||
711 | m_dwLastError = RegDeleteValue((HKEY) m_hKey, WXSTRINGCAST szValue); | |
712 | ||
713 | // deleting a value which doesn't exist is not considered an error | |
714 | if ( (m_dwLastError != ERROR_SUCCESS) && | |
715 | (m_dwLastError != ERROR_FILE_NOT_FOUND) ) | |
716 | { | |
717 | wxLogSysError(m_dwLastError, _("Can't delete value '%s' from key '%s'"), | |
718 | szValue, GetName().c_str()); | |
719 | return false; | |
720 | } | |
721 | ||
722 | return true; | |
723 | } | |
724 | ||
725 | // ---------------------------------------------------------------------------- | |
726 | // access to values and subkeys | |
727 | // ---------------------------------------------------------------------------- | |
728 | ||
729 | // return true if value exists | |
730 | bool wxRegKey::HasValue(const wxChar *szValue) const | |
731 | { | |
732 | // this function should be silent, so suppress possible messages from Open() | |
733 | wxLogNull nolog; | |
734 | ||
735 | if ( !CONST_CAST Open(Read) ) | |
736 | return false; | |
737 | ||
738 | LONG dwRet = ::RegQueryValueEx((HKEY) m_hKey, | |
739 | WXSTRINGCAST szValue, | |
740 | RESERVED, | |
741 | NULL, NULL, NULL); | |
742 | return dwRet == ERROR_SUCCESS; | |
743 | } | |
744 | ||
745 | // returns true if this key has any values | |
746 | bool wxRegKey::HasValues() const | |
747 | { | |
748 | // suppress possible messages from GetFirstValue() | |
749 | wxLogNull nolog; | |
750 | ||
751 | // just call GetFirstValue with dummy parameters | |
752 | wxString str; | |
753 | long l; | |
754 | return CONST_CAST GetFirstValue(str, l); | |
755 | } | |
756 | ||
757 | // returns true if this key has any subkeys | |
758 | bool wxRegKey::HasSubkeys() const | |
759 | { | |
760 | // suppress possible messages from GetFirstKey() | |
761 | wxLogNull nolog; | |
762 | ||
763 | // just call GetFirstKey with dummy parameters | |
764 | wxString str; | |
765 | long l; | |
766 | return CONST_CAST GetFirstKey(str, l); | |
767 | } | |
768 | ||
769 | // returns true if given subkey exists | |
770 | bool wxRegKey::HasSubKey(const wxChar *szKey) const | |
771 | { | |
772 | // this function should be silent, so suppress possible messages from Open() | |
773 | wxLogNull nolog; | |
774 | ||
775 | if ( !CONST_CAST Open(Read) ) | |
776 | return false; | |
777 | ||
778 | return KeyExists(m_hKey, szKey); | |
779 | } | |
780 | ||
781 | wxRegKey::ValueType wxRegKey::GetValueType(const wxChar *szValue) const | |
782 | { | |
783 | if ( ! CONST_CAST Open(Read) ) | |
784 | return Type_None; | |
785 | ||
786 | DWORD dwType; | |
787 | m_dwLastError = RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED, | |
788 | &dwType, NULL, NULL); | |
789 | if ( m_dwLastError != ERROR_SUCCESS ) { | |
790 | wxLogSysError(m_dwLastError, _("Can't read value of key '%s'"), | |
791 | GetName().c_str()); | |
792 | return Type_None; | |
793 | } | |
794 | ||
795 | return (ValueType)dwType; | |
796 | } | |
797 | ||
798 | bool wxRegKey::SetValue(const wxChar *szValue, long lValue) | |
799 | { | |
800 | if ( CONST_CAST Open() ) { | |
801 | m_dwLastError = RegSetValueEx((HKEY) m_hKey, szValue, (DWORD) RESERVED, REG_DWORD, | |
802 | (RegString)&lValue, sizeof(lValue)); | |
803 | if ( m_dwLastError == ERROR_SUCCESS ) | |
804 | return true; | |
805 | } | |
806 | ||
807 | wxLogSysError(m_dwLastError, _("Can't set value of '%s'"), | |
808 | GetFullName(this, szValue)); | |
809 | return false; | |
810 | } | |
811 | ||
812 | bool wxRegKey::QueryValue(const wxChar *szValue, long *plValue) const | |
813 | { | |
814 | if ( CONST_CAST Open(Read) ) { | |
815 | DWORD dwType, dwSize = sizeof(DWORD); | |
816 | RegString pBuf = (RegString)plValue; | |
817 | m_dwLastError = RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED, | |
818 | &dwType, pBuf, &dwSize); | |
819 | if ( m_dwLastError != ERROR_SUCCESS ) { | |
820 | wxLogSysError(m_dwLastError, _("Can't read value of key '%s'"), | |
821 | GetName().c_str()); | |
822 | return false; | |
823 | } | |
824 | else { | |
825 | // check that we read the value of right type | |
826 | wxASSERT_MSG( IsNumericValue(szValue), | |
827 | wxT("Type mismatch in wxRegKey::QueryValue().") ); | |
828 | ||
829 | return true; | |
830 | } | |
831 | } | |
832 | else | |
833 | return false; | |
834 | } | |
835 | ||
836 | bool wxRegKey::SetValue(const wxChar *szValue,const wxMemoryBuffer& buffer) | |
837 | { | |
838 | #ifdef __TWIN32__ | |
839 | wxFAIL_MSG("RegSetValueEx not implemented by TWIN32"); | |
840 | return false; | |
841 | #else | |
842 | if ( CONST_CAST Open() ) { | |
843 | m_dwLastError = RegSetValueEx((HKEY) m_hKey, szValue, (DWORD) RESERVED, REG_BINARY, | |
844 | (RegBinary)buffer.GetData(),buffer.GetDataLen()); | |
845 | if ( m_dwLastError == ERROR_SUCCESS ) | |
846 | return true; | |
847 | } | |
848 | ||
849 | wxLogSysError(m_dwLastError, _("Can't set value of '%s'"), | |
850 | GetFullName(this, szValue)); | |
851 | return false; | |
852 | #endif | |
853 | } | |
854 | ||
855 | bool wxRegKey::QueryValue(const wxChar *szValue, wxMemoryBuffer& buffer) const | |
856 | { | |
857 | if ( CONST_CAST Open(Read) ) { | |
858 | // first get the type and size of the data | |
859 | DWORD dwType, dwSize; | |
860 | m_dwLastError = RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED, | |
861 | &dwType, NULL, &dwSize); | |
862 | ||
863 | if ( m_dwLastError == ERROR_SUCCESS ) { | |
864 | if ( dwSize ) { | |
865 | const RegBinary pBuf = (RegBinary)buffer.GetWriteBuf(dwSize); | |
866 | m_dwLastError = RegQueryValueEx((HKEY) m_hKey, | |
867 | WXSTRINGCAST szValue, | |
868 | RESERVED, | |
869 | &dwType, | |
870 | pBuf, | |
871 | &dwSize); | |
872 | buffer.UngetWriteBuf(dwSize); | |
873 | } else { | |
874 | buffer.SetDataLen(0); | |
875 | } | |
876 | } | |
877 | ||
878 | ||
879 | if ( m_dwLastError != ERROR_SUCCESS ) { | |
880 | wxLogSysError(m_dwLastError, _("Can't read value of key '%s'"), | |
881 | GetName().c_str()); | |
882 | return false; | |
883 | } | |
884 | return true; | |
885 | } | |
886 | return false; | |
887 | } | |
888 | ||
889 | ||
890 | ||
891 | bool wxRegKey::QueryValue(const wxChar *szValue, | |
892 | wxString& strValue, | |
893 | bool WXUNUSED_IN_WINCE(raw)) const | |
894 | { | |
895 | if ( CONST_CAST Open(Read) ) | |
896 | { | |
897 | ||
898 | // first get the type and size of the data | |
899 | DWORD dwType=REG_NONE, dwSize=0; | |
900 | m_dwLastError = RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED, | |
901 | &dwType, NULL, &dwSize); | |
902 | if ( m_dwLastError == ERROR_SUCCESS ) | |
903 | { | |
904 | if ( !dwSize ) | |
905 | { | |
906 | // must treat this case specially as GetWriteBuf() doesn't like | |
907 | // being called with 0 size | |
908 | strValue.Empty(); | |
909 | } | |
910 | else | |
911 | { | |
912 | m_dwLastError = RegQueryValueEx((HKEY) m_hKey, | |
913 | WXSTRINGCAST szValue, | |
914 | RESERVED, | |
915 | &dwType, | |
916 | (RegString)(wxChar*)wxStringBuffer(strValue, dwSize), | |
917 | &dwSize); | |
918 | ||
919 | // expand the var expansions in the string unless disabled | |
920 | #ifndef __WXWINCE__ | |
921 | if ( (dwType == REG_EXPAND_SZ) && !raw ) | |
922 | { | |
923 | DWORD dwExpSize = ::ExpandEnvironmentStrings(strValue, NULL, 0); | |
924 | bool ok = dwExpSize != 0; | |
925 | if ( ok ) | |
926 | { | |
927 | wxString strExpValue; | |
928 | ok = ::ExpandEnvironmentStrings(strValue, | |
929 | wxStringBuffer(strExpValue, dwExpSize), | |
930 | dwExpSize | |
931 | ) != 0; | |
932 | strValue = strExpValue; | |
933 | } | |
934 | ||
935 | if ( !ok ) | |
936 | { | |
937 | wxLogLastError(_T("ExpandEnvironmentStrings")); | |
938 | } | |
939 | } | |
940 | #endif | |
941 | // __WXWINCE__ | |
942 | } | |
943 | ||
944 | if ( m_dwLastError == ERROR_SUCCESS ) | |
945 | { | |
946 | // check that it was the right type | |
947 | wxASSERT_MSG( !IsNumericValue(szValue), | |
948 | wxT("Type mismatch in wxRegKey::QueryValue().") ); | |
949 | ||
950 | return true; | |
951 | } | |
952 | } | |
953 | } | |
954 | ||
955 | wxLogSysError(m_dwLastError, _("Can't read value of '%s'"), | |
956 | GetFullName(this, szValue)); | |
957 | return false; | |
958 | } | |
959 | ||
960 | bool wxRegKey::SetValue(const wxChar *szValue, const wxString& strValue) | |
961 | { | |
962 | if ( CONST_CAST Open() ) { | |
963 | m_dwLastError = RegSetValueEx((HKEY) m_hKey, szValue, (DWORD) RESERVED, REG_SZ, | |
964 | (RegString)strValue.c_str(), | |
965 | (strValue.Len() + 1)*sizeof(wxChar)); | |
966 | if ( m_dwLastError == ERROR_SUCCESS ) | |
967 | return true; | |
968 | } | |
969 | ||
970 | wxLogSysError(m_dwLastError, _("Can't set value of '%s'"), | |
971 | GetFullName(this, szValue)); | |
972 | return false; | |
973 | } | |
974 | ||
975 | wxString wxRegKey::QueryDefaultValue() const | |
976 | { | |
977 | wxString str; | |
978 | QueryValue(NULL, str); | |
979 | return str; | |
980 | } | |
981 | ||
982 | // ---------------------------------------------------------------------------- | |
983 | // enumeration | |
984 | // NB: all these functions require an index variable which allows to have | |
985 | // several concurrently running indexations on the same key | |
986 | // ---------------------------------------------------------------------------- | |
987 | ||
988 | bool wxRegKey::GetFirstValue(wxString& strValueName, long& lIndex) | |
989 | { | |
990 | if ( !Open(Read) ) | |
991 | return false; | |
992 | ||
993 | lIndex = 0; | |
994 | return GetNextValue(strValueName, lIndex); | |
995 | } | |
996 | ||
997 | bool wxRegKey::GetNextValue(wxString& strValueName, long& lIndex) const | |
998 | { | |
999 | wxASSERT( IsOpened() ); | |
1000 | ||
1001 | // are we already at the end of enumeration? | |
1002 | if ( lIndex == -1 ) | |
1003 | return false; | |
1004 | ||
1005 | wxChar szValueName[1024]; // @@ use RegQueryInfoKey... | |
1006 | DWORD dwValueLen = WXSIZEOF(szValueName); | |
1007 | ||
1008 | m_dwLastError = RegEnumValue((HKEY) m_hKey, lIndex++, | |
1009 | szValueName, &dwValueLen, | |
1010 | RESERVED, | |
1011 | NULL, // [out] type | |
1012 | NULL, // [out] buffer for value | |
1013 | NULL); // [i/o] it's length | |
1014 | ||
1015 | if ( m_dwLastError != ERROR_SUCCESS ) { | |
1016 | if ( m_dwLastError == ERROR_NO_MORE_ITEMS ) { | |
1017 | m_dwLastError = ERROR_SUCCESS; | |
1018 | lIndex = -1; | |
1019 | } | |
1020 | else { | |
1021 | wxLogSysError(m_dwLastError, _("Can't enumerate values of key '%s'"), | |
1022 | GetName().c_str()); | |
1023 | } | |
1024 | ||
1025 | return false; | |
1026 | } | |
1027 | ||
1028 | strValueName = szValueName; | |
1029 | ||
1030 | return true; | |
1031 | } | |
1032 | ||
1033 | bool wxRegKey::GetFirstKey(wxString& strKeyName, long& lIndex) | |
1034 | { | |
1035 | if ( !Open(Read) ) | |
1036 | return false; | |
1037 | ||
1038 | lIndex = 0; | |
1039 | return GetNextKey(strKeyName, lIndex); | |
1040 | } | |
1041 | ||
1042 | bool wxRegKey::GetNextKey(wxString& strKeyName, long& lIndex) const | |
1043 | { | |
1044 | wxASSERT( IsOpened() ); | |
1045 | ||
1046 | // are we already at the end of enumeration? | |
1047 | if ( lIndex == -1 ) | |
1048 | return false; | |
1049 | ||
1050 | wxChar szKeyName[_MAX_PATH + 1]; | |
1051 | ||
1052 | #ifdef __WXWINCE__ | |
1053 | DWORD sizeName = WXSIZEOF(szKeyName); | |
1054 | m_dwLastError = RegEnumKeyEx((HKEY) m_hKey, lIndex++, szKeyName, & sizeName, | |
1055 | 0, NULL, NULL, NULL); | |
1056 | #else | |
1057 | m_dwLastError = RegEnumKey((HKEY) m_hKey, lIndex++, szKeyName, WXSIZEOF(szKeyName)); | |
1058 | #endif | |
1059 | ||
1060 | if ( m_dwLastError != ERROR_SUCCESS ) { | |
1061 | if ( m_dwLastError == ERROR_NO_MORE_ITEMS ) { | |
1062 | m_dwLastError = ERROR_SUCCESS; | |
1063 | lIndex = -1; | |
1064 | } | |
1065 | else { | |
1066 | wxLogSysError(m_dwLastError, _("Can't enumerate subkeys of key '%s'"), | |
1067 | GetName().c_str()); | |
1068 | } | |
1069 | ||
1070 | return false; | |
1071 | } | |
1072 | ||
1073 | strKeyName = szKeyName; | |
1074 | return true; | |
1075 | } | |
1076 | ||
1077 | // returns true if the value contains a number (else it's some string) | |
1078 | bool wxRegKey::IsNumericValue(const wxChar *szValue) const | |
1079 | { | |
1080 | ValueType type = GetValueType(szValue); | |
1081 | switch ( type ) { | |
1082 | case Type_Dword: | |
1083 | /* case Type_Dword_little_endian: == Type_Dword */ | |
1084 | case Type_Dword_big_endian: | |
1085 | return true; | |
1086 | ||
1087 | default: | |
1088 | return false; | |
1089 | } | |
1090 | } | |
1091 | ||
1092 | // ---------------------------------------------------------------------------- | |
1093 | // exporting registry keys to file | |
1094 | // ---------------------------------------------------------------------------- | |
1095 | ||
1096 | #if wxUSE_STREAMS | |
1097 | ||
1098 | // helper functions for writing ASCII strings (even in Unicode build) | |
1099 | static inline bool WriteAsciiChar(wxOutputStream& ostr, char ch) | |
1100 | { | |
1101 | ostr.PutC(ch); | |
1102 | return ostr.IsOk(); | |
1103 | } | |
1104 | ||
1105 | static inline bool WriteAsciiEOL(wxOutputStream& ostr) | |
1106 | { | |
1107 | // as we open the file in text mode, it is enough to write LF without CR | |
1108 | return WriteAsciiChar(ostr, '\n'); | |
1109 | } | |
1110 | ||
1111 | static inline bool WriteAsciiString(wxOutputStream& ostr, const char *p) | |
1112 | { | |
1113 | return ostr.Write(p, strlen(p)).IsOk(); | |
1114 | } | |
1115 | ||
1116 | static inline bool WriteAsciiString(wxOutputStream& ostr, const wxString& s) | |
1117 | { | |
1118 | #if wxUSE_UNICODE | |
1119 | wxCharBuffer name(s.mb_str()); | |
1120 | ostr.Write(name, strlen(name)); | |
1121 | #else | |
1122 | ostr.Write(s, s.length()); | |
1123 | #endif | |
1124 | ||
1125 | return ostr.IsOk(); | |
1126 | } | |
1127 | ||
1128 | #endif // wxUSE_STREAMS | |
1129 | ||
1130 | bool wxRegKey::Export(const wxString& filename) const | |
1131 | { | |
1132 | #if wxUSE_FFILE && wxUSE_STREAMS | |
1133 | if ( wxFile::Exists(filename) ) | |
1134 | { | |
1135 | wxLogError(_("Exporting registry key: file \"%s\" already exists and won't be overwritten."), | |
1136 | filename.c_str()); | |
1137 | return false; | |
1138 | } | |
1139 | ||
1140 | wxFFileOutputStream ostr(filename, _T("w")); | |
1141 | ||
1142 | return ostr.Ok() && Export(ostr); | |
1143 | #else | |
1144 | wxUnusedVar(filename); | |
1145 | return false; | |
1146 | #endif | |
1147 | } | |
1148 | ||
1149 | #if wxUSE_STREAMS | |
1150 | bool wxRegKey::Export(wxOutputStream& ostr) const | |
1151 | { | |
1152 | // write out the header | |
1153 | if ( !WriteAsciiString(ostr, "REGEDIT4\n\n") ) | |
1154 | return false; | |
1155 | ||
1156 | return DoExport(ostr); | |
1157 | } | |
1158 | #endif // wxUSE_STREAMS | |
1159 | ||
1160 | static | |
1161 | wxString | |
1162 | FormatAsHex(const void *data, | |
1163 | size_t size, | |
1164 | wxRegKey::ValueType type = wxRegKey::Type_Binary) | |
1165 | { | |
1166 | wxString value(_T("hex")); | |
1167 | ||
1168 | // binary values use just "hex:" prefix while the other ones must indicate | |
1169 | // the real type | |
1170 | if ( type != wxRegKey::Type_Binary ) | |
1171 | value << _T('(') << type << _T(')'); | |
1172 | value << _T(':'); | |
1173 | ||
1174 | // write all the rest as comma-separated bytes | |
1175 | value.reserve(3*size + 10); | |
1176 | const char * const p = wx_static_cast(const char *, data); | |
1177 | for ( size_t n = 0; n < size; n++ ) | |
1178 | { | |
1179 | // TODO: line wrapping: although not required by regedit, this makes | |
1180 | // the generated files easier to read and compare with the files | |
1181 | // produced by regedit | |
1182 | if ( n ) | |
1183 | value << _T(','); | |
1184 | ||
1185 | value << wxString::Format(_T("%02x"), (unsigned char)p[n]); | |
1186 | } | |
1187 | ||
1188 | return value; | |
1189 | } | |
1190 | ||
1191 | static inline | |
1192 | wxString FormatAsHex(const wxString& value, wxRegKey::ValueType type) | |
1193 | { | |
1194 | return FormatAsHex(value.c_str(), value.length() + 1, type); | |
1195 | } | |
1196 | ||
1197 | wxString wxRegKey::FormatValue(const wxString& name) const | |
1198 | { | |
1199 | wxString rhs; | |
1200 | const ValueType type = GetValueType(name); | |
1201 | switch ( type ) | |
1202 | { | |
1203 | case Type_String: | |
1204 | { | |
1205 | wxString value; | |
1206 | if ( !QueryValue(name, value) ) | |
1207 | break; | |
1208 | ||
1209 | // quotes and backslashes must be quoted, linefeeds are not | |
1210 | // allowed in string values | |
1211 | rhs.reserve(value.length() + 2); | |
1212 | rhs = _T('"'); | |
1213 | ||
1214 | // there can be no NULs here | |
1215 | bool useHex = false; | |
1216 | for ( const wxChar *p = value.c_str(); *p && !useHex; p++ ) | |
1217 | { | |
1218 | switch ( *p ) | |
1219 | { | |
1220 | case _T('\n'): | |
1221 | // we can only represent this string in hex | |
1222 | useHex = true; | |
1223 | break; | |
1224 | ||
1225 | case _T('"'): | |
1226 | case _T('\\'): | |
1227 | // escape special symbol | |
1228 | rhs += _T('\\'); | |
1229 | // fall through | |
1230 | ||
1231 | default: | |
1232 | rhs += *p; | |
1233 | } | |
1234 | } | |
1235 | ||
1236 | if ( useHex ) | |
1237 | rhs = FormatAsHex(value, Type_String); | |
1238 | else | |
1239 | rhs += _T('"'); | |
1240 | } | |
1241 | break; | |
1242 | ||
1243 | case Type_Dword: | |
1244 | /* case Type_Dword_little_endian: == Type_Dword */ | |
1245 | { | |
1246 | long value; | |
1247 | if ( !QueryValue(name, &value) ) | |
1248 | break; | |
1249 | ||
1250 | rhs.Printf(_T("dword:%08x"), (unsigned int)value); | |
1251 | } | |
1252 | break; | |
1253 | ||
1254 | case Type_Expand_String: | |
1255 | case Type_Multi_String: | |
1256 | { | |
1257 | wxString value; | |
1258 | if ( !QueryRawValue(name, value) ) | |
1259 | break; | |
1260 | ||
1261 | rhs = FormatAsHex(value, type); | |
1262 | } | |
1263 | break; | |
1264 | ||
1265 | case Type_Binary: | |
1266 | { | |
1267 | wxMemoryBuffer buf; | |
1268 | if ( !QueryValue(name, buf) ) | |
1269 | break; | |
1270 | ||
1271 | rhs = FormatAsHex(buf.GetData(), buf.GetDataLen()); | |
1272 | } | |
1273 | break; | |
1274 | ||
1275 | // no idea how those appear in REGEDIT4 files | |
1276 | case Type_None: | |
1277 | case Type_Dword_big_endian: | |
1278 | case Type_Link: | |
1279 | case Type_Resource_list: | |
1280 | case Type_Full_resource_descriptor: | |
1281 | case Type_Resource_requirements_list: | |
1282 | default: | |
1283 | wxLogWarning(_("Can't export value of unsupported type %d."), type); | |
1284 | } | |
1285 | ||
1286 | return rhs; | |
1287 | } | |
1288 | ||
1289 | #if wxUSE_STREAMS | |
1290 | ||
1291 | bool wxRegKey::DoExportValue(wxOutputStream& ostr, const wxString& name) const | |
1292 | { | |
1293 | // first examine the value type: if it's unsupported, simply skip it | |
1294 | // instead of aborting the entire export process because we failed to | |
1295 | // export a single value | |
1296 | wxString value = FormatValue(name); | |
1297 | if ( value.empty() ) | |
1298 | { | |
1299 | wxLogWarning(_("Ignoring value \"%s\" of the key \"%s\"."), | |
1300 | name.c_str(), GetName().c_str()); | |
1301 | return true; | |
1302 | } | |
1303 | ||
1304 | // we do have the text representation of the value, now write everything | |
1305 | // out | |
1306 | ||
1307 | // special case: unnamed/default value is represented as just "@" | |
1308 | if ( name.empty() ) | |
1309 | { | |
1310 | if ( !WriteAsciiChar(ostr, '@') ) | |
1311 | return false; | |
1312 | } | |
1313 | else // normal, named, value | |
1314 | { | |
1315 | if ( !WriteAsciiChar(ostr, '"') || | |
1316 | !WriteAsciiString(ostr, name) || | |
1317 | !WriteAsciiChar(ostr, '"') ) | |
1318 | return false; | |
1319 | } | |
1320 | ||
1321 | if ( !WriteAsciiChar(ostr, '=') ) | |
1322 | return false; | |
1323 | ||
1324 | return WriteAsciiString(ostr, value) && WriteAsciiEOL(ostr); | |
1325 | } | |
1326 | ||
1327 | bool wxRegKey::DoExport(wxOutputStream& ostr) const | |
1328 | { | |
1329 | // write out this key name | |
1330 | if ( !WriteAsciiChar(ostr, '[') ) | |
1331 | return false; | |
1332 | ||
1333 | if ( !WriteAsciiString(ostr, GetName(false /* no short prefix */)) ) | |
1334 | return false; | |
1335 | ||
1336 | if ( !WriteAsciiChar(ostr, ']') || !WriteAsciiEOL(ostr) ) | |
1337 | return false; | |
1338 | ||
1339 | // dump all our values | |
1340 | long dummy; | |
1341 | wxString name; | |
1342 | wxRegKey& self = wx_const_cast(wxRegKey&, *this); | |
1343 | bool cont = self.GetFirstValue(name, dummy); | |
1344 | while ( cont ) | |
1345 | { | |
1346 | if ( !DoExportValue(ostr, name) ) | |
1347 | return false; | |
1348 | ||
1349 | cont = GetNextValue(name, dummy); | |
1350 | } | |
1351 | ||
1352 | // always terminate values by blank line, even if there were no values | |
1353 | if ( !WriteAsciiEOL(ostr) ) | |
1354 | return false; | |
1355 | ||
1356 | // recurse to subkeys | |
1357 | cont = self.GetFirstKey(name, dummy); | |
1358 | while ( cont ) | |
1359 | { | |
1360 | wxRegKey subkey(*this, name); | |
1361 | if ( !subkey.DoExport(ostr) ) | |
1362 | return false; | |
1363 | ||
1364 | cont = GetNextKey(name, dummy); | |
1365 | } | |
1366 | ||
1367 | return true; | |
1368 | } | |
1369 | ||
1370 | #endif // wxUSE_STREAMS | |
1371 | ||
1372 | // ============================================================================ | |
1373 | // implementation of global private functions | |
1374 | // ============================================================================ | |
1375 | ||
1376 | bool KeyExists(WXHKEY hRootKey, const wxChar *szKey) | |
1377 | { | |
1378 | // don't close this key itself for the case of empty szKey! | |
1379 | if ( wxIsEmpty(szKey) ) | |
1380 | return true; | |
1381 | ||
1382 | HKEY hkeyDummy; | |
1383 | if ( ::RegOpenKeyEx | |
1384 | ( | |
1385 | (HKEY)hRootKey, | |
1386 | szKey, | |
1387 | RESERVED, | |
1388 | KEY_READ, // we might not have enough rights for rw access | |
1389 | &hkeyDummy | |
1390 | ) == ERROR_SUCCESS ) | |
1391 | { | |
1392 | ::RegCloseKey(hkeyDummy); | |
1393 | ||
1394 | return true; | |
1395 | } | |
1396 | ||
1397 | return false; | |
1398 | } | |
1399 | ||
1400 | const wxChar *GetFullName(const wxRegKey *pKey, const wxChar *szValue) | |
1401 | { | |
1402 | static wxString s_str; | |
1403 | s_str = pKey->GetName(); | |
1404 | if ( !wxIsEmpty(szValue) ) | |
1405 | s_str << wxT("\\") << szValue; | |
1406 | ||
1407 | return s_str.c_str(); | |
1408 | } | |
1409 | ||
1410 | void RemoveTrailingSeparator(wxString& str) | |
1411 | { | |
1412 | if ( !str.empty() && str.Last() == REG_SEPARATOR ) | |
1413 | str.Truncate(str.Len() - 1); | |
1414 | } |