]>
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 // ----------------------------------------------------------------------------
156 // ----------------------------------------------------------------------------
158 // this function is called a *lot* of times (as I learned after seeing from
159 // profiler output that it is called ~12000 times from Mahogany start up code!)
160 // so it is important to optimize it - in particular, avoid using generic
161 // string functions here and do everything manually because it is faster
163 // I still kept the old version to be able to check that the optimized code has
164 // the same output as the non optimized version.
165 void wxRegConfig::SetPath(const wxString
& strPath
)
167 // remember the old path
168 wxString strOldPath
= m_strPath
;
170 #ifdef WX_DEBUG_SET_PATH // non optimized version kept here for testing
171 wxString m_strPathAlt
;
174 wxArrayString aParts
;
176 // because GetPath() returns "" when we're at root, we must understand
177 // empty string as "/"
178 if ( strPath
.IsEmpty() || (strPath
[0] == wxCONFIG_PATH_SEPARATOR
) ) {
180 wxSplitPath(aParts
, strPath
);
183 // relative path, combine with current one
184 wxString strFullPath
= GetPath();
185 strFullPath
<< wxCONFIG_PATH_SEPARATOR
<< strPath
;
186 wxSplitPath(aParts
, strFullPath
);
189 // recombine path parts in one variable
191 m_strPathAlt
.Empty();
192 for ( size_t n
= 0; n
< aParts
.Count(); n
++ ) {
193 strRegPath
<< '\\' << aParts
[n
];
194 m_strPathAlt
<< wxCONFIG_PATH_SEPARATOR
<< aParts
[n
];
199 // check for the most common case first
200 if ( strPath
.empty() )
202 m_strPath
= wxCONFIG_PATH_SEPARATOR
;
206 // construct the full path
207 wxString strFullPath
;
208 if ( strPath
[0u] == wxCONFIG_PATH_SEPARATOR
)
211 strFullPath
= strPath
;
213 else // relative path
215 strFullPath
.reserve(2*m_strPath
.length());
217 strFullPath
<< m_strPath
;
218 if ( strFullPath
.Len() == 0 ||
219 strFullPath
.Last() != wxCONFIG_PATH_SEPARATOR
)
220 strFullPath
<< wxCONFIG_PATH_SEPARATOR
;
221 strFullPath
<< strPath
;
224 // simplify it: we need to handle ".." here
226 // count the total number of slashes we have to know if we can go upper
227 size_t totalSlashes
= 0;
229 // position of the last slash to be able to backtrack to it quickly if
230 // needed, but we set it to -1 if we don't have a valid position
232 // we only remember the last position which means that we handle ".."
233 // quite efficiently but not "../.." - however the latter should be
234 // much more rare, so it is probably ok
235 int posLastSlash
= -1;
237 const wxChar
*src
= strFullPath
.c_str();
238 size_t len
= strFullPath
.length();
239 const wxChar
*end
= src
+ len
;
241 wxStringBufferLength
buf(m_strPath
, len
);
245 for ( ; src
< end
; src
++, dst
++ )
247 if ( *src
== wxCONFIG_PATH_SEPARATOR
)
251 // note that we don't have to check for src < end here as
252 // *end == 0 so can't be '.'
253 if ( src
[1] == _T('.') && src
[2] == _T('.') &&
254 (src
+ 3 == end
|| src
[3] == wxCONFIG_PATH_SEPARATOR
) )
258 wxLogWarning(_("'%s' has extra '..', ignored."),
259 strFullPath
.c_str());
261 else // return to the previous path component
263 // do we already have its position?
264 if ( posLastSlash
== -1 )
266 // no, find it: note that we are sure to have one
267 // because totalSlashes > 0 so we don't have to
268 // check the boundary condition below
270 // 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 // we must have found a slash one way or another!
287 wxASSERT_MSG( *dst
== wxCONFIG_PATH_SEPARATOR
,
288 _T("error in wxRegConfig::SetPath") );
290 // stay at the same position
302 if ( (dst
== start
) || (dst
[-1] != wxCONFIG_PATH_SEPARATOR
) )
304 *dst
= wxCONFIG_PATH_SEPARATOR
;
306 posLastSlash
= dst
- start
;
310 else // previous char was a slash too
312 // squeeze several subsequent slashes into one: i.e.
313 // just ignore this one
318 else // normal character
325 // NUL terminate the string
326 if ( dst
[-1] == wxCONFIG_PATH_SEPARATOR
&& (dst
!= start
+ 1) )
328 // if it has a trailing slash we remove it unless it is the only
334 buf
.SetLength(dst
- start
);
337 #ifdef WX_DEBUG_SET_PATH
338 wxASSERT( m_strPath
== m_strPathAlt
);
341 if ( m_strPath
== strOldPath
)
344 // registry APIs want backslashes instead of slashes
346 if ( !m_strPath
.empty() )
348 size_t len
= m_strPath
.length();
350 const wxChar
*src
= m_strPath
.c_str();
351 wxStringBufferLength
buf(strRegPath
, len
);
354 const wxChar
*end
= src
+ len
;
355 for ( ; src
< end
; src
++, dst
++ )
357 if ( *src
== wxCONFIG_PATH_SEPARATOR
)
366 // this is not needed any longer as we don't create keys unnecessarily any
367 // more (now it is done on demand, i.e. only when they're going to contain
370 // as we create the registry key when SetPath(key) is done, we can be left
371 // with plenty of empty keys if this was only done to try to read some
372 // value which, in fact, doesn't exist - to prevent this from happening we
373 // automatically delete the old key if it was empty
374 if ( m_keyLocal
.Exists() && LocalKey().IsEmpty() )
376 m_keyLocal
.DeleteSelf();
380 // change current key(s)
381 m_keyLocal
.SetName(m_keyLocalRoot
, strRegPath
);
383 if ( GetStyle() & wxCONFIG_USE_GLOBAL_FILE
)
385 m_keyGlobal
.SetName(m_keyGlobalRoot
, strRegPath
);
388 m_keyGlobal
.Open(wxRegKey::Read
);
392 // ----------------------------------------------------------------------------
393 // enumeration (works only with current group)
394 // ----------------------------------------------------------------------------
397 We want to enumerate all local keys/values after the global ones, but, of
398 course, we don't want to repeat a key which appears locally as well as
401 We use the 15th bit of lIndex for distinction between global and local.
404 #define LOCAL_MASK 0x8000
405 #define IS_LOCAL_INDEX(l) (((l) & LOCAL_MASK) != 0)
407 bool wxRegConfig::GetFirstGroup(wxString
& str
, long& lIndex
) const
410 return GetNextGroup(str
, lIndex
);
413 bool wxRegConfig::GetNextGroup(wxString
& str
, long& lIndex
) const
415 // are we already enumerating local entries?
416 if ( m_keyGlobal
.IsOpened() && !IS_LOCAL_INDEX(lIndex
) ) {
417 // try to find a global entry which doesn't appear locally
418 while ( m_keyGlobal
.GetNextKey(str
, lIndex
) ) {
419 if ( !m_keyLocal
.Exists() || !LocalKey().HasSubKey(str
) ) {
420 // ok, found one - return it
425 // no more global entries
426 lIndex
|= LOCAL_MASK
;
429 // if we don't have the key at all, don't try to enumerate anything under it
430 if ( !m_keyLocal
.Exists() )
433 // much easier with local entries: get the next one we find
434 // (don't forget to clear our flag bit and set it again later)
435 lIndex
&= ~LOCAL_MASK
;
436 bool bOk
= LocalKey().GetNextKey(str
, lIndex
);
437 lIndex
|= LOCAL_MASK
;
442 bool wxRegConfig::GetFirstEntry(wxString
& str
, long& lIndex
) const
445 return GetNextEntry(str
, lIndex
);
448 bool wxRegConfig::GetNextEntry(wxString
& str
, long& lIndex
) const
450 // are we already enumerating local entries?
451 if ( m_keyGlobal
.IsOpened() && !IS_LOCAL_INDEX(lIndex
) ) {
452 // try to find a global entry which doesn't appear locally
453 while ( m_keyGlobal
.GetNextValue(str
, lIndex
) ) {
454 if ( !m_keyLocal
.Exists() || !LocalKey().HasValue(str
) ) {
455 // ok, found one - return it
460 // no more global entries
461 lIndex
|= LOCAL_MASK
;
464 // if we don't have the key at all, don't try to enumerate anything under it
465 if ( !m_keyLocal
.Exists() )
468 // much easier with local entries: get the next one we find
469 // (don't forget to clear our flag bit and set it again later)
470 lIndex
&= ~LOCAL_MASK
;
471 bool bOk
= LocalKey().GetNextValue(str
, lIndex
);
472 lIndex
|= LOCAL_MASK
;
477 size_t wxRegConfig::GetNumberOfEntries(bool WXUNUSED(bRecursive
)) const
484 bool bCont
= ((wxRegConfig
*)this)->GetFirstEntry(str
, l
);
488 bCont
= ((wxRegConfig
*)this)->GetNextEntry(str
, l
);
494 size_t wxRegConfig::GetNumberOfGroups(bool WXUNUSED(bRecursive
)) const
501 bool bCont
= ((wxRegConfig
*)this)->GetFirstGroup(str
, l
);
505 bCont
= ((wxRegConfig
*)this)->GetNextGroup(str
, l
);
511 // ----------------------------------------------------------------------------
512 // tests for existence
513 // ----------------------------------------------------------------------------
515 bool wxRegConfig::HasGroup(const wxString
& key
) const
517 wxConfigPathChanger
path(this, key
);
519 wxString
strName(path
.Name());
521 return (m_keyLocal
.Exists() && LocalKey().HasSubKey(strName
)) ||
522 m_keyGlobal
.HasSubKey(strName
);
525 bool wxRegConfig::HasEntry(const wxString
& key
) const
527 wxConfigPathChanger
path(this, key
);
529 wxString
strName(path
.Name());
531 return (m_keyLocal
.Exists() && LocalKey().HasValue(strName
)) ||
532 m_keyGlobal
.HasValue(strName
);
535 wxConfigBase::EntryType
wxRegConfig::GetEntryType(const wxString
& key
) const
537 wxConfigPathChanger
path(this, key
);
539 wxString
strName(path
.Name());
542 if ( m_keyLocal
.Exists() && LocalKey().HasValue(strName
) )
543 isNumeric
= m_keyLocal
.IsNumericValue(strName
);
544 else if ( m_keyGlobal
.HasValue(strName
) )
545 isNumeric
= m_keyGlobal
.IsNumericValue(strName
);
547 return wxConfigBase::Type_Unknown
;
549 return isNumeric
? wxConfigBase::Type_Integer
: wxConfigBase::Type_String
;
552 // ----------------------------------------------------------------------------
554 // ----------------------------------------------------------------------------
556 bool wxRegConfig::DoReadString(const wxString
& key
, wxString
*pStr
) const
558 wxCHECK_MSG( pStr
, false, _T("wxRegConfig::Read(): NULL param") );
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());
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
)) ) {
590 // this exactly reproduces the string version above except for ExpandEnvVars(),
591 // we really should avoid this code duplication somehow...
593 bool wxRegConfig::DoReadLong(const wxString
& key
, long *plResult
) const
595 wxCHECK_MSG( plResult
, false, _T("wxRegConfig::Read(): NULL param") );
597 wxConfigPathChanger
path(this, key
);
599 bool bQueryGlobal
= true;
601 // if immutable key exists in global key we must check that it's not
602 // overriden by the local key with the same name
603 if ( IsImmutable(path
.Name()) ) {
604 if ( TryGetValue(m_keyGlobal
, path
.Name(), plResult
) ) {
605 if ( m_keyLocal
.Exists() && LocalKey().HasValue(path
.Name()) ) {
606 wxLogWarning(wxT("User value for immutable key '%s' ignored."),
607 path
.Name().c_str());
613 // don't waste time - it's not there anyhow
614 bQueryGlobal
= false;
618 // first try local key
619 if ( (m_keyLocal
.Exists() && TryGetValue(LocalKey(), path
.Name(), plResult
)) ||
620 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), plResult
)) ) {
627 bool wxRegConfig::DoWriteString(const wxString
& key
, const wxString
& szValue
)
629 wxConfigPathChanger
path(this, key
);
631 if ( IsImmutable(path
.Name()) ) {
632 wxLogError(wxT("Can't change immutable entry '%s'."), path
.Name().c_str());
636 return LocalKey().SetValue(path
.Name(), szValue
);
639 bool wxRegConfig::DoWriteLong(const wxString
& key
, long lValue
)
641 wxConfigPathChanger
path(this, key
);
643 if ( IsImmutable(path
.Name()) ) {
644 wxLogError(wxT("Can't change immutable entry '%s'."), path
.Name().c_str());
648 return LocalKey().SetValue(path
.Name(), lValue
);
651 // ----------------------------------------------------------------------------
653 // ----------------------------------------------------------------------------
655 bool wxRegConfig::RenameEntry(const wxString
& oldName
, const wxString
& newName
)
657 // check that the old entry exists...
658 if ( !HasEntry(oldName
) )
661 // and that the new one doesn't
662 if ( HasEntry(newName
) )
665 return m_keyLocal
.RenameValue(oldName
, newName
);
668 bool wxRegConfig::RenameGroup(const wxString
& oldName
, const wxString
& newName
)
670 // check that the old group exists...
671 if ( !HasGroup(oldName
) )
674 // and that the new one doesn't
675 if ( HasGroup(newName
) )
678 return wxRegKey(m_keyLocal
, oldName
).Rename(newName
);
681 // ----------------------------------------------------------------------------
683 // ----------------------------------------------------------------------------
685 bool wxRegConfig::DeleteEntry(const wxString
& value
, bool bGroupIfEmptyAlso
)
687 wxConfigPathChanger
path(this, value
);
689 if ( m_keyLocal
.Exists() ) {
690 if ( !m_keyLocal
.DeleteValue(path
.Name()) )
693 if ( bGroupIfEmptyAlso
&& m_keyLocal
.IsEmpty() ) {
694 wxString strKey
= GetPath().AfterLast(wxCONFIG_PATH_SEPARATOR
);
695 SetPath(_T("..")); // changes m_keyLocal
696 return LocalKey().DeleteKey(strKey
);
703 bool wxRegConfig::DeleteGroup(const wxString
& key
)
705 wxConfigPathChanger
path(this, key
);
707 return m_keyLocal
.Exists() ? LocalKey().DeleteKey(path
.Name()) : true;
710 bool wxRegConfig::DeleteAll()
715 bool bOk
= m_keyLocalRoot
.DeleteSelf();
717 // make sure that we opened m_keyGlobalRoot and so it has a reasonable name:
718 // otherwise we will delete HKEY_CLASSES_ROOT recursively
719 if ( bOk
&& m_keyGlobalRoot
.IsOpened() )
720 bOk
= m_keyGlobalRoot
.DeleteSelf();