]>
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 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include <wx/config.h>
21 #include <wx/regconf.h>
22 #include <wx/registry.h>
24 // ----------------------------------------------------------------------------
26 // ----------------------------------------------------------------------------
28 // we put our data in HKLM\SOFTWARE_KEY\appname
29 #define SOFTWARE_KEY wxString("Software\\")
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
35 // get the value if the key is opened and it exists
36 bool TryGetValue(const wxRegKey
& key
, const wxString
& str
, wxString
& strVal
)
38 return key
.IsOpened() && key
.HasValue(str
) && key
.QueryValue(str
, strVal
);
41 bool TryGetValue(const wxRegKey
& key
, const wxString
& str
, long *plVal
)
43 return key
.IsOpened() && key
.HasValue(str
) && key
.QueryValue(str
, plVal
);
46 // ============================================================================
48 // ============================================================================
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
53 wxRegConfig::wxRegConfig(const wxString
& strRoot
)
54 : m_keyLocalRoot(wxRegKey::HKCU
, SOFTWARE_KEY
+ strRoot
),
55 m_keyLocal(m_keyLocalRoot
, ""),
56 m_keyGlobalRoot(wxRegKey::HKLM
, SOFTWARE_KEY
+ strRoot
),
57 m_keyGlobal(m_keyGlobalRoot
, "")
59 // Create() will Open() if key already exists
60 m_keyLocalRoot
.Create();
63 m_keyGlobalRoot
.Open();
66 wxRegConfig::~wxRegConfig()
68 // nothing to do - key will be closed in their dtors
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
74 void wxRegConfig::SetPath(const wxString
& strPath
)
78 if ( strPath
.IsEmpty() )
81 if ( strPath
[0] == APPCONF_PATH_SEPARATOR
) {
83 SplitPath(aParts
, strPath
);
86 // relative path, combine with current one
87 wxString strFullPath
= GetPath();
88 strFullPath
<< APPCONF_PATH_SEPARATOR
<< strPath
;
89 SplitPath(aParts
, strFullPath
);
92 // recombine path parts in one variable
95 for ( uint n
= 0; n
< aParts
.Count(); n
++ ) {
96 strRegPath
<< '\\' << aParts
[n
];
97 m_strPath
<< APPCONF_PATH_SEPARATOR
<< aParts
[n
];
100 // change current key(s)
101 m_keyLocal
.SetName(m_keyLocalRoot
, strRegPath
);
102 m_keyGlobal
.SetName(m_keyGlobalRoot
, strRegPath
);
109 // ----------------------------------------------------------------------------
110 // enumeration (works only with current group)
111 // ----------------------------------------------------------------------------
114 We want to enumerate all local keys/values after the global ones, but, of
115 course, we don't want to repeat a key which appears locally as well as
118 We use the 15th bit of lIndex for distinction between global and local.
121 #define LOCAL_MASK 0x8000
122 #define IS_LOCAL_INDEX(l) (((l) & LOCAL_MASK) != 0)
124 bool wxRegConfig::GetFirstGroup(wxString
& str
, long& lIndex
)
127 return GetNextGroup(str
, lIndex
);
130 bool wxRegConfig::GetNextGroup (wxString
& str
, long& lIndex
)
132 // are we already enumerating local entries?
133 if ( m_keyGlobal
.IsOpened() && !IS_LOCAL_INDEX(lIndex
) ) {
134 // try to find a global entry which doesn't appear locally
136 if ( !m_keyGlobal
.GetNextKey(str
, lIndex
) ) {
137 // no more global entries
138 lIndex
|= LOCAL_MASK
;
141 } while( m_keyLocal
.HasSubKey(str
) );
144 // much easier with local entries: get the next one we find
145 // (don't forget to clear our flag bit and set it again later)
146 lIndex
&= ~LOCAL_MASK
;
147 bool bOk
= m_keyLocal
.GetNextKey(str
, lIndex
);
148 lIndex
|= LOCAL_MASK
;
153 bool wxRegConfig::GetFirstEntry(wxString
& str
, long& lIndex
)
156 return GetNextGroup(str
, lIndex
);
159 bool wxRegConfig::GetNextEntry (wxString
& str
, long& lIndex
)
161 // are we already enumerating local entries?
162 if ( m_keyGlobal
.IsOpened() && !IS_LOCAL_INDEX(lIndex
) ) {
163 // try to find a global entry which doesn't appear locally
165 if ( !m_keyGlobal
.GetNextKey(str
, lIndex
) ) {
166 // no more global entries
167 lIndex
|= LOCAL_MASK
;
170 } while( m_keyLocal
.HasSubKey(str
) );
173 // much easier with local entries: get the next one we find
174 // (don't forget to clear our flag bit and set it again later)
175 lIndex
&= ~LOCAL_MASK
;
176 bool bOk
= m_keyLocal
.GetNextKey(str
, lIndex
);
177 lIndex
|= LOCAL_MASK
;
182 // ----------------------------------------------------------------------------
184 // ----------------------------------------------------------------------------
186 bool wxRegConfig::Read(wxString
& str
,
188 const char *szDefault
) const
190 PathChanger
path(this, szKey
);
192 bool bQueryGlobal
= true;
194 // if immutable key exists in global key we must check that it's not
195 // overriden by the local key with the same name
196 if ( IsImmutable(path
.Name()) ) {
197 if ( TryGetValue(m_keyGlobal
, path
.Name(), str
) ) {
198 if ( m_keyLocal
.HasValue(path
.Name()) ) {
199 wxLogWarning("User value for immutable key '%s' ignored.",
200 path
.Name().c_str());
206 // don't waste time - it's not there anyhow
207 bQueryGlobal
= false;
211 // first try local key
212 if ( TryGetValue(m_keyLocal
, path
.Name(), str
) ||
213 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), str
)) ) {
222 bool wxRegConfig::Read(long &lValue
, const char *szKey
, long lDefault
) const
224 PathChanger
path(this, szKey
);
226 bool bQueryGlobal
= true;
228 // if immutable key exists in global key we must check that it's not
229 // overriden by the local key with the same name
230 if ( IsImmutable(path
.Name()) ) {
231 if ( TryGetValue(m_keyGlobal
, path
.Name(), &lValue
) ) {
232 if ( m_keyLocal
.HasValue(path
.Name()) ) {
233 wxLogWarning("User value for immutable key '%s' ignored.",
234 path
.Name().c_str());
240 // don't waste time - it's not there anyhow
241 bQueryGlobal
= false;
245 // first try local key
246 if ( TryGetValue(m_keyLocal
, path
.Name(), &lValue
) ||
247 (bQueryGlobal
&& TryGetValue(m_keyGlobal
, path
.Name(), &lValue
)) ) {
256 bool wxRegConfig::Write(const char *szKey
, const char *szValue
)
258 PathChanger
path(this, szKey
);
260 if ( IsImmutable(path
.Name()) ) {
261 wxLogError("Can't change immutable entry '%s'.", path
.Name().c_str());
265 return m_keyLocal
.SetValue(path
.Name(), szValue
);
268 bool wxRegConfig::Write(const char *szKey
, long lValue
)
270 PathChanger
path(this, szKey
);
272 if ( IsImmutable(path
.Name()) ) {
273 wxLogError("Can't change immutable entry '%s'.", path
.Name().c_str());
277 return m_keyLocal
.SetValue(path
.Name(), lValue
);
280 // ----------------------------------------------------------------------------
282 // ----------------------------------------------------------------------------
283 bool wxRegConfig::DeleteEntry(const char *szValue
, bool bGroupIfEmptyAlso
)
285 PathChanger
path(this, szValue
);
287 if ( !m_keyLocal
.DeleteValue(path
.Name()) )
290 if ( m_keyLocal
.IsEmpty() ) {
291 wxString strKey
= GetPath().Right(APPCONF_PATH_SEPARATOR
);
292 SetPath(".."); // changes m_keyLocal
293 return m_keyLocal
.DeleteKey(strKey
);
299 bool wxRegConfig::DeleteGroup(const char *szKey
)
301 PathChanger
path(this, szKey
);
303 return m_keyLocal
.DeleteKey(path
.Name());
306 bool wxRegConfig::DeleteAll()
308 // first of all, prevent the creation of new registry entries
309 Config::EnableAutosave(false);
314 bool bOk
= m_keyLocalRoot
.DeleteSelf();
316 bOk
= m_keyGlobalRoot
.DeleteSelf();