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