]>
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
;
225 if ( strFullPath
.Len() == 0 ||
226 strFullPath
.Last() != wxCONFIG_PATH_SEPARATOR
)
227 strFullPath
<< wxCONFIG_PATH_SEPARATOR
;
228 strFullPath
<< strPath
;
231 // simplify it: we need to handle ".." here
233 // count the total number of slashes we have to know if we can go upper
234 size_t totalSlashes
= 0;
236 // position of the last slash to be able to backtrack to it quickly if
237 // needed, but we set it to -1 if we don't have a valid position
239 // we only remember the last position which means that we handle ".."
240 // quite efficiently but not "../.." - however the latter should be
241 // much more rare, so it is probably ok
242 int posLastSlash
= -1;
244 const wxChar
*src
= strFullPath
.c_str();
245 size_t len
= strFullPath
.length();
246 const wxChar
*end
= src
+ len
;
248 wxChar
*dst
= m_strPath
.GetWriteBuf(len
);
251 for ( ; src
< end
; src
++, dst
++ )
253 if ( *src
== wxCONFIG_PATH_SEPARATOR
)
257 // note that we don't have to check for src < end here as
258 // *end == 0 so can't be '.'
259 if ( src
[1] == _T('.') && src
[2] == _T('.') &&
260 (src
+ 3 == end
|| src
[3] == wxCONFIG_PATH_SEPARATOR
) )
264 wxLogWarning(_("'%s' has extra '..', ignored."),
265 strFullPath
.c_str());
267 else // return to the previous path component
269 // do we already have its position?
270 if ( posLastSlash
== -1 )
272 // no, find it: note that we are sure to have one
273 // because totalSlashes > 0 so we don't have to
274 // check the boundary condition below
276 // this is more efficient than strrchr()
278 while ( *dst
!= wxCONFIG_PATH_SEPARATOR
)
283 else // the position of last slash was stored
286 dst
= start
+ posLastSlash
;
288 // invalidate posLastSlash
292 // we must have found a slash one way or another!
293 wxASSERT_MSG( *dst
== wxCONFIG_PATH_SEPARATOR
,
294 _T("error in wxRegConfig::SetPath") );
296 // stay at the same position
308 if ( (dst
== start
) || (dst
[-1] != wxCONFIG_PATH_SEPARATOR
) )
310 *dst
= wxCONFIG_PATH_SEPARATOR
;
312 posLastSlash
= dst
- start
;
316 else // previous char was a slash too
318 // squeeze several subsequent slashes into one: i.e.
319 // just ignore this one
324 else // normal character
331 // NUL terminate the string
332 if ( dst
[-1] == wxCONFIG_PATH_SEPARATOR
&& (dst
!= start
+ 1) )
334 // if it has a trailing slash we remove it unless it is the only
341 m_strPath
.UngetWriteBuf(dst
- start
);
344 #ifdef WX_DEBUG_SET_PATH
345 wxASSERT( m_strPath
== m_strPathAlt
);
348 if ( m_strPath
== strOldPath
)
351 // registry APIs want backslashes instead of slashes
353 size_t len
= m_strPath
.length();
355 const wxChar
*src
= m_strPath
.c_str();
356 wxChar
*dst
= strRegPath
.GetWriteBuf(len
);
358 const wxChar
*end
= src
+ len
;
359 for ( ; src
< end
; src
++, dst
++ )
361 if ( *src
== wxCONFIG_PATH_SEPARATOR
)
367 strRegPath
.UngetWriteBuf(len
);
369 // this is not needed any longer as we don't create keys unnecessarily any
370 // more (now it is done on demand, i.e. only when they're going to contain
373 // as we create the registry key when SetPath(key) is done, we can be left
374 // with plenty of empty keys if this was only done to try to read some
375 // value which, in fact, doesn't exist - to prevent this from happening we
376 // automatically delete the old key if it was empty
377 if ( m_keyLocal
.Exists() && LocalKey().IsEmpty() )
379 m_keyLocal
.DeleteSelf();
383 // change current key(s)
384 m_keyLocal
.SetName(m_keyLocalRoot
, strRegPath
);
385 m_keyGlobal
.SetName(m_keyGlobalRoot
, strRegPath
);
387 // don't create it right now, wait until it is accessed
388 //m_keyLocal.Create();
394 // ----------------------------------------------------------------------------
395 // enumeration (works only with current group)
396 // ----------------------------------------------------------------------------
399 We want to enumerate all local keys/values after the global ones, but, of
400 course, we don't want to repeat a key which appears locally as well as
403 We use the 15th bit of lIndex for distinction between global and local.
406 #define LOCAL_MASK 0x8000
407 #define IS_LOCAL_INDEX(l) (((l) & LOCAL_MASK) != 0)
409 bool wxRegConfig::GetFirstGroup(wxString
& str
, long& lIndex
) const
412 return GetNextGroup(str
, lIndex
);
415 bool wxRegConfig::GetNextGroup(wxString
& str
, long& lIndex
) const
417 // are we already enumerating local entries?
418 if ( m_keyGlobal
.IsOpened() && !IS_LOCAL_INDEX(lIndex
) ) {
419 // try to find a global entry which doesn't appear locally
420 while ( m_keyGlobal
.GetNextKey(str
, lIndex
) ) {
421 if ( !m_keyLocal
.Exists() || !LocalKey().HasSubKey(str
) ) {
422 // ok, found one - return it
427 // no more global entries
428 lIndex
|= LOCAL_MASK
;
431 // if we don't have the key at all, don't try to enumerate anything under it
432 if ( !m_keyLocal
.Exists() )
435 // much easier with local entries: get the next one we find
436 // (don't forget to clear our flag bit and set it again later)
437 lIndex
&= ~LOCAL_MASK
;
438 bool bOk
= LocalKey().GetNextKey(str
, lIndex
);
439 lIndex
|= LOCAL_MASK
;
444 bool wxRegConfig::GetFirstEntry(wxString
& str
, long& lIndex
) const
447 return GetNextEntry(str
, lIndex
);
450 bool wxRegConfig::GetNextEntry(wxString
& str
, long& lIndex
) const
452 // are we already enumerating local entries?
453 if ( m_keyGlobal
.IsOpened() && !IS_LOCAL_INDEX(lIndex
) ) {
454 // try to find a global entry which doesn't appear locally
455 while ( m_keyGlobal
.GetNextValue(str
, lIndex
) ) {
456 if ( !m_keyLocal
.Exists() || !LocalKey().HasValue(str
) ) {
457 // ok, found one - return it
462 // no more global entries
463 lIndex
|= LOCAL_MASK
;
466 // if we don't have the key at all, don't try to enumerate anything under it
467 if ( !m_keyLocal
.Exists() )
470 // much easier with local entries: get the next one we find
471 // (don't forget to clear our flag bit and set it again later)
472 lIndex
&= ~LOCAL_MASK
;
473 bool bOk
= LocalKey().GetNextValue(str
, lIndex
);
474 lIndex
|= LOCAL_MASK
;
479 size_t wxRegConfig::GetNumberOfEntries(bool WXUNUSED(bRecursive
)) const
486 bool bCont
= ((wxRegConfig
*)this)->GetFirstEntry(str
, l
);
490 bCont
= ((wxRegConfig
*)this)->GetNextEntry(str
, l
);
496 size_t wxRegConfig::GetNumberOfGroups(bool WXUNUSED(bRecursive
)) const
503 bool bCont
= ((wxRegConfig
*)this)->GetFirstGroup(str
, l
);
507 bCont
= ((wxRegConfig
*)this)->GetNextGroup(str
, l
);
513 // ----------------------------------------------------------------------------
514 // tests for existence
515 // ----------------------------------------------------------------------------
517 bool wxRegConfig::HasGroup(const wxString
& key
) const
519 wxConfigPathChanger
path(this, key
);
521 wxString
strName(path
.Name());
523 return (m_keyLocal
.Exists() && LocalKey().HasSubKey(strName
)) ||
524 m_keyGlobal
.HasSubKey(strName
);
527 bool wxRegConfig::HasEntry(const wxString
& key
) const
529 wxConfigPathChanger
path(this, key
);
531 wxString
strName(path
.Name());
533 return (m_keyLocal
.Exists() && LocalKey().HasValue(strName
)) ||
534 m_keyGlobal
.HasValue(strName
);
537 wxConfigBase::EntryType
wxRegConfig::GetEntryType(const wxString
& key
) const
539 wxConfigPathChanger
path(this, key
);
541 wxString
strName(path
.Name());
544 if ( m_keyLocal
.Exists() && LocalKey().HasValue(strName
) )
545 isNumeric
= m_keyLocal
.IsNumericValue(strName
);
546 else if ( m_keyGlobal
.HasValue(strName
) )
547 isNumeric
= m_keyGlobal
.IsNumericValue(strName
);
549 return wxConfigBase::Type_Unknown
;
551 return isNumeric
? wxConfigBase::Type_Integer
: wxConfigBase::Type_String
;
554 // ----------------------------------------------------------------------------
556 // ----------------------------------------------------------------------------
558 bool wxRegConfig::Read(const wxString
& key
, wxString
*pStr
) const
560 wxConfigPathChanger
path(this, key
);
562 bool bQueryGlobal
= TRUE
;
564 // if immutable key exists in global key we must check that it's not
565 // overriden by the local key with the same name
566 if ( IsImmutable(path
.Name()) ) {
567 if ( TryGetValue(m_keyGlobal
, path
.Name(), *pStr
) ) {
568 if ( m_keyLocal
.Exists() && LocalKey().HasValue(path
.Name()) ) {
569 wxLogWarning(wxT("User value for immutable key '%s' ignored."),
570 path
.Name().c_str());
572 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
576 // don't waste time - it's not there anyhow
577 bQueryGlobal
= FALSE
;
581 // first try local key
582 if ( (m_keyLocal
.Exists() && TryGetValue(LocalKey(), path
.Name(), *pStr
)) ||
583 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), *pStr
)) ) {
586 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
593 bool wxRegConfig::Read(const wxString
& key
, wxString
*pStr
,
594 const wxString
& szDefault
) const
596 wxConfigPathChanger
path(this, key
);
598 bool bQueryGlobal
= TRUE
;
600 // if immutable key exists in global key we must check that it's not
601 // overriden by the local key with the same name
602 if ( IsImmutable(path
.Name()) ) {
603 if ( TryGetValue(m_keyGlobal
, path
.Name(), *pStr
) ) {
604 if ( m_keyLocal
.Exists() && LocalKey().HasValue(path
.Name()) ) {
605 wxLogWarning(wxT("User value for immutable key '%s' ignored."),
606 path
.Name().c_str());
612 // don't waste time - it's not there anyhow
613 bQueryGlobal
= FALSE
;
617 // first try local key
618 if ( (m_keyLocal
.Exists() && TryGetValue(LocalKey(), path
.Name(), *pStr
)) ||
619 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), *pStr
)) ) {
620 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
624 if ( IsRecordingDefaults() ) {
625 ((wxRegConfig
*)this)->Write(key
, szDefault
);
632 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
637 bool wxRegConfig::Read(const wxString
& key
, long *plResult
) const
639 wxConfigPathChanger
path(this, key
);
641 bool bQueryGlobal
= TRUE
;
643 // if immutable key exists in global key we must check that it's not
644 // overriden by the local key with the same name
645 if ( IsImmutable(path
.Name()) ) {
646 if ( TryGetValue(m_keyGlobal
, path
.Name(), plResult
) ) {
647 if ( m_keyLocal
.Exists() && LocalKey().HasValue(path
.Name()) ) {
648 wxLogWarning(wxT("User value for immutable key '%s' ignored."),
649 path
.Name().c_str());
655 // don't waste time - it's not there anyhow
656 bQueryGlobal
= FALSE
;
660 // first try local key
661 if ( (m_keyLocal
.Exists() && TryGetValue(LocalKey(), path
.Name(), plResult
)) ||
662 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), plResult
)) ) {
668 bool wxRegConfig::Write(const wxString
& key
, const wxString
& szValue
)
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(), szValue
);
680 bool wxRegConfig::Write(const wxString
& key
, long lValue
)
682 wxConfigPathChanger
path(this, key
);
684 if ( IsImmutable(path
.Name()) ) {
685 wxLogError(wxT("Can't change immutable entry '%s'."), path
.Name().c_str());
689 return LocalKey().SetValue(path
.Name(), lValue
);
692 // ----------------------------------------------------------------------------
694 // ----------------------------------------------------------------------------
696 bool wxRegConfig::RenameEntry(const wxString
& oldName
, const wxString
& newName
)
698 // check that the old entry exists...
699 if ( !HasEntry(oldName
) )
702 // and that the new one doesn't
703 if ( HasEntry(newName
) )
706 return m_keyLocal
.RenameValue(oldName
, newName
);
709 bool wxRegConfig::RenameGroup(const wxString
& oldName
, const wxString
& newName
)
711 // check that the old group exists...
712 if ( !HasGroup(oldName
) )
715 // and that the new one doesn't
716 if ( HasGroup(newName
) )
719 return wxRegKey(m_keyLocal
, oldName
).Rename(newName
);
722 // ----------------------------------------------------------------------------
724 // ----------------------------------------------------------------------------
725 bool wxRegConfig::DeleteEntry(const wxString
& value
, bool WXUNUSED(bGroupIfEmptyAlso
))
727 wxConfigPathChanger
path(this, value
);
729 if ( m_keyLocal
.Exists() ) {
730 if ( !m_keyLocal
.DeleteValue(path
.Name()) )
733 if ( m_keyLocal
.IsEmpty() ) {
734 wxString strKey
= GetPath().AfterLast(wxCONFIG_PATH_SEPARATOR
);
735 SetPath(".."); // changes m_keyLocal
736 return LocalKey().DeleteKey(strKey
);
743 bool wxRegConfig::DeleteGroup(const wxString
& key
)
745 wxConfigPathChanger
path(this, key
);
747 return m_keyLocal
.Exists() ? LocalKey().DeleteKey(path
.Name()) : TRUE
;
750 bool wxRegConfig::DeleteAll()
755 bool bOk
= m_keyLocalRoot
.DeleteSelf();
757 // make sure that we opened m_keyGlobalRoot and so it has a reasonable name:
758 // otherwise we will delete HKEY_CLASSES_ROOT recursively
759 if ( bOk
&& m_keyGlobalRoot
.IsOpened() )
760 bOk
= m_keyGlobalRoot
.DeleteSelf();