]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/regconf.cpp
854d39f7d011592dc8d31404cca40ca892840d90
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] == wxCONFIG_PATH_SEPARATOR
) {
102 wxSplitPath(aParts
, strPath
);
105 // relative path, combine with current one
106 wxString strFullPath
= GetPath();
107 strFullPath
<< wxCONFIG_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
<< wxCONFIG_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(bool bRecursive
) const
208 bool bCont
= ((wxRegConfig
*)this)->GetFirstEntry(str
, l
);
212 bCont
= ((wxRegConfig
*)this)->GetNextEntry(str
, l
);
218 uint
wxRegConfig::GetNumberOfGroups(bool bRecursive
) const
225 bool bCont
= ((wxRegConfig
*)this)->GetFirstGroup(str
, l
);
229 bCont
= ((wxRegConfig
*)this)->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
*pStr
,
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(), *pStr
) ) {
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(), *pStr
) ||
280 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), *pStr
)) ) {
284 if(IsRecordingDefaults())
285 ((wxRegConfig
*)this)->Write(szKey
,szDefault
);
292 bool wxRegConfig::Read(long *plResult
, const char *szKey
, long lDefault
) const
294 PathChanger
path(this, szKey
);
296 bool bQueryGlobal
= TRUE
;
298 // if immutable key exists in global key we must check that it's not
299 // overriden by the local key with the same name
300 if ( IsImmutable(path
.Name()) ) {
301 if ( TryGetValue(m_keyGlobal
, path
.Name(), plResult
) ) {
302 if ( m_keyLocal
.HasValue(path
.Name()) ) {
303 wxLogWarning("User value for immutable key '%s' ignored.",
304 path
.Name().c_str());
310 // don't waste time - it's not there anyhow
311 bQueryGlobal
= FALSE
;
315 // first try local key
316 if ( TryGetValue(m_keyLocal
, path
.Name(), plResult
) ||
317 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), plResult
)) ) {
322 *plResult
= lDefault
;
326 bool wxRegConfig::Write(const char *szKey
, const char *szValue
)
328 PathChanger
path(this, szKey
);
330 if ( IsImmutable(path
.Name()) ) {
331 wxLogError("Can't change immutable entry '%s'.", path
.Name().c_str());
335 return m_keyLocal
.SetValue(path
.Name(), szValue
);
338 bool wxRegConfig::Write(const char *szKey
, long lValue
)
340 PathChanger
path(this, szKey
);
342 if ( IsImmutable(path
.Name()) ) {
343 wxLogError("Can't change immutable entry '%s'.", path
.Name().c_str());
347 return m_keyLocal
.SetValue(path
.Name(), lValue
);
350 // ----------------------------------------------------------------------------
352 // ----------------------------------------------------------------------------
353 bool wxRegConfig::DeleteEntry(const char *szValue
, bool bGroupIfEmptyAlso
)
355 PathChanger
path(this, szValue
);
357 if ( !m_keyLocal
.DeleteValue(path
.Name()) )
360 if ( !m_keyLocal
.HasSubkeys() ) {
361 wxString strKey
= GetPath().Right(wxCONFIG_PATH_SEPARATOR
);
362 SetPath(".."); // changes m_keyLocal
363 return m_keyLocal
.DeleteKey(strKey
);
369 bool wxRegConfig::DeleteGroup(const char *szKey
)
371 PathChanger
path(this, szKey
);
373 return m_keyLocal
.DeleteKey(path
.Name());
376 bool wxRegConfig::DeleteAll()
381 bool bOk
= m_keyLocalRoot
.DeleteSelf();
383 bOk
= m_keyGlobalRoot
.DeleteSelf();