]>
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 // ----------------------------------------------------------------------------
24 #include <wx/config.h>
25 #include <wx/regconf.h>
26 #include <wx/msw/registry.h>
28 // ----------------------------------------------------------------------------
30 // ----------------------------------------------------------------------------
32 // we put our data in HKLM\SOFTWARE_KEY\appname
33 #define SOFTWARE_KEY wxString("Software\\")
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 // get the value if the key is opened and it exists
40 bool TryGetValue(const wxRegKey
& key
, const wxString
& str
, wxString
& strVal
)
42 return key
.IsOpened() && key
.HasValue(str
) && key
.QueryValue(str
, strVal
);
45 bool TryGetValue(const wxRegKey
& key
, const wxString
& str
, long *plVal
)
47 return key
.IsOpened() && key
.HasValue(str
) && key
.QueryValue(str
, plVal
);
50 // ============================================================================
52 // ============================================================================
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
57 wxRegConfig::wxRegConfig(const wxString
& strRoot
)
58 : m_keyLocalRoot(wxRegKey::HKCU
, SOFTWARE_KEY
+ strRoot
),
59 m_keyLocal(m_keyLocalRoot
, ""),
60 m_keyGlobalRoot(wxRegKey::HKLM
, SOFTWARE_KEY
+ strRoot
),
61 m_keyGlobal(m_keyGlobalRoot
, "")
63 // Create() will Open() if key already exists
64 m_keyLocalRoot
.Create();
67 m_keyGlobalRoot
.Open();
70 wxRegConfig::~wxRegConfig()
72 // nothing to do - key will be closed in their dtors
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
78 void wxRegConfig::SetPath(const wxString
& strPath
)
82 if ( strPath
.IsEmpty() )
85 if ( strPath
[0] == APPCONF_PATH_SEPARATOR
) {
87 SplitPath(aParts
, strPath
);
90 // relative path, combine with current one
91 wxString strFullPath
= GetPath();
92 strFullPath
<< APPCONF_PATH_SEPARATOR
<< strPath
;
93 SplitPath(aParts
, strFullPath
);
96 // recombine path parts in one variable
99 for ( uint n
= 0; n
< aParts
.Count(); n
++ ) {
100 strRegPath
<< '\\' << aParts
[n
];
101 m_strPath
<< APPCONF_PATH_SEPARATOR
<< aParts
[n
];
104 // change current key(s)
105 m_keyLocal
.SetName(m_keyLocalRoot
, strRegPath
);
106 m_keyGlobal
.SetName(m_keyGlobalRoot
, strRegPath
);
113 // ----------------------------------------------------------------------------
114 // enumeration (works only with current group)
115 // ----------------------------------------------------------------------------
118 We want to enumerate all local keys/values after the global ones, but, of
119 course, we don't want to repeat a key which appears locally as well as
122 We use the 15th bit of lIndex for distinction between global and local.
125 #define LOCAL_MASK 0x8000
126 #define IS_LOCAL_INDEX(l) (((l) & LOCAL_MASK) != 0)
128 bool wxRegConfig::GetFirstGroup(wxString
& str
, long& lIndex
)
131 return GetNextGroup(str
, lIndex
);
134 bool wxRegConfig::GetNextGroup (wxString
& str
, long& lIndex
)
136 // are we already enumerating local entries?
137 if ( m_keyGlobal
.IsOpened() && !IS_LOCAL_INDEX(lIndex
) ) {
138 // try to find a global entry which doesn't appear locally
140 if ( !m_keyGlobal
.GetNextKey(str
, lIndex
) ) {
141 // no more global entries
142 lIndex
|= LOCAL_MASK
;
145 } while( m_keyLocal
.HasSubKey(str
) );
148 // much easier with local entries: get the next one we find
149 // (don't forget to clear our flag bit and set it again later)
150 lIndex
&= ~LOCAL_MASK
;
151 bool bOk
= m_keyLocal
.GetNextKey(str
, lIndex
);
152 lIndex
|= LOCAL_MASK
;
157 bool wxRegConfig::GetFirstEntry(wxString
& str
, long& lIndex
)
160 return GetNextGroup(str
, lIndex
);
163 bool wxRegConfig::GetNextEntry (wxString
& str
, long& lIndex
)
165 // are we already enumerating local entries?
166 if ( m_keyGlobal
.IsOpened() && !IS_LOCAL_INDEX(lIndex
) ) {
167 // try to find a global entry which doesn't appear locally
169 if ( !m_keyGlobal
.GetNextKey(str
, lIndex
) ) {
170 // no more global entries
171 lIndex
|= LOCAL_MASK
;
174 } while( m_keyLocal
.HasSubKey(str
) );
177 // much easier with local entries: get the next one we find
178 // (don't forget to clear our flag bit and set it again later)
179 lIndex
&= ~LOCAL_MASK
;
180 bool bOk
= m_keyLocal
.GetNextKey(str
, lIndex
);
181 lIndex
|= LOCAL_MASK
;
186 // ----------------------------------------------------------------------------
187 // tests for existence
188 // ----------------------------------------------------------------------------
190 bool wxRegConfig::HasGroup(const wxString
& strName
) const
192 return m_keyLocal
.HasSubKey(strName
) || m_keyGlobal
.HasSubKey(strName
);
195 bool wxRegConfig::HasEntry(const wxString
& strName
) const
197 return m_keyLocal
.HasValue(strName
) || m_keyGlobal
.HasValue(strName
);
200 // ----------------------------------------------------------------------------
202 // ----------------------------------------------------------------------------
204 bool wxRegConfig::Read(wxString
& str
,
206 const char *szDefault
) const
208 PathChanger
path(this, szKey
);
210 bool bQueryGlobal
= TRUE
;
212 // if immutable key exists in global key we must check that it's not
213 // overriden by the local key with the same name
214 if ( IsImmutable(path
.Name()) ) {
215 if ( TryGetValue(m_keyGlobal
, path
.Name(), str
) ) {
216 if ( m_keyLocal
.HasValue(path
.Name()) ) {
217 wxLogWarning("User value for immutable key '%s' ignored.",
218 path
.Name().c_str());
224 // don't waste time - it's not there anyhow
225 bQueryGlobal
= FALSE
;
229 // first try local key
230 if ( TryGetValue(m_keyLocal
, path
.Name(), str
) ||
231 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), str
)) ) {
240 bool wxRegConfig::Read(long &lValue
, const char *szKey
, long lDefault
) const
242 PathChanger
path(this, szKey
);
244 bool bQueryGlobal
= TRUE
;
246 // if immutable key exists in global key we must check that it's not
247 // overriden by the local key with the same name
248 if ( IsImmutable(path
.Name()) ) {
249 if ( TryGetValue(m_keyGlobal
, path
.Name(), &lValue
) ) {
250 if ( m_keyLocal
.HasValue(path
.Name()) ) {
251 wxLogWarning("User value for immutable key '%s' ignored.",
252 path
.Name().c_str());
258 // don't waste time - it's not there anyhow
259 bQueryGlobal
= FALSE
;
263 // first try local key
264 if ( TryGetValue(m_keyLocal
, path
.Name(), &lValue
) ||
265 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), &lValue
)) ) {
274 bool wxRegConfig::Write(const char *szKey
, const char *szValue
)
276 PathChanger
path(this, szKey
);
278 if ( IsImmutable(path
.Name()) ) {
279 wxLogError("Can't change immutable entry '%s'.", path
.Name().c_str());
283 return m_keyLocal
.SetValue(path
.Name(), szValue
);
286 bool wxRegConfig::Write(const char *szKey
, long lValue
)
288 PathChanger
path(this, szKey
);
290 if ( IsImmutable(path
.Name()) ) {
291 wxLogError("Can't change immutable entry '%s'.", path
.Name().c_str());
295 return m_keyLocal
.SetValue(path
.Name(), lValue
);
298 // ----------------------------------------------------------------------------
300 // ----------------------------------------------------------------------------
301 bool wxRegConfig::DeleteEntry(const char *szValue
, bool bGroupIfEmptyAlso
)
303 PathChanger
path(this, szValue
);
305 if ( !m_keyLocal
.DeleteValue(path
.Name()) )
308 if ( m_keyLocal
.IsEmpty() ) {
309 wxString strKey
= GetPath().Right(APPCONF_PATH_SEPARATOR
);
310 SetPath(".."); // changes m_keyLocal
311 return m_keyLocal
.DeleteKey(strKey
);
317 bool wxRegConfig::DeleteGroup(const char *szKey
)
319 PathChanger
path(this, szKey
);
321 return m_keyLocal
.DeleteKey(path
.Name());
324 bool wxRegConfig::DeleteAll()
326 // first of all, prevent the creation of new registry entries
327 Config::EnableAutosave(FALSE
);
332 bool bOk
= m_keyLocalRoot
.DeleteSelf();
334 bOk
= m_keyGlobalRoot
.DeleteSelf();