]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/regconf.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/regconf.cpp
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
19 #if wxUSE_CONFIG && wxUSE_REGKEY
21 #include "wx/config.h"
24 #include "wx/string.h"
31 #include "wx/msw/registry.h"
32 #include "wx/msw/regconf.h"
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 // we put our data in HKLM\SOFTWARE_KEY\appname
39 #define SOFTWARE_KEY wxString(wxT("Software\\"))
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 // get the value if the key is opened and it exists
46 bool TryGetValue(const wxRegKey
& key
, const wxString
& str
, wxString
& strVal
)
48 return key
.IsOpened() && key
.HasValue(str
) && key
.QueryValue(str
, strVal
);
51 bool TryGetValue(const wxRegKey
& key
, const wxString
& str
, long *plVal
)
53 return key
.IsOpened() && key
.HasValue(str
) && key
.QueryValue(str
, plVal
);
56 bool TryGetValue(const wxRegKey
& key
, const wxString
& str
, wxMemoryBuffer
&plVal
)
58 return key
.IsOpened() && key
.HasValue(str
) && key
.QueryValue(str
, plVal
);
61 // ============================================================================
63 // ============================================================================
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
68 IMPLEMENT_ABSTRACT_CLASS(wxRegConfig
, wxConfigBase
)
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
,
75 : wxConfigBase(appName
, vendorName
, strLocal
, strGlobal
, style
)
79 bool bDoUseGlobal
= (style
& wxCONFIG_USE_GLOBAL_FILE
) != 0;
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
84 if ( strLocal
.empty() || (strGlobal
.empty() && bDoUseGlobal
) )
86 if ( vendorName
.empty() )
89 strRoot
= wxTheApp
->GetVendorName();
96 // no '\\' needed if no vendor name
97 if ( !strRoot
.empty() )
102 if ( appName
.empty() )
104 wxCHECK_RET( wxTheApp
, wxT("No application name in wxRegConfig ctor!") );
105 strRoot
<< wxTheApp
->GetAppName();
112 //else: we don't need to do all the complicated stuff above
114 wxString str
= strLocal
.empty() ? strRoot
: strLocal
;
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;
122 m_keyLocalRoot
.ReserveMemoryForName(MEMORY_PREALLOC
);
123 m_keyLocal
.ReserveMemoryForName(MEMORY_PREALLOC
);
125 m_keyLocalRoot
.SetName(wxRegKey::HKCU
, SOFTWARE_KEY
+ str
);
126 m_keyLocal
.SetName(m_keyLocalRoot
, wxEmptyString
);
130 str
= strGlobal
.empty() ? strRoot
: strGlobal
;
132 m_keyGlobalRoot
.ReserveMemoryForName(MEMORY_PREALLOC
);
133 m_keyGlobal
.ReserveMemoryForName(MEMORY_PREALLOC
);
135 m_keyGlobalRoot
.SetName(wxRegKey::HKLM
, SOFTWARE_KEY
+ str
);
136 m_keyGlobal
.SetName(m_keyGlobalRoot
, wxEmptyString
);
139 // Create() will Open() if key already exists
140 m_keyLocalRoot
.Create();
142 // as it's the same key, Open() shouldn't fail (i.e. no need for Create())
145 // OTOH, this key may perfectly not exist, so suppress error messages the call
146 // to Open() might generate
150 m_keyGlobalRoot
.Open(wxRegKey::Read
);
151 m_keyGlobal
.Open(wxRegKey::Read
);
155 // ----------------------------------------------------------------------------
157 // ----------------------------------------------------------------------------
159 // this function is called a *lot* of times (as I learned after seeing from
160 // profiler output that it is called ~12000 times from Mahogany start up code!)
161 // so it is important to optimize it - in particular, avoid using generic
162 // string functions here and do everything manually because it is faster
164 // I still kept the old version to be able to check that the optimized code has
165 // the same output as the non optimized version.
166 void wxRegConfig::SetPath(const wxString
& strPath
)
168 // remember the old path
169 wxString strOldPath
= m_strPath
;
171 #ifdef WX_DEBUG_SET_PATH // non optimized version kept here for testing
172 wxString m_strPathAlt
;
175 wxArrayString aParts
;
177 // because GetPath() returns "" when we're at root, we must understand
178 // empty string as "/"
179 if ( strPath
.empty() || (strPath
[0] == wxCONFIG_PATH_SEPARATOR
) ) {
181 wxSplitPath(aParts
, strPath
);
184 // relative path, combine with current one
185 wxString strFullPath
= GetPath();
186 strFullPath
<< wxCONFIG_PATH_SEPARATOR
<< strPath
;
187 wxSplitPath(aParts
, strFullPath
);
190 // recombine path parts in one variable
192 m_strPathAlt
.Empty();
193 for ( size_t n
= 0; n
< aParts
.Count(); n
++ ) {
194 strRegPath
<< '\\' << aParts
[n
];
195 m_strPathAlt
<< wxCONFIG_PATH_SEPARATOR
<< aParts
[n
];
200 // check for the most common case first
201 if ( strPath
.empty() )
203 m_strPath
= wxCONFIG_PATH_SEPARATOR
;
207 // construct the full path
208 wxString strFullPath
;
209 if ( strPath
[0u] == wxCONFIG_PATH_SEPARATOR
)
212 strFullPath
= strPath
;
214 else // relative path
216 strFullPath
.reserve(2*m_strPath
.length());
218 strFullPath
<< m_strPath
;
219 if ( strFullPath
.Len() == 0 ||
220 strFullPath
.Last() != wxCONFIG_PATH_SEPARATOR
)
221 strFullPath
<< wxCONFIG_PATH_SEPARATOR
;
222 strFullPath
<< strPath
;
225 // simplify it: we need to handle ".." here
227 // count the total number of slashes we have to know if we can go upper
228 size_t totalSlashes
= 0;
230 // position of the last slash to be able to backtrack to it quickly if
231 // needed, but we set it to -1 if we don't have a valid position
233 // we only remember the last position which means that we handle ".."
234 // quite efficiently but not "../.." - however the latter should be
235 // much more rare, so it is probably ok
236 int posLastSlash
= -1;
238 const wxChar
*src
= strFullPath
.c_str();
239 size_t len
= strFullPath
.length();
240 const wxChar
*end
= src
+ len
;
242 wxStringBufferLength
buf(m_strPath
, len
);
246 for ( ; src
< end
; src
++, dst
++ )
248 if ( *src
== wxCONFIG_PATH_SEPARATOR
)
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
) )
259 wxLogWarning(_("'%s' has extra '..', ignored."),
260 strFullPath
.c_str());
262 else // return to the previous path component
264 // do we already have its position?
265 if ( posLastSlash
== -1 )
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
271 // this is more efficient than strrchr()
273 while ( *dst
!= wxCONFIG_PATH_SEPARATOR
)
278 else // the position of last slash was stored
281 dst
= start
+ posLastSlash
;
283 // invalidate posLastSlash
287 // we must have found a slash one way or another!
288 wxASSERT_MSG( *dst
== wxCONFIG_PATH_SEPARATOR
,
289 _T("error in wxRegConfig::SetPath") );
291 // stay at the same position
303 if ( (dst
== start
) || (dst
[-1] != wxCONFIG_PATH_SEPARATOR
) )
305 *dst
= wxCONFIG_PATH_SEPARATOR
;
307 posLastSlash
= dst
- start
;
311 else // previous char was a slash too
313 // squeeze several subsequent slashes into one: i.e.
314 // just ignore this one
319 else // normal character
326 // NUL terminate the string
327 if ( dst
[-1] == wxCONFIG_PATH_SEPARATOR
&& (dst
!= start
+ 1) )
329 // if it has a trailing slash we remove it unless it is the only
335 buf
.SetLength(dst
- start
);
338 #ifdef WX_DEBUG_SET_PATH
339 wxASSERT( m_strPath
== m_strPathAlt
);
342 if ( m_strPath
== strOldPath
)
345 // registry APIs want backslashes instead of slashes
347 if ( !m_strPath
.empty() )
349 size_t len
= m_strPath
.length();
351 const wxChar
*src
= m_strPath
.c_str();
352 wxStringBufferLength
buf(strRegPath
, len
);
355 const wxChar
*end
= src
+ len
;
356 for ( ; src
< end
; src
++, dst
++ )
358 if ( *src
== wxCONFIG_PATH_SEPARATOR
)
367 // this is not needed any longer as we don't create keys unnecessarily any
368 // more (now it is done on demand, i.e. only when they're going to contain
371 // as we create the registry key when SetPath(key) is done, we can be left
372 // with plenty of empty keys if this was only done to try to read some
373 // value which, in fact, doesn't exist - to prevent this from happening we
374 // automatically delete the old key if it was empty
375 if ( m_keyLocal
.Exists() && LocalKey().IsEmpty() )
377 m_keyLocal
.DeleteSelf();
381 // change current key(s)
382 m_keyLocal
.SetName(m_keyLocalRoot
, strRegPath
);
384 if ( GetStyle() & wxCONFIG_USE_GLOBAL_FILE
)
386 m_keyGlobal
.SetName(m_keyGlobalRoot
, strRegPath
);
389 m_keyGlobal
.Open(wxRegKey::Read
);
393 // ----------------------------------------------------------------------------
394 // enumeration (works only with current group)
395 // ----------------------------------------------------------------------------
398 We want to enumerate all local keys/values after the global ones, but, of
399 course, we don't want to repeat a key which appears locally as well as
402 We use the 15th bit of lIndex for distinction between global and local.
405 #define LOCAL_MASK 0x8000
406 #define IS_LOCAL_INDEX(l) (((l) & LOCAL_MASK) != 0)
408 bool wxRegConfig::GetFirstGroup(wxString
& str
, long& lIndex
) const
411 return GetNextGroup(str
, lIndex
);
414 bool wxRegConfig::GetNextGroup(wxString
& str
, long& lIndex
) const
416 // are we already enumerating local entries?
417 if ( m_keyGlobal
.IsOpened() && !IS_LOCAL_INDEX(lIndex
) ) {
418 // try to find a global entry which doesn't appear locally
419 while ( m_keyGlobal
.GetNextKey(str
, lIndex
) ) {
420 if ( !m_keyLocal
.Exists() || !LocalKey().HasSubKey(str
) ) {
421 // ok, found one - return it
426 // no more global entries
427 lIndex
|= LOCAL_MASK
;
430 // if we don't have the key at all, don't try to enumerate anything under it
431 if ( !m_keyLocal
.Exists() )
434 // much easier with local entries: get the next one we find
435 // (don't forget to clear our flag bit and set it again later)
436 lIndex
&= ~LOCAL_MASK
;
437 bool bOk
= LocalKey().GetNextKey(str
, lIndex
);
438 lIndex
|= LOCAL_MASK
;
443 bool wxRegConfig::GetFirstEntry(wxString
& str
, long& lIndex
) const
446 return GetNextEntry(str
, lIndex
);
449 bool wxRegConfig::GetNextEntry(wxString
& str
, long& lIndex
) const
451 // are we already enumerating local entries?
452 if ( m_keyGlobal
.IsOpened() && !IS_LOCAL_INDEX(lIndex
) ) {
453 // try to find a global entry which doesn't appear locally
454 while ( m_keyGlobal
.GetNextValue(str
, lIndex
) ) {
455 if ( !m_keyLocal
.Exists() || !LocalKey().HasValue(str
) ) {
456 // ok, found one - return it
461 // no more global entries
462 lIndex
|= LOCAL_MASK
;
465 // if we don't have the key at all, don't try to enumerate anything under it
466 if ( !m_keyLocal
.Exists() )
469 // much easier with local entries: get the next one we find
470 // (don't forget to clear our flag bit and set it again later)
471 lIndex
&= ~LOCAL_MASK
;
472 bool bOk
= LocalKey().GetNextValue(str
, lIndex
);
473 lIndex
|= LOCAL_MASK
;
478 size_t wxRegConfig::GetNumberOfEntries(bool WXUNUSED(bRecursive
)) const
485 bool bCont
= ((wxRegConfig
*)this)->GetFirstEntry(str
, l
);
489 bCont
= ((wxRegConfig
*)this)->GetNextEntry(str
, l
);
495 size_t wxRegConfig::GetNumberOfGroups(bool WXUNUSED(bRecursive
)) const
502 bool bCont
= ((wxRegConfig
*)this)->GetFirstGroup(str
, l
);
506 bCont
= ((wxRegConfig
*)this)->GetNextGroup(str
, l
);
512 // ----------------------------------------------------------------------------
513 // tests for existence
514 // ----------------------------------------------------------------------------
516 bool wxRegConfig::HasGroup(const wxString
& key
) const
518 wxConfigPathChanger
path(this, key
);
520 wxString
strName(path
.Name());
522 return (m_keyLocal
.Exists() && LocalKey().HasSubKey(strName
)) ||
523 m_keyGlobal
.HasSubKey(strName
);
526 bool wxRegConfig::HasEntry(const wxString
& key
) const
528 wxConfigPathChanger
path(this, key
);
530 wxString
strName(path
.Name());
532 return (m_keyLocal
.Exists() && LocalKey().HasValue(strName
)) ||
533 m_keyGlobal
.HasValue(strName
);
536 wxConfigBase::EntryType
wxRegConfig::GetEntryType(const wxString
& key
) const
538 wxConfigPathChanger
path(this, key
);
540 wxString
strName(path
.Name());
543 if ( m_keyLocal
.Exists() && LocalKey().HasValue(strName
) )
544 isNumeric
= m_keyLocal
.IsNumericValue(strName
);
545 else if ( m_keyGlobal
.HasValue(strName
) )
546 isNumeric
= m_keyGlobal
.IsNumericValue(strName
);
548 return wxConfigBase::Type_Unknown
;
550 return isNumeric
? wxConfigBase::Type_Integer
: wxConfigBase::Type_String
;
553 // ----------------------------------------------------------------------------
555 // ----------------------------------------------------------------------------
557 bool wxRegConfig::DoReadString(const wxString
& key
, wxString
*pStr
) const
559 wxCHECK_MSG( pStr
, false, _T("wxRegConfig::Read(): NULL param") );
561 wxConfigPathChanger
path(this, key
);
563 bool bQueryGlobal
= true;
565 // if immutable key exists in global key we must check that it's not
566 // overriden by the local key with the same name
567 if ( IsImmutable(path
.Name()) ) {
568 if ( TryGetValue(m_keyGlobal
, path
.Name(), *pStr
) ) {
569 if ( m_keyLocal
.Exists() && LocalKey().HasValue(path
.Name()) ) {
570 wxLogWarning(wxT("User value for immutable key '%s' ignored."),
571 path
.Name().c_str());
577 // don't waste time - it's not there anyhow
578 bQueryGlobal
= false;
582 // first try local key
583 if ( (m_keyLocal
.Exists() && TryGetValue(LocalKey(), path
.Name(), *pStr
)) ||
584 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), *pStr
)) ) {
591 // this exactly reproduces the string version above except for ExpandEnvVars(),
592 // we really should avoid this code duplication somehow...
594 bool wxRegConfig::DoReadLong(const wxString
& key
, long *plResult
) const
596 wxCHECK_MSG( plResult
, false, _T("wxRegConfig::Read(): NULL param") );
598 wxConfigPathChanger
path(this, key
);
600 bool bQueryGlobal
= true;
602 // if immutable key exists in global key we must check that it's not
603 // overriden by the local key with the same name
604 if ( IsImmutable(path
.Name()) ) {
605 if ( TryGetValue(m_keyGlobal
, path
.Name(), plResult
) ) {
606 if ( m_keyLocal
.Exists() && LocalKey().HasValue(path
.Name()) ) {
607 wxLogWarning(wxT("User value for immutable key '%s' ignored."),
608 path
.Name().c_str());
614 // don't waste time - it's not there anyhow
615 bQueryGlobal
= false;
619 // first try local key
620 if ( (m_keyLocal
.Exists() && TryGetValue(LocalKey(), path
.Name(), plResult
)) ||
621 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), plResult
)) ) {
628 bool wxRegConfig::DoReadBinary(const wxString
& key
, wxMemoryBuffer
*buf
) const
630 wxCHECK_MSG( buf
, false, _T("wxRegConfig::Read(): NULL param") );
632 wxConfigPathChanger
path(this, key
);
634 bool bQueryGlobal
= true;
636 // if immutable key exists in global key we must check that it's not
637 // overriden by the local key with the same name
638 if ( IsImmutable(path
.Name()) ) {
639 if ( TryGetValue(m_keyGlobal
, path
.Name(), *buf
) ) {
640 if ( m_keyLocal
.Exists() && LocalKey().HasValue(path
.Name()) ) {
641 wxLogWarning(wxT("User value for immutable key '%s' ignored."),
642 path
.Name().c_str());
648 // don't waste time - it's not there anyhow
649 bQueryGlobal
= false;
653 // first try local key
654 if ( (m_keyLocal
.Exists() && TryGetValue(LocalKey(), path
.Name(), *buf
)) ||
655 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), *buf
)) ) {
662 bool wxRegConfig::DoWriteString(const wxString
& key
, const wxString
& szValue
)
664 wxConfigPathChanger
path(this, key
);
666 if ( IsImmutable(path
.Name()) ) {
667 wxLogError(wxT("Can't change immutable entry '%s'."), path
.Name().c_str());
671 return LocalKey().SetValue(path
.Name(), szValue
);
674 bool wxRegConfig::DoWriteLong(const wxString
& key
, long lValue
)
676 wxConfigPathChanger
path(this, key
);
678 if ( IsImmutable(path
.Name()) ) {
679 wxLogError(wxT("Can't change immutable entry '%s'."), path
.Name().c_str());
683 return LocalKey().SetValue(path
.Name(), lValue
);
686 bool wxRegConfig::DoWriteBinary(const wxString
& key
, const wxMemoryBuffer
& buf
)
688 wxConfigPathChanger
path(this, key
);
690 if ( IsImmutable(path
.Name()) ) {
691 wxLogError(wxT("Can't change immutable entry '%s'."), path
.Name().c_str());
695 return LocalKey().SetValue(path
.Name(), buf
);
698 // ----------------------------------------------------------------------------
700 // ----------------------------------------------------------------------------
702 bool wxRegConfig::RenameEntry(const wxString
& oldName
, const wxString
& newName
)
704 // check that the old entry exists...
705 if ( !HasEntry(oldName
) )
708 // and that the new one doesn't
709 if ( HasEntry(newName
) )
712 return m_keyLocal
.RenameValue(oldName
, newName
);
715 bool wxRegConfig::RenameGroup(const wxString
& oldName
, const wxString
& newName
)
717 // check that the old group exists...
718 if ( !HasGroup(oldName
) )
721 // and that the new one doesn't
722 if ( HasGroup(newName
) )
725 return wxRegKey(m_keyLocal
, oldName
).Rename(newName
);
728 // ----------------------------------------------------------------------------
730 // ----------------------------------------------------------------------------
732 bool wxRegConfig::DeleteEntry(const wxString
& value
, bool bGroupIfEmptyAlso
)
734 wxConfigPathChanger
path(this, value
);
736 if ( m_keyLocal
.Exists() ) {
737 if ( !m_keyLocal
.DeleteValue(path
.Name()) )
740 if ( bGroupIfEmptyAlso
&& m_keyLocal
.IsEmpty() ) {
741 wxString strKey
= GetPath().AfterLast(wxCONFIG_PATH_SEPARATOR
);
742 SetPath(_T("..")); // changes m_keyLocal
743 return LocalKey().DeleteKey(strKey
);
750 bool wxRegConfig::DeleteGroup(const wxString
& key
)
752 wxConfigPathChanger
path(this, RemoveTrailingSeparator(key
));
754 if ( !m_keyLocal
.Exists() )
760 if ( !LocalKey().DeleteKey(path
.Name()) )
763 path
.UpdateIfDeleted();
768 bool wxRegConfig::DeleteAll()
773 bool bOk
= m_keyLocalRoot
.DeleteSelf();
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() )
778 bOk
= m_keyGlobalRoot
.DeleteSelf();
783 #endif // wxUSE_CONFIG && wxUSE_REGKEY