]>
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"
33 #include "wx/config.h"
37 #include "wx/msw/registry.h"
38 #include "wx/msw/regconf.h"
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 // we put our data in HKLM\SOFTWARE_KEY\appname
45 #define SOFTWARE_KEY wxString("Software\\")
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 // get the value if the key is opened and it exists
52 bool TryGetValue(const wxRegKey
& key
, const wxString
& str
, wxString
& strVal
)
54 return key
.IsOpened() && key
.HasValue(str
) && key
.QueryValue(str
, strVal
);
57 bool TryGetValue(const wxRegKey
& key
, const wxString
& str
, long *plVal
)
59 return key
.IsOpened() && key
.HasValue(str
) && key
.QueryValue(str
, plVal
);
62 // ============================================================================
64 // ============================================================================
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
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
.IsEmpty() || (strGlobal
.IsEmpty() && bDoUseGlobal
) )
86 if ( vendorName
.IsEmpty() )
89 strRoot
= wxTheApp
->GetVendorName();
96 // no '\\' needed if no vendor name
97 if ( !strRoot
.IsEmpty() )
102 if ( appName
.IsEmpty() )
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
.IsEmpty() ? 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
, _T(""));
130 str
= strGlobal
.IsEmpty() ? 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
, _T(""));
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();
155 wxRegConfig::~wxRegConfig()
157 // nothing to do - key will be closed in their dtors
160 // ----------------------------------------------------------------------------
162 // ----------------------------------------------------------------------------
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
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.
171 void wxRegConfig::SetPath(const wxString
& strPath
)
173 // remember the old path
174 wxString strOldPath
= m_strPath
;
176 #ifdef WX_DEBUG_SET_PATH // non optimized version kept here for testing
177 wxString m_strPathAlt
;
180 wxArrayString aParts
;
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
) ) {
186 wxSplitPath(aParts
, strPath
);
189 // relative path, combine with current one
190 wxString strFullPath
= GetPath();
191 strFullPath
<< wxCONFIG_PATH_SEPARATOR
<< strPath
;
192 wxSplitPath(aParts
, strFullPath
);
195 // recombine path parts in one variable
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
];
205 // check for the most common case first
206 if ( strPath
.empty() )
208 m_strPath
= wxCONFIG_PATH_SEPARATOR
;
212 // construct the full path
213 wxString strFullPath
;
214 if ( strPath
[0u] == wxCONFIG_PATH_SEPARATOR
)
217 strFullPath
= strPath
;
219 else // relative path
221 strFullPath
.reserve(2*m_strPath
.length());
223 strFullPath
<< m_strPath
<< wxCONFIG_PATH_SEPARATOR
<< strPath
;
226 // simplify it: we need to handle ".." here
228 // count the total number of slashes we have to know if we can go upper
229 size_t totalSlashes
= 0;
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
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;
239 const wxChar
*src
= strFullPath
.c_str();
240 size_t len
= strFullPath
.length();
241 const wxChar
*end
= src
+ len
;
243 wxChar
*dst
= m_strPath
.GetWriteBuf(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()
272 while ( *dst
!= wxCONFIG_PATH_SEPARATOR
)
277 else // the position of last slash was stored
280 dst
= start
+ posLastSlash
;
282 // invalidate posLastSlash
286 // this shouldn't happen
287 wxASSERT_MSG( *dst
== wxCONFIG_PATH_SEPARATOR
,
288 _T("error in wxRegConfig::SetPath") );
299 if ( (dst
== start
) || (dst
[-1] != wxCONFIG_PATH_SEPARATOR
) )
301 *dst
= wxCONFIG_PATH_SEPARATOR
;
303 posLastSlash
= dst
- start
;
307 //else: nothing to do, we squeeze several subseuquent
311 else // normal character
318 // NUL terminate the string
319 if ( dst
[-1] == wxCONFIG_PATH_SEPARATOR
&& (dst
!= start
+ 1) )
321 // if it has a trailing slash we remove it unless it is the only
328 m_strPath
.UngetWriteBuf(dst
- start
);
331 #ifdef WX_DEBUG_SET_PATH
332 wxASSERT( m_strPath
== m_strPathAlt
);
335 if ( m_strPath
== strOldPath
)
338 // registry APIs want backslashes instead of slashes
340 size_t len
= m_strPath
.length();
342 const wxChar
*src
= m_strPath
.c_str();
343 wxChar
*dst
= strRegPath
.GetWriteBuf(len
);
345 const wxChar
*end
= src
+ len
;
346 for ( ; src
< end
; src
++, dst
++ )
348 if ( *src
== wxCONFIG_PATH_SEPARATOR
)
354 strRegPath
.UngetWriteBuf(len
);
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
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() )
366 m_keyLocal
.DeleteSelf();
370 // change current key(s)
371 m_keyLocal
.SetName(m_keyLocalRoot
, strRegPath
);
372 m_keyGlobal
.SetName(m_keyGlobalRoot
, strRegPath
);
374 // don't create it right now, wait until it is accessed
375 //m_keyLocal.Create();
381 // ----------------------------------------------------------------------------
382 // enumeration (works only with current group)
383 // ----------------------------------------------------------------------------
386 We want to enumerate all local keys/values after the global ones, but, of
387 course, we don't want to repeat a key which appears locally as well as
390 We use the 15th bit of lIndex for distinction between global and local.
393 #define LOCAL_MASK 0x8000
394 #define IS_LOCAL_INDEX(l) (((l) & LOCAL_MASK) != 0)
396 bool wxRegConfig::GetFirstGroup(wxString
& str
, long& lIndex
) const
399 return GetNextGroup(str
, lIndex
);
402 bool wxRegConfig::GetNextGroup(wxString
& str
, long& lIndex
) const
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
407 while ( m_keyGlobal
.GetNextKey(str
, lIndex
) ) {
408 if ( !m_keyLocal
.Exists() || !LocalKey().HasSubKey(str
) ) {
409 // ok, found one - return it
414 // no more global entries
415 lIndex
|= LOCAL_MASK
;
418 // if we don't have the key at all, don't try to enumerate anything under it
419 if ( !m_keyLocal
.Exists() )
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
;
425 bool bOk
= LocalKey().GetNextKey(str
, lIndex
);
426 lIndex
|= LOCAL_MASK
;
431 bool wxRegConfig::GetFirstEntry(wxString
& str
, long& lIndex
) const
434 return GetNextEntry(str
, lIndex
);
437 bool wxRegConfig::GetNextEntry(wxString
& str
, long& lIndex
) const
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
442 while ( m_keyGlobal
.GetNextValue(str
, lIndex
) ) {
443 if ( !m_keyLocal
.Exists() || !LocalKey().HasValue(str
) ) {
444 // ok, found one - return it
449 // no more global entries
450 lIndex
|= LOCAL_MASK
;
453 // if we don't have the key at all, don't try to enumerate anything under it
454 if ( !m_keyLocal
.Exists() )
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
;
460 bool bOk
= LocalKey().GetNextValue(str
, lIndex
);
461 lIndex
|= LOCAL_MASK
;
466 size_t wxRegConfig::GetNumberOfEntries(bool bRecursive
) const
473 bool bCont
= ((wxRegConfig
*)this)->GetFirstEntry(str
, l
);
477 bCont
= ((wxRegConfig
*)this)->GetNextEntry(str
, l
);
483 size_t wxRegConfig::GetNumberOfGroups(bool bRecursive
) const
490 bool bCont
= ((wxRegConfig
*)this)->GetFirstGroup(str
, l
);
494 bCont
= ((wxRegConfig
*)this)->GetNextGroup(str
, l
);
500 // ----------------------------------------------------------------------------
501 // tests for existence
502 // ----------------------------------------------------------------------------
504 bool wxRegConfig::HasGroup(const wxString
& key
) const
506 wxConfigPathChanger
path(this, key
);
508 wxString
strName(path
.Name());
510 return (m_keyLocal
.Exists() && LocalKey().HasSubKey(strName
)) ||
511 m_keyGlobal
.HasSubKey(strName
);
514 bool wxRegConfig::HasEntry(const wxString
& key
) const
516 wxConfigPathChanger
path(this, key
);
518 wxString
strName(path
.Name());
520 return (m_keyLocal
.Exists() && LocalKey().HasValue(strName
)) ||
521 m_keyGlobal
.HasValue(strName
);
524 wxConfigBase::EntryType
wxRegConfig::GetEntryType(const wxString
& key
) const
526 wxConfigPathChanger
path(this, key
);
528 wxString
strName(path
.Name());
531 if ( m_keyLocal
.Exists() && LocalKey().HasValue(strName
) )
532 isNumeric
= m_keyLocal
.IsNumericValue(strName
);
533 else if ( m_keyGlobal
.HasValue(strName
) )
534 isNumeric
= m_keyGlobal
.IsNumericValue(strName
);
536 return wxConfigBase::Type_Unknown
;
538 return isNumeric
? wxConfigBase::Type_Integer
: wxConfigBase::Type_String
;
541 // ----------------------------------------------------------------------------
543 // ----------------------------------------------------------------------------
545 bool wxRegConfig::Read(const wxString
& key
, wxString
*pStr
) const
547 wxConfigPathChanger
path(this, key
);
549 bool bQueryGlobal
= TRUE
;
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()) ) {
554 if ( TryGetValue(m_keyGlobal
, path
.Name(), *pStr
) ) {
555 if ( m_keyLocal
.Exists() && LocalKey().HasValue(path
.Name()) ) {
556 wxLogWarning(wxT("User value for immutable key '%s' ignored."),
557 path
.Name().c_str());
559 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
563 // don't waste time - it's not there anyhow
564 bQueryGlobal
= FALSE
;
568 // first try local key
569 if ( (m_keyLocal
.Exists() && TryGetValue(LocalKey(), path
.Name(), *pStr
)) ||
570 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), *pStr
)) ) {
573 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
580 bool wxRegConfig::Read(const wxString
& key
, wxString
*pStr
,
581 const wxString
& szDefault
) const
583 wxConfigPathChanger
path(this, key
);
585 bool bQueryGlobal
= TRUE
;
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
) ) {
591 if ( m_keyLocal
.Exists() && LocalKey().HasValue(path
.Name()) ) {
592 wxLogWarning(wxT("User value for immutable key '%s' ignored."),
593 path
.Name().c_str());
599 // don't waste time - it's not there anyhow
600 bQueryGlobal
= FALSE
;
604 // first try local key
605 if ( (m_keyLocal
.Exists() && TryGetValue(LocalKey(), path
.Name(), *pStr
)) ||
606 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), *pStr
)) ) {
607 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
611 if ( IsRecordingDefaults() ) {
612 ((wxRegConfig
*)this)->Write(key
, szDefault
);
619 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
624 bool wxRegConfig::Read(const wxString
& key
, long *plResult
) const
626 wxConfigPathChanger
path(this, key
);
628 bool bQueryGlobal
= TRUE
;
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()) ) {
633 if ( TryGetValue(m_keyGlobal
, path
.Name(), plResult
) ) {
634 if ( m_keyLocal
.Exists() && LocalKey().HasValue(path
.Name()) ) {
635 wxLogWarning(wxT("User value for immutable key '%s' ignored."),
636 path
.Name().c_str());
642 // don't waste time - it's not there anyhow
643 bQueryGlobal
= FALSE
;
647 // first try local key
648 if ( (m_keyLocal
.Exists() && TryGetValue(LocalKey(), path
.Name(), plResult
)) ||
649 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), plResult
)) ) {
655 bool wxRegConfig::Write(const wxString
& key
, const wxString
& szValue
)
657 wxConfigPathChanger
path(this, key
);
659 if ( IsImmutable(path
.Name()) ) {
660 wxLogError(wxT("Can't change immutable entry '%s'."), path
.Name().c_str());
664 return LocalKey().SetValue(path
.Name(), szValue
);
667 bool wxRegConfig::Write(const wxString
& key
, long lValue
)
669 wxConfigPathChanger
path(this, key
);
671 if ( IsImmutable(path
.Name()) ) {
672 wxLogError(wxT("Can't change immutable entry '%s'."), path
.Name().c_str());
676 return LocalKey().SetValue(path
.Name(), lValue
);
679 // ----------------------------------------------------------------------------
681 // ----------------------------------------------------------------------------
683 bool wxRegConfig::RenameEntry(const wxString
& oldName
, const wxString
& newName
)
685 // check that the old entry exists...
686 if ( !HasEntry(oldName
) )
689 // and that the new one doesn't
690 if ( HasEntry(newName
) )
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
697 if ( m_keyLocal
.IsNumericValue(oldName
) )
700 ok
= m_keyLocal
.QueryValue(oldName
, &val
) &&
701 m_keyLocal
.SetValue(newName
, val
);
706 ok
= m_keyLocal
.QueryValue(oldName
, val
) &&
707 m_keyLocal
.SetValue(newName
, val
);
713 if ( !m_keyLocal
.DeleteValue(oldName
) )
715 m_keyLocal
.DeleteValue(newName
);
723 bool wxRegConfig::RenameGroup(const wxString
& oldName
, const wxString
& newName
)
725 // check that the old group exists...
726 if ( !HasGroup(oldName
) )
729 // and that the new one doesn't
730 if ( HasGroup(newName
) )
733 // TODO there is no way to rename a registry key - we must do a deep copy
735 wxFAIL_MSG(wxT("Registry key renaming not implemented"));
740 // ----------------------------------------------------------------------------
742 // ----------------------------------------------------------------------------
743 bool wxRegConfig::DeleteEntry(const wxString
& value
, bool bGroupIfEmptyAlso
)
745 wxConfigPathChanger
path(this, value
);
747 if ( m_keyLocal
.Exists() ) {
748 if ( !m_keyLocal
.DeleteValue(path
.Name()) )
751 if ( m_keyLocal
.IsEmpty() ) {
752 wxString strKey
= GetPath().AfterLast(wxCONFIG_PATH_SEPARATOR
);
753 SetPath(".."); // changes m_keyLocal
754 return LocalKey().DeleteKey(strKey
);
761 bool wxRegConfig::DeleteGroup(const wxString
& key
)
763 wxConfigPathChanger
path(this, key
);
765 return m_keyLocal
.Exists() ? LocalKey().DeleteKey(path
.Name()) : TRUE
;
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();