]> git.saurik.com Git - wxWidgets.git/blob - src/common/config.cpp
Fix to reflect wxUNIVERSAL conditions from header.
[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::Set(wxConfigBase *pConfig)
77 {
78 wxConfigBase *pOld = ms_pConfig;
79 ms_pConfig = pConfig;
80 return pOld;
81 }
82
83 wxConfigBase *wxConfigBase::Create()
84 {
85 if ( ms_bAutoCreate && ms_pConfig == NULL ) {
86 ms_pConfig =
87 #if defined(__WXMSW__) && wxUSE_CONFIG_NATIVE
88 new wxRegConfig(wxTheApp->GetAppName(), wxTheApp->GetVendorName());
89 #else // either we're under Unix or wish to use files even under Windows
90 new wxFileConfig(wxTheApp->GetAppName());
91 #endif
92 }
93
94 return ms_pConfig;
95 }
96
97 // ----------------------------------------------------------------------------
98 // wxConfigBase reading entries
99 // ----------------------------------------------------------------------------
100
101 // implement both Read() overloads for the given type in terms of DoRead()
102 #define IMPLEMENT_READ_FOR_TYPE(name, type, deftype, extra) \
103 bool wxConfigBase::Read(const wxString& key, type *val) const \
104 { \
105 wxCHECK_MSG( val, false, _T("wxConfig::Read(): NULL parameter") ); \
106 \
107 if ( !DoRead##name(key, val) ) \
108 return false; \
109 \
110 *val = extra(*val); \
111 \
112 return true; \
113 } \
114 \
115 bool wxConfigBase::Read(const wxString& key, \
116 type *val, \
117 deftype defVal) const \
118 { \
119 wxCHECK_MSG( val, false, _T("wxConfig::Read(): NULL parameter") ); \
120 \
121 bool read = DoRead##name(key, val); \
122 if ( !read ) \
123 { \
124 if ( IsRecordingDefaults() ) \
125 { \
126 ((wxConfigBase *)this)->DoWrite##name(key, defVal); \
127 } \
128 \
129 *val = defVal; \
130 } \
131 \
132 *val = extra(*val); \
133 \
134 return read; \
135 }
136
137
138 IMPLEMENT_READ_FOR_TYPE(String, wxString, const wxString&, ExpandEnvVars)
139 IMPLEMENT_READ_FOR_TYPE(Long, long, long, long)
140 IMPLEMENT_READ_FOR_TYPE(Int, int, int, int)
141 IMPLEMENT_READ_FOR_TYPE(Double, double, double, double)
142 IMPLEMENT_READ_FOR_TYPE(Bool, bool, bool, bool)
143
144 #undef IMPLEMENT_READ_FOR_TYPE
145
146 // the DoReadXXX() for the other types have implementation in the base class
147 // but can be overridden in the derived ones
148 bool wxConfigBase::DoReadInt(const wxString& key, int *pi) const
149 {
150 wxCHECK_MSG( pi, false, _T("wxConfig::Read(): NULL parameter") );
151
152 long l;
153 if ( !DoReadLong(key, &l) )
154 return false;
155
156 wxASSERT_MSG( l < INT_MAX, _T("overflow in wxConfig::DoReadInt") );
157
158 *pi = (int)l;
159
160 return true;
161 }
162
163 bool wxConfigBase::DoReadBool(const wxString& key, bool* val) const
164 {
165 wxCHECK_MSG( val, false, _T("wxConfig::Read(): NULL parameter") );
166
167 long l;
168 if ( !DoReadLong(key, &l) )
169 return false;
170
171 wxASSERT_MSG( l == 0 || l == 1, _T("bad bool value in wxConfig::DoReadInt") );
172
173 *val = l != 0;
174
175 return true;
176 }
177
178 bool wxConfigBase::DoReadDouble(const wxString& key, double* val) const
179 {
180 wxString str;
181 if ( Read(key, &str) )
182 {
183 return str.ToDouble(val);
184 }
185
186 return false;
187 }
188
189 // string reading helper
190 wxString wxConfigBase::ExpandEnvVars(const wxString& str) const
191 {
192 wxString tmp; // Required for BC++
193 if (IsExpandingEnvVars())
194 tmp = wxExpandEnvVars(str);
195 else
196 tmp = str;
197 return tmp;
198 }
199
200 // ----------------------------------------------------------------------------
201 // wxConfigBase writing
202 // ----------------------------------------------------------------------------
203
204 bool wxConfigBase::DoWriteDouble(const wxString& key, double val)
205 {
206 return DoWriteString(key, wxString::Format(_T("%g"), val));
207 }
208
209 bool wxConfigBase::DoWriteInt(const wxString& key, int value)
210 {
211 return DoWriteLong(key, (long)value);
212 }
213
214 bool wxConfigBase::DoWriteBool(const wxString& key, bool value)
215 {
216 return DoWriteLong(key, value ? 1l : 0l);
217 }
218
219 // ----------------------------------------------------------------------------
220 // wxConfigPathChanger
221 // ----------------------------------------------------------------------------
222
223 wxConfigPathChanger::wxConfigPathChanger(const wxConfigBase *pContainer,
224 const wxString& strEntry)
225 {
226 m_pContainer = (wxConfigBase *)pContainer;
227
228 // the path is everything which precedes the last slash
229 wxString strPath = strEntry.BeforeLast(wxCONFIG_PATH_SEPARATOR);
230
231 // except in the special case of "/keyname" when there is nothing before "/"
232 if ( strPath.IsEmpty() &&
233 ((!strEntry.IsEmpty()) && strEntry[0] == wxCONFIG_PATH_SEPARATOR) )
234 {
235 strPath = wxCONFIG_PATH_SEPARATOR;
236 }
237
238 if ( !strPath.IsEmpty() && m_pContainer->GetPath() != strPath ) {
239 // do change the path
240 m_bChanged = true;
241 m_strName = strEntry.AfterLast(wxCONFIG_PATH_SEPARATOR);
242 m_strOldPath = m_pContainer->GetPath();
243 if ( m_strOldPath.Len() == 0 ||
244 m_strOldPath.Last() != wxCONFIG_PATH_SEPARATOR )
245 m_strOldPath += wxCONFIG_PATH_SEPARATOR;
246 m_pContainer->SetPath(strPath);
247 }
248 else {
249 // it's a name only, without path - nothing to do
250 m_bChanged = false;
251 m_strName = strEntry;
252 }
253 }
254
255 wxConfigPathChanger::~wxConfigPathChanger()
256 {
257 // only restore path if it was changed
258 if ( m_bChanged ) {
259 m_pContainer->SetPath(m_strOldPath);
260 }
261 }
262
263 #endif // wxUSE_CONFIG
264
265 // ----------------------------------------------------------------------------
266 // static & global functions
267 // ----------------------------------------------------------------------------
268
269 // understands both Unix and Windows (but only under Windows) environment
270 // variables expansion: i.e. $var, $(var) and ${var} are always understood
271 // and in addition under Windows %var% is also.
272 wxString wxExpandEnvVars(const wxString& str)
273 {
274 wxString strResult;
275 strResult.Alloc(str.Len());
276
277 // don't change the values the enum elements: they must be equal
278 // to the matching [closing] delimiter.
279 enum Bracket
280 {
281 Bracket_None,
282 Bracket_Normal = ')',
283 Bracket_Curly = '}',
284 #ifdef __WXMSW__
285 Bracket_Windows = '%', // yeah, Windows people are a bit strange ;-)
286 #endif
287 Bracket_Max
288 };
289
290 size_t m;
291 for ( size_t n = 0; n < str.Len(); n++ ) {
292 switch ( str[n] ) {
293 #ifdef __WXMSW__
294 case wxT('%'):
295 #endif //WINDOWS
296 case wxT('$'):
297 {
298 Bracket bracket;
299 #ifdef __WXMSW__
300 if ( str[n] == wxT('%') )
301 bracket = Bracket_Windows;
302 else
303 #endif //WINDOWS
304 if ( n == str.Len() - 1 ) {
305 bracket = Bracket_None;
306 }
307 else {
308 switch ( str[n + 1] ) {
309 case wxT('('):
310 bracket = Bracket_Normal;
311 n++; // skip the bracket
312 break;
313
314 case wxT('{'):
315 bracket = Bracket_Curly;
316 n++; // skip the bracket
317 break;
318
319 default:
320 bracket = Bracket_None;
321 }
322 }
323
324 m = n + 1;
325
326 while ( m < str.Len() && (wxIsalnum(str[m]) || str[m] == wxT('_')) )
327 m++;
328
329 wxString strVarName(str.c_str() + n + 1, m - n - 1);
330
331 #ifdef __WXWINCE__
332 const wxChar *pszValue = NULL;
333 #else
334 const wxChar *pszValue = wxGetenv(strVarName);
335 #endif
336 if ( pszValue != NULL ) {
337 strResult += pszValue;
338 }
339 else {
340 // variable doesn't exist => don't change anything
341 #ifdef __WXMSW__
342 if ( bracket != Bracket_Windows )
343 #endif
344 if ( bracket != Bracket_None )
345 strResult << str[n - 1];
346 strResult << str[n] << strVarName;
347 }
348
349 // check the closing bracket
350 if ( bracket != Bracket_None ) {
351 if ( m == str.Len() || str[m] != (wxChar)bracket ) {
352 // under MSW it's common to have '%' characters in the registry
353 // and it's annoying to have warnings about them each time, so
354 // ignroe them silently if they are not used for env vars
355 //
356 // under Unix, OTOH, this warning could be useful for the user to
357 // understand why isn't the variable expanded as intended
358 #ifndef __WXMSW__
359 wxLogWarning(_("Environment variables expansion failed: missing '%c' at position %d in '%s'."),
360 (char)bracket, m + 1, str.c_str());
361 #endif // __WXMSW__
362 }
363 else {
364 // skip closing bracket unless the variables wasn't expanded
365 if ( pszValue == NULL )
366 strResult << (char)bracket;
367 m++;
368 }
369 }
370
371 n = m - 1; // skip variable name
372 }
373 break;
374
375 case '\\':
376 // backslash can be used to suppress special meaning of % and $
377 if ( n != str.Len() && (str[n + 1] == wxT('%') || str[n + 1] == wxT('$')) ) {
378 strResult += str[++n];
379
380 break;
381 }
382 //else: fall through
383
384 default:
385 strResult += str[n];
386 }
387 }
388
389 return strResult;
390 }
391
392 // this function is used to properly interpret '..' in path
393 void wxSplitPath(wxArrayString& aParts, const wxChar *sz)
394 {
395 aParts.clear();
396
397 wxString strCurrent;
398 const wxChar *pc = sz;
399 for ( ;; ) {
400 if ( *pc == wxT('\0') || *pc == wxCONFIG_PATH_SEPARATOR ) {
401 if ( strCurrent == wxT(".") ) {
402 // ignore
403 }
404 else if ( strCurrent == wxT("..") ) {
405 // go up one level
406 if ( aParts.size() == 0 )
407 wxLogWarning(_("'%s' has extra '..', ignored."), sz);
408 else
409 aParts.erase(aParts.end() - 1);
410
411 strCurrent.Empty();
412 }
413 else if ( !strCurrent.IsEmpty() ) {
414 aParts.push_back(strCurrent);
415 strCurrent.Empty();
416 }
417 //else:
418 // could log an error here, but we prefer to ignore extra '/'
419
420 if ( *pc == wxT('\0') )
421 break;
422 }
423 else
424 strCurrent += *pc;
425
426 pc++;
427 }
428 }
429
430