]>
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 | ||
e9c4c02c VS |
224 | strFullPath << m_strPath; |
225 | if ( strFullPath.Len() == 0 || | |
226 | strFullPath.Last() != wxCONFIG_PATH_SEPARATOR ) | |
227 | strFullPath << wxCONFIG_PATH_SEPARATOR; | |
228 | strFullPath << strPath; | |
807a903e VZ |
229 | } |
230 | ||
231 | // simplify it: we need to handle ".." here | |
232 | ||
233 | // count the total number of slashes we have to know if we can go upper | |
234 | size_t totalSlashes = 0; | |
235 | ||
236 | // position of the last slash to be able to backtrack to it quickly if | |
237 | // needed, but we set it to -1 if we don't have a valid position | |
238 | // | |
239 | // we only remember the last position which means that we handle ".." | |
240 | // quite efficiently but not "../.." - however the latter should be | |
241 | // much more rare, so it is probably ok | |
242 | int posLastSlash = -1; | |
243 | ||
244 | const wxChar *src = strFullPath.c_str(); | |
245 | size_t len = strFullPath.length(); | |
246 | const wxChar *end = src + len; | |
247 | ||
248 | wxChar *dst = m_strPath.GetWriteBuf(len); | |
249 | wxChar *start = dst; | |
250 | ||
251 | for ( ; src < end; src++, dst++ ) | |
252 | { | |
253 | if ( *src == wxCONFIG_PATH_SEPARATOR ) | |
254 | { | |
255 | // check for "/.." | |
256 | ||
257 | // note that we don't have to check for src < end here as | |
258 | // *end == 0 so can't be '.' | |
259 | if ( src[1] == _T('.') && src[2] == _T('.') && | |
260 | (src + 3 == end || src[3] == wxCONFIG_PATH_SEPARATOR) ) | |
261 | { | |
262 | if ( !totalSlashes ) | |
263 | { | |
264 | wxLogWarning(_("'%s' has extra '..', ignored."), | |
265 | strFullPath.c_str()); | |
266 | } | |
267 | else // return to the previous path component | |
268 | { | |
269 | // do we already have its position? | |
270 | if ( posLastSlash == -1 ) | |
271 | { | |
272 | // no, find it: note that we are sure to have one | |
273 | // because totalSlashes > 0 so we don't have to | |
274 | // check the boundary condition below | |
275 | ||
276 | // this is more efficient than strrchr() | |
277 | while ( *dst != wxCONFIG_PATH_SEPARATOR ) | |
278 | { | |
279 | dst--; | |
280 | } | |
281 | } | |
282 | else // the position of last slash was stored | |
283 | { | |
284 | // go directly there | |
285 | dst = start + posLastSlash; | |
286 | ||
287 | // invalidate posLastSlash | |
288 | posLastSlash = -1; | |
289 | } | |
290 | ||
291 | // this shouldn't happen | |
292 | wxASSERT_MSG( *dst == wxCONFIG_PATH_SEPARATOR, | |
293 | _T("error in wxRegConfig::SetPath") ); | |
294 | ||
295 | // we killed one | |
296 | totalSlashes--; | |
297 | } | |
298 | ||
299 | // skip both dots | |
300 | src += 2; | |
301 | } | |
302 | else // not "/.." | |
303 | { | |
304 | if ( (dst == start) || (dst[-1] != wxCONFIG_PATH_SEPARATOR) ) | |
305 | { | |
306 | *dst = wxCONFIG_PATH_SEPARATOR; | |
307 | ||
308 | posLastSlash = dst - start; | |
309 | ||
310 | totalSlashes++; | |
311 | } | |
eaac8805 VZ |
312 | else // previous char was a slash too |
313 | { | |
314 | // squeeze several subsequent slashes into one: i.e. | |
315 | // just ignore this one | |
316 | dst--; | |
317 | } | |
807a903e VZ |
318 | } |
319 | } | |
320 | else // normal character | |
321 | { | |
322 | // just copy | |
323 | *dst = *src; | |
324 | } | |
325 | } | |
326 | ||
327 | // NUL terminate the string | |
328 | if ( dst[-1] == wxCONFIG_PATH_SEPARATOR && (dst != start + 1) ) | |
329 | { | |
330 | // if it has a trailing slash we remove it unless it is the only | |
331 | // string character | |
332 | dst--; | |
333 | } | |
334 | ||
335 | *dst = _T('\0'); | |
336 | ||
337 | m_strPath.UngetWriteBuf(dst - start); | |
338 | } | |
b568d04f | 339 | |
807a903e VZ |
340 | #ifdef WX_DEBUG_SET_PATH |
341 | wxASSERT( m_strPath == m_strPathAlt ); | |
342 | #endif | |
343 | ||
344 | if ( m_strPath == strOldPath ) | |
345 | return; | |
346 | ||
347 | // registry APIs want backslashes instead of slashes | |
348 | wxString strRegPath; | |
349 | size_t len = m_strPath.length(); | |
b568d04f | 350 | |
807a903e VZ |
351 | const wxChar *src = m_strPath.c_str(); |
352 | wxChar *dst = strRegPath.GetWriteBuf(len); | |
353 | ||
354 | const wxChar *end = src + len; | |
355 | for ( ; src < end; src++, dst++ ) | |
356 | { | |
357 | if ( *src == wxCONFIG_PATH_SEPARATOR ) | |
358 | *dst = _T('\\'); | |
359 | else | |
360 | *dst = *src; | |
361 | } | |
362 | ||
363 | strRegPath.UngetWriteBuf(len); | |
364 | ||
365 | // this is not needed any longer as we don't create keys unnecessarily any | |
366 | // more (now it is done on demand, i.e. only when they're going to contain | |
367 | // something) | |
368 | #if 0 | |
369 | // as we create the registry key when SetPath(key) is done, we can be left | |
370 | // with plenty of empty keys if this was only done to try to read some | |
371 | // value which, in fact, doesn't exist - to prevent this from happening we | |
372 | // automatically delete the old key if it was empty | |
373 | if ( m_keyLocal.Exists() && LocalKey().IsEmpty() ) | |
374 | { | |
375 | m_keyLocal.DeleteSelf(); | |
376 | } | |
377 | #endif // 0 | |
19454fa0 | 378 | |
807a903e VZ |
379 | // change current key(s) |
380 | m_keyLocal.SetName(m_keyLocalRoot, strRegPath); | |
381 | m_keyGlobal.SetName(m_keyGlobalRoot, strRegPath); | |
382 | ||
383 | // don't create it right now, wait until it is accessed | |
384 | //m_keyLocal.Create(); | |
385 | ||
386 | wxLogNull nolog; | |
387 | m_keyGlobal.Open(); | |
19454fa0 VZ |
388 | } |
389 | ||
390 | // ---------------------------------------------------------------------------- | |
391 | // enumeration (works only with current group) | |
392 | // ---------------------------------------------------------------------------- | |
393 | ||
394 | /* | |
6d833566 | 395 | We want to enumerate all local keys/values after the global ones, but, of |
19454fa0 VZ |
396 | course, we don't want to repeat a key which appears locally as well as |
397 | globally twice. | |
398 | ||
399 | We use the 15th bit of lIndex for distinction between global and local. | |
400 | */ | |
401 | ||
402 | #define LOCAL_MASK 0x8000 | |
403 | #define IS_LOCAL_INDEX(l) (((l) & LOCAL_MASK) != 0) | |
404 | ||
02569ba8 | 405 | bool wxRegConfig::GetFirstGroup(wxString& str, long& lIndex) const |
19454fa0 VZ |
406 | { |
407 | lIndex = 0; | |
408 | return GetNextGroup(str, lIndex); | |
409 | } | |
410 | ||
02569ba8 | 411 | bool wxRegConfig::GetNextGroup(wxString& str, long& lIndex) const |
19454fa0 VZ |
412 | { |
413 | // are we already enumerating local entries? | |
414 | if ( m_keyGlobal.IsOpened() && !IS_LOCAL_INDEX(lIndex) ) { | |
415 | // try to find a global entry which doesn't appear locally | |
09f50eb3 | 416 | while ( m_keyGlobal.GetNextKey(str, lIndex) ) { |
807a903e | 417 | if ( !m_keyLocal.Exists() || !LocalKey().HasSubKey(str) ) { |
09f50eb3 VZ |
418 | // ok, found one - return it |
419 | return TRUE; | |
19454fa0 | 420 | } |
09f50eb3 VZ |
421 | } |
422 | ||
423 | // no more global entries | |
424 | lIndex |= LOCAL_MASK; | |
19454fa0 VZ |
425 | } |
426 | ||
807a903e VZ |
427 | // if we don't have the key at all, don't try to enumerate anything under it |
428 | if ( !m_keyLocal.Exists() ) | |
429 | return FALSE; | |
430 | ||
19454fa0 VZ |
431 | // much easier with local entries: get the next one we find |
432 | // (don't forget to clear our flag bit and set it again later) | |
433 | lIndex &= ~LOCAL_MASK; | |
807a903e | 434 | bool bOk = LocalKey().GetNextKey(str, lIndex); |
19454fa0 VZ |
435 | lIndex |= LOCAL_MASK; |
436 | ||
437 | return bOk; | |
438 | } | |
439 | ||
02569ba8 | 440 | bool wxRegConfig::GetFirstEntry(wxString& str, long& lIndex) const |
19454fa0 VZ |
441 | { |
442 | lIndex = 0; | |
82cf4761 | 443 | return GetNextEntry(str, lIndex); |
19454fa0 VZ |
444 | } |
445 | ||
02569ba8 | 446 | bool wxRegConfig::GetNextEntry(wxString& str, long& lIndex) const |
19454fa0 VZ |
447 | { |
448 | // are we already enumerating local entries? | |
449 | if ( m_keyGlobal.IsOpened() && !IS_LOCAL_INDEX(lIndex) ) { | |
450 | // try to find a global entry which doesn't appear locally | |
50f7d0a3 | 451 | while ( m_keyGlobal.GetNextValue(str, lIndex) ) { |
807a903e | 452 | if ( !m_keyLocal.Exists() || !LocalKey().HasValue(str) ) { |
50f7d0a3 VZ |
453 | // ok, found one - return it |
454 | return TRUE; | |
19454fa0 | 455 | } |
50f7d0a3 VZ |
456 | } |
457 | ||
458 | // no more global entries | |
459 | lIndex |= LOCAL_MASK; | |
19454fa0 VZ |
460 | } |
461 | ||
807a903e VZ |
462 | // if we don't have the key at all, don't try to enumerate anything under it |
463 | if ( !m_keyLocal.Exists() ) | |
464 | return FALSE; | |
465 | ||
19454fa0 VZ |
466 | // much easier with local entries: get the next one we find |
467 | // (don't forget to clear our flag bit and set it again later) | |
468 | lIndex &= ~LOCAL_MASK; | |
807a903e | 469 | bool bOk = LocalKey().GetNextValue(str, lIndex); |
19454fa0 VZ |
470 | lIndex |= LOCAL_MASK; |
471 | ||
472 | return bOk; | |
473 | } | |
474 | ||
33ac7e6f | 475 | size_t wxRegConfig::GetNumberOfEntries(bool WXUNUSED(bRecursive)) const |
82cf4761 | 476 | { |
c86f1403 | 477 | size_t nEntries = 0; |
82cf4761 VZ |
478 | |
479 | // dummy vars | |
480 | wxString str; | |
481 | long l; | |
6a23cbce | 482 | bool bCont = ((wxRegConfig*)this)->GetFirstEntry(str, l); |
82cf4761 VZ |
483 | while ( bCont ) { |
484 | nEntries++; | |
485 | ||
6a23cbce | 486 | bCont = ((wxRegConfig*)this)->GetNextEntry(str, l); |
82cf4761 VZ |
487 | } |
488 | ||
489 | return nEntries; | |
490 | } | |
491 | ||
33ac7e6f | 492 | size_t wxRegConfig::GetNumberOfGroups(bool WXUNUSED(bRecursive)) const |
82cf4761 | 493 | { |
c86f1403 | 494 | size_t nGroups = 0; |
82cf4761 VZ |
495 | |
496 | // dummy vars | |
497 | wxString str; | |
498 | long l; | |
6a23cbce | 499 | bool bCont = ((wxRegConfig*)this)->GetFirstGroup(str, l); |
82cf4761 VZ |
500 | while ( bCont ) { |
501 | nGroups++; | |
502 | ||
6a23cbce | 503 | bCont = ((wxRegConfig*)this)->GetNextGroup(str, l); |
82cf4761 VZ |
504 | } |
505 | ||
506 | return nGroups; | |
507 | } | |
508 | ||
6d833566 VZ |
509 | // ---------------------------------------------------------------------------- |
510 | // tests for existence | |
511 | // ---------------------------------------------------------------------------- | |
512 | ||
61ba49f2 | 513 | bool wxRegConfig::HasGroup(const wxString& key) const |
6d833566 | 514 | { |
61ba49f2 VZ |
515 | wxConfigPathChanger path(this, key); |
516 | ||
517 | wxString strName(path.Name()); | |
518 | ||
807a903e VZ |
519 | return (m_keyLocal.Exists() && LocalKey().HasSubKey(strName)) || |
520 | m_keyGlobal.HasSubKey(strName); | |
6d833566 VZ |
521 | } |
522 | ||
61ba49f2 | 523 | bool wxRegConfig::HasEntry(const wxString& key) const |
6d833566 | 524 | { |
61ba49f2 VZ |
525 | wxConfigPathChanger path(this, key); |
526 | ||
527 | wxString strName(path.Name()); | |
528 | ||
807a903e VZ |
529 | return (m_keyLocal.Exists() && LocalKey().HasValue(strName)) || |
530 | m_keyGlobal.HasValue(strName); | |
61ba49f2 VZ |
531 | } |
532 | ||
533 | wxConfigBase::EntryType wxRegConfig::GetEntryType(const wxString& key) const | |
534 | { | |
535 | wxConfigPathChanger path(this, key); | |
536 | ||
537 | wxString strName(path.Name()); | |
538 | ||
539 | bool isNumeric; | |
807a903e | 540 | if ( m_keyLocal.Exists() && LocalKey().HasValue(strName) ) |
61ba49f2 VZ |
541 | isNumeric = m_keyLocal.IsNumericValue(strName); |
542 | else if ( m_keyGlobal.HasValue(strName) ) | |
543 | isNumeric = m_keyGlobal.IsNumericValue(strName); | |
544 | else | |
545 | return wxConfigBase::Type_Unknown; | |
546 | ||
547 | return isNumeric ? wxConfigBase::Type_Integer : wxConfigBase::Type_String; | |
6d833566 VZ |
548 | } |
549 | ||
19454fa0 VZ |
550 | // ---------------------------------------------------------------------------- |
551 | // reading/writing | |
552 | // ---------------------------------------------------------------------------- | |
553 | ||
18244936 | 554 | bool wxRegConfig::Read(const wxString& key, wxString *pStr) const |
19454fa0 | 555 | { |
18244936 | 556 | wxConfigPathChanger path(this, key); |
19454fa0 | 557 | |
cf447356 | 558 | bool bQueryGlobal = TRUE; |
19454fa0 VZ |
559 | |
560 | // if immutable key exists in global key we must check that it's not | |
561 | // overriden by the local key with the same name | |
562 | if ( IsImmutable(path.Name()) ) { | |
02569ba8 | 563 | if ( TryGetValue(m_keyGlobal, path.Name(), *pStr) ) { |
807a903e | 564 | if ( m_keyLocal.Exists() && LocalKey().HasValue(path.Name()) ) { |
223d09f6 | 565 | wxLogWarning(wxT("User value for immutable key '%s' ignored."), |
19454fa0 VZ |
566 | path.Name().c_str()); |
567 | } | |
18244936 | 568 | *pStr = wxConfigBase::ExpandEnvVars(*pStr); |
cf447356 | 569 | return TRUE; |
19454fa0 VZ |
570 | } |
571 | else { | |
572 | // don't waste time - it's not there anyhow | |
cf447356 | 573 | bQueryGlobal = FALSE; |
19454fa0 VZ |
574 | } |
575 | } | |
576 | ||
577 | // first try local key | |
807a903e | 578 | if ( (m_keyLocal.Exists() && TryGetValue(LocalKey(), path.Name(), *pStr)) || |
02569ba8 | 579 | (bQueryGlobal && TryGetValue(m_keyGlobal, path.Name(), *pStr)) ) { |
41286812 | 580 | // nothing to do |
18244936 | 581 | |
18244936 JS |
582 | *pStr = wxConfigBase::ExpandEnvVars(*pStr); |
583 | return TRUE; | |
584 | } | |
585 | ||
586 | return FALSE; | |
587 | } | |
588 | ||
589 | bool wxRegConfig::Read(const wxString& key, wxString *pStr, | |
590 | const wxString& szDefault) const | |
591 | { | |
592 | wxConfigPathChanger path(this, key); | |
593 | ||
594 | bool bQueryGlobal = TRUE; | |
595 | ||
596 | // if immutable key exists in global key we must check that it's not | |
597 | // overriden by the local key with the same name | |
598 | if ( IsImmutable(path.Name()) ) { | |
599 | if ( TryGetValue(m_keyGlobal, path.Name(), *pStr) ) { | |
807a903e | 600 | if ( m_keyLocal.Exists() && LocalKey().HasValue(path.Name()) ) { |
223d09f6 | 601 | wxLogWarning(wxT("User value for immutable key '%s' ignored."), |
18244936 JS |
602 | path.Name().c_str()); |
603 | } | |
604 | ||
605 | return TRUE; | |
606 | } | |
607 | else { | |
608 | // don't waste time - it's not there anyhow | |
609 | bQueryGlobal = FALSE; | |
610 | } | |
611 | } | |
612 | ||
613 | // first try local key | |
807a903e | 614 | if ( (m_keyLocal.Exists() && TryGetValue(LocalKey(), path.Name(), *pStr)) || |
18244936 JS |
615 | (bQueryGlobal && TryGetValue(m_keyGlobal, path.Name(), *pStr)) ) { |
616 | *pStr = wxConfigBase::ExpandEnvVars(*pStr); | |
617 | return TRUE; | |
41286812 VZ |
618 | } |
619 | else { | |
620 | if ( IsRecordingDefaults() ) { | |
18244936 | 621 | ((wxRegConfig*)this)->Write(key, szDefault); |
41286812 VZ |
622 | } |
623 | ||
624 | // default value | |
625 | *pStr = szDefault; | |
19454fa0 VZ |
626 | } |
627 | ||
41286812 | 628 | *pStr = wxConfigBase::ExpandEnvVars(*pStr); |
baeed289 | 629 | |
cf447356 | 630 | return FALSE; |
19454fa0 VZ |
631 | } |
632 | ||
18244936 | 633 | bool wxRegConfig::Read(const wxString& key, long *plResult) const |
19454fa0 | 634 | { |
18244936 | 635 | wxConfigPathChanger path(this, key); |
19454fa0 | 636 | |
cf447356 | 637 | bool bQueryGlobal = TRUE; |
19454fa0 VZ |
638 | |
639 | // if immutable key exists in global key we must check that it's not | |
640 | // overriden by the local key with the same name | |
641 | if ( IsImmutable(path.Name()) ) { | |
02569ba8 | 642 | if ( TryGetValue(m_keyGlobal, path.Name(), plResult) ) { |
807a903e | 643 | if ( m_keyLocal.Exists() && LocalKey().HasValue(path.Name()) ) { |
223d09f6 | 644 | wxLogWarning(wxT("User value for immutable key '%s' ignored."), |
19454fa0 VZ |
645 | path.Name().c_str()); |
646 | } | |
647 | ||
cf447356 | 648 | return TRUE; |
19454fa0 VZ |
649 | } |
650 | else { | |
651 | // don't waste time - it's not there anyhow | |
cf447356 | 652 | bQueryGlobal = FALSE; |
19454fa0 VZ |
653 | } |
654 | } | |
655 | ||
656 | // first try local key | |
807a903e | 657 | if ( (m_keyLocal.Exists() && TryGetValue(LocalKey(), path.Name(), plResult)) || |
02569ba8 | 658 | (bQueryGlobal && TryGetValue(m_keyGlobal, path.Name(), plResult)) ) { |
cf447356 | 659 | return TRUE; |
19454fa0 | 660 | } |
cf447356 | 661 | return FALSE; |
19454fa0 VZ |
662 | } |
663 | ||
18244936 | 664 | bool wxRegConfig::Write(const wxString& key, const wxString& szValue) |
19454fa0 | 665 | { |
18244936 | 666 | wxConfigPathChanger path(this, key); |
19454fa0 VZ |
667 | |
668 | if ( IsImmutable(path.Name()) ) { | |
223d09f6 | 669 | wxLogError(wxT("Can't change immutable entry '%s'."), path.Name().c_str()); |
cf447356 | 670 | return FALSE; |
19454fa0 VZ |
671 | } |
672 | ||
807a903e | 673 | return LocalKey().SetValue(path.Name(), szValue); |
19454fa0 VZ |
674 | } |
675 | ||
18244936 | 676 | bool wxRegConfig::Write(const wxString& key, long lValue) |
19454fa0 | 677 | { |
18244936 | 678 | wxConfigPathChanger path(this, key); |
19454fa0 VZ |
679 | |
680 | if ( IsImmutable(path.Name()) ) { | |
223d09f6 | 681 | wxLogError(wxT("Can't change immutable entry '%s'."), path.Name().c_str()); |
cf447356 | 682 | return FALSE; |
19454fa0 VZ |
683 | } |
684 | ||
807a903e | 685 | return LocalKey().SetValue(path.Name(), lValue); |
19454fa0 VZ |
686 | } |
687 | ||
688 | // ---------------------------------------------------------------------------- | |
89077ebc VZ |
689 | // renaming |
690 | // ---------------------------------------------------------------------------- | |
691 | ||
692 | bool wxRegConfig::RenameEntry(const wxString& oldName, const wxString& newName) | |
693 | { | |
694 | // check that the old entry exists... | |
695 | if ( !HasEntry(oldName) ) | |
696 | return FALSE; | |
697 | ||
698 | // and that the new one doesn't | |
699 | if ( HasEntry(newName) ) | |
700 | return FALSE; | |
701 | ||
442d08ce | 702 | return m_keyLocal.RenameValue(oldName, newName); |
89077ebc VZ |
703 | } |
704 | ||
705 | bool wxRegConfig::RenameGroup(const wxString& oldName, const wxString& newName) | |
706 | { | |
707 | // check that the old group exists... | |
708 | if ( !HasGroup(oldName) ) | |
709 | return FALSE; | |
710 | ||
711 | // and that the new one doesn't | |
712 | if ( HasGroup(newName) ) | |
713 | return FALSE; | |
714 | ||
442d08ce | 715 | return wxRegKey(m_keyLocal, oldName).Rename(newName); |
89077ebc VZ |
716 | } |
717 | ||
718 | // ---------------------------------------------------------------------------- | |
19454fa0 VZ |
719 | // deleting |
720 | // ---------------------------------------------------------------------------- | |
33ac7e6f | 721 | bool wxRegConfig::DeleteEntry(const wxString& value, bool WXUNUSED(bGroupIfEmptyAlso)) |
19454fa0 | 722 | { |
18244936 | 723 | wxConfigPathChanger path(this, value); |
19454fa0 | 724 | |
807a903e VZ |
725 | if ( m_keyLocal.Exists() ) { |
726 | if ( !m_keyLocal.DeleteValue(path.Name()) ) | |
727 | return FALSE; | |
19454fa0 | 728 | |
807a903e VZ |
729 | if ( m_keyLocal.IsEmpty() ) { |
730 | wxString strKey = GetPath().AfterLast(wxCONFIG_PATH_SEPARATOR); | |
731 | SetPath(".."); // changes m_keyLocal | |
732 | return LocalKey().DeleteKey(strKey); | |
733 | } | |
19454fa0 VZ |
734 | } |
735 | ||
cf447356 | 736 | return TRUE; |
19454fa0 VZ |
737 | } |
738 | ||
18244936 | 739 | bool wxRegConfig::DeleteGroup(const wxString& key) |
19454fa0 | 740 | { |
18244936 | 741 | wxConfigPathChanger path(this, key); |
19454fa0 | 742 | |
807a903e | 743 | return m_keyLocal.Exists() ? LocalKey().DeleteKey(path.Name()) : TRUE; |
19454fa0 VZ |
744 | } |
745 | ||
746 | bool wxRegConfig::DeleteAll() | |
747 | { | |
19454fa0 VZ |
748 | m_keyLocal.Close(); |
749 | m_keyGlobal.Close(); | |
90186e52 | 750 | |
19454fa0 | 751 | bool bOk = m_keyLocalRoot.DeleteSelf(); |
90186e52 VZ |
752 | |
753 | // make sure that we opened m_keyGlobalRoot and so it has a reasonable name: | |
754 | // otherwise we will delete HKEY_CLASSES_ROOT recursively | |
755 | if ( bOk && m_keyGlobalRoot.IsOpened() ) | |
19454fa0 VZ |
756 | bOk = m_keyGlobalRoot.DeleteSelf(); |
757 | ||
758 | return bOk; | |
759 | } | |
3d05544e JS |
760 | |
761 | #endif | |
762 | // __WIN16__ | |
763 | ||
f6bcfd97 BP |
764 | #endif |
765 | // wxUSE_CONFIG |