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