]> git.saurik.com Git - wxWidgets.git/blob - src/msw/regconf.cpp
globally renamed uint to size_t. This has _not_ been checked under Windows,
[wxWidgets.git] / src / msw / regconf.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/regconf.cpp
3 // Purpose:
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 27.04.98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "regconf.h"
14 #endif
15
16 // ============================================================================
17 // declarations
18 // ============================================================================
19
20 // ----------------------------------------------------------------------------
21 // headers
22 // ----------------------------------------------------------------------------
23
24 #ifdef __GNUG__
25 #pragma implementation "regconf.h"
26 #endif
27
28 #include "wx/wxprec.h"
29
30 #ifdef __BORLANDC__
31 #pragma hdrstop
32 #endif //__BORLANDC__
33
34 #ifndef WX_PRECOMP
35 #include <wx/string.h>
36 #endif //WX_PRECOMP
37
38 #include <wx/log.h>
39 #include <wx/config.h>
40 #include <wx/msw/registry.h>
41 #include <wx/msw/regconf.h>
42
43 // ----------------------------------------------------------------------------
44 // constants
45 // ----------------------------------------------------------------------------
46
47 // we put our data in HKLM\SOFTWARE_KEY\appname
48 #define SOFTWARE_KEY wxString("Software\\")
49
50 // ----------------------------------------------------------------------------
51 // global functions
52 // ----------------------------------------------------------------------------
53
54 // get the value if the key is opened and it exists
55 bool TryGetValue(const wxRegKey& key, const wxString& str, wxString& strVal)
56 {
57 return key.IsOpened() && key.HasValue(str) && key.QueryValue(str, strVal);
58 }
59
60 bool TryGetValue(const wxRegKey& key, const wxString& str, long *plVal)
61 {
62 return key.IsOpened() && key.HasValue(str) && key.QueryValue(str, plVal);
63 }
64
65 // ============================================================================
66 // implementation
67 // ============================================================================
68
69 // ----------------------------------------------------------------------------
70 // ctor/dtor
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, "")
77 {
78 // Create() will Open() if key already exists
79 m_keyLocalRoot.Create();
80
81 wxLogNull nolog;
82 m_keyGlobalRoot.Open();
83 }
84
85 wxRegConfig::~wxRegConfig()
86 {
87 // nothing to do - key will be closed in their dtors
88 }
89
90 // ----------------------------------------------------------------------------
91 // path management
92 // ----------------------------------------------------------------------------
93 void wxRegConfig::SetPath(const wxString& strPath)
94 {
95 wxArrayString aParts;
96
97 if ( strPath.IsEmpty() )
98 return;
99
100 if ( strPath[0] == wxCONFIG_PATH_SEPARATOR ) {
101 // absolute path
102 wxSplitPath(aParts, strPath);
103 }
104 else {
105 // relative path, combine with current one
106 wxString strFullPath = GetPath();
107 strFullPath << wxCONFIG_PATH_SEPARATOR << strPath;
108 wxSplitPath(aParts, strFullPath);
109 }
110
111 // recombine path parts in one variable
112 wxString strRegPath;
113 m_strPath.Empty();
114 for ( size_t n = 0; n < aParts.Count(); n++ ) {
115 strRegPath << '\\' << aParts[n];
116 m_strPath << wxCONFIG_PATH_SEPARATOR << aParts[n];
117 }
118
119 // change current key(s)
120 m_keyLocal.SetName(m_keyLocalRoot, strRegPath);
121 m_keyGlobal.SetName(m_keyGlobalRoot, strRegPath);
122 m_keyLocal.Create();
123
124 wxLogNull nolog;
125 m_keyGlobal.Open();
126 }
127
128 // ----------------------------------------------------------------------------
129 // enumeration (works only with current group)
130 // ----------------------------------------------------------------------------
131
132 /*
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
135 globally twice.
136
137 We use the 15th bit of lIndex for distinction between global and local.
138 */
139
140 #define LOCAL_MASK 0x8000
141 #define IS_LOCAL_INDEX(l) (((l) & LOCAL_MASK) != 0)
142
143 bool wxRegConfig::GetFirstGroup(wxString& str, long& lIndex) const
144 {
145 lIndex = 0;
146 return GetNextGroup(str, lIndex);
147 }
148
149 bool wxRegConfig::GetNextGroup(wxString& str, long& lIndex) const
150 {
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
154 do {
155 if ( !m_keyGlobal.GetNextKey(str, lIndex) ) {
156 // no more global entries
157 lIndex |= LOCAL_MASK;
158 break;
159 }
160 } while( m_keyLocal.HasSubKey(str) );
161 }
162
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;
168
169 return bOk;
170 }
171
172 bool wxRegConfig::GetFirstEntry(wxString& str, long& lIndex) const
173 {
174 lIndex = 0;
175 return GetNextEntry(str, lIndex);
176 }
177
178 bool wxRegConfig::GetNextEntry(wxString& str, long& lIndex) const
179 {
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
183 do {
184 if ( !m_keyGlobal.GetNextValue(str, lIndex) ) {
185 // no more global entries
186 lIndex |= LOCAL_MASK;
187 break;
188 }
189 } while( m_keyLocal.HasValue(str) );
190 }
191
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;
197
198 return bOk;
199 }
200
201 size_t wxRegConfig::GetNumberOfEntries(bool bRecursive) const
202 {
203 size_t nEntries = 0;
204
205 // dummy vars
206 wxString str;
207 long l;
208 bool bCont = ((wxRegConfig*)this)->GetFirstEntry(str, l);
209 while ( bCont ) {
210 nEntries++;
211
212 bCont = ((wxRegConfig*)this)->GetNextEntry(str, l);
213 }
214
215 return nEntries;
216 }
217
218 size_t wxRegConfig::GetNumberOfGroups(bool bRecursive) const
219 {
220 size_t nGroups = 0;
221
222 // dummy vars
223 wxString str;
224 long l;
225 bool bCont = ((wxRegConfig*)this)->GetFirstGroup(str, l);
226 while ( bCont ) {
227 nGroups++;
228
229 bCont = ((wxRegConfig*)this)->GetNextGroup(str, l);
230 }
231
232 return nGroups;
233 }
234
235 // ----------------------------------------------------------------------------
236 // tests for existence
237 // ----------------------------------------------------------------------------
238
239 bool wxRegConfig::HasGroup(const wxString& strName) const
240 {
241 return m_keyLocal.HasSubKey(strName) || m_keyGlobal.HasSubKey(strName);
242 }
243
244 bool wxRegConfig::HasEntry(const wxString& strName) const
245 {
246 return m_keyLocal.HasValue(strName) || m_keyGlobal.HasValue(strName);
247 }
248
249 // ----------------------------------------------------------------------------
250 // reading/writing
251 // ----------------------------------------------------------------------------
252
253 bool wxRegConfig::Read(wxString *pStr,
254 const char *szKey,
255 const char *szDefault) const
256 {
257 PathChanger path(this, szKey);
258
259 bool bQueryGlobal = TRUE;
260
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());
268 }
269
270 return TRUE;
271 }
272 else {
273 // don't waste time - it's not there anyhow
274 bQueryGlobal = FALSE;
275 }
276 }
277
278 // first try local key
279 if ( TryGetValue(m_keyLocal, path.Name(), *pStr) ||
280 (bQueryGlobal && TryGetValue(m_keyGlobal, path.Name(), *pStr)) ) {
281 return TRUE;
282 }
283
284 if(IsRecordingDefaults())
285 ((wxRegConfig*)this)->Write(szKey,szDefault);
286
287 // default value
288 *pStr = szDefault;
289 return FALSE;
290 }
291
292 bool wxRegConfig::Read(long *plResult, const char *szKey, long lDefault) const
293 {
294 PathChanger path(this, szKey);
295
296 bool bQueryGlobal = TRUE;
297
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());
305 }
306
307 return TRUE;
308 }
309 else {
310 // don't waste time - it's not there anyhow
311 bQueryGlobal = FALSE;
312 }
313 }
314
315 // first try local key
316 if ( TryGetValue(m_keyLocal, path.Name(), plResult) ||
317 (bQueryGlobal && TryGetValue(m_keyGlobal, path.Name(), plResult)) ) {
318 return TRUE;
319 }
320
321 // default
322 *plResult = lDefault;
323 return FALSE;
324 }
325
326 bool wxRegConfig::Write(const char *szKey, const char *szValue)
327 {
328 PathChanger path(this, szKey);
329
330 if ( IsImmutable(path.Name()) ) {
331 wxLogError("Can't change immutable entry '%s'.", path.Name().c_str());
332 return FALSE;
333 }
334
335 return m_keyLocal.SetValue(path.Name(), szValue);
336 }
337
338 bool wxRegConfig::Write(const char *szKey, long lValue)
339 {
340 PathChanger path(this, szKey);
341
342 if ( IsImmutable(path.Name()) ) {
343 wxLogError("Can't change immutable entry '%s'.", path.Name().c_str());
344 return FALSE;
345 }
346
347 return m_keyLocal.SetValue(path.Name(), lValue);
348 }
349
350 // ----------------------------------------------------------------------------
351 // deleting
352 // ----------------------------------------------------------------------------
353 bool wxRegConfig::DeleteEntry(const char *szValue, bool bGroupIfEmptyAlso)
354 {
355 PathChanger path(this, szValue);
356
357 if ( !m_keyLocal.DeleteValue(path.Name()) )
358 return FALSE;
359
360 if ( !m_keyLocal.HasSubkeys() ) {
361 wxString strKey = GetPath().Right(wxCONFIG_PATH_SEPARATOR);
362 SetPath(".."); // changes m_keyLocal
363 return m_keyLocal.DeleteKey(strKey);
364 }
365
366 return TRUE;
367 }
368
369 bool wxRegConfig::DeleteGroup(const char *szKey)
370 {
371 PathChanger path(this, szKey);
372
373 return m_keyLocal.DeleteKey(path.Name());
374 }
375
376 bool wxRegConfig::DeleteAll()
377 {
378 m_keyLocal.Close();
379 m_keyGlobal.Close();
380
381 bool bOk = m_keyLocalRoot.DeleteSelf();
382 if ( bOk )
383 bOk = m_keyGlobalRoot.DeleteSelf();
384
385 return bOk;
386 }