]>
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>
30 #include <wx/config.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("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 // ----------------------------------------------------------------------------
64 // create the config object which stores its data under HKCU\vendor\app and, if
65 // style & wxCONFIG_USE_GLOBAL_FILE, under HKLM\vendor\app
66 wxRegConfig::wxRegConfig(const wxString
& appName
, const wxString
& vendorName
,
67 const wxString
& strLocal
, const wxString
& strGlobal
,
69 : wxConfigBase(appName
, vendorName
, strLocal
, strGlobal
, style
)
73 bool bDoUseGlobal
= (style
& wxCONFIG_USE_GLOBAL_FILE
) != 0;
75 // the convention is to put the programs keys under <vendor>\<appname>
76 // (but it can be overriden by specifying the pathes explicitly in strLocal
78 if ( strLocal
.IsEmpty() || (strGlobal
.IsEmpty() && bDoUseGlobal
) )
80 if ( vendorName
.IsEmpty() )
83 strRoot
= wxTheApp
->GetVendorName();
90 // no '\\' needed if no vendor name
91 if ( !strRoot
.IsEmpty() )
96 if ( appName
.IsEmpty() )
98 wxCHECK_RET( wxTheApp
, "No application name in wxRegConfig ctor!" );
99 strRoot
<< wxTheApp
->GetAppName();
106 //else: we don't need to do all the complicated stuff above
108 wxString str
= strLocal
.IsEmpty() ? strRoot
: strLocal
;
109 m_keyLocalRoot
.SetName(wxRegKey::HKCU
, SOFTWARE_KEY
+ str
);
110 m_keyLocal
.SetName(m_keyLocalRoot
, "");
114 str
= strGlobal
.IsEmpty() ? strRoot
: strGlobal
;
115 m_keyGlobalRoot
.SetName(wxRegKey::HKLM
, SOFTWARE_KEY
+ str
);
116 m_keyGlobal
.SetName(m_keyGlobalRoot
, "");
119 // Create() will Open() if key already exists
120 m_keyLocalRoot
.Create();
122 // as it's the same key, Open() shouldn't fail (i.e. no need for Create())
125 // OTOH, this key may perfectly not exist, so suppress error messages the call
126 // to Open() might generate
130 m_keyGlobalRoot
.Open();
134 wxRegConfig::~wxRegConfig()
136 // nothing to do - key will be closed in their dtors
139 // ----------------------------------------------------------------------------
141 // ----------------------------------------------------------------------------
142 void wxRegConfig::SetPath(const wxString
& strPath
)
144 wxArrayString aParts
;
146 // because GetPath() returns "" when we're at root, we must understand
147 // empty string as "/"
148 if ( strPath
.IsEmpty() || (strPath
[0] == wxCONFIG_PATH_SEPARATOR
) ) {
150 wxSplitPath(aParts
, strPath
);
153 // relative path, combine with current one
154 wxString strFullPath
= GetPath();
155 strFullPath
<< wxCONFIG_PATH_SEPARATOR
<< strPath
;
156 wxSplitPath(aParts
, strFullPath
);
159 // recombine path parts in one variable
162 for ( size_t n
= 0; n
< aParts
.Count(); n
++ ) {
163 strRegPath
<< '\\' << aParts
[n
];
164 m_strPath
<< wxCONFIG_PATH_SEPARATOR
<< aParts
[n
];
167 // change current key(s)
168 m_keyLocal
.SetName(m_keyLocalRoot
, strRegPath
);
169 m_keyGlobal
.SetName(m_keyGlobalRoot
, strRegPath
);
176 // ----------------------------------------------------------------------------
177 // enumeration (works only with current group)
178 // ----------------------------------------------------------------------------
181 We want to enumerate all local keys/values after the global ones, but, of
182 course, we don't want to repeat a key which appears locally as well as
185 We use the 15th bit of lIndex for distinction between global and local.
188 #define LOCAL_MASK 0x8000
189 #define IS_LOCAL_INDEX(l) (((l) & LOCAL_MASK) != 0)
191 bool wxRegConfig::GetFirstGroup(wxString
& str
, long& lIndex
) const
194 return GetNextGroup(str
, lIndex
);
197 bool wxRegConfig::GetNextGroup(wxString
& str
, long& lIndex
) const
199 // are we already enumerating local entries?
200 if ( m_keyGlobal
.IsOpened() && !IS_LOCAL_INDEX(lIndex
) ) {
201 // try to find a global entry which doesn't appear locally
203 if ( !m_keyGlobal
.GetNextKey(str
, lIndex
) ) {
204 // no more global entries
205 lIndex
|= LOCAL_MASK
;
208 } while( m_keyLocal
.HasSubKey(str
) );
211 // much easier with local entries: get the next one we find
212 // (don't forget to clear our flag bit and set it again later)
213 lIndex
&= ~LOCAL_MASK
;
214 bool bOk
= m_keyLocal
.GetNextKey(str
, lIndex
);
215 lIndex
|= LOCAL_MASK
;
220 bool wxRegConfig::GetFirstEntry(wxString
& str
, long& lIndex
) const
223 return GetNextEntry(str
, lIndex
);
226 bool wxRegConfig::GetNextEntry(wxString
& str
, long& lIndex
) const
228 // are we already enumerating local entries?
229 if ( m_keyGlobal
.IsOpened() && !IS_LOCAL_INDEX(lIndex
) ) {
230 // try to find a global entry which doesn't appear locally
232 if ( !m_keyGlobal
.GetNextValue(str
, lIndex
) ) {
233 // no more global entries
234 lIndex
|= LOCAL_MASK
;
237 } while( m_keyLocal
.HasValue(str
) );
240 // much easier with local entries: get the next one we find
241 // (don't forget to clear our flag bit and set it again later)
242 lIndex
&= ~LOCAL_MASK
;
243 bool bOk
= m_keyLocal
.GetNextValue(str
, lIndex
);
244 lIndex
|= LOCAL_MASK
;
249 size_t wxRegConfig::GetNumberOfEntries(bool bRecursive
) const
256 bool bCont
= ((wxRegConfig
*)this)->GetFirstEntry(str
, l
);
260 bCont
= ((wxRegConfig
*)this)->GetNextEntry(str
, l
);
266 size_t wxRegConfig::GetNumberOfGroups(bool bRecursive
) const
273 bool bCont
= ((wxRegConfig
*)this)->GetFirstGroup(str
, l
);
277 bCont
= ((wxRegConfig
*)this)->GetNextGroup(str
, l
);
283 // ----------------------------------------------------------------------------
284 // tests for existence
285 // ----------------------------------------------------------------------------
287 bool wxRegConfig::HasGroup(const wxString
& strName
) const
289 return m_keyLocal
.HasSubKey(strName
) || m_keyGlobal
.HasSubKey(strName
);
292 bool wxRegConfig::HasEntry(const wxString
& strName
) const
294 return m_keyLocal
.HasValue(strName
) || m_keyGlobal
.HasValue(strName
);
297 // ----------------------------------------------------------------------------
299 // ----------------------------------------------------------------------------
301 bool wxRegConfig::Read(const wxString
& key
, wxString
*pStr
) const
303 wxConfigPathChanger
path(this, key
);
305 bool bQueryGlobal
= TRUE
;
307 // if immutable key exists in global key we must check that it's not
308 // overriden by the local key with the same name
309 if ( IsImmutable(path
.Name()) ) {
310 if ( TryGetValue(m_keyGlobal
, path
.Name(), *pStr
) ) {
311 if ( m_keyLocal
.HasValue(path
.Name()) ) {
312 wxLogWarning("User value for immutable key '%s' ignored.",
313 path
.Name().c_str());
315 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
319 // don't waste time - it's not there anyhow
320 bQueryGlobal
= FALSE
;
324 // first try local key
325 if ( TryGetValue(m_keyLocal
, path
.Name(), *pStr
) ||
326 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), *pStr
)) ) {
329 // TODO: do we return TRUE? Not in original implementation,
330 // but I don't see why not. -- JACS
331 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
338 bool wxRegConfig::Read(const wxString
& key
, wxString
*pStr
,
339 const wxString
& szDefault
) const
341 wxConfigPathChanger
path(this, key
);
343 bool bQueryGlobal
= TRUE
;
345 // if immutable key exists in global key we must check that it's not
346 // overriden by the local key with the same name
347 if ( IsImmutable(path
.Name()) ) {
348 if ( TryGetValue(m_keyGlobal
, path
.Name(), *pStr
) ) {
349 if ( m_keyLocal
.HasValue(path
.Name()) ) {
350 wxLogWarning("User value for immutable key '%s' ignored.",
351 path
.Name().c_str());
357 // don't waste time - it's not there anyhow
358 bQueryGlobal
= FALSE
;
362 // first try local key
363 if ( TryGetValue(m_keyLocal
, path
.Name(), *pStr
) ||
364 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), *pStr
)) ) {
365 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
369 if ( IsRecordingDefaults() ) {
370 ((wxRegConfig
*)this)->Write(key
, szDefault
);
377 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
382 bool wxRegConfig::Read(const wxString
& key
, long *plResult
) const
384 wxConfigPathChanger
path(this, key
);
386 bool bQueryGlobal
= TRUE
;
388 // if immutable key exists in global key we must check that it's not
389 // overriden by the local key with the same name
390 if ( IsImmutable(path
.Name()) ) {
391 if ( TryGetValue(m_keyGlobal
, path
.Name(), plResult
) ) {
392 if ( m_keyLocal
.HasValue(path
.Name()) ) {
393 wxLogWarning("User value for immutable key '%s' ignored.",
394 path
.Name().c_str());
400 // don't waste time - it's not there anyhow
401 bQueryGlobal
= FALSE
;
405 // first try local key
406 if ( TryGetValue(m_keyLocal
, path
.Name(), plResult
) ||
407 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), plResult
)) ) {
413 bool wxRegConfig::Write(const wxString
& key
, const wxString
& szValue
)
415 wxConfigPathChanger
path(this, key
);
417 if ( IsImmutable(path
.Name()) ) {
418 wxLogError("Can't change immutable entry '%s'.", path
.Name().c_str());
422 return m_keyLocal
.SetValue(path
.Name(), szValue
);
425 bool wxRegConfig::Write(const wxString
& key
, long lValue
)
427 wxConfigPathChanger
path(this, key
);
429 if ( IsImmutable(path
.Name()) ) {
430 wxLogError("Can't change immutable entry '%s'.", path
.Name().c_str());
434 return m_keyLocal
.SetValue(path
.Name(), lValue
);
437 // ----------------------------------------------------------------------------
439 // ----------------------------------------------------------------------------
440 bool wxRegConfig::DeleteEntry(const wxString
& value
, bool bGroupIfEmptyAlso
)
442 wxConfigPathChanger
path(this, value
);
444 if ( !m_keyLocal
.DeleteValue(path
.Name()) )
447 if ( !m_keyLocal
.HasSubkeys() ) {
448 wxString strKey
= GetPath().Right(wxCONFIG_PATH_SEPARATOR
);
449 SetPath(".."); // changes m_keyLocal
450 return m_keyLocal
.DeleteKey(strKey
);
456 bool wxRegConfig::DeleteGroup(const wxString
& key
)
458 wxConfigPathChanger
path(this, key
);
460 return m_keyLocal
.DeleteKey(path
.Name());
463 bool wxRegConfig::DeleteAll()
468 bool bOk
= m_keyLocalRoot
.DeleteSelf();
470 bOk
= m_keyGlobalRoot
.DeleteSelf();