]>
Commit | Line | Data |
---|---|---|
19454fa0 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: msw/regconf.cpp | |
6d833566 | 3 | // Purpose: |
19454fa0 | 4 | // Author: Vadim Zeitlin |
6d833566 | 5 | // Modified by: |
19454fa0 VZ |
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 | ||
cfe780fb JS |
12 | #ifdef __GNUG__ |
13 | #pragma implementation "regconf.h" | |
14 | #endif | |
15 | ||
a3b46648 UU |
16 | // For compilers that support precompilation, includes "wx.h". |
17 | #include "wx/wxprec.h" | |
19454fa0 | 18 | |
a3b46648 UU |
19 | #ifdef __BORLANDC__ |
20 | #pragma hdrstop | |
82cf4761 VZ |
21 | #endif |
22 | ||
82cf4761 | 23 | #ifndef WX_PRECOMP |
61ba49f2 | 24 | #include "wx/string.h" |
1f3943e0 | 25 | #include "wx/intl.h" |
82cf4761 VZ |
26 | #endif //WX_PRECOMP |
27 | ||
61ba49f2 VZ |
28 | #include "wx/event.h" |
29 | #include "wx/app.h" | |
30 | #include "wx/log.h" | |
f6bcfd97 BP |
31 | |
32 | #if wxUSE_CONFIG | |
33 | ||
61ba49f2 | 34 | #include "wx/config.h" |
3d05544e JS |
35 | |
36 | #ifndef __WIN16__ | |
37 | ||
61ba49f2 VZ |
38 | #include "wx/msw/registry.h" |
39 | #include "wx/msw/regconf.h" | |
19454fa0 VZ |
40 | |
41 | // ---------------------------------------------------------------------------- | |
42 | // constants | |
43 | // ---------------------------------------------------------------------------- | |
44 | ||
45 | // we put our data in HKLM\SOFTWARE_KEY\appname | |
c19a8a9a | 46 | #define SOFTWARE_KEY wxString("Software\\") |
19454fa0 VZ |
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 | // ---------------------------------------------------------------------------- | |
18244936 | 70 | |
040f0110 VZ |
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) | |
19454fa0 | 77 | { |
040f0110 | 78 | wxString strRoot; |
19454fa0 | 79 | |
040f0110 | 80 | bool bDoUseGlobal = (style & wxCONFIG_USE_GLOBAL_FILE) != 0; |
41286812 | 81 | |
040f0110 VZ |
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 | } | |
18244936 | 96 | |
040f0110 VZ |
97 | // no '\\' needed if no vendor name |
98 | if ( !strRoot.IsEmpty() ) | |
18244936 | 99 | { |
040f0110 | 100 | strRoot += '\\'; |
18244936 | 101 | } |
18244936 | 102 | |
040f0110 VZ |
103 | if ( appName.IsEmpty() ) |
104 | { | |
223d09f6 | 105 | wxCHECK_RET( wxTheApp, wxT("No application name in wxRegConfig ctor!") ); |
040f0110 VZ |
106 | strRoot << wxTheApp->GetAppName(); |
107 | } | |
108 | else | |
18244936 | 109 | { |
040f0110 | 110 | strRoot << appName; |
18244936 | 111 | } |
040f0110 VZ |
112 | } |
113 | //else: we don't need to do all the complicated stuff above | |
18244936 | 114 | |
040f0110 | 115 | wxString str = strLocal.IsEmpty() ? strRoot : strLocal; |
807a903e VZ |
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 | ||
040f0110 | 126 | m_keyLocalRoot.SetName(wxRegKey::HKCU, SOFTWARE_KEY + str); |
807a903e | 127 | m_keyLocal.SetName(m_keyLocalRoot, _T("")); |
18244936 | 128 | |
040f0110 VZ |
129 | if ( bDoUseGlobal ) |
130 | { | |
131 | str = strGlobal.IsEmpty() ? strRoot : strGlobal; | |
807a903e VZ |
132 | |
133 | m_keyGlobalRoot.ReserveMemoryForName(MEMORY_PREALLOC); | |
134 | m_keyGlobal.ReserveMemoryForName(MEMORY_PREALLOC); | |
135 | ||
040f0110 | 136 | m_keyGlobalRoot.SetName(wxRegKey::HKLM, SOFTWARE_KEY + str); |
807a903e | 137 | m_keyGlobal.SetName(m_keyGlobalRoot, _T("")); |
040f0110 | 138 | } |
18244936 JS |
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 | ||
040f0110 VZ |
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(); | |
194330c7 | 152 | m_keyGlobal.Open(); |
040f0110 | 153 | } |
18244936 | 154 | } |
19454fa0 VZ |
155 | |
156 | wxRegConfig::~wxRegConfig() | |
157 | { | |
158 | // nothing to do - key will be closed in their dtors | |
159 | } | |
160 | ||
161 | // ---------------------------------------------------------------------------- | |
162 | // path management | |
163 | // ---------------------------------------------------------------------------- | |
807a903e VZ |
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. | |
19454fa0 VZ |
172 | void wxRegConfig::SetPath(const wxString& strPath) |
173 | { | |
807a903e VZ |
174 | // remember the old path |
175 | wxString strOldPath = m_strPath; | |
19454fa0 | 176 | |
807a903e VZ |
177 | #ifdef WX_DEBUG_SET_PATH // non optimized version kept here for testing |
178 | wxString m_strPathAlt; | |
19454fa0 | 179 | |
807a903e VZ |
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 | |
19454fa0 | 205 | |
807a903e VZ |
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 | } | |
b568d04f | 331 | |
807a903e VZ |
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(); | |
b568d04f | 342 | |
807a903e VZ |
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 | |
19454fa0 | 370 | |
807a903e VZ |
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(); | |
19454fa0 VZ |
380 | } |
381 | ||
382 | // ---------------------------------------------------------------------------- | |
383 | // enumeration (works only with current group) | |
384 | // ---------------------------------------------------------------------------- | |
385 | ||
386 | /* | |
6d833566 | 387 | We want to enumerate all local keys/values after the global ones, but, of |
19454fa0 VZ |
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 | ||
02569ba8 | 397 | bool wxRegConfig::GetFirstGroup(wxString& str, long& lIndex) const |
19454fa0 VZ |
398 | { |
399 | lIndex = 0; | |
400 | return GetNextGroup(str, lIndex); | |
401 | } | |
402 | ||
02569ba8 | 403 | bool wxRegConfig::GetNextGroup(wxString& str, long& lIndex) const |
19454fa0 VZ |
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 | |
09f50eb3 | 408 | while ( m_keyGlobal.GetNextKey(str, lIndex) ) { |
807a903e | 409 | if ( !m_keyLocal.Exists() || !LocalKey().HasSubKey(str) ) { |
09f50eb3 VZ |
410 | // ok, found one - return it |
411 | return TRUE; | |
19454fa0 | 412 | } |
09f50eb3 VZ |
413 | } |
414 | ||
415 | // no more global entries | |
416 | lIndex |= LOCAL_MASK; | |
19454fa0 VZ |
417 | } |
418 | ||
807a903e VZ |
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 | ||
19454fa0 VZ |
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; | |
807a903e | 426 | bool bOk = LocalKey().GetNextKey(str, lIndex); |
19454fa0 VZ |
427 | lIndex |= LOCAL_MASK; |
428 | ||
429 | return bOk; | |
430 | } | |
431 | ||
02569ba8 | 432 | bool wxRegConfig::GetFirstEntry(wxString& str, long& lIndex) const |
19454fa0 VZ |
433 | { |
434 | lIndex = 0; | |
82cf4761 | 435 | return GetNextEntry(str, lIndex); |
19454fa0 VZ |
436 | } |
437 | ||
02569ba8 | 438 | bool wxRegConfig::GetNextEntry(wxString& str, long& lIndex) const |
19454fa0 VZ |
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 | |
50f7d0a3 | 443 | while ( m_keyGlobal.GetNextValue(str, lIndex) ) { |
807a903e | 444 | if ( !m_keyLocal.Exists() || !LocalKey().HasValue(str) ) { |
50f7d0a3 VZ |
445 | // ok, found one - return it |
446 | return TRUE; | |
19454fa0 | 447 | } |
50f7d0a3 VZ |
448 | } |
449 | ||
450 | // no more global entries | |
451 | lIndex |= LOCAL_MASK; | |
19454fa0 VZ |
452 | } |
453 | ||
807a903e VZ |
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 | ||
19454fa0 VZ |
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; | |
807a903e | 461 | bool bOk = LocalKey().GetNextValue(str, lIndex); |
19454fa0 VZ |
462 | lIndex |= LOCAL_MASK; |
463 | ||
464 | return bOk; | |
465 | } | |
466 | ||
c86f1403 | 467 | size_t wxRegConfig::GetNumberOfEntries(bool bRecursive) const |
82cf4761 | 468 | { |
c86f1403 | 469 | size_t nEntries = 0; |
82cf4761 VZ |
470 | |
471 | // dummy vars | |
472 | wxString str; | |
473 | long l; | |
6a23cbce | 474 | bool bCont = ((wxRegConfig*)this)->GetFirstEntry(str, l); |
82cf4761 VZ |
475 | while ( bCont ) { |
476 | nEntries++; | |
477 | ||
6a23cbce | 478 | bCont = ((wxRegConfig*)this)->GetNextEntry(str, l); |
82cf4761 VZ |
479 | } |
480 | ||
481 | return nEntries; | |
482 | } | |
483 | ||
c86f1403 | 484 | size_t wxRegConfig::GetNumberOfGroups(bool bRecursive) const |
82cf4761 | 485 | { |
c86f1403 | 486 | size_t nGroups = 0; |
82cf4761 VZ |
487 | |
488 | // dummy vars | |
489 | wxString str; | |
490 | long l; | |
6a23cbce | 491 | bool bCont = ((wxRegConfig*)this)->GetFirstGroup(str, l); |
82cf4761 VZ |
492 | while ( bCont ) { |
493 | nGroups++; | |
494 | ||
6a23cbce | 495 | bCont = ((wxRegConfig*)this)->GetNextGroup(str, l); |
82cf4761 VZ |
496 | } |
497 | ||
498 | return nGroups; | |
499 | } | |
500 | ||
6d833566 VZ |
501 | // ---------------------------------------------------------------------------- |
502 | // tests for existence | |
503 | // ---------------------------------------------------------------------------- | |
504 | ||
61ba49f2 | 505 | bool wxRegConfig::HasGroup(const wxString& key) const |
6d833566 | 506 | { |
61ba49f2 VZ |
507 | wxConfigPathChanger path(this, key); |
508 | ||
509 | wxString strName(path.Name()); | |
510 | ||
807a903e VZ |
511 | return (m_keyLocal.Exists() && LocalKey().HasSubKey(strName)) || |
512 | m_keyGlobal.HasSubKey(strName); | |
6d833566 VZ |
513 | } |
514 | ||
61ba49f2 | 515 | bool wxRegConfig::HasEntry(const wxString& key) const |
6d833566 | 516 | { |
61ba49f2 VZ |
517 | wxConfigPathChanger path(this, key); |
518 | ||
519 | wxString strName(path.Name()); | |
520 | ||
807a903e VZ |
521 | return (m_keyLocal.Exists() && LocalKey().HasValue(strName)) || |
522 | m_keyGlobal.HasValue(strName); | |
61ba49f2 VZ |
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; | |
807a903e | 532 | if ( m_keyLocal.Exists() && LocalKey().HasValue(strName) ) |
61ba49f2 VZ |
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; | |
6d833566 VZ |
540 | } |
541 | ||
19454fa0 VZ |
542 | // ---------------------------------------------------------------------------- |
543 | // reading/writing | |
544 | // ---------------------------------------------------------------------------- | |
545 | ||
18244936 | 546 | bool wxRegConfig::Read(const wxString& key, wxString *pStr) const |
19454fa0 | 547 | { |
18244936 | 548 | wxConfigPathChanger path(this, key); |
19454fa0 | 549 | |
cf447356 | 550 | bool bQueryGlobal = TRUE; |
19454fa0 VZ |
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()) ) { | |
02569ba8 | 555 | if ( TryGetValue(m_keyGlobal, path.Name(), *pStr) ) { |
807a903e | 556 | if ( m_keyLocal.Exists() && LocalKey().HasValue(path.Name()) ) { |
223d09f6 | 557 | wxLogWarning(wxT("User value for immutable key '%s' ignored."), |
19454fa0 VZ |
558 | path.Name().c_str()); |
559 | } | |
18244936 | 560 | *pStr = wxConfigBase::ExpandEnvVars(*pStr); |
cf447356 | 561 | return TRUE; |
19454fa0 VZ |
562 | } |
563 | else { | |
564 | // don't waste time - it's not there anyhow | |
cf447356 | 565 | bQueryGlobal = FALSE; |
19454fa0 VZ |
566 | } |
567 | } | |
568 | ||
569 | // first try local key | |
807a903e | 570 | if ( (m_keyLocal.Exists() && TryGetValue(LocalKey(), path.Name(), *pStr)) || |
02569ba8 | 571 | (bQueryGlobal && TryGetValue(m_keyGlobal, path.Name(), *pStr)) ) { |
41286812 | 572 | // nothing to do |
18244936 | 573 | |
18244936 JS |
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) ) { | |
807a903e | 592 | if ( m_keyLocal.Exists() && LocalKey().HasValue(path.Name()) ) { |
223d09f6 | 593 | wxLogWarning(wxT("User value for immutable key '%s' ignored."), |
18244936 JS |
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 | |
807a903e | 606 | if ( (m_keyLocal.Exists() && TryGetValue(LocalKey(), path.Name(), *pStr)) || |
18244936 JS |
607 | (bQueryGlobal && TryGetValue(m_keyGlobal, path.Name(), *pStr)) ) { |
608 | *pStr = wxConfigBase::ExpandEnvVars(*pStr); | |
609 | return TRUE; | |
41286812 VZ |
610 | } |
611 | else { | |
612 | if ( IsRecordingDefaults() ) { | |
18244936 | 613 | ((wxRegConfig*)this)->Write(key, szDefault); |
41286812 VZ |
614 | } |
615 | ||
616 | // default value | |
617 | *pStr = szDefault; | |
19454fa0 VZ |
618 | } |
619 | ||
41286812 | 620 | *pStr = wxConfigBase::ExpandEnvVars(*pStr); |
baeed289 | 621 | |
cf447356 | 622 | return FALSE; |
19454fa0 VZ |
623 | } |
624 | ||
18244936 | 625 | bool wxRegConfig::Read(const wxString& key, long *plResult) const |
19454fa0 | 626 | { |
18244936 | 627 | wxConfigPathChanger path(this, key); |
19454fa0 | 628 | |
cf447356 | 629 | bool bQueryGlobal = TRUE; |
19454fa0 VZ |
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()) ) { | |
02569ba8 | 634 | if ( TryGetValue(m_keyGlobal, path.Name(), plResult) ) { |
807a903e | 635 | if ( m_keyLocal.Exists() && LocalKey().HasValue(path.Name()) ) { |
223d09f6 | 636 | wxLogWarning(wxT("User value for immutable key '%s' ignored."), |
19454fa0 VZ |
637 | path.Name().c_str()); |
638 | } | |
639 | ||
cf447356 | 640 | return TRUE; |
19454fa0 VZ |
641 | } |
642 | else { | |
643 | // don't waste time - it's not there anyhow | |
cf447356 | 644 | bQueryGlobal = FALSE; |
19454fa0 VZ |
645 | } |
646 | } | |
647 | ||
648 | // first try local key | |
807a903e | 649 | if ( (m_keyLocal.Exists() && TryGetValue(LocalKey(), path.Name(), plResult)) || |
02569ba8 | 650 | (bQueryGlobal && TryGetValue(m_keyGlobal, path.Name(), plResult)) ) { |
cf447356 | 651 | return TRUE; |
19454fa0 | 652 | } |
cf447356 | 653 | return FALSE; |
19454fa0 VZ |
654 | } |
655 | ||
18244936 | 656 | bool wxRegConfig::Write(const wxString& key, const wxString& szValue) |
19454fa0 | 657 | { |
18244936 | 658 | wxConfigPathChanger path(this, key); |
19454fa0 VZ |
659 | |
660 | if ( IsImmutable(path.Name()) ) { | |
223d09f6 | 661 | wxLogError(wxT("Can't change immutable entry '%s'."), path.Name().c_str()); |
cf447356 | 662 | return FALSE; |
19454fa0 VZ |
663 | } |
664 | ||
807a903e | 665 | return LocalKey().SetValue(path.Name(), szValue); |
19454fa0 VZ |
666 | } |
667 | ||
18244936 | 668 | bool wxRegConfig::Write(const wxString& key, long lValue) |
19454fa0 | 669 | { |
18244936 | 670 | wxConfigPathChanger path(this, key); |
19454fa0 VZ |
671 | |
672 | if ( IsImmutable(path.Name()) ) { | |
223d09f6 | 673 | wxLogError(wxT("Can't change immutable entry '%s'."), path.Name().c_str()); |
cf447356 | 674 | return FALSE; |
19454fa0 VZ |
675 | } |
676 | ||
807a903e | 677 | return LocalKey().SetValue(path.Name(), lValue); |
19454fa0 VZ |
678 | } |
679 | ||
680 | // ---------------------------------------------------------------------------- | |
89077ebc VZ |
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 | |
223d09f6 | 736 | wxFAIL_MSG(wxT("Registry key renaming not implemented")); |
89077ebc VZ |
737 | |
738 | return FALSE; | |
739 | } | |
740 | ||
741 | // ---------------------------------------------------------------------------- | |
19454fa0 VZ |
742 | // deleting |
743 | // ---------------------------------------------------------------------------- | |
18244936 | 744 | bool wxRegConfig::DeleteEntry(const wxString& value, bool bGroupIfEmptyAlso) |
19454fa0 | 745 | { |
18244936 | 746 | wxConfigPathChanger path(this, value); |
19454fa0 | 747 | |
807a903e VZ |
748 | if ( m_keyLocal.Exists() ) { |
749 | if ( !m_keyLocal.DeleteValue(path.Name()) ) | |
750 | return FALSE; | |
19454fa0 | 751 | |
807a903e VZ |
752 | if ( m_keyLocal.IsEmpty() ) { |
753 | wxString strKey = GetPath().AfterLast(wxCONFIG_PATH_SEPARATOR); | |
754 | SetPath(".."); // changes m_keyLocal | |
755 | return LocalKey().DeleteKey(strKey); | |
756 | } | |
19454fa0 VZ |
757 | } |
758 | ||
cf447356 | 759 | return TRUE; |
19454fa0 VZ |
760 | } |
761 | ||
18244936 | 762 | bool wxRegConfig::DeleteGroup(const wxString& key) |
19454fa0 | 763 | { |
18244936 | 764 | wxConfigPathChanger path(this, key); |
19454fa0 | 765 | |
807a903e | 766 | return m_keyLocal.Exists() ? LocalKey().DeleteKey(path.Name()) : TRUE; |
19454fa0 VZ |
767 | } |
768 | ||
769 | bool wxRegConfig::DeleteAll() | |
770 | { | |
19454fa0 VZ |
771 | m_keyLocal.Close(); |
772 | m_keyGlobal.Close(); | |
90186e52 | 773 | |
19454fa0 | 774 | bool bOk = m_keyLocalRoot.DeleteSelf(); |
90186e52 VZ |
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() ) | |
19454fa0 VZ |
779 | bOk = m_keyGlobalRoot.DeleteSelf(); |
780 | ||
781 | return bOk; | |
782 | } | |
3d05544e JS |
783 | |
784 | #endif | |
785 | // __WIN16__ | |
786 | ||
f6bcfd97 BP |
787 | #endif |
788 | // wxUSE_CONFIG |