]>
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 // ----------------------------------------------------------------------------
63 IMPLEMENT_ABSTRACT_CLASS(wxRegConfig
, wxConfigBase
)
65 // create the config object which stores its data under HKCU\vendor\app and, if
66 // style & wxCONFIG_USE_GLOBAL_FILE, under HKLM\vendor\app
67 wxRegConfig::wxRegConfig(const wxString
& appName
, const wxString
& vendorName
,
68 const wxString
& strLocal
, const wxString
& strGlobal
,
70 : wxConfigBase(appName
, vendorName
, strLocal
, strGlobal
, style
)
74 bool bDoUseGlobal
= (style
& wxCONFIG_USE_GLOBAL_FILE
) != 0;
76 // the convention is to put the programs keys under <vendor>\<appname>
77 // (but it can be overriden by specifying the pathes explicitly in strLocal
79 if ( strLocal
.empty() || (strGlobal
.empty() && bDoUseGlobal
) )
81 if ( vendorName
.empty() )
84 strRoot
= wxTheApp
->GetVendorName();
91 // no '\\' needed if no vendor name
92 if ( !strRoot
.empty() )
97 if ( appName
.empty() )
99 wxCHECK_RET( wxTheApp
, wxT("No application name in wxRegConfig ctor!") );
100 strRoot
<< wxTheApp
->GetAppName();
107 //else: we don't need to do all the complicated stuff above
109 wxString str
= strLocal
.empty() ? strRoot
: strLocal
;
111 // as we're going to change the name of these keys fairly often and as
112 // there are only few of wxRegConfig objects (usually 1), we can allow
113 // ourselves to be generous and spend some memory to significantly improve
114 // performance of SetPath()
115 static const size_t MEMORY_PREALLOC
= 512;
117 m_keyLocalRoot
.ReserveMemoryForName(MEMORY_PREALLOC
);
118 m_keyLocal
.ReserveMemoryForName(MEMORY_PREALLOC
);
120 m_keyLocalRoot
.SetName(wxRegKey::HKCU
, SOFTWARE_KEY
+ str
);
121 m_keyLocal
.SetName(m_keyLocalRoot
, wxEmptyString
);
125 str
= strGlobal
.empty() ? strRoot
: strGlobal
;
127 m_keyGlobalRoot
.ReserveMemoryForName(MEMORY_PREALLOC
);
128 m_keyGlobal
.ReserveMemoryForName(MEMORY_PREALLOC
);
130 m_keyGlobalRoot
.SetName(wxRegKey::HKLM
, SOFTWARE_KEY
+ str
);
131 m_keyGlobal
.SetName(m_keyGlobalRoot
, wxEmptyString
);
134 // Create() will Open() if key already exists
135 m_keyLocalRoot
.Create();
137 // as it's the same key, Open() shouldn't fail (i.e. no need for Create())
140 // OTOH, this key may perfectly not exist, so suppress error messages the call
141 // to Open() might generate
145 m_keyGlobalRoot
.Open(wxRegKey::Read
);
146 m_keyGlobal
.Open(wxRegKey::Read
);
150 // ----------------------------------------------------------------------------
152 // ----------------------------------------------------------------------------
154 // this function is called a *lot* of times (as I learned after seeing from
155 // profiler output that it is called ~12000 times from Mahogany start up code!)
156 // so it is important to optimize it - in particular, avoid using generic
157 // string functions here and do everything manually because it is faster
159 // I still kept the old version to be able to check that the optimized code has
160 // the same output as the non optimized version.
161 void wxRegConfig::SetPath(const wxString
& strPath
)
163 // remember the old path
164 wxString strOldPath
= m_strPath
;
166 #ifdef WX_DEBUG_SET_PATH // non optimized version kept here for testing
167 wxString m_strPathAlt
;
170 wxArrayString aParts
;
172 // because GetPath() returns "" when we're at root, we must understand
173 // empty string as "/"
174 if ( strPath
.empty() || (strPath
[0] == wxCONFIG_PATH_SEPARATOR
) ) {
176 wxSplitPath(aParts
, strPath
);
179 // relative path, combine with current one
180 wxString strFullPath
= GetPath();
181 strFullPath
<< wxCONFIG_PATH_SEPARATOR
<< strPath
;
182 wxSplitPath(aParts
, strFullPath
);
185 // recombine path parts in one variable
187 m_strPathAlt
.Empty();
188 for ( size_t n
= 0; n
< aParts
.Count(); n
++ ) {
189 strRegPath
<< '\\' << aParts
[n
];
190 m_strPathAlt
<< wxCONFIG_PATH_SEPARATOR
<< aParts
[n
];
195 // check for the most common case first
196 if ( strPath
.empty() )
198 m_strPath
= wxCONFIG_PATH_SEPARATOR
;
202 // construct the full path
203 wxString strFullPath
;
204 if ( strPath
[0u] == wxCONFIG_PATH_SEPARATOR
)
207 strFullPath
= strPath
;
209 else // relative path
211 strFullPath
.reserve(2*m_strPath
.length());
213 strFullPath
<< m_strPath
;
214 if ( strFullPath
.Len() == 0 ||
215 strFullPath
.Last() != wxCONFIG_PATH_SEPARATOR
)
216 strFullPath
<< wxCONFIG_PATH_SEPARATOR
;
217 strFullPath
<< strPath
;
220 // simplify it: we need to handle ".." here
222 // count the total number of slashes we have to know if we can go upper
223 size_t totalSlashes
= 0;
225 // position of the last slash to be able to backtrack to it quickly if
226 // needed, but we set it to -1 if we don't have a valid position
228 // we only remember the last position which means that we handle ".."
229 // quite efficiently but not "../.." - however the latter should be
230 // much more rare, so it is probably ok
231 int posLastSlash
= -1;
233 const wxChar
*src
= strFullPath
.c_str();
234 size_t len
= strFullPath
.length();
235 const wxChar
*end
= src
+ len
;
237 wxStringBufferLength
buf(m_strPath
, len
);
241 for ( ; src
< end
; src
++, dst
++ )
243 if ( *src
== wxCONFIG_PATH_SEPARATOR
)
247 // note that we don't have to check for src < end here as
248 // *end == 0 so can't be '.'
249 if ( src
[1] == _T('.') && src
[2] == _T('.') &&
250 (src
+ 3 == end
|| src
[3] == wxCONFIG_PATH_SEPARATOR
) )
254 wxLogWarning(_("'%s' has extra '..', ignored."),
255 strFullPath
.c_str());
257 else // return to the previous path component
259 // do we already have its position?
260 if ( posLastSlash
== -1 )
262 // no, find it: note that we are sure to have one
263 // because totalSlashes > 0 so we don't have to
264 // check the boundary condition below
266 // this is more efficient than strrchr()
268 while ( *dst
!= wxCONFIG_PATH_SEPARATOR
)
273 else // the position of last slash was stored
276 dst
= start
+ posLastSlash
;
278 // invalidate posLastSlash
282 // we must have found a slash one way or another!
283 wxASSERT_MSG( *dst
== wxCONFIG_PATH_SEPARATOR
,
284 _T("error in wxRegConfig::SetPath") );
286 // stay at the same position
298 if ( (dst
== start
) || (dst
[-1] != wxCONFIG_PATH_SEPARATOR
) )
300 *dst
= wxCONFIG_PATH_SEPARATOR
;
302 posLastSlash
= dst
- start
;
306 else // previous char was a slash too
308 // squeeze several subsequent slashes into one: i.e.
309 // just ignore this one
314 else // normal character
321 // NUL terminate the string
322 if ( dst
[-1] == wxCONFIG_PATH_SEPARATOR
&& (dst
!= start
+ 1) )
324 // if it has a trailing slash we remove it unless it is the only
330 buf
.SetLength(dst
- start
);
333 #ifdef WX_DEBUG_SET_PATH
334 wxASSERT( m_strPath
== m_strPathAlt
);
337 if ( m_strPath
== strOldPath
)
340 // registry APIs want backslashes instead of slashes
342 if ( !m_strPath
.empty() )
344 size_t len
= m_strPath
.length();
346 const wxChar
*src
= m_strPath
.c_str();
347 wxStringBufferLength
buf(strRegPath
, len
);
350 const wxChar
*end
= src
+ len
;
351 for ( ; src
< end
; src
++, dst
++ )
353 if ( *src
== wxCONFIG_PATH_SEPARATOR
)
362 // this is not needed any longer as we don't create keys unnecessarily any
363 // more (now it is done on demand, i.e. only when they're going to contain
366 // as we create the registry key when SetPath(key) is done, we can be left
367 // with plenty of empty keys if this was only done to try to read some
368 // value which, in fact, doesn't exist - to prevent this from happening we
369 // automatically delete the old key if it was empty
370 if ( m_keyLocal
.Exists() && LocalKey().IsEmpty() )
372 m_keyLocal
.DeleteSelf();
376 // change current key(s)
377 m_keyLocal
.SetName(m_keyLocalRoot
, strRegPath
);
379 if ( GetStyle() & wxCONFIG_USE_GLOBAL_FILE
)
381 m_keyGlobal
.SetName(m_keyGlobalRoot
, strRegPath
);
384 m_keyGlobal
.Open(wxRegKey::Read
);
388 // ----------------------------------------------------------------------------
389 // enumeration (works only with current group)
390 // ----------------------------------------------------------------------------
393 We want to enumerate all local keys/values after the global ones, but, of
394 course, we don't want to repeat a key which appears locally as well as
397 We use the 15th bit of lIndex for distinction between global and local.
400 #define LOCAL_MASK 0x8000
401 #define IS_LOCAL_INDEX(l) (((l) & LOCAL_MASK) != 0)
403 bool wxRegConfig::GetFirstGroup(wxString
& str
, long& lIndex
) const
406 return GetNextGroup(str
, lIndex
);
409 bool wxRegConfig::GetNextGroup(wxString
& str
, long& lIndex
) const
411 // are we already enumerating local entries?
412 if ( m_keyGlobal
.IsOpened() && !IS_LOCAL_INDEX(lIndex
) ) {
413 // try to find a global entry which doesn't appear locally
414 while ( m_keyGlobal
.GetNextKey(str
, lIndex
) ) {
415 if ( !m_keyLocal
.Exists() || !LocalKey().HasSubKey(str
) ) {
416 // ok, found one - return it
421 // no more global entries
422 lIndex
|= LOCAL_MASK
;
425 // if we don't have the key at all, don't try to enumerate anything under it
426 if ( !m_keyLocal
.Exists() )
429 // much easier with local entries: get the next one we find
430 // (don't forget to clear our flag bit and set it again later)
431 lIndex
&= ~LOCAL_MASK
;
432 bool bOk
= LocalKey().GetNextKey(str
, lIndex
);
433 lIndex
|= LOCAL_MASK
;
438 bool wxRegConfig::GetFirstEntry(wxString
& str
, long& lIndex
) const
441 return GetNextEntry(str
, lIndex
);
444 bool wxRegConfig::GetNextEntry(wxString
& str
, long& lIndex
) const
446 // are we already enumerating local entries?
447 if ( m_keyGlobal
.IsOpened() && !IS_LOCAL_INDEX(lIndex
) ) {
448 // try to find a global entry which doesn't appear locally
449 while ( m_keyGlobal
.GetNextValue(str
, lIndex
) ) {
450 if ( !m_keyLocal
.Exists() || !LocalKey().HasValue(str
) ) {
451 // ok, found one - return it
456 // no more global entries
457 lIndex
|= LOCAL_MASK
;
460 // if we don't have the key at all, don't try to enumerate anything under it
461 if ( !m_keyLocal
.Exists() )
464 // much easier with local entries: get the next one we find
465 // (don't forget to clear our flag bit and set it again later)
466 lIndex
&= ~LOCAL_MASK
;
467 bool bOk
= LocalKey().GetNextValue(str
, lIndex
);
468 lIndex
|= LOCAL_MASK
;
473 size_t wxRegConfig::GetNumberOfEntries(bool WXUNUSED(bRecursive
)) const
480 bool bCont
= ((wxRegConfig
*)this)->GetFirstEntry(str
, l
);
484 bCont
= ((wxRegConfig
*)this)->GetNextEntry(str
, l
);
490 size_t wxRegConfig::GetNumberOfGroups(bool WXUNUSED(bRecursive
)) const
497 bool bCont
= ((wxRegConfig
*)this)->GetFirstGroup(str
, l
);
501 bCont
= ((wxRegConfig
*)this)->GetNextGroup(str
, l
);
507 // ----------------------------------------------------------------------------
508 // tests for existence
509 // ----------------------------------------------------------------------------
511 bool wxRegConfig::HasGroup(const wxString
& key
) const
513 wxConfigPathChanger
path(this, key
);
515 wxString
strName(path
.Name());
517 return (m_keyLocal
.Exists() && LocalKey().HasSubKey(strName
)) ||
518 m_keyGlobal
.HasSubKey(strName
);
521 bool wxRegConfig::HasEntry(const wxString
& key
) const
523 wxConfigPathChanger
path(this, key
);
525 wxString
strName(path
.Name());
527 return (m_keyLocal
.Exists() && LocalKey().HasValue(strName
)) ||
528 m_keyGlobal
.HasValue(strName
);
531 wxConfigBase::EntryType
wxRegConfig::GetEntryType(const wxString
& key
) const
533 wxConfigPathChanger
path(this, key
);
535 wxString
strName(path
.Name());
538 if ( m_keyLocal
.Exists() && LocalKey().HasValue(strName
) )
539 isNumeric
= m_keyLocal
.IsNumericValue(strName
);
540 else if ( m_keyGlobal
.HasValue(strName
) )
541 isNumeric
= m_keyGlobal
.IsNumericValue(strName
);
543 return wxConfigBase::Type_Unknown
;
545 return isNumeric
? wxConfigBase::Type_Integer
: wxConfigBase::Type_String
;
548 // ----------------------------------------------------------------------------
550 // ----------------------------------------------------------------------------
552 bool wxRegConfig::DoReadString(const wxString
& key
, wxString
*pStr
) const
554 wxCHECK_MSG( pStr
, false, _T("wxRegConfig::Read(): NULL param") );
556 wxConfigPathChanger
path(this, key
);
558 bool bQueryGlobal
= true;
560 // if immutable key exists in global key we must check that it's not
561 // overriden by the local key with the same name
562 if ( IsImmutable(path
.Name()) ) {
563 if ( TryGetValue(m_keyGlobal
, path
.Name(), *pStr
) ) {
564 if ( m_keyLocal
.Exists() && LocalKey().HasValue(path
.Name()) ) {
565 wxLogWarning(wxT("User value for immutable key '%s' ignored."),
566 path
.Name().c_str());
572 // don't waste time - it's not there anyhow
573 bQueryGlobal
= false;
577 // first try local key
578 if ( (m_keyLocal
.Exists() && TryGetValue(LocalKey(), path
.Name(), *pStr
)) ||
579 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), *pStr
)) ) {
586 // this exactly reproduces the string version above except for ExpandEnvVars(),
587 // we really should avoid this code duplication somehow...
589 bool wxRegConfig::DoReadLong(const wxString
& key
, long *plResult
) const
591 wxCHECK_MSG( plResult
, false, _T("wxRegConfig::Read(): NULL param") );
593 wxConfigPathChanger
path(this, key
);
595 bool bQueryGlobal
= true;
597 // if immutable key exists in global key we must check that it's not
598 // overriden by the local key with the same name
599 if ( IsImmutable(path
.Name()) ) {
600 if ( TryGetValue(m_keyGlobal
, path
.Name(), plResult
) ) {
601 if ( m_keyLocal
.Exists() && LocalKey().HasValue(path
.Name()) ) {
602 wxLogWarning(wxT("User value for immutable key '%s' ignored."),
603 path
.Name().c_str());
609 // don't waste time - it's not there anyhow
610 bQueryGlobal
= false;
614 // first try local key
615 if ( (m_keyLocal
.Exists() && TryGetValue(LocalKey(), path
.Name(), plResult
)) ||
616 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), plResult
)) ) {
623 bool wxRegConfig::DoWriteString(const wxString
& key
, const wxString
& szValue
)
625 wxConfigPathChanger
path(this, key
);
627 if ( IsImmutable(path
.Name()) ) {
628 wxLogError(wxT("Can't change immutable entry '%s'."), path
.Name().c_str());
632 return LocalKey().SetValue(path
.Name(), szValue
);
635 bool wxRegConfig::DoWriteLong(const wxString
& key
, long lValue
)
637 wxConfigPathChanger
path(this, key
);
639 if ( IsImmutable(path
.Name()) ) {
640 wxLogError(wxT("Can't change immutable entry '%s'."), path
.Name().c_str());
644 return LocalKey().SetValue(path
.Name(), lValue
);
647 // ----------------------------------------------------------------------------
649 // ----------------------------------------------------------------------------
651 bool wxRegConfig::RenameEntry(const wxString
& oldName
, const wxString
& newName
)
653 // check that the old entry exists...
654 if ( !HasEntry(oldName
) )
657 // and that the new one doesn't
658 if ( HasEntry(newName
) )
661 return m_keyLocal
.RenameValue(oldName
, newName
);
664 bool wxRegConfig::RenameGroup(const wxString
& oldName
, const wxString
& newName
)
666 // check that the old group exists...
667 if ( !HasGroup(oldName
) )
670 // and that the new one doesn't
671 if ( HasGroup(newName
) )
674 return wxRegKey(m_keyLocal
, oldName
).Rename(newName
);
677 // ----------------------------------------------------------------------------
679 // ----------------------------------------------------------------------------
681 bool wxRegConfig::DeleteEntry(const wxString
& value
, bool bGroupIfEmptyAlso
)
683 wxConfigPathChanger
path(this, value
);
685 if ( m_keyLocal
.Exists() ) {
686 if ( !m_keyLocal
.DeleteValue(path
.Name()) )
689 if ( bGroupIfEmptyAlso
&& m_keyLocal
.IsEmpty() ) {
690 wxString strKey
= GetPath().AfterLast(wxCONFIG_PATH_SEPARATOR
);
691 SetPath(_T("..")); // changes m_keyLocal
692 return LocalKey().DeleteKey(strKey
);
699 bool wxRegConfig::DeleteGroup(const wxString
& key
)
701 wxConfigPathChanger
path(this, RemoveTrailingSeparator(key
));
703 if ( !m_keyLocal
.Exists() )
709 if ( !LocalKey().DeleteKey(path
.Name()) )
712 path
.UpdateIfDeleted();
717 bool wxRegConfig::DeleteAll()
722 bool bOk
= m_keyLocalRoot
.DeleteSelf();
724 // make sure that we opened m_keyGlobalRoot and so it has a reasonable name:
725 // otherwise we will delete HKEY_CLASSES_ROOT recursively
726 if ( bOk
&& m_keyGlobalRoot
.IsOpened() )
727 bOk
= m_keyGlobalRoot
.DeleteSelf();
732 #endif // wxUSE_CONFIG