]>
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>
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();
82 m_keyGlobalRoot
.Open();
85 wxRegConfig::~wxRegConfig()
87 // nothing to do - key will be closed in their dtors
90 // ----------------------------------------------------------------------------
92 // ----------------------------------------------------------------------------
93 void wxRegConfig::SetPath(const wxString
& strPath
)
97 if ( strPath
.IsEmpty() )
100 if ( strPath
[0] == APPCONF_PATH_SEPARATOR
) {
102 wxSplitPath(aParts
, strPath
);
105 // relative path, combine with current one
106 wxString strFullPath
= GetPath();
107 strFullPath
<< APPCONF_PATH_SEPARATOR
<< strPath
;
108 wxSplitPath(aParts
, strFullPath
);
111 // recombine path parts in one variable
114 for ( uint n
= 0; n
< aParts
.Count(); n
++ ) {
115 strRegPath
<< '\\' << aParts
[n
];
116 m_strPath
<< APPCONF_PATH_SEPARATOR
<< aParts
[n
];
119 // change current key(s)
120 m_keyLocal
.SetName(m_keyLocalRoot
, strRegPath
);
121 m_keyGlobal
.SetName(m_keyGlobalRoot
, strRegPath
);
128 // ----------------------------------------------------------------------------
129 // enumeration (works only with current group)
130 // ----------------------------------------------------------------------------
133 We want to enumerate all local keys/values after the global ones, but, of
134 course, we don't want to repeat a key which appears locally as well as
137 We use the 15th bit of lIndex for distinction between global and local.
140 #define LOCAL_MASK 0x8000
141 #define IS_LOCAL_INDEX(l) (((l) & LOCAL_MASK) != 0)
143 bool wxRegConfig::GetFirstGroup(wxString
& str
, long& lIndex
) const
146 return GetNextGroup(str
, lIndex
);
149 bool wxRegConfig::GetNextGroup(wxString
& str
, long& lIndex
) const
151 // are we already enumerating local entries?
152 if ( m_keyGlobal
.IsOpened() && !IS_LOCAL_INDEX(lIndex
) ) {
153 // try to find a global entry which doesn't appear locally
155 if ( !m_keyGlobal
.GetNextKey(str
, lIndex
) ) {
156 // no more global entries
157 lIndex
|= LOCAL_MASK
;
160 } while( m_keyLocal
.HasSubKey(str
) );
163 // much easier with local entries: get the next one we find
164 // (don't forget to clear our flag bit and set it again later)
165 lIndex
&= ~LOCAL_MASK
;
166 bool bOk
= m_keyLocal
.GetNextKey(str
, lIndex
);
167 lIndex
|= LOCAL_MASK
;
172 bool wxRegConfig::GetFirstEntry(wxString
& str
, long& lIndex
) const
175 return GetNextEntry(str
, lIndex
);
178 bool wxRegConfig::GetNextEntry(wxString
& str
, long& lIndex
) const
180 // are we already enumerating local entries?
181 if ( m_keyGlobal
.IsOpened() && !IS_LOCAL_INDEX(lIndex
) ) {
182 // try to find a global entry which doesn't appear locally
184 if ( !m_keyGlobal
.GetNextValue(str
, lIndex
) ) {
185 // no more global entries
186 lIndex
|= LOCAL_MASK
;
189 } while( m_keyLocal
.HasValue(str
) );
192 // much easier with local entries: get the next one we find
193 // (don't forget to clear our flag bit and set it again later)
194 lIndex
&= ~LOCAL_MASK
;
195 bool bOk
= m_keyLocal
.GetNextValue(str
, lIndex
);
196 lIndex
|= LOCAL_MASK
;
201 uint
wxRegConfig::GetNumberOfEntries() const
208 bool bCont
= GetFirstEntry(str
, l
);
212 bCont
= GetNextEntry(str
, l
);
218 uint
wxRegConfig::GetNumberOfGroups() const
225 bool bCont
= GetFirstGroup(str
, l
);
229 bCont
= GetNextGroup(str
, l
);
235 // ----------------------------------------------------------------------------
236 // tests for existence
237 // ----------------------------------------------------------------------------
239 bool wxRegConfig::HasGroup(const wxString
& strName
) const
241 return m_keyLocal
.HasSubKey(strName
) || m_keyGlobal
.HasSubKey(strName
);
244 bool wxRegConfig::HasEntry(const wxString
& strName
) const
246 return m_keyLocal
.HasValue(strName
) || m_keyGlobal
.HasValue(strName
);
249 // ----------------------------------------------------------------------------
251 // ----------------------------------------------------------------------------
253 bool wxRegConfig::Read(wxString
& str
,
255 const char *szDefault
) const
257 PathChanger
path(this, szKey
);
259 bool bQueryGlobal
= TRUE
;
261 // if immutable key exists in global key we must check that it's not
262 // overriden by the local key with the same name
263 if ( IsImmutable(path
.Name()) ) {
264 if ( TryGetValue(m_keyGlobal
, path
.Name(), str
) ) {
265 if ( m_keyLocal
.HasValue(path
.Name()) ) {
266 wxLogWarning("User value for immutable key '%s' ignored.",
267 path
.Name().c_str());
273 // don't waste time - it's not there anyhow
274 bQueryGlobal
= FALSE
;
278 // first try local key
279 if ( TryGetValue(m_keyLocal
, path
.Name(), str
) ||
280 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), str
)) ) {
289 bool wxRegConfig::Read(long &lValue
, const char *szKey
, long lDefault
) const
291 PathChanger
path(this, szKey
);
293 bool bQueryGlobal
= TRUE
;
295 // if immutable key exists in global key we must check that it's not
296 // overriden by the local key with the same name
297 if ( IsImmutable(path
.Name()) ) {
298 if ( TryGetValue(m_keyGlobal
, path
.Name(), &lValue
) ) {
299 if ( m_keyLocal
.HasValue(path
.Name()) ) {
300 wxLogWarning("User value for immutable key '%s' ignored.",
301 path
.Name().c_str());
307 // don't waste time - it's not there anyhow
308 bQueryGlobal
= FALSE
;
312 // first try local key
313 if ( TryGetValue(m_keyLocal
, path
.Name(), &lValue
) ||
314 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), &lValue
)) ) {
323 bool wxRegConfig::Write(const char *szKey
, const char *szValue
)
325 PathChanger
path(this, szKey
);
327 if ( IsImmutable(path
.Name()) ) {
328 wxLogError("Can't change immutable entry '%s'.", path
.Name().c_str());
332 return m_keyLocal
.SetValue(path
.Name(), szValue
);
335 bool wxRegConfig::Write(const char *szKey
, long lValue
)
337 PathChanger
path(this, szKey
);
339 if ( IsImmutable(path
.Name()) ) {
340 wxLogError("Can't change immutable entry '%s'.", path
.Name().c_str());
344 return m_keyLocal
.SetValue(path
.Name(), lValue
);
347 // ----------------------------------------------------------------------------
349 // ----------------------------------------------------------------------------
350 bool wxRegConfig::DeleteEntry(const char *szValue
, bool bGroupIfEmptyAlso
)
352 PathChanger
path(this, szValue
);
354 if ( !m_keyLocal
.DeleteValue(path
.Name()) )
357 if ( !m_keyLocal
.HasSubkeys() ) {
358 wxString strKey
= GetPath().Right(APPCONF_PATH_SEPARATOR
);
359 SetPath(".."); // changes m_keyLocal
360 return m_keyLocal
.DeleteKey(strKey
);
366 bool wxRegConfig::DeleteGroup(const char *szKey
)
368 PathChanger
path(this, szKey
);
370 return m_keyLocal
.DeleteKey(path
.Name());
373 bool wxRegConfig::DeleteAll()
378 bool bOk
= m_keyLocalRoot
.DeleteSelf();
380 bOk
= m_keyGlobalRoot
.DeleteSelf();