]> git.saurik.com Git - wxWidgets.git/blob - src/common/config.cpp
removed cleanup module which was only used for working around bugs in memory debuggin...
[wxWidgets.git] / src / common / config.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: config.cpp
3 // Purpose: implementation of wxConfigBase class
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 07.04.98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1997 Karsten Ballüder Ballueder@usa.net
9 // Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
10 // Licence: wxWindows licence
11 ///////////////////////////////////////////////////////////////////////////////
12
13 // ----------------------------------------------------------------------------
14 // headers
15 // ----------------------------------------------------------------------------
16 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
17 #pragma implementation "confbase.h"
18 #endif
19
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif //__BORLANDC__
25
26 #ifndef wxUSE_CONFIG_NATIVE
27 #define wxUSE_CONFIG_NATIVE 1
28 #endif
29
30 #include "wx/config.h"
31 #include "wx/intl.h"
32 #include "wx/log.h"
33 #include "wx/arrstr.h"
34
35 #if wxUSE_CONFIG && ((wxUSE_FILE && wxUSE_TEXTFILE) || wxUSE_CONFIG_NATIVE)
36
37 #include "wx/app.h"
38 #include "wx/file.h"
39 #include "wx/textfile.h"
40 #include "wx/utils.h"
41 #include "wx/utils.h"
42
43 #include <stdlib.h>
44 #include <math.h>
45 #include <ctype.h>
46 #include <limits.h> // for INT_MAX
47
48 // ----------------------------------------------------------------------------
49 // global and class static variables
50 // ----------------------------------------------------------------------------
51
52 wxConfigBase *wxConfigBase::ms_pConfig = NULL;
53 bool wxConfigBase::ms_bAutoCreate = true;
54
55 // ============================================================================
56 // implementation
57 // ============================================================================
58
59 // ----------------------------------------------------------------------------
60 // wxConfigBase
61 // ----------------------------------------------------------------------------
62
63 // Not all args will always be used by derived classes, but including them all
64 // in each class ensures compatibility.
65 wxConfigBase::wxConfigBase(const wxString& appName,
66 const wxString& vendorName,
67 const wxString& WXUNUSED(localFilename),
68 const wxString& WXUNUSED(globalFilename),
69 long style)
70 : m_appName(appName), m_vendorName(vendorName), m_style(style)
71 {
72 m_bExpandEnvVars = true;
73 m_bRecordDefaults = false;
74 }
75
76 wxConfigBase::~wxConfigBase()
77 {
78 // required here for Darwin
79 }
80
81 wxConfigBase *wxConfigBase::Set(wxConfigBase *pConfig)
82 {
83 wxConfigBase *pOld = ms_pConfig;
84 ms_pConfig = pConfig;
85 return pOld;
86 }
87
88 wxConfigBase *wxConfigBase::Create()
89 {
90 if ( ms_bAutoCreate && ms_pConfig == NULL ) {
91 ms_pConfig =
92 #if defined(__WXMSW__) && wxUSE_CONFIG_NATIVE
93 new wxRegConfig(wxTheApp->GetAppName(), wxTheApp->GetVendorName());
94 #else // either we're under Unix or wish to use files even under Windows
95 new wxFileConfig(wxTheApp->GetAppName());
96 #endif
97 }
98
99 return ms_pConfig;
100 }
101
102 // ----------------------------------------------------------------------------
103 // wxConfigBase reading entries
104 // ----------------------------------------------------------------------------
105
106 // implement both Read() overloads for the given type in terms of DoRead()
107 #define IMPLEMENT_READ_FOR_TYPE(name, type, deftype, extra) \
108 bool wxConfigBase::Read(const wxString& key, type *val) const \
109 { \
110 wxCHECK_MSG( val, false, _T("wxConfig::Read(): NULL parameter") ); \
111 \
112 if ( !DoRead##name(key, val) ) \
113 return false; \
114 \
115 *val = extra(*val); \
116 \
117 return true; \
118 } \
119 \
120 bool wxConfigBase::Read(const wxString& key, \
121 type *val, \
122 deftype defVal) const \
123 { \
124 wxCHECK_MSG( val, false, _T("wxConfig::Read(): NULL parameter") ); \
125 \
126 bool read = DoRead##name(key, val); \
127 if ( !read ) \
128 { \
129 if ( IsRecordingDefaults() ) \
130 { \
131 ((wxConfigBase *)this)->DoWrite##name(key, defVal); \
132 } \
133 \
134 *val = defVal; \
135 } \
136 \
137 *val = extra(*val); \
138 \
139 return read; \
140 }
141
142
143 IMPLEMENT_READ_FOR_TYPE(String, wxString, const wxString&, ExpandEnvVars)
144 IMPLEMENT_READ_FOR_TYPE(Long, long, long, long)
145 IMPLEMENT_READ_FOR_TYPE(Int, int, int, int)
146 IMPLEMENT_READ_FOR_TYPE(Double, double, double, double)
147 IMPLEMENT_READ_FOR_TYPE(Bool, bool, bool, bool)
148
149 #undef IMPLEMENT_READ_FOR_TYPE
150
151 // the DoReadXXX() for the other types have implementation in the base class
152 // but can be overridden in the derived ones
153 bool wxConfigBase::DoReadInt(const wxString& key, int *pi) const
154 {
155 wxCHECK_MSG( pi, false, _T("wxConfig::Read(): NULL parameter") );
156
157 long l;
158 if ( !DoReadLong(key, &l) )
159 return false;
160
161 wxASSERT_MSG( l < INT_MAX, _T("overflow in wxConfig::DoReadInt") );
162
163 *pi = (int)l;
164
165 return true;
166 }
167
168 bool wxConfigBase::DoReadBool(const wxString& key, bool* val) const
169 {
170 wxCHECK_MSG( val, false, _T("wxConfig::Read(): NULL parameter") );
171
172 long l;
173 if ( !DoReadLong(key, &l) )
174 return false;
175
176 wxASSERT_MSG( l == 0 || l == 1, _T("bad bool value in wxConfig::DoReadInt") );
177
178 *val = l != 0;
179
180 return true;
181 }
182
183 bool wxConfigBase::DoReadDouble(const wxString& key, double* val) const
184 {
185 wxString str;
186 if ( Read(key, &str) )
187 {
188 return str.ToDouble(val);
189 }
190
191 return false;
192 }
193
194 // string reading helper
195 wxString wxConfigBase::ExpandEnvVars(const wxString& str) const
196 {
197 wxString tmp; // Required for BC++
198 if (IsExpandingEnvVars())
199 tmp = wxExpandEnvVars(str);
200 else
201 tmp = str;
202 return tmp;
203 }
204
205 // ----------------------------------------------------------------------------
206 // wxConfigBase writing
207 // ----------------------------------------------------------------------------
208
209 bool wxConfigBase::DoWriteDouble(const wxString& key, double val)
210 {
211 return DoWriteString(key, wxString::Format(_T("%g"), val));
212 }
213
214 bool wxConfigBase::DoWriteInt(const wxString& key, int value)
215 {
216 return DoWriteLong(key, (long)value);
217 }
218
219 bool wxConfigBase::DoWriteBool(const wxString& key, bool value)
220 {
221 return DoWriteLong(key, value ? 1l : 0l);
222 }
223
224 // ----------------------------------------------------------------------------
225 // wxConfigPathChanger
226 // ----------------------------------------------------------------------------
227
228 wxConfigPathChanger::wxConfigPathChanger(const wxConfigBase *pContainer,
229 const wxString& strEntry)
230 {
231 m_pContainer = (wxConfigBase *)pContainer;
232
233 // the path is everything which precedes the last slash
234 wxString strPath = strEntry.BeforeLast(wxCONFIG_PATH_SEPARATOR);
235
236 // except in the special case of "/keyname" when there is nothing before "/"
237 if ( strPath.IsEmpty() &&
238 ((!strEntry.IsEmpty()) && strEntry[0] == wxCONFIG_PATH_SEPARATOR) )
239 {
240 strPath = wxCONFIG_PATH_SEPARATOR;
241 }
242
243 if ( !strPath.IsEmpty() )
244 {
245 if ( m_pContainer->GetPath() != strPath )
246 {
247 // do change the path
248 m_bChanged = true;
249 m_strOldPath = m_pContainer->GetPath();
250 if ( *m_strOldPath.c_str() != wxCONFIG_PATH_SEPARATOR )
251 m_strOldPath += wxCONFIG_PATH_SEPARATOR;
252 m_pContainer->SetPath(strPath);
253 }
254
255 // in any case, use the just the name, not full path
256 m_strName = strEntry.AfterLast(wxCONFIG_PATH_SEPARATOR);
257 }
258 else {
259 // it's a name only, without path - nothing to do
260 m_bChanged = false;
261 m_strName = strEntry;
262 }
263 }
264
265 wxConfigPathChanger::~wxConfigPathChanger()
266 {
267 // only restore path if it was changed
268 if ( m_bChanged ) {
269 m_pContainer->SetPath(m_strOldPath);
270 }
271 }
272
273 #endif // wxUSE_CONFIG
274
275 // ----------------------------------------------------------------------------
276 // static & global functions
277 // ----------------------------------------------------------------------------
278
279 // understands both Unix and Windows (but only under Windows) environment
280 // variables expansion: i.e. $var, $(var) and ${var} are always understood
281 // and in addition under Windows %var% is also.
282 wxString wxExpandEnvVars(const wxString& str)
283 {
284 wxString strResult;
285 strResult.Alloc(str.Len());
286
287 // don't change the values the enum elements: they must be equal
288 // to the matching [closing] delimiter.
289 enum Bracket
290 {
291 Bracket_None,
292 Bracket_Normal = ')',
293 Bracket_Curly = '}',
294 #ifdef __WXMSW__
295 Bracket_Windows = '%', // yeah, Windows people are a bit strange ;-)
296 #endif
297 Bracket_Max
298 };
299
300 size_t m;
301 for ( size_t n = 0; n < str.Len(); n++ ) {
302 switch ( str[n] ) {
303 #ifdef __WXMSW__
304 case wxT('%'):
305 #endif //WINDOWS
306 case wxT('$'):
307 {
308 Bracket bracket;
309 #ifdef __WXMSW__
310 if ( str[n] == wxT('%') )
311 bracket = Bracket_Windows;
312 else
313 #endif //WINDOWS
314 if ( n == str.Len() - 1 ) {
315 bracket = Bracket_None;
316 }
317 else {
318 switch ( str[n + 1] ) {
319 case wxT('('):
320 bracket = Bracket_Normal;
321 n++; // skip the bracket
322 break;
323
324 case wxT('{'):
325 bracket = Bracket_Curly;
326 n++; // skip the bracket
327 break;
328
329 default:
330 bracket = Bracket_None;
331 }
332 }
333
334 m = n + 1;
335
336 while ( m < str.Len() && (wxIsalnum(str[m]) || str[m] == wxT('_')) )
337 m++;
338
339 wxString strVarName(str.c_str() + n + 1, m - n - 1);
340
341 #ifdef __WXWINCE__
342 const wxChar *pszValue = NULL;
343 #else
344 const wxChar *pszValue = wxGetenv(strVarName);
345 #endif
346 if ( pszValue != NULL ) {
347 strResult += pszValue;
348 }
349 else {
350 // variable doesn't exist => don't change anything
351 #ifdef __WXMSW__
352 if ( bracket != Bracket_Windows )
353 #endif
354 if ( bracket != Bracket_None )
355 strResult << str[n - 1];
356 strResult << str[n] << strVarName;
357 }
358
359 // check the closing bracket
360 if ( bracket != Bracket_None ) {
361 if ( m == str.Len() || str[m] != (wxChar)bracket ) {
362 // under MSW it's common to have '%' characters in the registry
363 // and it's annoying to have warnings about them each time, so
364 // ignroe them silently if they are not used for env vars
365 //
366 // under Unix, OTOH, this warning could be useful for the user to
367 // understand why isn't the variable expanded as intended
368 #ifndef __WXMSW__
369 wxLogWarning(_("Environment variables expansion failed: missing '%c' at position %d in '%s'."),
370 (char)bracket, m + 1, str.c_str());
371 #endif // __WXMSW__
372 }
373 else {
374 // skip closing bracket unless the variables wasn't expanded
375 if ( pszValue == NULL )
376 strResult << (char)bracket;
377 m++;
378 }
379 }
380
381 n = m - 1; // skip variable name
382 }
383 break;
384
385 case '\\':
386 // backslash can be used to suppress special meaning of % and $
387 if ( n != str.Len() - 1 &&
388 (str[n + 1] == wxT('%') || str[n + 1] == wxT('$')) ) {
389 strResult += str[++n];
390
391 break;
392 }
393 //else: fall through
394
395 default:
396 strResult += str[n];
397 }
398 }
399
400 return strResult;
401 }
402
403 // this function is used to properly interpret '..' in path
404 void wxSplitPath(wxArrayString& aParts, const wxChar *sz)
405 {
406 aParts.clear();
407
408 wxString strCurrent;
409 const wxChar *pc = sz;
410 for ( ;; ) {
411 if ( *pc == wxT('\0') || *pc == wxCONFIG_PATH_SEPARATOR ) {
412 if ( strCurrent == wxT(".") ) {
413 // ignore
414 }
415 else if ( strCurrent == wxT("..") ) {
416 // go up one level
417 if ( aParts.size() == 0 )
418 wxLogWarning(_("'%s' has extra '..', ignored."), sz);
419 else
420 aParts.erase(aParts.end() - 1);
421
422 strCurrent.Empty();
423 }
424 else if ( !strCurrent.IsEmpty() ) {
425 aParts.push_back(strCurrent);
426 strCurrent.Empty();
427 }
428 //else:
429 // could log an error here, but we prefer to ignore extra '/'
430
431 if ( *pc == wxT('\0') )
432 break;
433 }
434 else
435 strCurrent += *pc;
436
437 pc++;
438 }
439 }
440
441