]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/regconf.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/regconf.cpp
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "regconf.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
24 #include "wx/string.h"
34 #include "wx/config.h"
38 #include "wx/msw/registry.h"
39 #include "wx/msw/regconf.h"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 // we put our data in HKLM\SOFTWARE_KEY\appname
46 #define SOFTWARE_KEY wxString("Software\\")
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 // get the value if the key is opened and it exists
53 bool TryGetValue(const wxRegKey
& key
, const wxString
& str
, wxString
& strVal
)
55 return key
.IsOpened() && key
.HasValue(str
) && key
.QueryValue(str
, strVal
);
58 bool TryGetValue(const wxRegKey
& key
, const wxString
& str
, long *plVal
)
60 return key
.IsOpened() && key
.HasValue(str
) && key
.QueryValue(str
, plVal
);
63 // ============================================================================
65 // ============================================================================
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
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
,
76 : wxConfigBase(appName
, vendorName
, strLocal
, strGlobal
, style
)
80 bool bDoUseGlobal
= (style
& wxCONFIG_USE_GLOBAL_FILE
) != 0;
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
85 if ( strLocal
.IsEmpty() || (strGlobal
.IsEmpty() && bDoUseGlobal
) )
87 if ( vendorName
.IsEmpty() )
90 strRoot
= wxTheApp
->GetVendorName();
97 // no '\\' needed if no vendor name
98 if ( !strRoot
.IsEmpty() )
103 if ( appName
.IsEmpty() )
105 wxCHECK_RET( wxTheApp
, wxT("No application name in wxRegConfig ctor!") );
106 strRoot
<< wxTheApp
->GetAppName();
113 //else: we don't need to do all the complicated stuff above
115 wxString str
= strLocal
.IsEmpty() ? strRoot
: strLocal
;
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;
123 m_keyLocalRoot
.ReserveMemoryForName(MEMORY_PREALLOC
);
124 m_keyLocal
.ReserveMemoryForName(MEMORY_PREALLOC
);
126 m_keyLocalRoot
.SetName(wxRegKey::HKCU
, SOFTWARE_KEY
+ str
);
127 m_keyLocal
.SetName(m_keyLocalRoot
, _T(""));
131 str
= strGlobal
.IsEmpty() ? strRoot
: strGlobal
;
133 m_keyGlobalRoot
.ReserveMemoryForName(MEMORY_PREALLOC
);
134 m_keyGlobal
.ReserveMemoryForName(MEMORY_PREALLOC
);
136 m_keyGlobalRoot
.SetName(wxRegKey::HKLM
, SOFTWARE_KEY
+ str
);
137 m_keyGlobal
.SetName(m_keyGlobalRoot
, _T(""));
140 // Create() will Open() if key already exists
141 m_keyLocalRoot
.Create();
143 // as it's the same key, Open() shouldn't fail (i.e. no need for Create())
146 // OTOH, this key may perfectly not exist, so suppress error messages the call
147 // to Open() might generate
151 m_keyGlobalRoot
.Open();
156 wxRegConfig::~wxRegConfig()
158 // nothing to do - key will be closed in their dtors
161 // ----------------------------------------------------------------------------
163 // ----------------------------------------------------------------------------
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
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.
172 void wxRegConfig::SetPath(const wxString
& strPath
)
174 // remember the old path
175 wxString strOldPath
= m_strPath
;
177 #ifdef WX_DEBUG_SET_PATH // non optimized version kept here for testing
178 wxString m_strPathAlt
;
181 wxArrayString aParts
;
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
) ) {
187 wxSplitPath(aParts
, strPath
);
190 // relative path, combine with current one
191 wxString strFullPath
= GetPath();
192 strFullPath
<< wxCONFIG_PATH_SEPARATOR
<< strPath
;
193 wxSplitPath(aParts
, strFullPath
);
196 // recombine path parts in one variable
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
];
206 // check for the most common case first
207 if ( strPath
.empty() )
209 m_strPath
= wxCONFIG_PATH_SEPARATOR
;
213 // construct the full path
214 wxString strFullPath
;
215 if ( strPath
[0u] == wxCONFIG_PATH_SEPARATOR
)
218 strFullPath
= strPath
;
220 else // relative path
222 strFullPath
.reserve(2*m_strPath
.length());
224 strFullPath
<< m_strPath
<< wxCONFIG_PATH_SEPARATOR
<< strPath
;
227 // simplify it: we need to handle ".." here
229 // count the total number of slashes we have to know if we can go upper
230 size_t totalSlashes
= 0;
232 // position of the last slash to be able to backtrack to it quickly if
233 // needed, but we set it to -1 if we don't have a valid position
235 // we only remember the last position which means that we handle ".."
236 // quite efficiently but not "../.." - however the latter should be
237 // much more rare, so it is probably ok
238 int posLastSlash
= -1;
240 const wxChar
*src
= strFullPath
.c_str();
241 size_t len
= strFullPath
.length();
242 const wxChar
*end
= src
+ len
;
244 wxChar
*dst
= m_strPath
.GetWriteBuf(len
);
247 for ( ; src
< end
; src
++, dst
++ )
249 if ( *src
== wxCONFIG_PATH_SEPARATOR
)
253 // note that we don't have to check for src < end here as
254 // *end == 0 so can't be '.'
255 if ( src
[1] == _T('.') && src
[2] == _T('.') &&
256 (src
+ 3 == end
|| src
[3] == wxCONFIG_PATH_SEPARATOR
) )
260 wxLogWarning(_("'%s' has extra '..', ignored."),
261 strFullPath
.c_str());
263 else // return to the previous path component
265 // do we already have its position?
266 if ( posLastSlash
== -1 )
268 // no, find it: note that we are sure to have one
269 // because totalSlashes > 0 so we don't have to
270 // check the boundary condition below
272 // 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 // this shouldn't happen
288 wxASSERT_MSG( *dst
== wxCONFIG_PATH_SEPARATOR
,
289 _T("error in wxRegConfig::SetPath") );
300 if ( (dst
== start
) || (dst
[-1] != wxCONFIG_PATH_SEPARATOR
) )
302 *dst
= wxCONFIG_PATH_SEPARATOR
;
304 posLastSlash
= dst
- start
;
308 //else: nothing to do, we squeeze several subseuquent
312 else // normal character
319 // NUL terminate the string
320 if ( dst
[-1] == wxCONFIG_PATH_SEPARATOR
&& (dst
!= start
+ 1) )
322 // if it has a trailing slash we remove it unless it is the only
329 m_strPath
.UngetWriteBuf(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 size_t len
= m_strPath
.length();
343 const wxChar
*src
= m_strPath
.c_str();
344 wxChar
*dst
= strRegPath
.GetWriteBuf(len
);
346 const wxChar
*end
= src
+ len
;
347 for ( ; src
< end
; src
++, dst
++ )
349 if ( *src
== wxCONFIG_PATH_SEPARATOR
)
355 strRegPath
.UngetWriteBuf(len
);
357 // this is not needed any longer as we don't create keys unnecessarily any
358 // more (now it is done on demand, i.e. only when they're going to contain
361 // as we create the registry key when SetPath(key) is done, we can be left
362 // with plenty of empty keys if this was only done to try to read some
363 // value which, in fact, doesn't exist - to prevent this from happening we
364 // automatically delete the old key if it was empty
365 if ( m_keyLocal
.Exists() && LocalKey().IsEmpty() )
367 m_keyLocal
.DeleteSelf();
371 // change current key(s)
372 m_keyLocal
.SetName(m_keyLocalRoot
, strRegPath
);
373 m_keyGlobal
.SetName(m_keyGlobalRoot
, strRegPath
);
375 // don't create it right now, wait until it is accessed
376 //m_keyLocal.Create();
382 // ----------------------------------------------------------------------------
383 // enumeration (works only with current group)
384 // ----------------------------------------------------------------------------
387 We want to enumerate all local keys/values after the global ones, but, of
388 course, we don't want to repeat a key which appears locally as well as
391 We use the 15th bit of lIndex for distinction between global and local.
394 #define LOCAL_MASK 0x8000
395 #define IS_LOCAL_INDEX(l) (((l) & LOCAL_MASK) != 0)
397 bool wxRegConfig::GetFirstGroup(wxString
& str
, long& lIndex
) const
400 return GetNextGroup(str
, lIndex
);
403 bool wxRegConfig::GetNextGroup(wxString
& str
, long& lIndex
) const
405 // are we already enumerating local entries?
406 if ( m_keyGlobal
.IsOpened() && !IS_LOCAL_INDEX(lIndex
) ) {
407 // try to find a global entry which doesn't appear locally
408 while ( m_keyGlobal
.GetNextKey(str
, lIndex
) ) {
409 if ( !m_keyLocal
.Exists() || !LocalKey().HasSubKey(str
) ) {
410 // ok, found one - return it
415 // no more global entries
416 lIndex
|= LOCAL_MASK
;
419 // if we don't have the key at all, don't try to enumerate anything under it
420 if ( !m_keyLocal
.Exists() )
423 // much easier with local entries: get the next one we find
424 // (don't forget to clear our flag bit and set it again later)
425 lIndex
&= ~LOCAL_MASK
;
426 bool bOk
= LocalKey().GetNextKey(str
, lIndex
);
427 lIndex
|= LOCAL_MASK
;
432 bool wxRegConfig::GetFirstEntry(wxString
& str
, long& lIndex
) const
435 return GetNextEntry(str
, lIndex
);
438 bool wxRegConfig::GetNextEntry(wxString
& str
, long& lIndex
) const
440 // are we already enumerating local entries?
441 if ( m_keyGlobal
.IsOpened() && !IS_LOCAL_INDEX(lIndex
) ) {
442 // try to find a global entry which doesn't appear locally
443 while ( m_keyGlobal
.GetNextValue(str
, lIndex
) ) {
444 if ( !m_keyLocal
.Exists() || !LocalKey().HasValue(str
) ) {
445 // ok, found one - return it
450 // no more global entries
451 lIndex
|= LOCAL_MASK
;
454 // if we don't have the key at all, don't try to enumerate anything under it
455 if ( !m_keyLocal
.Exists() )
458 // much easier with local entries: get the next one we find
459 // (don't forget to clear our flag bit and set it again later)
460 lIndex
&= ~LOCAL_MASK
;
461 bool bOk
= LocalKey().GetNextValue(str
, lIndex
);
462 lIndex
|= LOCAL_MASK
;
467 size_t wxRegConfig::GetNumberOfEntries(bool bRecursive
) const
474 bool bCont
= ((wxRegConfig
*)this)->GetFirstEntry(str
, l
);
478 bCont
= ((wxRegConfig
*)this)->GetNextEntry(str
, l
);
484 size_t wxRegConfig::GetNumberOfGroups(bool bRecursive
) const
491 bool bCont
= ((wxRegConfig
*)this)->GetFirstGroup(str
, l
);
495 bCont
= ((wxRegConfig
*)this)->GetNextGroup(str
, l
);
501 // ----------------------------------------------------------------------------
502 // tests for existence
503 // ----------------------------------------------------------------------------
505 bool wxRegConfig::HasGroup(const wxString
& key
) const
507 wxConfigPathChanger
path(this, key
);
509 wxString
strName(path
.Name());
511 return (m_keyLocal
.Exists() && LocalKey().HasSubKey(strName
)) ||
512 m_keyGlobal
.HasSubKey(strName
);
515 bool wxRegConfig::HasEntry(const wxString
& key
) const
517 wxConfigPathChanger
path(this, key
);
519 wxString
strName(path
.Name());
521 return (m_keyLocal
.Exists() && LocalKey().HasValue(strName
)) ||
522 m_keyGlobal
.HasValue(strName
);
525 wxConfigBase::EntryType
wxRegConfig::GetEntryType(const wxString
& key
) const
527 wxConfigPathChanger
path(this, key
);
529 wxString
strName(path
.Name());
532 if ( m_keyLocal
.Exists() && LocalKey().HasValue(strName
) )
533 isNumeric
= m_keyLocal
.IsNumericValue(strName
);
534 else if ( m_keyGlobal
.HasValue(strName
) )
535 isNumeric
= m_keyGlobal
.IsNumericValue(strName
);
537 return wxConfigBase::Type_Unknown
;
539 return isNumeric
? wxConfigBase::Type_Integer
: wxConfigBase::Type_String
;
542 // ----------------------------------------------------------------------------
544 // ----------------------------------------------------------------------------
546 bool wxRegConfig::Read(const wxString
& key
, wxString
*pStr
) const
548 wxConfigPathChanger
path(this, key
);
550 bool bQueryGlobal
= TRUE
;
552 // if immutable key exists in global key we must check that it's not
553 // overriden by the local key with the same name
554 if ( IsImmutable(path
.Name()) ) {
555 if ( TryGetValue(m_keyGlobal
, path
.Name(), *pStr
) ) {
556 if ( m_keyLocal
.Exists() && LocalKey().HasValue(path
.Name()) ) {
557 wxLogWarning(wxT("User value for immutable key '%s' ignored."),
558 path
.Name().c_str());
560 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
564 // don't waste time - it's not there anyhow
565 bQueryGlobal
= FALSE
;
569 // first try local key
570 if ( (m_keyLocal
.Exists() && TryGetValue(LocalKey(), path
.Name(), *pStr
)) ||
571 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), *pStr
)) ) {
574 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
581 bool wxRegConfig::Read(const wxString
& key
, wxString
*pStr
,
582 const wxString
& szDefault
) const
584 wxConfigPathChanger
path(this, key
);
586 bool bQueryGlobal
= TRUE
;
588 // if immutable key exists in global key we must check that it's not
589 // overriden by the local key with the same name
590 if ( IsImmutable(path
.Name()) ) {
591 if ( TryGetValue(m_keyGlobal
, path
.Name(), *pStr
) ) {
592 if ( m_keyLocal
.Exists() && LocalKey().HasValue(path
.Name()) ) {
593 wxLogWarning(wxT("User value for immutable key '%s' ignored."),
594 path
.Name().c_str());
600 // don't waste time - it's not there anyhow
601 bQueryGlobal
= FALSE
;
605 // first try local key
606 if ( (m_keyLocal
.Exists() && TryGetValue(LocalKey(), path
.Name(), *pStr
)) ||
607 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), *pStr
)) ) {
608 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
612 if ( IsRecordingDefaults() ) {
613 ((wxRegConfig
*)this)->Write(key
, szDefault
);
620 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
625 bool wxRegConfig::Read(const wxString
& key
, long *plResult
) const
627 wxConfigPathChanger
path(this, key
);
629 bool bQueryGlobal
= TRUE
;
631 // if immutable key exists in global key we must check that it's not
632 // overriden by the local key with the same name
633 if ( IsImmutable(path
.Name()) ) {
634 if ( TryGetValue(m_keyGlobal
, path
.Name(), plResult
) ) {
635 if ( m_keyLocal
.Exists() && LocalKey().HasValue(path
.Name()) ) {
636 wxLogWarning(wxT("User value for immutable key '%s' ignored."),
637 path
.Name().c_str());
643 // don't waste time - it's not there anyhow
644 bQueryGlobal
= FALSE
;
648 // first try local key
649 if ( (m_keyLocal
.Exists() && TryGetValue(LocalKey(), path
.Name(), plResult
)) ||
650 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), plResult
)) ) {
656 bool wxRegConfig::Write(const wxString
& key
, const wxString
& szValue
)
658 wxConfigPathChanger
path(this, key
);
660 if ( IsImmutable(path
.Name()) ) {
661 wxLogError(wxT("Can't change immutable entry '%s'."), path
.Name().c_str());
665 return LocalKey().SetValue(path
.Name(), szValue
);
668 bool wxRegConfig::Write(const wxString
& key
, long lValue
)
670 wxConfigPathChanger
path(this, key
);
672 if ( IsImmutable(path
.Name()) ) {
673 wxLogError(wxT("Can't change immutable entry '%s'."), path
.Name().c_str());
677 return LocalKey().SetValue(path
.Name(), lValue
);
680 // ----------------------------------------------------------------------------
682 // ----------------------------------------------------------------------------
684 bool wxRegConfig::RenameEntry(const wxString
& oldName
, const wxString
& newName
)
686 // check that the old entry exists...
687 if ( !HasEntry(oldName
) )
690 // and that the new one doesn't
691 if ( HasEntry(newName
) )
694 // delete the old entry and create the new one - but do in the reverse
695 // order to not lose the data if Create() fails
698 if ( m_keyLocal
.IsNumericValue(oldName
) )
701 ok
= m_keyLocal
.QueryValue(oldName
, &val
) &&
702 m_keyLocal
.SetValue(newName
, val
);
707 ok
= m_keyLocal
.QueryValue(oldName
, val
) &&
708 m_keyLocal
.SetValue(newName
, val
);
714 if ( !m_keyLocal
.DeleteValue(oldName
) )
716 m_keyLocal
.DeleteValue(newName
);
724 bool wxRegConfig::RenameGroup(const wxString
& oldName
, const wxString
& newName
)
726 // check that the old group exists...
727 if ( !HasGroup(oldName
) )
730 // and that the new one doesn't
731 if ( HasGroup(newName
) )
734 // TODO there is no way to rename a registry key - we must do a deep copy
736 wxFAIL_MSG(wxT("Registry key renaming not implemented"));
741 // ----------------------------------------------------------------------------
743 // ----------------------------------------------------------------------------
744 bool wxRegConfig::DeleteEntry(const wxString
& value
, bool bGroupIfEmptyAlso
)
746 wxConfigPathChanger
path(this, value
);
748 if ( m_keyLocal
.Exists() ) {
749 if ( !m_keyLocal
.DeleteValue(path
.Name()) )
752 if ( m_keyLocal
.IsEmpty() ) {
753 wxString strKey
= GetPath().AfterLast(wxCONFIG_PATH_SEPARATOR
);
754 SetPath(".."); // changes m_keyLocal
755 return LocalKey().DeleteKey(strKey
);
762 bool wxRegConfig::DeleteGroup(const wxString
& key
)
764 wxConfigPathChanger
path(this, key
);
766 return m_keyLocal
.Exists() ? LocalKey().DeleteKey(path
.Name()) : TRUE
;
769 bool wxRegConfig::DeleteAll()
774 bool bOk
= m_keyLocalRoot
.DeleteSelf();
776 // make sure that we opened m_keyGlobalRoot and so it has a reasonable name:
777 // otherwise we will delete HKEY_CLASSES_ROOT recursively
778 if ( bOk
&& m_keyGlobalRoot
.IsOpened() )
779 bOk
= m_keyGlobalRoot
.DeleteSelf();