]>
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 // ============================================================================
18 // ============================================================================
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
25 #pragma implementation "regconf.h"
28 #include "wx/wxprec.h"
35 #include <wx/string.h>
40 #include <wx/config.h>
41 #include <wx/msw/registry.h>
42 #include <wx/msw/regconf.h>
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 // we put our data in HKLM\SOFTWARE_KEY\appname
49 #define SOFTWARE_KEY wxString("Software\\")
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 // get the value if the key is opened and it exists
56 bool TryGetValue(const wxRegKey
& key
, const wxString
& str
, wxString
& strVal
)
58 return key
.IsOpened() && key
.HasValue(str
) && key
.QueryValue(str
, strVal
);
61 bool TryGetValue(const wxRegKey
& key
, const wxString
& str
, long *plVal
)
63 return key
.IsOpened() && key
.HasValue(str
) && key
.QueryValue(str
, plVal
);
66 // ============================================================================
68 // ============================================================================
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 // create the config object which stores its data under HKCU\vendor\app and, if
75 // style & wxCONFIG_USE_GLOBAL_FILE, under HKLM\vendor\app
76 wxRegConfig::wxRegConfig(const wxString
& appName
, const wxString
& vendorName
,
77 const wxString
& strLocal
, const wxString
& strGlobal
,
79 : wxConfigBase(appName
, vendorName
, strLocal
, strGlobal
, style
)
83 bool bDoUseGlobal
= (style
& wxCONFIG_USE_GLOBAL_FILE
) != 0;
85 // the convention is to put the programs keys under <vendor>\<appname>
86 // (but it can be overriden by specifying the pathes explicitly in strLocal
88 if ( strLocal
.IsEmpty() || (strGlobal
.IsEmpty() && bDoUseGlobal
) )
90 if ( vendorName
.IsEmpty() )
93 strRoot
= wxTheApp
->GetVendorName();
100 // no '\\' needed if no vendor name
101 if ( !strRoot
.IsEmpty() )
106 if ( appName
.IsEmpty() )
108 wxCHECK_RET( wxTheApp
, "No application name in wxRegConfig ctor!" );
109 strRoot
<< wxTheApp
->GetAppName();
116 //else: we don't need to do all the complicated stuff above
118 wxString str
= strLocal
.IsEmpty() ? strRoot
: strLocal
;
119 m_keyLocalRoot
.SetName(wxRegKey::HKCU
, SOFTWARE_KEY
+ str
);
120 m_keyLocal
.SetName(m_keyLocalRoot
, "");
124 str
= strGlobal
.IsEmpty() ? strRoot
: strGlobal
;
125 m_keyGlobalRoot
.SetName(wxRegKey::HKLM
, SOFTWARE_KEY
+ str
);
126 m_keyGlobal
.SetName(m_keyGlobalRoot
, "");
129 // Create() will Open() if key already exists
130 m_keyLocalRoot
.Create();
132 // as it's the same key, Open() shouldn't fail (i.e. no need for Create())
135 // OTOH, this key may perfectly not exist, so suppress error messages the call
136 // to Open() might generate
140 m_keyGlobalRoot
.Open();
144 wxRegConfig::~wxRegConfig()
146 // nothing to do - key will be closed in their dtors
149 // ----------------------------------------------------------------------------
151 // ----------------------------------------------------------------------------
152 void wxRegConfig::SetPath(const wxString
& strPath
)
154 wxArrayString aParts
;
156 // because GetPath() returns "" when we're at root, we must understand
157 // empty string as "/"
158 if ( strPath
.IsEmpty() || (strPath
[0] == wxCONFIG_PATH_SEPARATOR
) ) {
160 wxSplitPath(aParts
, strPath
);
163 // relative path, combine with current one
164 wxString strFullPath
= GetPath();
165 strFullPath
<< wxCONFIG_PATH_SEPARATOR
<< strPath
;
166 wxSplitPath(aParts
, strFullPath
);
169 // recombine path parts in one variable
172 for ( size_t n
= 0; n
< aParts
.Count(); n
++ ) {
173 strRegPath
<< '\\' << aParts
[n
];
174 m_strPath
<< wxCONFIG_PATH_SEPARATOR
<< aParts
[n
];
177 // change current key(s)
178 m_keyLocal
.SetName(m_keyLocalRoot
, strRegPath
);
179 m_keyGlobal
.SetName(m_keyGlobalRoot
, strRegPath
);
186 // ----------------------------------------------------------------------------
187 // enumeration (works only with current group)
188 // ----------------------------------------------------------------------------
191 We want to enumerate all local keys/values after the global ones, but, of
192 course, we don't want to repeat a key which appears locally as well as
195 We use the 15th bit of lIndex for distinction between global and local.
198 #define LOCAL_MASK 0x8000
199 #define IS_LOCAL_INDEX(l) (((l) & LOCAL_MASK) != 0)
201 bool wxRegConfig::GetFirstGroup(wxString
& str
, long& lIndex
) const
204 return GetNextGroup(str
, lIndex
);
207 bool wxRegConfig::GetNextGroup(wxString
& str
, long& lIndex
) const
209 // are we already enumerating local entries?
210 if ( m_keyGlobal
.IsOpened() && !IS_LOCAL_INDEX(lIndex
) ) {
211 // try to find a global entry which doesn't appear locally
213 if ( !m_keyGlobal
.GetNextKey(str
, lIndex
) ) {
214 // no more global entries
215 lIndex
|= LOCAL_MASK
;
218 } while( m_keyLocal
.HasSubKey(str
) );
221 // much easier with local entries: get the next one we find
222 // (don't forget to clear our flag bit and set it again later)
223 lIndex
&= ~LOCAL_MASK
;
224 bool bOk
= m_keyLocal
.GetNextKey(str
, lIndex
);
225 lIndex
|= LOCAL_MASK
;
230 bool wxRegConfig::GetFirstEntry(wxString
& str
, long& lIndex
) const
233 return GetNextEntry(str
, lIndex
);
236 bool wxRegConfig::GetNextEntry(wxString
& str
, long& lIndex
) const
238 // are we already enumerating local entries?
239 if ( m_keyGlobal
.IsOpened() && !IS_LOCAL_INDEX(lIndex
) ) {
240 // try to find a global entry which doesn't appear locally
242 if ( !m_keyGlobal
.GetNextValue(str
, lIndex
) ) {
243 // no more global entries
244 lIndex
|= LOCAL_MASK
;
247 } while( m_keyLocal
.HasValue(str
) );
250 // much easier with local entries: get the next one we find
251 // (don't forget to clear our flag bit and set it again later)
252 lIndex
&= ~LOCAL_MASK
;
253 bool bOk
= m_keyLocal
.GetNextValue(str
, lIndex
);
254 lIndex
|= LOCAL_MASK
;
259 size_t wxRegConfig::GetNumberOfEntries(bool bRecursive
) const
266 bool bCont
= ((wxRegConfig
*)this)->GetFirstEntry(str
, l
);
270 bCont
= ((wxRegConfig
*)this)->GetNextEntry(str
, l
);
276 size_t wxRegConfig::GetNumberOfGroups(bool bRecursive
) const
283 bool bCont
= ((wxRegConfig
*)this)->GetFirstGroup(str
, l
);
287 bCont
= ((wxRegConfig
*)this)->GetNextGroup(str
, l
);
293 // ----------------------------------------------------------------------------
294 // tests for existence
295 // ----------------------------------------------------------------------------
297 bool wxRegConfig::HasGroup(const wxString
& strName
) const
299 return m_keyLocal
.HasSubKey(strName
) || m_keyGlobal
.HasSubKey(strName
);
302 bool wxRegConfig::HasEntry(const wxString
& strName
) const
304 return m_keyLocal
.HasValue(strName
) || m_keyGlobal
.HasValue(strName
);
307 // ----------------------------------------------------------------------------
309 // ----------------------------------------------------------------------------
311 bool wxRegConfig::Read(const wxString
& key
, wxString
*pStr
) const
313 wxConfigPathChanger
path(this, key
);
315 bool bQueryGlobal
= TRUE
;
317 // if immutable key exists in global key we must check that it's not
318 // overriden by the local key with the same name
319 if ( IsImmutable(path
.Name()) ) {
320 if ( TryGetValue(m_keyGlobal
, path
.Name(), *pStr
) ) {
321 if ( m_keyLocal
.HasValue(path
.Name()) ) {
322 wxLogWarning("User value for immutable key '%s' ignored.",
323 path
.Name().c_str());
325 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
329 // don't waste time - it's not there anyhow
330 bQueryGlobal
= FALSE
;
334 // first try local key
335 if ( TryGetValue(m_keyLocal
, path
.Name(), *pStr
) ||
336 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), *pStr
)) ) {
339 // TODO: do we return TRUE? Not in original implementation,
340 // but I don't see why not. -- JACS
341 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
348 bool wxRegConfig::Read(const wxString
& key
, wxString
*pStr
,
349 const wxString
& szDefault
) const
351 wxConfigPathChanger
path(this, key
);
353 bool bQueryGlobal
= TRUE
;
355 // if immutable key exists in global key we must check that it's not
356 // overriden by the local key with the same name
357 if ( IsImmutable(path
.Name()) ) {
358 if ( TryGetValue(m_keyGlobal
, path
.Name(), *pStr
) ) {
359 if ( m_keyLocal
.HasValue(path
.Name()) ) {
360 wxLogWarning("User value for immutable key '%s' ignored.",
361 path
.Name().c_str());
367 // don't waste time - it's not there anyhow
368 bQueryGlobal
= FALSE
;
372 // first try local key
373 if ( TryGetValue(m_keyLocal
, path
.Name(), *pStr
) ||
374 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), *pStr
)) ) {
375 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
379 if ( IsRecordingDefaults() ) {
380 ((wxRegConfig
*)this)->Write(key
, szDefault
);
387 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
392 bool wxRegConfig::Read(const wxString
& key
, long *plResult
) const
394 wxConfigPathChanger
path(this, key
);
396 bool bQueryGlobal
= TRUE
;
398 // if immutable key exists in global key we must check that it's not
399 // overriden by the local key with the same name
400 if ( IsImmutable(path
.Name()) ) {
401 if ( TryGetValue(m_keyGlobal
, path
.Name(), plResult
) ) {
402 if ( m_keyLocal
.HasValue(path
.Name()) ) {
403 wxLogWarning("User value for immutable key '%s' ignored.",
404 path
.Name().c_str());
410 // don't waste time - it's not there anyhow
411 bQueryGlobal
= FALSE
;
415 // first try local key
416 if ( TryGetValue(m_keyLocal
, path
.Name(), plResult
) ||
417 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), plResult
)) ) {
423 bool wxRegConfig::Write(const wxString
& key
, const wxString
& szValue
)
425 wxConfigPathChanger
path(this, key
);
427 if ( IsImmutable(path
.Name()) ) {
428 wxLogError("Can't change immutable entry '%s'.", path
.Name().c_str());
432 return m_keyLocal
.SetValue(path
.Name(), szValue
);
435 bool wxRegConfig::Write(const wxString
& key
, long lValue
)
437 wxConfigPathChanger
path(this, key
);
439 if ( IsImmutable(path
.Name()) ) {
440 wxLogError("Can't change immutable entry '%s'.", path
.Name().c_str());
444 return m_keyLocal
.SetValue(path
.Name(), lValue
);
447 // ----------------------------------------------------------------------------
449 // ----------------------------------------------------------------------------
450 bool wxRegConfig::DeleteEntry(const wxString
& value
, bool bGroupIfEmptyAlso
)
452 wxConfigPathChanger
path(this, value
);
454 if ( !m_keyLocal
.DeleteValue(path
.Name()) )
457 if ( !m_keyLocal
.HasSubkeys() ) {
458 wxString strKey
= GetPath().Right(wxCONFIG_PATH_SEPARATOR
);
459 SetPath(".."); // changes m_keyLocal
460 return m_keyLocal
.DeleteKey(strKey
);
466 bool wxRegConfig::DeleteGroup(const wxString
& key
)
468 wxConfigPathChanger
path(this, key
);
470 return m_keyLocal
.DeleteKey(path
.Name());
473 bool wxRegConfig::DeleteAll()
478 bool bOk
= m_keyLocalRoot
.DeleteSelf();
480 bOk
= m_keyGlobalRoot
.DeleteSelf();