]>
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" 
  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 // ============================================================================ 
  58 // ============================================================================ 
  60 // ---------------------------------------------------------------------------- 
  62 // ---------------------------------------------------------------------------- 
  64 // create the config object which stores its data under HKCU\vendor\app and, if 
  65 // style & wxCONFIG_USE_GLOBAL_FILE, under HKLM\vendor\app 
  66 wxRegConfig::wxRegConfig(const wxString
& appName
, const wxString
& vendorName
, 
  67                          const wxString
& strLocal
, const wxString
& strGlobal
, 
  69            : wxConfigBase(appName
, vendorName
, strLocal
, strGlobal
, style
) 
  73   bool bDoUseGlobal 
= (style 
& wxCONFIG_USE_GLOBAL_FILE
) != 0; 
  75   // the convention is to put the programs keys under <vendor>\<appname> 
  76   // (but it can be overriden by specifying the pathes explicitly in strLocal 
  78   if ( strLocal
.empty() || (strGlobal
.empty() && bDoUseGlobal
) ) 
  80     if ( vendorName
.empty() ) 
  83         strRoot 
= wxTheApp
->GetVendorName(); 
  90     // no '\\' needed if no vendor name 
  91     if ( !strRoot
.empty() ) 
  96     if ( appName
.empty() ) 
  98       wxCHECK_RET( wxTheApp
, wxT("No application name in wxRegConfig ctor!") ); 
  99       strRoot 
<< wxTheApp
->GetAppName(); 
 106   //else: we don't need to do all the complicated stuff above 
 108   wxString str 
= strLocal
.empty() ? strRoot 
: strLocal
; 
 110   // as we're going to change the name of these keys fairly often and as 
 111   // there are only few of wxRegConfig objects (usually 1), we can allow 
 112   // ourselves to be generous and spend some memory to significantly improve 
 113   // performance of SetPath() 
 114   static const size_t MEMORY_PREALLOC 
= 512; 
 116   m_keyLocalRoot
.ReserveMemoryForName(MEMORY_PREALLOC
); 
 117   m_keyLocal
.ReserveMemoryForName(MEMORY_PREALLOC
); 
 119   m_keyLocalRoot
.SetName(wxRegKey::HKCU
, SOFTWARE_KEY 
+ str
); 
 120   m_keyLocal
.SetName(m_keyLocalRoot
, wxEmptyString
); 
 124     str 
= strGlobal
.empty() ? strRoot 
: strGlobal
; 
 126     m_keyGlobalRoot
.ReserveMemoryForName(MEMORY_PREALLOC
); 
 127     m_keyGlobal
.ReserveMemoryForName(MEMORY_PREALLOC
); 
 129     m_keyGlobalRoot
.SetName(wxRegKey::HKLM
, SOFTWARE_KEY 
+ str
); 
 130     m_keyGlobal
.SetName(m_keyGlobalRoot
, wxEmptyString
); 
 133   // Create() will Open() if key already exists 
 134   m_keyLocalRoot
.Create(); 
 136   // as it's the same key, Open() shouldn't fail (i.e. no need for Create()) 
 139   // OTOH, this key may perfectly not exist, so suppress error messages the call 
 140   // to Open() might generate 
 144     m_keyGlobalRoot
.Open(wxRegKey::Read
); 
 145     m_keyGlobal
.Open(wxRegKey::Read
); 
 149 // ---------------------------------------------------------------------------- 
 151 // ---------------------------------------------------------------------------- 
 153 // this function is called a *lot* of times (as I learned after seeing from 
 154 // profiler output that it is called ~12000 times from Mahogany start up code!) 
 155 // so it is important to optimize it - in particular, avoid using generic 
 156 // string functions here and do everything manually because it is faster 
 158 // I still kept the old version to be able to check that the optimized code has 
 159 // the same output as the non optimized version. 
 160 void wxRegConfig::SetPath(const wxString
& strPath
) 
 162     // remember the old path 
 163     wxString strOldPath 
= m_strPath
; 
 165 #ifdef WX_DEBUG_SET_PATH // non optimized version kept here for testing 
 166     wxString m_strPathAlt
; 
 169         wxArrayString aParts
; 
 171         // because GetPath() returns "" when we're at root, we must understand 
 172         // empty string as "/" 
 173         if ( strPath
.empty() || (strPath
[0] == wxCONFIG_PATH_SEPARATOR
) ) { 
 175             wxSplitPath(aParts
, strPath
); 
 178             // relative path, combine with current one 
 179             wxString strFullPath 
= GetPath(); 
 180             strFullPath 
<< wxCONFIG_PATH_SEPARATOR 
<< strPath
; 
 181             wxSplitPath(aParts
, strFullPath
); 
 184         // recombine path parts in one variable 
 186         m_strPathAlt
.Empty(); 
 187         for ( size_t n 
= 0; n 
< aParts
.Count(); n
++ ) { 
 188             strRegPath 
<< '\\' << aParts
[n
]; 
 189             m_strPathAlt 
<< wxCONFIG_PATH_SEPARATOR 
<< aParts
[n
]; 
 194     // check for the most common case first 
 195     if ( strPath
.empty() ) 
 197         m_strPath 
= wxCONFIG_PATH_SEPARATOR
; 
 201         // construct the full path 
 202         wxString strFullPath
; 
 203         if ( strPath
[0u] == wxCONFIG_PATH_SEPARATOR 
) 
 206             strFullPath 
= strPath
; 
 208         else // relative path 
 210             strFullPath
.reserve(2*m_strPath
.length()); 
 212             strFullPath 
<< m_strPath
; 
 213             if ( strFullPath
.Len() == 0 || 
 214                  strFullPath
.Last() != wxCONFIG_PATH_SEPARATOR 
) 
 215                 strFullPath 
<< wxCONFIG_PATH_SEPARATOR
; 
 216             strFullPath 
<< strPath
; 
 219         // simplify it: we need to handle ".." here 
 221         // count the total number of slashes we have to know if we can go upper 
 222         size_t totalSlashes 
= 0; 
 224         // position of the last slash to be able to backtrack to it quickly if 
 225         // needed, but we set it to -1 if we don't have a valid position 
 227         // we only remember the last position which means that we handle ".." 
 228         // quite efficiently but not "../.." - however the latter should be 
 229         // much more rare, so it is probably ok 
 230         int posLastSlash 
= -1; 
 232         const wxChar 
*src 
= strFullPath
.c_str(); 
 233         size_t len 
= strFullPath
.length(); 
 234         const wxChar 
*end 
= src 
+ len
; 
 236         wxStringBufferLength 
buf(m_strPath
, len
); 
 240         for ( ; src 
< end
; src
++, dst
++ ) 
 242             if ( *src 
== wxCONFIG_PATH_SEPARATOR 
) 
 246                 // note that we don't have to check for src < end here as 
 247                 // *end == 0 so can't be '.' 
 248                 if ( src
[1] == _T('.') && src
[2] == _T('.') && 
 249                      (src 
+ 3 == end 
|| src
[3] == wxCONFIG_PATH_SEPARATOR
) ) 
 253                         wxLogWarning(_("'%s' has extra '..', ignored."), 
 254                                      strFullPath
.c_str()); 
 256                     else // return to the previous path component 
 258                         // do we already have its position? 
 259                         if ( posLastSlash 
== -1 ) 
 261                             // no, find it: note that we are sure to have one 
 262                             // because totalSlashes > 0 so we don't have to 
 263                             // check the boundary condition below 
 265                             // this is more efficient than strrchr() 
 267                             while ( *dst 
!= wxCONFIG_PATH_SEPARATOR 
) 
 272                         else // the position of last slash was stored 
 275                             dst 
= start 
+ posLastSlash
; 
 277                             // invalidate posLastSlash 
 281                         // we must have found a slash one way or another! 
 282                         wxASSERT_MSG( *dst 
== wxCONFIG_PATH_SEPARATOR
, 
 283                                       _T("error in wxRegConfig::SetPath") ); 
 285                         // stay at the same position 
 297                     if ( (dst 
== start
) || (dst
[-1] != wxCONFIG_PATH_SEPARATOR
) ) 
 299                         *dst 
= wxCONFIG_PATH_SEPARATOR
; 
 301                         posLastSlash 
= dst 
- start
; 
 305                     else // previous char was a slash too 
 307                         // squeeze several subsequent slashes into one: i.e. 
 308                         // just ignore this one 
 313             else // normal character 
 320         // NUL terminate the string 
 321         if ( dst
[-1] == wxCONFIG_PATH_SEPARATOR 
&& (dst 
!= start 
+ 1) ) 
 323             // if it has a trailing slash we remove it unless it is the only 
 329         buf
.SetLength(dst 
- start
); 
 332 #ifdef WX_DEBUG_SET_PATH 
 333     wxASSERT( m_strPath 
== m_strPathAlt 
); 
 336     if ( m_strPath 
== strOldPath 
) 
 339     // registry APIs want backslashes instead of slashes 
 341     if ( !m_strPath
.empty() ) 
 343         size_t len 
= m_strPath
.length(); 
 345         const wxChar 
*src 
= m_strPath
.c_str(); 
 346         wxStringBufferLength 
buf(strRegPath
, len
); 
 349         const wxChar 
*end 
= src 
+ len
; 
 350         for ( ; src 
< end
; src
++, dst
++ ) 
 352             if ( *src 
== wxCONFIG_PATH_SEPARATOR 
) 
 361     // this is not needed any longer as we don't create keys unnecessarily any 
 362     // more (now it is done on demand, i.e. only when they're going to contain 
 365     // as we create the registry key when SetPath(key) is done, we can be left 
 366     // with plenty of empty keys if this was only done to try to read some 
 367     // value which, in fact, doesn't exist - to prevent this from happening we 
 368     // automatically delete the old key if it was empty 
 369     if ( m_keyLocal
.Exists() && LocalKey().IsEmpty() ) 
 371         m_keyLocal
.DeleteSelf(); 
 375     // change current key(s) 
 376     m_keyLocal
.SetName(m_keyLocalRoot
, strRegPath
); 
 378     if ( GetStyle() & wxCONFIG_USE_GLOBAL_FILE 
) 
 380       m_keyGlobal
.SetName(m_keyGlobalRoot
, strRegPath
); 
 383       m_keyGlobal
.Open(wxRegKey::Read
); 
 387 // ---------------------------------------------------------------------------- 
 388 // enumeration (works only with current group) 
 389 // ---------------------------------------------------------------------------- 
 392   We want to enumerate all local keys/values after the global ones, but, of 
 393   course, we don't want to repeat a key which appears locally as well as 
 396   We use the 15th bit of lIndex for distinction between global and local. 
 399 #define LOCAL_MASK        0x8000 
 400 #define IS_LOCAL_INDEX(l) (((l) & LOCAL_MASK) != 0) 
 402 bool wxRegConfig::GetFirstGroup(wxString
& str
, long& lIndex
) const 
 405   return GetNextGroup(str
, lIndex
); 
 408 bool wxRegConfig::GetNextGroup(wxString
& str
, long& lIndex
) const 
 410   // are we already enumerating local entries? 
 411   if ( m_keyGlobal
.IsOpened() && !IS_LOCAL_INDEX(lIndex
) ) { 
 412     // try to find a global entry which doesn't appear locally 
 413     while ( m_keyGlobal
.GetNextKey(str
, lIndex
) ) { 
 414       if ( !m_keyLocal
.Exists() || !LocalKey().HasSubKey(str
) ) { 
 415         // ok, found one - return it 
 420     // no more global entries 
 421     lIndex 
|= LOCAL_MASK
; 
 424   // if we don't have the key at all, don't try to enumerate anything under it 
 425   if ( !m_keyLocal
.Exists() ) 
 428   // much easier with local entries: get the next one we find 
 429   // (don't forget to clear our flag bit and set it again later) 
 430   lIndex 
&= ~LOCAL_MASK
; 
 431   bool bOk 
= LocalKey().GetNextKey(str
, lIndex
); 
 432   lIndex 
|= LOCAL_MASK
; 
 437 bool wxRegConfig::GetFirstEntry(wxString
& str
, long& lIndex
) const 
 440   return GetNextEntry(str
, lIndex
); 
 443 bool wxRegConfig::GetNextEntry(wxString
& str
, long& lIndex
) const 
 445   // are we already enumerating local entries? 
 446   if ( m_keyGlobal
.IsOpened() && !IS_LOCAL_INDEX(lIndex
) ) { 
 447     // try to find a global entry which doesn't appear locally 
 448     while ( m_keyGlobal
.GetNextValue(str
, lIndex
) ) { 
 449       if ( !m_keyLocal
.Exists() || !LocalKey().HasValue(str
) ) { 
 450         // ok, found one - return it 
 455     // no more global entries 
 456     lIndex 
|= LOCAL_MASK
; 
 459   // if we don't have the key at all, don't try to enumerate anything under it 
 460   if ( !m_keyLocal
.Exists() ) 
 463   // much easier with local entries: get the next one we find 
 464   // (don't forget to clear our flag bit and set it again later) 
 465   lIndex 
&= ~LOCAL_MASK
; 
 466   bool bOk 
= LocalKey().GetNextValue(str
, lIndex
); 
 467   lIndex 
|= LOCAL_MASK
; 
 472 size_t wxRegConfig::GetNumberOfEntries(bool WXUNUSED(bRecursive
)) const 
 479   bool bCont 
= ((wxRegConfig
*)this)->GetFirstEntry(str
, l
); 
 483     bCont 
= ((wxRegConfig
*)this)->GetNextEntry(str
, l
); 
 489 size_t wxRegConfig::GetNumberOfGroups(bool WXUNUSED(bRecursive
)) const 
 496   bool bCont 
= ((wxRegConfig
*)this)->GetFirstGroup(str
, l
); 
 500     bCont 
= ((wxRegConfig
*)this)->GetNextGroup(str
, l
); 
 506 // ---------------------------------------------------------------------------- 
 507 // tests for existence 
 508 // ---------------------------------------------------------------------------- 
 510 bool wxRegConfig::HasGroup(const wxString
& key
) const 
 512     wxConfigPathChanger 
path(this, key
); 
 514     wxString 
strName(path
.Name()); 
 516     return (m_keyLocal
.Exists() && LocalKey().HasSubKey(strName
)) || 
 517            m_keyGlobal
.HasSubKey(strName
); 
 520 bool wxRegConfig::HasEntry(const wxString
& key
) const 
 522     wxConfigPathChanger 
path(this, key
); 
 524     wxString 
strName(path
.Name()); 
 526     return (m_keyLocal
.Exists() && LocalKey().HasValue(strName
)) || 
 527            m_keyGlobal
.HasValue(strName
); 
 530 wxConfigBase::EntryType 
wxRegConfig::GetEntryType(const wxString
& key
) const 
 532     wxConfigPathChanger 
path(this, key
); 
 534     wxString 
strName(path
.Name()); 
 537     if ( m_keyLocal
.Exists() && LocalKey().HasValue(strName
) ) 
 538         isNumeric 
= m_keyLocal
.IsNumericValue(strName
); 
 539     else if ( m_keyGlobal
.HasValue(strName
) ) 
 540         isNumeric 
= m_keyGlobal
.IsNumericValue(strName
); 
 542         return wxConfigBase::Type_Unknown
; 
 544     return isNumeric 
? wxConfigBase::Type_Integer 
: wxConfigBase::Type_String
; 
 547 // ---------------------------------------------------------------------------- 
 549 // ---------------------------------------------------------------------------- 
 551 bool wxRegConfig::DoReadString(const wxString
& key
, wxString 
*pStr
) const 
 553     wxCHECK_MSG( pStr
, false, _T("wxRegConfig::Read(): NULL param") ); 
 555   wxConfigPathChanger 
path(this, key
); 
 557   bool bQueryGlobal 
= true; 
 559   // if immutable key exists in global key we must check that it's not 
 560   // overriden by the local key with the same name 
 561   if ( IsImmutable(path
.Name()) ) { 
 562     if ( TryGetValue(m_keyGlobal
, path
.Name(), *pStr
) ) { 
 563       if ( m_keyLocal
.Exists() && LocalKey().HasValue(path
.Name()) ) { 
 564         wxLogWarning(wxT("User value for immutable key '%s' ignored."), 
 565                    path
.Name().c_str()); 
 571       // don't waste time - it's not there anyhow 
 572       bQueryGlobal 
= false; 
 576   // first try local key 
 577   if ( (m_keyLocal
.Exists() && TryGetValue(LocalKey(), path
.Name(), *pStr
)) || 
 578        (bQueryGlobal 
&& TryGetValue(m_keyGlobal
, path
.Name(), *pStr
)) ) { 
 585 // this exactly reproduces the string version above except for ExpandEnvVars(), 
 586 // we really should avoid this code duplication somehow... 
 588 bool wxRegConfig::DoReadLong(const wxString
& key
, long *plResult
) const 
 590     wxCHECK_MSG( plResult
, false, _T("wxRegConfig::Read(): NULL param") ); 
 592   wxConfigPathChanger 
path(this, key
); 
 594   bool bQueryGlobal 
= true; 
 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(), plResult
) ) { 
 600       if ( m_keyLocal
.Exists() && LocalKey().HasValue(path
.Name()) ) { 
 601         wxLogWarning(wxT("User value for immutable key '%s' ignored."), 
 602                      path
.Name().c_str()); 
 608       // don't waste time - it's not there anyhow 
 609       bQueryGlobal 
= false; 
 613   // first try local key 
 614   if ( (m_keyLocal
.Exists() && TryGetValue(LocalKey(), path
.Name(), plResult
)) || 
 615        (bQueryGlobal 
&& TryGetValue(m_keyGlobal
, path
.Name(), plResult
)) ) { 
 622 bool wxRegConfig::DoWriteString(const wxString
& key
, const wxString
& szValue
) 
 624   wxConfigPathChanger 
path(this, key
); 
 626   if ( IsImmutable(path
.Name()) ) { 
 627     wxLogError(wxT("Can't change immutable entry '%s'."), path
.Name().c_str()); 
 631   return LocalKey().SetValue(path
.Name(), szValue
); 
 634 bool wxRegConfig::DoWriteLong(const wxString
& key
, long lValue
) 
 636   wxConfigPathChanger 
path(this, key
); 
 638   if ( IsImmutable(path
.Name()) ) { 
 639     wxLogError(wxT("Can't change immutable entry '%s'."), path
.Name().c_str()); 
 643   return LocalKey().SetValue(path
.Name(), lValue
); 
 646 // ---------------------------------------------------------------------------- 
 648 // ---------------------------------------------------------------------------- 
 650 bool wxRegConfig::RenameEntry(const wxString
& oldName
, const wxString
& newName
) 
 652     // check that the old entry exists... 
 653     if ( !HasEntry(oldName
) ) 
 656     // and that the new one doesn't 
 657     if ( HasEntry(newName
) ) 
 660     return m_keyLocal
.RenameValue(oldName
, newName
); 
 663 bool wxRegConfig::RenameGroup(const wxString
& oldName
, const wxString
& newName
) 
 665     // check that the old group exists... 
 666     if ( !HasGroup(oldName
) ) 
 669     // and that the new one doesn't 
 670     if ( HasGroup(newName
) ) 
 673     return wxRegKey(m_keyLocal
, oldName
).Rename(newName
); 
 676 // ---------------------------------------------------------------------------- 
 678 // ---------------------------------------------------------------------------- 
 680 bool wxRegConfig::DeleteEntry(const wxString
& value
, bool bGroupIfEmptyAlso
) 
 682   wxConfigPathChanger 
path(this, value
); 
 684   if ( m_keyLocal
.Exists() ) { 
 685     if ( !m_keyLocal
.DeleteValue(path
.Name()) ) 
 688     if ( bGroupIfEmptyAlso 
&& m_keyLocal
.IsEmpty() ) { 
 689       wxString strKey 
= GetPath().AfterLast(wxCONFIG_PATH_SEPARATOR
); 
 690       SetPath(_T(".."));  // changes m_keyLocal 
 691       return LocalKey().DeleteKey(strKey
); 
 698 bool wxRegConfig::DeleteGroup(const wxString
& key
) 
 700   wxConfigPathChanger 
path(this, key
); 
 702   if ( !m_keyLocal
.Exists() ) 
 708   if ( !LocalKey().DeleteKey(path
.Name()) ) 
 711   path
.UpdateIfDeleted(); 
 716 bool wxRegConfig::DeleteAll() 
 721   bool bOk 
= m_keyLocalRoot
.DeleteSelf(); 
 723   // make sure that we opened m_keyGlobalRoot and so it has a reasonable name: 
 724   // otherwise we will delete HKEY_CLASSES_ROOT recursively 
 725   if ( bOk 
&& m_keyGlobalRoot
.IsOpened() ) 
 726     bOk 
= m_keyGlobalRoot
.DeleteSelf(); 
 731 #endif // wxUSE_CONFIG