]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/regconf.cpp
9b6bdf0962b40cf2884ab983c19f9eef32637585
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>
39 #include <wx/config.h>
40 #include <wx/msw/registry.h>
41 #include <wx/msw/regconf.h>
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 // we put our data in HKLM\SOFTWARE_KEY\appname
48 #define SOFTWARE_KEY wxString("Software\\")
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 // get the value if the key is opened and it exists
55 bool TryGetValue(const wxRegKey
& key
, const wxString
& str
, wxString
& strVal
)
57 return key
.IsOpened() && key
.HasValue(str
) && key
.QueryValue(str
, strVal
);
60 bool TryGetValue(const wxRegKey
& key
, const wxString
& str
, long *plVal
)
62 return key
.IsOpened() && key
.HasValue(str
) && key
.QueryValue(str
, plVal
);
65 // ============================================================================
67 // ============================================================================
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
72 wxRegConfig::wxRegConfig(const wxString
& strRoot
)
73 : m_keyLocalRoot(wxRegKey::HKCU
, SOFTWARE_KEY
+ strRoot
),
74 m_keyLocal(m_keyLocalRoot
, ""),
75 m_keyGlobalRoot(wxRegKey::HKLM
, SOFTWARE_KEY
+ strRoot
),
76 m_keyGlobal(m_keyGlobalRoot
, "")
78 // Create() will Open() if key already exists
79 m_keyLocalRoot
.Create();
81 // as it's the same key, Open() shouldn't fail (i.e. no need for Create())
85 m_keyGlobalRoot
.Open();
88 wxRegConfig::~wxRegConfig()
90 // nothing to do - key will be closed in their dtors
93 // ----------------------------------------------------------------------------
95 // ----------------------------------------------------------------------------
96 void wxRegConfig::SetPath(const wxString
& strPath
)
100 // because GetPath() returns "" when we're at root, we must understand
101 // empty string as "/"
102 if ( strPath
.IsEmpty() || (strPath
[0] == wxCONFIG_PATH_SEPARATOR
) ) {
104 wxSplitPath(aParts
, strPath
);
107 // relative path, combine with current one
108 wxString strFullPath
= GetPath();
109 strFullPath
<< wxCONFIG_PATH_SEPARATOR
<< strPath
;
110 wxSplitPath(aParts
, strFullPath
);
113 // recombine path parts in one variable
116 for ( size_t n
= 0; n
< aParts
.Count(); n
++ ) {
117 strRegPath
<< '\\' << aParts
[n
];
118 m_strPath
<< wxCONFIG_PATH_SEPARATOR
<< aParts
[n
];
121 // change current key(s)
122 m_keyLocal
.SetName(m_keyLocalRoot
, strRegPath
);
123 m_keyGlobal
.SetName(m_keyGlobalRoot
, strRegPath
);
130 // ----------------------------------------------------------------------------
131 // enumeration (works only with current group)
132 // ----------------------------------------------------------------------------
135 We want to enumerate all local keys/values after the global ones, but, of
136 course, we don't want to repeat a key which appears locally as well as
139 We use the 15th bit of lIndex for distinction between global and local.
142 #define LOCAL_MASK 0x8000
143 #define IS_LOCAL_INDEX(l) (((l) & LOCAL_MASK) != 0)
145 bool wxRegConfig::GetFirstGroup(wxString
& str
, long& lIndex
) const
148 return GetNextGroup(str
, lIndex
);
151 bool wxRegConfig::GetNextGroup(wxString
& str
, long& lIndex
) const
153 // are we already enumerating local entries?
154 if ( m_keyGlobal
.IsOpened() && !IS_LOCAL_INDEX(lIndex
) ) {
155 // try to find a global entry which doesn't appear locally
157 if ( !m_keyGlobal
.GetNextKey(str
, lIndex
) ) {
158 // no more global entries
159 lIndex
|= LOCAL_MASK
;
162 } while( m_keyLocal
.HasSubKey(str
) );
165 // much easier with local entries: get the next one we find
166 // (don't forget to clear our flag bit and set it again later)
167 lIndex
&= ~LOCAL_MASK
;
168 bool bOk
= m_keyLocal
.GetNextKey(str
, lIndex
);
169 lIndex
|= LOCAL_MASK
;
174 bool wxRegConfig::GetFirstEntry(wxString
& str
, long& lIndex
) const
177 return GetNextEntry(str
, lIndex
);
180 bool wxRegConfig::GetNextEntry(wxString
& str
, long& lIndex
) const
182 // are we already enumerating local entries?
183 if ( m_keyGlobal
.IsOpened() && !IS_LOCAL_INDEX(lIndex
) ) {
184 // try to find a global entry which doesn't appear locally
186 if ( !m_keyGlobal
.GetNextValue(str
, lIndex
) ) {
187 // no more global entries
188 lIndex
|= LOCAL_MASK
;
191 } while( m_keyLocal
.HasValue(str
) );
194 // much easier with local entries: get the next one we find
195 // (don't forget to clear our flag bit and set it again later)
196 lIndex
&= ~LOCAL_MASK
;
197 bool bOk
= m_keyLocal
.GetNextValue(str
, lIndex
);
198 lIndex
|= LOCAL_MASK
;
203 size_t wxRegConfig::GetNumberOfEntries(bool bRecursive
) const
210 bool bCont
= ((wxRegConfig
*)this)->GetFirstEntry(str
, l
);
214 bCont
= ((wxRegConfig
*)this)->GetNextEntry(str
, l
);
220 size_t wxRegConfig::GetNumberOfGroups(bool bRecursive
) const
227 bool bCont
= ((wxRegConfig
*)this)->GetFirstGroup(str
, l
);
231 bCont
= ((wxRegConfig
*)this)->GetNextGroup(str
, l
);
237 // ----------------------------------------------------------------------------
238 // tests for existence
239 // ----------------------------------------------------------------------------
241 bool wxRegConfig::HasGroup(const wxString
& strName
) const
243 return m_keyLocal
.HasSubKey(strName
) || m_keyGlobal
.HasSubKey(strName
);
246 bool wxRegConfig::HasEntry(const wxString
& strName
) const
248 return m_keyLocal
.HasValue(strName
) || m_keyGlobal
.HasValue(strName
);
251 // ----------------------------------------------------------------------------
253 // ----------------------------------------------------------------------------
255 bool wxRegConfig::Read(wxString
*pStr
,
257 const char *szDefault
) const
259 PathChanger
path(this, szKey
);
261 bool bQueryGlobal
= TRUE
;
263 // if immutable key exists in global key we must check that it's not
264 // overriden by the local key with the same name
265 if ( IsImmutable(path
.Name()) ) {
266 if ( TryGetValue(m_keyGlobal
, path
.Name(), *pStr
) ) {
267 if ( m_keyLocal
.HasValue(path
.Name()) ) {
268 wxLogWarning("User value for immutable key '%s' ignored.",
269 path
.Name().c_str());
275 // don't waste time - it's not there anyhow
276 bQueryGlobal
= FALSE
;
280 // first try local key
281 if ( TryGetValue(m_keyLocal
, path
.Name(), *pStr
) ||
282 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), *pStr
)) ) {
286 if ( IsRecordingDefaults() ) {
287 ((wxRegConfig
*)this)->Write(szKey
, szDefault
);
294 *pStr
= wxConfigBase::ExpandEnvVars(*pStr
);
299 bool wxRegConfig::Read(long *plResult
, const char *szKey
, long lDefault
) const
301 PathChanger
path(this, szKey
);
303 bool bQueryGlobal
= TRUE
;
305 // if immutable key exists in global key we must check that it's not
306 // overriden by the local key with the same name
307 if ( IsImmutable(path
.Name()) ) {
308 if ( TryGetValue(m_keyGlobal
, path
.Name(), plResult
) ) {
309 if ( m_keyLocal
.HasValue(path
.Name()) ) {
310 wxLogWarning("User value for immutable key '%s' ignored.",
311 path
.Name().c_str());
317 // don't waste time - it's not there anyhow
318 bQueryGlobal
= FALSE
;
322 // first try local key
323 if ( TryGetValue(m_keyLocal
, path
.Name(), plResult
) ||
324 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), plResult
)) ) {
329 *plResult
= lDefault
;
333 bool wxRegConfig::Write(const char *szKey
, const char *szValue
)
335 PathChanger
path(this, szKey
);
337 if ( IsImmutable(path
.Name()) ) {
338 wxLogError("Can't change immutable entry '%s'.", path
.Name().c_str());
342 return m_keyLocal
.SetValue(path
.Name(), szValue
);
345 bool wxRegConfig::Write(const char *szKey
, long lValue
)
347 PathChanger
path(this, szKey
);
349 if ( IsImmutable(path
.Name()) ) {
350 wxLogError("Can't change immutable entry '%s'.", path
.Name().c_str());
354 return m_keyLocal
.SetValue(path
.Name(), lValue
);
357 // ----------------------------------------------------------------------------
359 // ----------------------------------------------------------------------------
360 bool wxRegConfig::DeleteEntry(const char *szValue
, bool bGroupIfEmptyAlso
)
362 PathChanger
path(this, szValue
);
364 if ( !m_keyLocal
.DeleteValue(path
.Name()) )
367 if ( !m_keyLocal
.HasSubkeys() ) {
368 wxString strKey
= GetPath().Right(wxCONFIG_PATH_SEPARATOR
);
369 SetPath(".."); // changes m_keyLocal
370 return m_keyLocal
.DeleteKey(strKey
);
376 bool wxRegConfig::DeleteGroup(const char *szKey
)
378 PathChanger
path(this, szKey
);
380 return m_keyLocal
.DeleteKey(path
.Name());
383 bool wxRegConfig::DeleteAll()
388 bool bOk
= m_keyLocalRoot
.DeleteSelf();
390 bOk
= m_keyGlobalRoot
.DeleteSelf();