]>
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>
29 #include <wx/config.h>
30 #include <wx/msw/registry.h>
31 #include <wx/msw/regconf.h>
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
37 // we put our data in HKLM\SOFTWARE_KEY\appname
38 #define SOFTWARE_KEY wxString("Software\\")
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 // get the value if the key is opened and it exists
45 bool TryGetValue(const wxRegKey
& key
, const wxString
& str
, wxString
& strVal
)
47 return key
.IsOpened() && key
.HasValue(str
) && key
.QueryValue(str
, strVal
);
50 bool TryGetValue(const wxRegKey
& key
, const wxString
& str
, long *plVal
)
52 return key
.IsOpened() && key
.HasValue(str
) && key
.QueryValue(str
, plVal
);
55 // ============================================================================
57 // ============================================================================
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 // create the config object which stores its data under HKCU\vendor\app and, if
64 // style & wxCONFIG_USE_GLOBAL_FILE, under HKLM\vendor\app
65 wxRegConfig::wxRegConfig(const wxString
& appName
, const wxString
& vendorName
,
66 const wxString
& strLocal
, const wxString
& strGlobal
,
68 : wxConfigBase(appName
, vendorName
, strLocal
, strGlobal
, style
)
72 bool bDoUseGlobal
= (style
& wxCONFIG_USE_GLOBAL_FILE
) != 0;
74 // the convention is to put the programs keys under <vendor>\<appname>
75 // (but it can be overriden by specifying the pathes explicitly in strLocal
77 if ( strLocal
.IsEmpty() || (strGlobal
.IsEmpty() && bDoUseGlobal
) )
79 if ( vendorName
.IsEmpty() )
82 strRoot
= wxTheApp
->GetVendorName();
89 // no '\\' needed if no vendor name
90 if ( !strRoot
.IsEmpty() )
95 if ( appName
.IsEmpty() )
97 wxCHECK_RET( wxTheApp
, "No application name in wxRegConfig ctor!" );
98 strRoot
<< wxTheApp
->GetAppName();
105 //else: we don't need to do all the complicated stuff above
107 wxString str
= strLocal
.IsEmpty() ? strRoot
: strLocal
;
108 m_keyLocalRoot
.SetName(wxRegKey::HKCU
, SOFTWARE_KEY
+ str
);
109 m_keyLocal
.SetName(m_keyLocalRoot
, "");
113 str
= strGlobal
.IsEmpty() ? strRoot
: strGlobal
;
114 m_keyGlobalRoot
.SetName(wxRegKey::HKLM
, SOFTWARE_KEY
+ str
);
115 m_keyGlobal
.SetName(m_keyGlobalRoot
, "");
118 // Create() will Open() if key already exists
119 m_keyLocalRoot
.Create();
121 // as it's the same key, Open() shouldn't fail (i.e. no need for Create())
124 // OTOH, this key may perfectly not exist, so suppress error messages the call
125 // to Open() might generate
129 m_keyGlobalRoot
.Open();
133 wxRegConfig::~wxRegConfig()
135 // nothing to do - key will be closed in their dtors
138 // ----------------------------------------------------------------------------
140 // ----------------------------------------------------------------------------
141 void wxRegConfig::SetPath(const wxString
& strPath
)
143 wxArrayString aParts
;
145 // because GetPath() returns "" when we're at root, we must understand
146 // empty string as "/"
147 if ( strPath
.IsEmpty() || (strPath
[0] == wxCONFIG_PATH_SEPARATOR
) ) {
149 wxSplitPath(aParts
, strPath
);
152 // relative path, combine with current one
153 wxString strFullPath
= GetPath();
154 strFullPath
<< wxCONFIG_PATH_SEPARATOR
<< strPath
;
155 wxSplitPath(aParts
, strFullPath
);
158 // recombine path parts in one variable
161 for ( size_t n
= 0; n
< aParts
.Count(); n
++ ) {
162 strRegPath
<< '\\' << aParts
[n
];
163 m_strPath
<< wxCONFIG_PATH_SEPARATOR
<< aParts
[n
];
166 // change current key(s)
167 m_keyLocal
.SetName(m_keyLocalRoot
, strRegPath
);
168 m_keyGlobal
.SetName(m_keyGlobalRoot
, strRegPath
);
175 // ----------------------------------------------------------------------------
176 // enumeration (works only with current group)
177 // ----------------------------------------------------------------------------
180 We want to enumerate all local keys/values after the global ones, but, of
181 course, we don't want to repeat a key which appears locally as well as
184 We use the 15th bit of lIndex for distinction between global and local.
187 #define LOCAL_MASK 0x8000
188 #define IS_LOCAL_INDEX(l) (((l) & LOCAL_MASK) != 0)
190 bool wxRegConfig::GetFirstGroup(wxString
& str
, long& lIndex
) const
193 return GetNextGroup(str
, lIndex
);
196 bool wxRegConfig::GetNextGroup(wxString
& str
, long& lIndex
) const
198 // are we already enumerating local entries?
199 if ( m_keyGlobal
.IsOpened() && !IS_LOCAL_INDEX(lIndex
) ) {
200 // try to find a global entry which doesn't appear locally
202 if ( !m_keyGlobal
.GetNextKey(str
, lIndex
) ) {
203 // no more global entries
204 lIndex
|= LOCAL_MASK
;
207 } while( m_keyLocal
.HasSubKey(str
) );
210 // much easier with local entries: get the next one we find
211 // (don't forget to clear our flag bit and set it again later)
212 lIndex
&= ~LOCAL_MASK
;
213 bool bOk
= m_keyLocal
.GetNextKey(str
, lIndex
);
214 lIndex
|= LOCAL_MASK
;
219 bool wxRegConfig::GetFirstEntry(wxString
& str
, long& lIndex
) const
222 return GetNextEntry(str
, lIndex
);
225 bool wxRegConfig::GetNextEntry(wxString
& str
, long& lIndex
) const
227 // are we already enumerating local entries?
228 if ( m_keyGlobal
.IsOpened() && !IS_LOCAL_INDEX(lIndex
) ) {
229 // try to find a global entry which doesn't appear locally
231 if ( !m_keyGlobal
.GetNextValue(str
, lIndex
) ) {
232 // no more global entries
233 lIndex
|= LOCAL_MASK
;
236 } while( m_keyLocal
.HasValue(str
) );
239 // much easier with local entries: get the next one we find
240 // (don't forget to clear our flag bit and set it again later)
241 lIndex
&= ~LOCAL_MASK
;
242 bool bOk
= m_keyLocal
.GetNextValue(str
, lIndex
);
243 lIndex
|= LOCAL_MASK
;
248 size_t wxRegConfig::GetNumberOfEntries(bool bRecursive
) const
255 bool bCont
= ((wxRegConfig
*)this)->GetFirstEntry(str
, l
);
259 bCont
= ((wxRegConfig
*)this)->GetNextEntry(str
, l
);
265 size_t wxRegConfig::GetNumberOfGroups(bool bRecursive
) const
272 bool bCont
= ((wxRegConfig
*)this)->GetFirstGroup(str
, l
);
276 bCont
= ((wxRegConfig
*)this)->GetNextGroup(str
, l
);
282 // ----------------------------------------------------------------------------
283 // tests for existence
284 // ----------------------------------------------------------------------------
286 bool wxRegConfig::HasGroup(const wxString
& strName
) const
288 return m_keyLocal
.HasSubKey(strName
) || m_keyGlobal
.HasSubKey(strName
);
291 bool wxRegConfig::HasEntry(const wxString
& strName
) const
293 return m_keyLocal
.HasValue(strName
) || m_keyGlobal
.HasValue(strName
);
296 // ----------------------------------------------------------------------------
298 // ----------------------------------------------------------------------------
300 bool wxRegConfig::Read(const wxString
& key
, wxString
*pStr
) const
302 wxConfigPathChanger
path(this, key
);
304 bool bQueryGlobal
= TRUE
;
306 // if immutable key exists in global key we must check that it's not
307 // overriden by the local key with the same name
308 if ( IsImmutable(path
.Name()) ) {
309 if ( TryGetValue(m_keyGlobal
, path
.Name(), *pStr
) ) {
310 if ( m_keyLocal
.HasValue(path
.Name()) ) {
311 wxLogWarning("User value for immutable key '%s' ignored.",
312 path
.Name().c_str());
314 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
318 // don't waste time - it's not there anyhow
319 bQueryGlobal
= FALSE
;
323 // first try local key
324 if ( TryGetValue(m_keyLocal
, path
.Name(), *pStr
) ||
325 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), *pStr
)) ) {
328 // TODO: do we return TRUE? Not in original implementation,
329 // but I don't see why not. -- JACS
330 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
337 bool wxRegConfig::Read(const wxString
& key
, wxString
*pStr
,
338 const wxString
& szDefault
) const
340 wxConfigPathChanger
path(this, key
);
342 bool bQueryGlobal
= TRUE
;
344 // if immutable key exists in global key we must check that it's not
345 // overriden by the local key with the same name
346 if ( IsImmutable(path
.Name()) ) {
347 if ( TryGetValue(m_keyGlobal
, path
.Name(), *pStr
) ) {
348 if ( m_keyLocal
.HasValue(path
.Name()) ) {
349 wxLogWarning("User value for immutable key '%s' ignored.",
350 path
.Name().c_str());
356 // don't waste time - it's not there anyhow
357 bQueryGlobal
= FALSE
;
361 // first try local key
362 if ( TryGetValue(m_keyLocal
, path
.Name(), *pStr
) ||
363 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), *pStr
)) ) {
364 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
368 if ( IsRecordingDefaults() ) {
369 ((wxRegConfig
*)this)->Write(key
, szDefault
);
376 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
381 bool wxRegConfig::Read(const wxString
& key
, long *plResult
) const
383 wxConfigPathChanger
path(this, key
);
385 bool bQueryGlobal
= TRUE
;
387 // if immutable key exists in global key we must check that it's not
388 // overriden by the local key with the same name
389 if ( IsImmutable(path
.Name()) ) {
390 if ( TryGetValue(m_keyGlobal
, path
.Name(), plResult
) ) {
391 if ( m_keyLocal
.HasValue(path
.Name()) ) {
392 wxLogWarning("User value for immutable key '%s' ignored.",
393 path
.Name().c_str());
399 // don't waste time - it's not there anyhow
400 bQueryGlobal
= FALSE
;
404 // first try local key
405 if ( TryGetValue(m_keyLocal
, path
.Name(), plResult
) ||
406 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), plResult
)) ) {
412 bool wxRegConfig::Write(const wxString
& key
, const wxString
& szValue
)
414 wxConfigPathChanger
path(this, key
);
416 if ( IsImmutable(path
.Name()) ) {
417 wxLogError("Can't change immutable entry '%s'.", path
.Name().c_str());
421 return m_keyLocal
.SetValue(path
.Name(), szValue
);
424 bool wxRegConfig::Write(const wxString
& key
, long lValue
)
426 wxConfigPathChanger
path(this, key
);
428 if ( IsImmutable(path
.Name()) ) {
429 wxLogError("Can't change immutable entry '%s'.", path
.Name().c_str());
433 return m_keyLocal
.SetValue(path
.Name(), lValue
);
436 // ----------------------------------------------------------------------------
438 // ----------------------------------------------------------------------------
439 bool wxRegConfig::DeleteEntry(const wxString
& value
, bool bGroupIfEmptyAlso
)
441 wxConfigPathChanger
path(this, value
);
443 if ( !m_keyLocal
.DeleteValue(path
.Name()) )
446 if ( !m_keyLocal
.HasSubkeys() ) {
447 wxString strKey
= GetPath().Right(wxCONFIG_PATH_SEPARATOR
);
448 SetPath(".."); // changes m_keyLocal
449 return m_keyLocal
.DeleteKey(strKey
);
455 bool wxRegConfig::DeleteGroup(const wxString
& key
)
457 wxConfigPathChanger
path(this, key
);
459 return m_keyLocal
.DeleteKey(path
.Name());
462 bool wxRegConfig::DeleteAll()
467 bool bOk
= m_keyLocalRoot
.DeleteSelf();
469 bOk
= m_keyGlobalRoot
.DeleteSelf();