]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: msw/regconf.cpp | |
3 | // Purpose: | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 27.04.98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
9 | // Licence: wxWindows license | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "regconf.h" | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #ifndef WX_PRECOMP | |
24 | #include "wx/string.h" | |
25 | #include "wx/intl.h" | |
26 | #endif //WX_PRECOMP | |
27 | ||
28 | #include "wx/event.h" | |
29 | #include "wx/app.h" | |
30 | #include "wx/log.h" | |
31 | ||
32 | #if wxUSE_CONFIG | |
33 | ||
34 | #include "wx/config.h" | |
35 | ||
36 | #ifndef __WIN16__ | |
37 | ||
38 | #include "wx/msw/registry.h" | |
39 | #include "wx/msw/regconf.h" | |
40 | ||
41 | // ---------------------------------------------------------------------------- | |
42 | // constants | |
43 | // ---------------------------------------------------------------------------- | |
44 | ||
45 | // we put our data in HKLM\SOFTWARE_KEY\appname | |
46 | #define SOFTWARE_KEY wxString("Software\\") | |
47 | ||
48 | // ---------------------------------------------------------------------------- | |
49 | // global functions | |
50 | // ---------------------------------------------------------------------------- | |
51 | ||
52 | // get the value if the key is opened and it exists | |
53 | bool TryGetValue(const wxRegKey& key, const wxString& str, wxString& strVal) | |
54 | { | |
55 | return key.IsOpened() && key.HasValue(str) && key.QueryValue(str, strVal); | |
56 | } | |
57 | ||
58 | bool TryGetValue(const wxRegKey& key, const wxString& str, long *plVal) | |
59 | { | |
60 | return key.IsOpened() && key.HasValue(str) && key.QueryValue(str, plVal); | |
61 | } | |
62 | ||
63 | // ============================================================================ | |
64 | // implementation | |
65 | // ============================================================================ | |
66 | ||
67 | // ---------------------------------------------------------------------------- | |
68 | // ctor/dtor | |
69 | // ---------------------------------------------------------------------------- | |
70 | ||
71 | // create the config object which stores its data under HKCU\vendor\app and, if | |
72 | // style & wxCONFIG_USE_GLOBAL_FILE, under HKLM\vendor\app | |
73 | wxRegConfig::wxRegConfig(const wxString& appName, const wxString& vendorName, | |
74 | const wxString& strLocal, const wxString& strGlobal, | |
75 | long style) | |
76 | : wxConfigBase(appName, vendorName, strLocal, strGlobal, style) | |
77 | { | |
78 | wxString strRoot; | |
79 | ||
80 | bool bDoUseGlobal = (style & wxCONFIG_USE_GLOBAL_FILE) != 0; | |
81 | ||
82 | // the convention is to put the programs keys under <vendor>\<appname> | |
83 | // (but it can be overriden by specifying the pathes explicitly in strLocal | |
84 | // and/or strGlobal) | |
85 | if ( strLocal.IsEmpty() || (strGlobal.IsEmpty() && bDoUseGlobal) ) | |
86 | { | |
87 | if ( vendorName.IsEmpty() ) | |
88 | { | |
89 | if ( wxTheApp ) | |
90 | strRoot = wxTheApp->GetVendorName(); | |
91 | } | |
92 | else | |
93 | { | |
94 | strRoot = vendorName; | |
95 | } | |
96 | ||
97 | // no '\\' needed if no vendor name | |
98 | if ( !strRoot.IsEmpty() ) | |
99 | { | |
100 | strRoot += '\\'; | |
101 | } | |
102 | ||
103 | if ( appName.IsEmpty() ) | |
104 | { | |
105 | wxCHECK_RET( wxTheApp, wxT("No application name in wxRegConfig ctor!") ); | |
106 | strRoot << wxTheApp->GetAppName(); | |
107 | } | |
108 | else | |
109 | { | |
110 | strRoot << appName; | |
111 | } | |
112 | } | |
113 | //else: we don't need to do all the complicated stuff above | |
114 | ||
115 | wxString str = strLocal.IsEmpty() ? strRoot : strLocal; | |
116 | ||
117 | // as we're going to change the name of these keys fairly often and as | |
118 | // there are only few of wxRegConfig objects (usually 1), we can allow | |
119 | // ourselves to be generous and spend some memory to significantly improve | |
120 | // performance of SetPath() | |
121 | static const size_t MEMORY_PREALLOC = 512; | |
122 | ||
123 | m_keyLocalRoot.ReserveMemoryForName(MEMORY_PREALLOC); | |
124 | m_keyLocal.ReserveMemoryForName(MEMORY_PREALLOC); | |
125 | ||
126 | m_keyLocalRoot.SetName(wxRegKey::HKCU, SOFTWARE_KEY + str); | |
127 | m_keyLocal.SetName(m_keyLocalRoot, _T("")); | |
128 | ||
129 | if ( bDoUseGlobal ) | |
130 | { | |
131 | str = strGlobal.IsEmpty() ? strRoot : strGlobal; | |
132 | ||
133 | m_keyGlobalRoot.ReserveMemoryForName(MEMORY_PREALLOC); | |
134 | m_keyGlobal.ReserveMemoryForName(MEMORY_PREALLOC); | |
135 | ||
136 | m_keyGlobalRoot.SetName(wxRegKey::HKLM, SOFTWARE_KEY + str); | |
137 | m_keyGlobal.SetName(m_keyGlobalRoot, _T("")); | |
138 | } | |
139 | ||
140 | // Create() will Open() if key already exists | |
141 | m_keyLocalRoot.Create(); | |
142 | ||
143 | // as it's the same key, Open() shouldn't fail (i.e. no need for Create()) | |
144 | m_keyLocal.Open(); | |
145 | ||
146 | // OTOH, this key may perfectly not exist, so suppress error messages the call | |
147 | // to Open() might generate | |
148 | if ( bDoUseGlobal ) | |
149 | { | |
150 | wxLogNull nolog; | |
151 | m_keyGlobalRoot.Open(); | |
152 | m_keyGlobal.Open(); | |
153 | } | |
154 | } | |
155 | ||
156 | wxRegConfig::~wxRegConfig() | |
157 | { | |
158 | // nothing to do - key will be closed in their dtors | |
159 | } | |
160 | ||
161 | // ---------------------------------------------------------------------------- | |
162 | // path management | |
163 | // ---------------------------------------------------------------------------- | |
164 | ||
165 | // this function is called a *lot* of times (as I learned after seeing from | |
166 | // profiler output that it is called ~12000 times from Mahogany start up code!) | |
167 | // so it is important to optimize it - in particular, avoid using generic | |
168 | // string functions here and do everything manually because it is faster | |
169 | // | |
170 | // I still kept the old version to be able to check that the optimized code has | |
171 | // the same output as the non optimized version. | |
172 | void wxRegConfig::SetPath(const wxString& strPath) | |
173 | { | |
174 | // remember the old path | |
175 | wxString strOldPath = m_strPath; | |
176 | ||
177 | #ifdef WX_DEBUG_SET_PATH // non optimized version kept here for testing | |
178 | wxString m_strPathAlt; | |
179 | ||
180 | { | |
181 | wxArrayString aParts; | |
182 | ||
183 | // because GetPath() returns "" when we're at root, we must understand | |
184 | // empty string as "/" | |
185 | if ( strPath.IsEmpty() || (strPath[0] == wxCONFIG_PATH_SEPARATOR) ) { | |
186 | // absolute path | |
187 | wxSplitPath(aParts, strPath); | |
188 | } | |
189 | else { | |
190 | // relative path, combine with current one | |
191 | wxString strFullPath = GetPath(); | |
192 | strFullPath << wxCONFIG_PATH_SEPARATOR << strPath; | |
193 | wxSplitPath(aParts, strFullPath); | |
194 | } | |
195 | ||
196 | // recombine path parts in one variable | |
197 | wxString strRegPath; | |
198 | m_strPathAlt.Empty(); | |
199 | for ( size_t n = 0; n < aParts.Count(); n++ ) { | |
200 | strRegPath << '\\' << aParts[n]; | |
201 | m_strPathAlt << wxCONFIG_PATH_SEPARATOR << aParts[n]; | |
202 | } | |
203 | } | |
204 | #endif // 0 | |
205 | ||
206 | // check for the most common case first | |
207 | if ( strPath.empty() ) | |
208 | { | |
209 | m_strPath = wxCONFIG_PATH_SEPARATOR; | |
210 | } | |
211 | else // not root | |
212 | { | |
213 | // construct the full path | |
214 | wxString strFullPath; | |
215 | if ( strPath[0u] == wxCONFIG_PATH_SEPARATOR ) | |
216 | { | |
217 | // absolute path | |
218 | strFullPath = strPath; | |
219 | } | |
220 | else // relative path | |
221 | { | |
222 | strFullPath.reserve(2*m_strPath.length()); | |
223 | ||
224 | strFullPath << m_strPath << wxCONFIG_PATH_SEPARATOR << strPath; | |
225 | } | |
226 | ||
227 | // simplify it: we need to handle ".." here | |
228 | ||
229 | // count the total number of slashes we have to know if we can go upper | |
230 | size_t totalSlashes = 0; | |
231 | ||
232 | // position of the last slash to be able to backtrack to it quickly if | |
233 | // needed, but we set it to -1 if we don't have a valid position | |
234 | // | |
235 | // we only remember the last position which means that we handle ".." | |
236 | // quite efficiently but not "../.." - however the latter should be | |
237 | // much more rare, so it is probably ok | |
238 | int posLastSlash = -1; | |
239 | ||
240 | const wxChar *src = strFullPath.c_str(); | |
241 | size_t len = strFullPath.length(); | |
242 | const wxChar *end = src + len; | |
243 | ||
244 | wxChar *dst = m_strPath.GetWriteBuf(len); | |
245 | wxChar *start = dst; | |
246 | ||
247 | for ( ; src < end; src++, dst++ ) | |
248 | { | |
249 | if ( *src == wxCONFIG_PATH_SEPARATOR ) | |
250 | { | |
251 | // check for "/.." | |
252 | ||
253 | // note that we don't have to check for src < end here as | |
254 | // *end == 0 so can't be '.' | |
255 | if ( src[1] == _T('.') && src[2] == _T('.') && | |
256 | (src + 3 == end || src[3] == wxCONFIG_PATH_SEPARATOR) ) | |
257 | { | |
258 | if ( !totalSlashes ) | |
259 | { | |
260 | wxLogWarning(_("'%s' has extra '..', ignored."), | |
261 | strFullPath.c_str()); | |
262 | } | |
263 | else // return to the previous path component | |
264 | { | |
265 | // do we already have its position? | |
266 | if ( posLastSlash == -1 ) | |
267 | { | |
268 | // no, find it: note that we are sure to have one | |
269 | // because totalSlashes > 0 so we don't have to | |
270 | // check the boundary condition below | |
271 | ||
272 | // this is more efficient than strrchr() | |
273 | while ( *dst != wxCONFIG_PATH_SEPARATOR ) | |
274 | { | |
275 | dst--; | |
276 | } | |
277 | } | |
278 | else // the position of last slash was stored | |
279 | { | |
280 | // go directly there | |
281 | dst = start + posLastSlash; | |
282 | ||
283 | // invalidate posLastSlash | |
284 | posLastSlash = -1; | |
285 | } | |
286 | ||
287 | // this shouldn't happen | |
288 | wxASSERT_MSG( *dst == wxCONFIG_PATH_SEPARATOR, | |
289 | _T("error in wxRegConfig::SetPath") ); | |
290 | ||
291 | // we killed one | |
292 | totalSlashes--; | |
293 | } | |
294 | ||
295 | // skip both dots | |
296 | src += 2; | |
297 | } | |
298 | else // not "/.." | |
299 | { | |
300 | if ( (dst == start) || (dst[-1] != wxCONFIG_PATH_SEPARATOR) ) | |
301 | { | |
302 | *dst = wxCONFIG_PATH_SEPARATOR; | |
303 | ||
304 | posLastSlash = dst - start; | |
305 | ||
306 | totalSlashes++; | |
307 | } | |
308 | //else: nothing to do, we squeeze several subseuquent | |
309 | // slashes into one | |
310 | } | |
311 | } | |
312 | else // normal character | |
313 | { | |
314 | // just copy | |
315 | *dst = *src; | |
316 | } | |
317 | } | |
318 | ||
319 | // NUL terminate the string | |
320 | if ( dst[-1] == wxCONFIG_PATH_SEPARATOR && (dst != start + 1) ) | |
321 | { | |
322 | // if it has a trailing slash we remove it unless it is the only | |
323 | // string character | |
324 | dst--; | |
325 | } | |
326 | ||
327 | *dst = _T('\0'); | |
328 | ||
329 | m_strPath.UngetWriteBuf(dst - start); | |
330 | } | |
331 | ||
332 | #ifdef WX_DEBUG_SET_PATH | |
333 | wxASSERT( m_strPath == m_strPathAlt ); | |
334 | #endif | |
335 | ||
336 | if ( m_strPath == strOldPath ) | |
337 | return; | |
338 | ||
339 | // registry APIs want backslashes instead of slashes | |
340 | wxString strRegPath; | |
341 | size_t len = m_strPath.length(); | |
342 | ||
343 | const wxChar *src = m_strPath.c_str(); | |
344 | wxChar *dst = strRegPath.GetWriteBuf(len); | |
345 | ||
346 | const wxChar *end = src + len; | |
347 | for ( ; src < end; src++, dst++ ) | |
348 | { | |
349 | if ( *src == wxCONFIG_PATH_SEPARATOR ) | |
350 | *dst = _T('\\'); | |
351 | else | |
352 | *dst = *src; | |
353 | } | |
354 | ||
355 | strRegPath.UngetWriteBuf(len); | |
356 | ||
357 | // this is not needed any longer as we don't create keys unnecessarily any | |
358 | // more (now it is done on demand, i.e. only when they're going to contain | |
359 | // something) | |
360 | #if 0 | |
361 | // as we create the registry key when SetPath(key) is done, we can be left | |
362 | // with plenty of empty keys if this was only done to try to read some | |
363 | // value which, in fact, doesn't exist - to prevent this from happening we | |
364 | // automatically delete the old key if it was empty | |
365 | if ( m_keyLocal.Exists() && LocalKey().IsEmpty() ) | |
366 | { | |
367 | m_keyLocal.DeleteSelf(); | |
368 | } | |
369 | #endif // 0 | |
370 | ||
371 | // change current key(s) | |
372 | m_keyLocal.SetName(m_keyLocalRoot, strRegPath); | |
373 | m_keyGlobal.SetName(m_keyGlobalRoot, strRegPath); | |
374 | ||
375 | // don't create it right now, wait until it is accessed | |
376 | //m_keyLocal.Create(); | |
377 | ||
378 | wxLogNull nolog; | |
379 | m_keyGlobal.Open(); | |
380 | } | |
381 | ||
382 | // ---------------------------------------------------------------------------- | |
383 | // enumeration (works only with current group) | |
384 | // ---------------------------------------------------------------------------- | |
385 | ||
386 | /* | |
387 | We want to enumerate all local keys/values after the global ones, but, of | |
388 | course, we don't want to repeat a key which appears locally as well as | |
389 | globally twice. | |
390 | ||
391 | We use the 15th bit of lIndex for distinction between global and local. | |
392 | */ | |
393 | ||
394 | #define LOCAL_MASK 0x8000 | |
395 | #define IS_LOCAL_INDEX(l) (((l) & LOCAL_MASK) != 0) | |
396 | ||
397 | bool wxRegConfig::GetFirstGroup(wxString& str, long& lIndex) const | |
398 | { | |
399 | lIndex = 0; | |
400 | return GetNextGroup(str, lIndex); | |
401 | } | |
402 | ||
403 | bool wxRegConfig::GetNextGroup(wxString& str, long& lIndex) const | |
404 | { | |
405 | // are we already enumerating local entries? | |
406 | if ( m_keyGlobal.IsOpened() && !IS_LOCAL_INDEX(lIndex) ) { | |
407 | // try to find a global entry which doesn't appear locally | |
408 | while ( m_keyGlobal.GetNextKey(str, lIndex) ) { | |
409 | if ( !m_keyLocal.Exists() || !LocalKey().HasSubKey(str) ) { | |
410 | // ok, found one - return it | |
411 | return TRUE; | |
412 | } | |
413 | } | |
414 | ||
415 | // no more global entries | |
416 | lIndex |= LOCAL_MASK; | |
417 | } | |
418 | ||
419 | // if we don't have the key at all, don't try to enumerate anything under it | |
420 | if ( !m_keyLocal.Exists() ) | |
421 | return FALSE; | |
422 | ||
423 | // much easier with local entries: get the next one we find | |
424 | // (don't forget to clear our flag bit and set it again later) | |
425 | lIndex &= ~LOCAL_MASK; | |
426 | bool bOk = LocalKey().GetNextKey(str, lIndex); | |
427 | lIndex |= LOCAL_MASK; | |
428 | ||
429 | return bOk; | |
430 | } | |
431 | ||
432 | bool wxRegConfig::GetFirstEntry(wxString& str, long& lIndex) const | |
433 | { | |
434 | lIndex = 0; | |
435 | return GetNextEntry(str, lIndex); | |
436 | } | |
437 | ||
438 | bool wxRegConfig::GetNextEntry(wxString& str, long& lIndex) const | |
439 | { | |
440 | // are we already enumerating local entries? | |
441 | if ( m_keyGlobal.IsOpened() && !IS_LOCAL_INDEX(lIndex) ) { | |
442 | // try to find a global entry which doesn't appear locally | |
443 | while ( m_keyGlobal.GetNextValue(str, lIndex) ) { | |
444 | if ( !m_keyLocal.Exists() || !LocalKey().HasValue(str) ) { | |
445 | // ok, found one - return it | |
446 | return TRUE; | |
447 | } | |
448 | } | |
449 | ||
450 | // no more global entries | |
451 | lIndex |= LOCAL_MASK; | |
452 | } | |
453 | ||
454 | // if we don't have the key at all, don't try to enumerate anything under it | |
455 | if ( !m_keyLocal.Exists() ) | |
456 | return FALSE; | |
457 | ||
458 | // much easier with local entries: get the next one we find | |
459 | // (don't forget to clear our flag bit and set it again later) | |
460 | lIndex &= ~LOCAL_MASK; | |
461 | bool bOk = LocalKey().GetNextValue(str, lIndex); | |
462 | lIndex |= LOCAL_MASK; | |
463 | ||
464 | return bOk; | |
465 | } | |
466 | ||
467 | size_t wxRegConfig::GetNumberOfEntries(bool bRecursive) const | |
468 | { | |
469 | size_t nEntries = 0; | |
470 | ||
471 | // dummy vars | |
472 | wxString str; | |
473 | long l; | |
474 | bool bCont = ((wxRegConfig*)this)->GetFirstEntry(str, l); | |
475 | while ( bCont ) { | |
476 | nEntries++; | |
477 | ||
478 | bCont = ((wxRegConfig*)this)->GetNextEntry(str, l); | |
479 | } | |
480 | ||
481 | return nEntries; | |
482 | } | |
483 | ||
484 | size_t wxRegConfig::GetNumberOfGroups(bool bRecursive) const | |
485 | { | |
486 | size_t nGroups = 0; | |
487 | ||
488 | // dummy vars | |
489 | wxString str; | |
490 | long l; | |
491 | bool bCont = ((wxRegConfig*)this)->GetFirstGroup(str, l); | |
492 | while ( bCont ) { | |
493 | nGroups++; | |
494 | ||
495 | bCont = ((wxRegConfig*)this)->GetNextGroup(str, l); | |
496 | } | |
497 | ||
498 | return nGroups; | |
499 | } | |
500 | ||
501 | // ---------------------------------------------------------------------------- | |
502 | // tests for existence | |
503 | // ---------------------------------------------------------------------------- | |
504 | ||
505 | bool wxRegConfig::HasGroup(const wxString& key) const | |
506 | { | |
507 | wxConfigPathChanger path(this, key); | |
508 | ||
509 | wxString strName(path.Name()); | |
510 | ||
511 | return (m_keyLocal.Exists() && LocalKey().HasSubKey(strName)) || | |
512 | m_keyGlobal.HasSubKey(strName); | |
513 | } | |
514 | ||
515 | bool wxRegConfig::HasEntry(const wxString& key) const | |
516 | { | |
517 | wxConfigPathChanger path(this, key); | |
518 | ||
519 | wxString strName(path.Name()); | |
520 | ||
521 | return (m_keyLocal.Exists() && LocalKey().HasValue(strName)) || | |
522 | m_keyGlobal.HasValue(strName); | |
523 | } | |
524 | ||
525 | wxConfigBase::EntryType wxRegConfig::GetEntryType(const wxString& key) const | |
526 | { | |
527 | wxConfigPathChanger path(this, key); | |
528 | ||
529 | wxString strName(path.Name()); | |
530 | ||
531 | bool isNumeric; | |
532 | if ( m_keyLocal.Exists() && LocalKey().HasValue(strName) ) | |
533 | isNumeric = m_keyLocal.IsNumericValue(strName); | |
534 | else if ( m_keyGlobal.HasValue(strName) ) | |
535 | isNumeric = m_keyGlobal.IsNumericValue(strName); | |
536 | else | |
537 | return wxConfigBase::Type_Unknown; | |
538 | ||
539 | return isNumeric ? wxConfigBase::Type_Integer : wxConfigBase::Type_String; | |
540 | } | |
541 | ||
542 | // ---------------------------------------------------------------------------- | |
543 | // reading/writing | |
544 | // ---------------------------------------------------------------------------- | |
545 | ||
546 | bool wxRegConfig::Read(const wxString& key, wxString *pStr) const | |
547 | { | |
548 | wxConfigPathChanger path(this, key); | |
549 | ||
550 | bool bQueryGlobal = TRUE; | |
551 | ||
552 | // if immutable key exists in global key we must check that it's not | |
553 | // overriden by the local key with the same name | |
554 | if ( IsImmutable(path.Name()) ) { | |
555 | if ( TryGetValue(m_keyGlobal, path.Name(), *pStr) ) { | |
556 | if ( m_keyLocal.Exists() && LocalKey().HasValue(path.Name()) ) { | |
557 | wxLogWarning(wxT("User value for immutable key '%s' ignored."), | |
558 | path.Name().c_str()); | |
559 | } | |
560 | *pStr = wxConfigBase::ExpandEnvVars(*pStr); | |
561 | return TRUE; | |
562 | } | |
563 | else { | |
564 | // don't waste time - it's not there anyhow | |
565 | bQueryGlobal = FALSE; | |
566 | } | |
567 | } | |
568 | ||
569 | // first try local key | |
570 | if ( (m_keyLocal.Exists() && TryGetValue(LocalKey(), path.Name(), *pStr)) || | |
571 | (bQueryGlobal && TryGetValue(m_keyGlobal, path.Name(), *pStr)) ) { | |
572 | // nothing to do | |
573 | ||
574 | *pStr = wxConfigBase::ExpandEnvVars(*pStr); | |
575 | return TRUE; | |
576 | } | |
577 | ||
578 | return FALSE; | |
579 | } | |
580 | ||
581 | bool wxRegConfig::Read(const wxString& key, wxString *pStr, | |
582 | const wxString& szDefault) const | |
583 | { | |
584 | wxConfigPathChanger path(this, key); | |
585 | ||
586 | bool bQueryGlobal = TRUE; | |
587 | ||
588 | // if immutable key exists in global key we must check that it's not | |
589 | // overriden by the local key with the same name | |
590 | if ( IsImmutable(path.Name()) ) { | |
591 | if ( TryGetValue(m_keyGlobal, path.Name(), *pStr) ) { | |
592 | if ( m_keyLocal.Exists() && LocalKey().HasValue(path.Name()) ) { | |
593 | wxLogWarning(wxT("User value for immutable key '%s' ignored."), | |
594 | path.Name().c_str()); | |
595 | } | |
596 | ||
597 | return TRUE; | |
598 | } | |
599 | else { | |
600 | // don't waste time - it's not there anyhow | |
601 | bQueryGlobal = FALSE; | |
602 | } | |
603 | } | |
604 | ||
605 | // first try local key | |
606 | if ( (m_keyLocal.Exists() && TryGetValue(LocalKey(), path.Name(), *pStr)) || | |
607 | (bQueryGlobal && TryGetValue(m_keyGlobal, path.Name(), *pStr)) ) { | |
608 | *pStr = wxConfigBase::ExpandEnvVars(*pStr); | |
609 | return TRUE; | |
610 | } | |
611 | else { | |
612 | if ( IsRecordingDefaults() ) { | |
613 | ((wxRegConfig*)this)->Write(key, szDefault); | |
614 | } | |
615 | ||
616 | // default value | |
617 | *pStr = szDefault; | |
618 | } | |
619 | ||
620 | *pStr = wxConfigBase::ExpandEnvVars(*pStr); | |
621 | ||
622 | return FALSE; | |
623 | } | |
624 | ||
625 | bool wxRegConfig::Read(const wxString& key, long *plResult) const | |
626 | { | |
627 | wxConfigPathChanger path(this, key); | |
628 | ||
629 | bool bQueryGlobal = TRUE; | |
630 | ||
631 | // if immutable key exists in global key we must check that it's not | |
632 | // overriden by the local key with the same name | |
633 | if ( IsImmutable(path.Name()) ) { | |
634 | if ( TryGetValue(m_keyGlobal, path.Name(), plResult) ) { | |
635 | if ( m_keyLocal.Exists() && LocalKey().HasValue(path.Name()) ) { | |
636 | wxLogWarning(wxT("User value for immutable key '%s' ignored."), | |
637 | path.Name().c_str()); | |
638 | } | |
639 | ||
640 | return TRUE; | |
641 | } | |
642 | else { | |
643 | // don't waste time - it's not there anyhow | |
644 | bQueryGlobal = FALSE; | |
645 | } | |
646 | } | |
647 | ||
648 | // first try local key | |
649 | if ( (m_keyLocal.Exists() && TryGetValue(LocalKey(), path.Name(), plResult)) || | |
650 | (bQueryGlobal && TryGetValue(m_keyGlobal, path.Name(), plResult)) ) { | |
651 | return TRUE; | |
652 | } | |
653 | return FALSE; | |
654 | } | |
655 | ||
656 | bool wxRegConfig::Write(const wxString& key, const wxString& szValue) | |
657 | { | |
658 | wxConfigPathChanger path(this, key); | |
659 | ||
660 | if ( IsImmutable(path.Name()) ) { | |
661 | wxLogError(wxT("Can't change immutable entry '%s'."), path.Name().c_str()); | |
662 | return FALSE; | |
663 | } | |
664 | ||
665 | return LocalKey().SetValue(path.Name(), szValue); | |
666 | } | |
667 | ||
668 | bool wxRegConfig::Write(const wxString& key, long lValue) | |
669 | { | |
670 | wxConfigPathChanger path(this, key); | |
671 | ||
672 | if ( IsImmutable(path.Name()) ) { | |
673 | wxLogError(wxT("Can't change immutable entry '%s'."), path.Name().c_str()); | |
674 | return FALSE; | |
675 | } | |
676 | ||
677 | return LocalKey().SetValue(path.Name(), lValue); | |
678 | } | |
679 | ||
680 | // ---------------------------------------------------------------------------- | |
681 | // renaming | |
682 | // ---------------------------------------------------------------------------- | |
683 | ||
684 | bool wxRegConfig::RenameEntry(const wxString& oldName, const wxString& newName) | |
685 | { | |
686 | // check that the old entry exists... | |
687 | if ( !HasEntry(oldName) ) | |
688 | return FALSE; | |
689 | ||
690 | // and that the new one doesn't | |
691 | if ( HasEntry(newName) ) | |
692 | return FALSE; | |
693 | ||
694 | // delete the old entry and create the new one - but do in the reverse | |
695 | // order to not lose the data if Create() fails | |
696 | ||
697 | bool ok; | |
698 | if ( m_keyLocal.IsNumericValue(oldName) ) | |
699 | { | |
700 | long val; | |
701 | ok = m_keyLocal.QueryValue(oldName, &val) && | |
702 | m_keyLocal.SetValue(newName, val); | |
703 | } | |
704 | else | |
705 | { | |
706 | wxString val; | |
707 | ok = m_keyLocal.QueryValue(oldName, val) && | |
708 | m_keyLocal.SetValue(newName, val); | |
709 | } | |
710 | ||
711 | if ( !ok ) | |
712 | return FALSE; | |
713 | ||
714 | if ( !m_keyLocal.DeleteValue(oldName) ) | |
715 | { | |
716 | m_keyLocal.DeleteValue(newName); | |
717 | ||
718 | return FALSE; | |
719 | } | |
720 | ||
721 | return TRUE; | |
722 | } | |
723 | ||
724 | bool wxRegConfig::RenameGroup(const wxString& oldName, const wxString& newName) | |
725 | { | |
726 | // check that the old group exists... | |
727 | if ( !HasGroup(oldName) ) | |
728 | return FALSE; | |
729 | ||
730 | // and that the new one doesn't | |
731 | if ( HasGroup(newName) ) | |
732 | return FALSE; | |
733 | ||
734 | // TODO there is no way to rename a registry key - we must do a deep copy | |
735 | // ourselves | |
736 | wxFAIL_MSG(wxT("Registry key renaming not implemented")); | |
737 | ||
738 | return FALSE; | |
739 | } | |
740 | ||
741 | // ---------------------------------------------------------------------------- | |
742 | // deleting | |
743 | // ---------------------------------------------------------------------------- | |
744 | bool wxRegConfig::DeleteEntry(const wxString& value, bool bGroupIfEmptyAlso) | |
745 | { | |
746 | wxConfigPathChanger path(this, value); | |
747 | ||
748 | if ( m_keyLocal.Exists() ) { | |
749 | if ( !m_keyLocal.DeleteValue(path.Name()) ) | |
750 | return FALSE; | |
751 | ||
752 | if ( m_keyLocal.IsEmpty() ) { | |
753 | wxString strKey = GetPath().AfterLast(wxCONFIG_PATH_SEPARATOR); | |
754 | SetPath(".."); // changes m_keyLocal | |
755 | return LocalKey().DeleteKey(strKey); | |
756 | } | |
757 | } | |
758 | ||
759 | return TRUE; | |
760 | } | |
761 | ||
762 | bool wxRegConfig::DeleteGroup(const wxString& key) | |
763 | { | |
764 | wxConfigPathChanger path(this, key); | |
765 | ||
766 | return m_keyLocal.Exists() ? LocalKey().DeleteKey(path.Name()) : TRUE; | |
767 | } | |
768 | ||
769 | bool wxRegConfig::DeleteAll() | |
770 | { | |
771 | m_keyLocal.Close(); | |
772 | m_keyGlobal.Close(); | |
773 | ||
774 | bool bOk = m_keyLocalRoot.DeleteSelf(); | |
775 | ||
776 | // make sure that we opened m_keyGlobalRoot and so it has a reasonable name: | |
777 | // otherwise we will delete HKEY_CLASSES_ROOT recursively | |
778 | if ( bOk && m_keyGlobalRoot.IsOpened() ) | |
779 | bOk = m_keyGlobalRoot.DeleteSelf(); | |
780 | ||
781 | return bOk; | |
782 | } | |
783 | ||
784 | #endif | |
785 | // __WIN16__ | |
786 | ||
787 | #endif | |
788 | // wxUSE_CONFIG |