]>
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 licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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"
36 #include "wx/msw/registry.h"
37 #include "wx/msw/regconf.h"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 // we put our data in HKLM\SOFTWARE_KEY\appname
44 #define SOFTWARE_KEY wxString(wxT("Software\\"))
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 // get the value if the key is opened and it exists
51 bool TryGetValue(const wxRegKey
& key
, const wxString
& str
, wxString
& strVal
)
53 return key
.IsOpened() && key
.HasValue(str
) && key
.QueryValue(str
, strVal
);
56 bool TryGetValue(const wxRegKey
& key
, const wxString
& str
, long *plVal
)
58 return key
.IsOpened() && key
.HasValue(str
) && key
.QueryValue(str
, plVal
);
61 // ============================================================================
63 // ============================================================================
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 // create the config object which stores its data under HKCU\vendor\app and, if
70 // style & wxCONFIG_USE_GLOBAL_FILE, under HKLM\vendor\app
71 wxRegConfig::wxRegConfig(const wxString
& appName
, const wxString
& vendorName
,
72 const wxString
& strLocal
, const wxString
& strGlobal
,
74 : wxConfigBase(appName
, vendorName
, strLocal
, strGlobal
, style
)
78 bool bDoUseGlobal
= (style
& wxCONFIG_USE_GLOBAL_FILE
) != 0;
80 // the convention is to put the programs keys under <vendor>\<appname>
81 // (but it can be overriden by specifying the pathes explicitly in strLocal
83 if ( strLocal
.IsEmpty() || (strGlobal
.IsEmpty() && bDoUseGlobal
) )
85 if ( vendorName
.IsEmpty() )
88 strRoot
= wxTheApp
->GetVendorName();
95 // no '\\' needed if no vendor name
96 if ( !strRoot
.IsEmpty() )
101 if ( appName
.IsEmpty() )
103 wxCHECK_RET( wxTheApp
, wxT("No application name in wxRegConfig ctor!") );
104 strRoot
<< wxTheApp
->GetAppName();
111 //else: we don't need to do all the complicated stuff above
113 wxString str
= strLocal
.IsEmpty() ? strRoot
: strLocal
;
115 // as we're going to change the name of these keys fairly often and as
116 // there are only few of wxRegConfig objects (usually 1), we can allow
117 // ourselves to be generous and spend some memory to significantly improve
118 // performance of SetPath()
119 static const size_t MEMORY_PREALLOC
= 512;
121 m_keyLocalRoot
.ReserveMemoryForName(MEMORY_PREALLOC
);
122 m_keyLocal
.ReserveMemoryForName(MEMORY_PREALLOC
);
124 m_keyLocalRoot
.SetName(wxRegKey::HKCU
, SOFTWARE_KEY
+ str
);
125 m_keyLocal
.SetName(m_keyLocalRoot
, wxEmptyString
);
129 str
= strGlobal
.IsEmpty() ? strRoot
: strGlobal
;
131 m_keyGlobalRoot
.ReserveMemoryForName(MEMORY_PREALLOC
);
132 m_keyGlobal
.ReserveMemoryForName(MEMORY_PREALLOC
);
134 m_keyGlobalRoot
.SetName(wxRegKey::HKLM
, SOFTWARE_KEY
+ str
);
135 m_keyGlobal
.SetName(m_keyGlobalRoot
, wxEmptyString
);
138 // Create() will Open() if key already exists
139 m_keyLocalRoot
.Create();
141 // as it's the same key, Open() shouldn't fail (i.e. no need for Create())
144 // OTOH, this key may perfectly not exist, so suppress error messages the call
145 // to Open() might generate
149 m_keyGlobalRoot
.Open(wxRegKey::Read
);
150 m_keyGlobal
.Open(wxRegKey::Read
);
154 wxRegConfig::~wxRegConfig()
156 // nothing to do - key will be closed in their dtors
159 // ----------------------------------------------------------------------------
161 // ----------------------------------------------------------------------------
163 // this function is called a *lot* of times (as I learned after seeing from
164 // profiler output that it is called ~12000 times from Mahogany start up code!)
165 // so it is important to optimize it - in particular, avoid using generic
166 // string functions here and do everything manually because it is faster
168 // I still kept the old version to be able to check that the optimized code has
169 // the same output as the non optimized version.
170 void wxRegConfig::SetPath(const wxString
& strPath
)
172 // remember the old path
173 wxString strOldPath
= m_strPath
;
175 #ifdef WX_DEBUG_SET_PATH // non optimized version kept here for testing
176 wxString m_strPathAlt
;
179 wxArrayString aParts
;
181 // because GetPath() returns "" when we're at root, we must understand
182 // empty string as "/"
183 if ( strPath
.IsEmpty() || (strPath
[0] == wxCONFIG_PATH_SEPARATOR
) ) {
185 wxSplitPath(aParts
, strPath
);
188 // relative path, combine with current one
189 wxString strFullPath
= GetPath();
190 strFullPath
<< wxCONFIG_PATH_SEPARATOR
<< strPath
;
191 wxSplitPath(aParts
, strFullPath
);
194 // recombine path parts in one variable
196 m_strPathAlt
.Empty();
197 for ( size_t n
= 0; n
< aParts
.Count(); n
++ ) {
198 strRegPath
<< '\\' << aParts
[n
];
199 m_strPathAlt
<< wxCONFIG_PATH_SEPARATOR
<< aParts
[n
];
204 // check for the most common case first
205 if ( strPath
.empty() )
207 m_strPath
= wxCONFIG_PATH_SEPARATOR
;
211 // construct the full path
212 wxString strFullPath
;
213 if ( strPath
[0u] == wxCONFIG_PATH_SEPARATOR
)
216 strFullPath
= strPath
;
218 else // relative path
220 strFullPath
.reserve(2*m_strPath
.length());
222 strFullPath
<< m_strPath
;
223 if ( strFullPath
.Len() == 0 ||
224 strFullPath
.Last() != wxCONFIG_PATH_SEPARATOR
)
225 strFullPath
<< wxCONFIG_PATH_SEPARATOR
;
226 strFullPath
<< strPath
;
229 // simplify it: we need to handle ".." here
231 // count the total number of slashes we have to know if we can go upper
232 size_t totalSlashes
= 0;
234 // position of the last slash to be able to backtrack to it quickly if
235 // needed, but we set it to -1 if we don't have a valid position
237 // we only remember the last position which means that we handle ".."
238 // quite efficiently but not "../.." - however the latter should be
239 // much more rare, so it is probably ok
240 int posLastSlash
= -1;
242 const wxChar
*src
= strFullPath
.c_str();
243 size_t len
= strFullPath
.length();
244 const wxChar
*end
= src
+ len
;
246 wxStringBufferLength
buf(m_strPath
, len
);
250 for ( ; src
< end
; src
++, dst
++ )
252 if ( *src
== wxCONFIG_PATH_SEPARATOR
)
256 // note that we don't have to check for src < end here as
257 // *end == 0 so can't be '.'
258 if ( src
[1] == _T('.') && src
[2] == _T('.') &&
259 (src
+ 3 == end
|| src
[3] == wxCONFIG_PATH_SEPARATOR
) )
263 wxLogWarning(_("'%s' has extra '..', ignored."),
264 strFullPath
.c_str());
266 else // return to the previous path component
268 // do we already have its position?
269 if ( posLastSlash
== -1 )
271 // no, find it: note that we are sure to have one
272 // because totalSlashes > 0 so we don't have to
273 // check the boundary condition below
275 // this is more efficient than strrchr()
277 while ( *dst
!= wxCONFIG_PATH_SEPARATOR
)
282 else // the position of last slash was stored
285 dst
= start
+ posLastSlash
;
287 // invalidate posLastSlash
291 // we must have found a slash one way or another!
292 wxASSERT_MSG( *dst
== wxCONFIG_PATH_SEPARATOR
,
293 _T("error in wxRegConfig::SetPath") );
295 // stay at the same position
307 if ( (dst
== start
) || (dst
[-1] != wxCONFIG_PATH_SEPARATOR
) )
309 *dst
= wxCONFIG_PATH_SEPARATOR
;
311 posLastSlash
= dst
- start
;
315 else // previous char was a slash too
317 // squeeze several subsequent slashes into one: i.e.
318 // just ignore this one
323 else // normal character
330 // NUL terminate the string
331 if ( dst
[-1] == wxCONFIG_PATH_SEPARATOR
&& (dst
!= start
+ 1) )
333 // if it has a trailing slash we remove it unless it is the only
339 buf
.SetLength(dst
- start
);
342 #ifdef WX_DEBUG_SET_PATH
343 wxASSERT( m_strPath
== m_strPathAlt
);
346 if ( m_strPath
== strOldPath
)
349 // registry APIs want backslashes instead of slashes
351 if ( !m_strPath
.empty() )
353 size_t len
= m_strPath
.length();
355 const wxChar
*src
= m_strPath
.c_str();
356 wxStringBufferLength
buf(strRegPath
, len
);
359 const wxChar
*end
= src
+ len
;
360 for ( ; src
< end
; src
++, dst
++ )
362 if ( *src
== wxCONFIG_PATH_SEPARATOR
)
371 // this is not needed any longer as we don't create keys unnecessarily any
372 // more (now it is done on demand, i.e. only when they're going to contain
375 // as we create the registry key when SetPath(key) is done, we can be left
376 // with plenty of empty keys if this was only done to try to read some
377 // value which, in fact, doesn't exist - to prevent this from happening we
378 // automatically delete the old key if it was empty
379 if ( m_keyLocal
.Exists() && LocalKey().IsEmpty() )
381 m_keyLocal
.DeleteSelf();
385 // change current key(s)
386 m_keyLocal
.SetName(m_keyLocalRoot
, strRegPath
);
388 if ( GetStyle() & wxCONFIG_USE_GLOBAL_FILE
)
390 m_keyGlobal
.SetName(m_keyGlobalRoot
, strRegPath
);
393 m_keyGlobal
.Open(wxRegKey::Read
);
397 // ----------------------------------------------------------------------------
398 // enumeration (works only with current group)
399 // ----------------------------------------------------------------------------
402 We want to enumerate all local keys/values after the global ones, but, of
403 course, we don't want to repeat a key which appears locally as well as
406 We use the 15th bit of lIndex for distinction between global and local.
409 #define LOCAL_MASK 0x8000
410 #define IS_LOCAL_INDEX(l) (((l) & LOCAL_MASK) != 0)
412 bool wxRegConfig::GetFirstGroup(wxString
& str
, long& lIndex
) const
415 return GetNextGroup(str
, lIndex
);
418 bool wxRegConfig::GetNextGroup(wxString
& str
, long& lIndex
) const
420 // are we already enumerating local entries?
421 if ( m_keyGlobal
.IsOpened() && !IS_LOCAL_INDEX(lIndex
) ) {
422 // try to find a global entry which doesn't appear locally
423 while ( m_keyGlobal
.GetNextKey(str
, lIndex
) ) {
424 if ( !m_keyLocal
.Exists() || !LocalKey().HasSubKey(str
) ) {
425 // ok, found one - return it
430 // no more global entries
431 lIndex
|= LOCAL_MASK
;
434 // if we don't have the key at all, don't try to enumerate anything under it
435 if ( !m_keyLocal
.Exists() )
438 // much easier with local entries: get the next one we find
439 // (don't forget to clear our flag bit and set it again later)
440 lIndex
&= ~LOCAL_MASK
;
441 bool bOk
= LocalKey().GetNextKey(str
, lIndex
);
442 lIndex
|= LOCAL_MASK
;
447 bool wxRegConfig::GetFirstEntry(wxString
& str
, long& lIndex
) const
450 return GetNextEntry(str
, lIndex
);
453 bool wxRegConfig::GetNextEntry(wxString
& str
, long& lIndex
) const
455 // are we already enumerating local entries?
456 if ( m_keyGlobal
.IsOpened() && !IS_LOCAL_INDEX(lIndex
) ) {
457 // try to find a global entry which doesn't appear locally
458 while ( m_keyGlobal
.GetNextValue(str
, lIndex
) ) {
459 if ( !m_keyLocal
.Exists() || !LocalKey().HasValue(str
) ) {
460 // ok, found one - return it
465 // no more global entries
466 lIndex
|= LOCAL_MASK
;
469 // if we don't have the key at all, don't try to enumerate anything under it
470 if ( !m_keyLocal
.Exists() )
473 // much easier with local entries: get the next one we find
474 // (don't forget to clear our flag bit and set it again later)
475 lIndex
&= ~LOCAL_MASK
;
476 bool bOk
= LocalKey().GetNextValue(str
, lIndex
);
477 lIndex
|= LOCAL_MASK
;
482 size_t wxRegConfig::GetNumberOfEntries(bool WXUNUSED(bRecursive
)) const
489 bool bCont
= ((wxRegConfig
*)this)->GetFirstEntry(str
, l
);
493 bCont
= ((wxRegConfig
*)this)->GetNextEntry(str
, l
);
499 size_t wxRegConfig::GetNumberOfGroups(bool WXUNUSED(bRecursive
)) const
506 bool bCont
= ((wxRegConfig
*)this)->GetFirstGroup(str
, l
);
510 bCont
= ((wxRegConfig
*)this)->GetNextGroup(str
, l
);
516 // ----------------------------------------------------------------------------
517 // tests for existence
518 // ----------------------------------------------------------------------------
520 bool wxRegConfig::HasGroup(const wxString
& key
) const
522 wxConfigPathChanger
path(this, key
);
524 wxString
strName(path
.Name());
526 return (m_keyLocal
.Exists() && LocalKey().HasSubKey(strName
)) ||
527 m_keyGlobal
.HasSubKey(strName
);
530 bool wxRegConfig::HasEntry(const wxString
& key
) const
532 wxConfigPathChanger
path(this, key
);
534 wxString
strName(path
.Name());
536 return (m_keyLocal
.Exists() && LocalKey().HasValue(strName
)) ||
537 m_keyGlobal
.HasValue(strName
);
540 wxConfigBase::EntryType
wxRegConfig::GetEntryType(const wxString
& key
) const
542 wxConfigPathChanger
path(this, key
);
544 wxString
strName(path
.Name());
547 if ( m_keyLocal
.Exists() && LocalKey().HasValue(strName
) )
548 isNumeric
= m_keyLocal
.IsNumericValue(strName
);
549 else if ( m_keyGlobal
.HasValue(strName
) )
550 isNumeric
= m_keyGlobal
.IsNumericValue(strName
);
552 return wxConfigBase::Type_Unknown
;
554 return isNumeric
? wxConfigBase::Type_Integer
: wxConfigBase::Type_String
;
557 // ----------------------------------------------------------------------------
559 // ----------------------------------------------------------------------------
561 bool wxRegConfig::DoReadString(const wxString
& key
, wxString
*pStr
) const
563 wxCHECK_MSG( pStr
, FALSE
, _T("wxRegConfig::Read(): NULL param") );
565 wxConfigPathChanger
path(this, key
);
567 bool bQueryGlobal
= TRUE
;
569 // if immutable key exists in global key we must check that it's not
570 // overriden by the local key with the same name
571 if ( IsImmutable(path
.Name()) ) {
572 if ( TryGetValue(m_keyGlobal
, path
.Name(), *pStr
) ) {
573 if ( m_keyLocal
.Exists() && LocalKey().HasValue(path
.Name()) ) {
574 wxLogWarning(wxT("User value for immutable key '%s' ignored."),
575 path
.Name().c_str());
581 // don't waste time - it's not there anyhow
582 bQueryGlobal
= FALSE
;
586 // first try local key
587 if ( (m_keyLocal
.Exists() && TryGetValue(LocalKey(), path
.Name(), *pStr
)) ||
588 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), *pStr
)) ) {
595 // this exactly reproduces the string version above except for ExpandEnvVars(),
596 // we really should avoid this code duplication somehow...
598 bool wxRegConfig::DoReadLong(const wxString
& key
, long *plResult
) const
600 wxCHECK_MSG( plResult
, FALSE
, _T("wxRegConfig::Read(): NULL param") );
602 wxConfigPathChanger
path(this, key
);
604 bool bQueryGlobal
= TRUE
;
606 // if immutable key exists in global key we must check that it's not
607 // overriden by the local key with the same name
608 if ( IsImmutable(path
.Name()) ) {
609 if ( TryGetValue(m_keyGlobal
, path
.Name(), plResult
) ) {
610 if ( m_keyLocal
.Exists() && LocalKey().HasValue(path
.Name()) ) {
611 wxLogWarning(wxT("User value for immutable key '%s' ignored."),
612 path
.Name().c_str());
618 // don't waste time - it's not there anyhow
619 bQueryGlobal
= FALSE
;
623 // first try local key
624 if ( (m_keyLocal
.Exists() && TryGetValue(LocalKey(), path
.Name(), plResult
)) ||
625 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), plResult
)) ) {
632 bool wxRegConfig::DoWriteString(const wxString
& key
, const wxString
& szValue
)
634 wxConfigPathChanger
path(this, key
);
636 if ( IsImmutable(path
.Name()) ) {
637 wxLogError(wxT("Can't change immutable entry '%s'."), path
.Name().c_str());
641 return LocalKey().SetValue(path
.Name(), szValue
);
644 bool wxRegConfig::DoWriteLong(const wxString
& key
, long lValue
)
646 wxConfigPathChanger
path(this, key
);
648 if ( IsImmutable(path
.Name()) ) {
649 wxLogError(wxT("Can't change immutable entry '%s'."), path
.Name().c_str());
653 return LocalKey().SetValue(path
.Name(), lValue
);
656 // ----------------------------------------------------------------------------
658 // ----------------------------------------------------------------------------
660 bool wxRegConfig::RenameEntry(const wxString
& oldName
, const wxString
& newName
)
662 // check that the old entry exists...
663 if ( !HasEntry(oldName
) )
666 // and that the new one doesn't
667 if ( HasEntry(newName
) )
670 return m_keyLocal
.RenameValue(oldName
, newName
);
673 bool wxRegConfig::RenameGroup(const wxString
& oldName
, const wxString
& newName
)
675 // check that the old group exists...
676 if ( !HasGroup(oldName
) )
679 // and that the new one doesn't
680 if ( HasGroup(newName
) )
683 return wxRegKey(m_keyLocal
, oldName
).Rename(newName
);
686 // ----------------------------------------------------------------------------
688 // ----------------------------------------------------------------------------
690 bool wxRegConfig::DeleteEntry(const wxString
& value
, bool bGroupIfEmptyAlso
)
692 wxConfigPathChanger
path(this, value
);
694 if ( m_keyLocal
.Exists() ) {
695 if ( !m_keyLocal
.DeleteValue(path
.Name()) )
698 if ( bGroupIfEmptyAlso
&& m_keyLocal
.IsEmpty() ) {
699 wxString strKey
= GetPath().AfterLast(wxCONFIG_PATH_SEPARATOR
);
700 SetPath(_T("..")); // changes m_keyLocal
701 return LocalKey().DeleteKey(strKey
);
708 bool wxRegConfig::DeleteGroup(const wxString
& key
)
710 wxConfigPathChanger
path(this, key
);
712 return m_keyLocal
.Exists() ? LocalKey().DeleteKey(path
.Name()) : TRUE
;
715 bool wxRegConfig::DeleteAll()
720 bool bOk
= m_keyLocalRoot
.DeleteSelf();
722 // make sure that we opened m_keyGlobalRoot and so it has a reasonable name:
723 // otherwise we will delete HKEY_CLASSES_ROOT recursively
724 if ( bOk
&& m_keyGlobalRoot
.IsOpened() )
725 bOk
= m_keyGlobalRoot
.DeleteSelf();