]> git.saurik.com Git - wxWidgets.git/blob - src/common/config.cpp
corrected virtual function prototype
[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 license
11 ///////////////////////////////////////////////////////////////////////////////
12
13 // ----------------------------------------------------------------------------
14 // headers
15 // ----------------------------------------------------------------------------
16 #ifdef __GNUG__
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
32 #if wxUSE_CONFIG && ((wxUSE_FILE && wxUSE_TEXTFILE) || wxUSE_CONFIG_NATIVE)
33
34 #include "wx/app.h"
35 #include "wx/file.h"
36 #include "wx/log.h"
37 #include "wx/textfile.h"
38 #include "wx/utils.h"
39 #include "wx/log.h"
40 #include "wx/utils.h"
41 #include "wx/intl.h"
42
43 #include <stdlib.h>
44 #include <math.h>
45 #include <ctype.h>
46
47 // ----------------------------------------------------------------------------
48 // global and class static variables
49 // ----------------------------------------------------------------------------
50
51 wxConfigBase *wxConfigBase::ms_pConfig = NULL;
52 bool wxConfigBase::ms_bAutoCreate = TRUE;
53
54 // ============================================================================
55 // implementation
56 // ============================================================================
57
58 // ----------------------------------------------------------------------------
59 // wxConfigBase
60 // ----------------------------------------------------------------------------
61
62 // Not all args will always be used by derived classes, but including them all
63 // in each class ensures compatibility.
64 wxConfigBase::wxConfigBase(const wxString& appName,
65 const wxString& vendorName,
66 const wxString& WXUNUSED(localFilename),
67 const wxString& WXUNUSED(globalFilename),
68 long style)
69 : m_appName(appName), m_vendorName(vendorName), m_style(style)
70 {
71 m_bExpandEnvVars = TRUE; m_bRecordDefaults = FALSE;
72 }
73
74 wxConfigBase::~wxConfigBase()
75 {
76 }
77
78 wxConfigBase *wxConfigBase::Set(wxConfigBase *pConfig)
79 {
80 wxConfigBase *pOld = ms_pConfig;
81 ms_pConfig = pConfig;
82 return pOld;
83 }
84
85 wxConfigBase *wxConfigBase::Create()
86 {
87 if ( ms_bAutoCreate && ms_pConfig == NULL ) {
88 ms_pConfig =
89 #if defined(__WXMSW__) && wxUSE_CONFIG_NATIVE
90 #ifdef __WIN32__
91 new wxRegConfig(wxTheApp->GetAppName(), wxTheApp->GetVendorName());
92 #else //WIN16
93 new wxIniConfig(wxTheApp->GetAppName(), wxTheApp->GetVendorName());
94 #endif
95 #else // either we're under Unix or wish to use files even under Windows
96 new wxFileConfig(wxTheApp->GetAppName());
97 #endif
98 }
99
100 return ms_pConfig;
101 }
102
103 wxString wxConfigBase::Read(const wxString& key, const wxString& defVal) const
104 {
105 wxString s;
106 Read(key, &s, defVal);
107 return s;
108 }
109
110 bool wxConfigBase::Read(const wxString& key, wxString *str, const wxString& defVal) const
111 {
112 if (!Read(key, str))
113 {
114 *str = ExpandEnvVars(defVal);
115 return FALSE;
116 }
117 else
118 return TRUE;
119 }
120
121 bool wxConfigBase::Read(const wxString& key, long *pl, long defVal) const
122 {
123 if (!Read(key, pl))
124 {
125 *pl = defVal;
126 return FALSE;
127 }
128 else
129 return TRUE;
130 }
131
132 bool wxConfigBase::Read(const wxString& key, double* val) const
133 {
134 wxString str;
135 if ( Read(key, &str) )
136 {
137 return str.ToDouble(val);
138 }
139
140 return FALSE;
141 }
142
143 bool wxConfigBase::Read(const wxString& key, double* val, double defVal) const
144 {
145 if (!Read(key, val))
146 {
147 *val = defVal;
148 return FALSE;
149 }
150 else
151 return TRUE;
152 }
153
154 bool wxConfigBase::Read(const wxString& key, bool* val) const
155 {
156 long l;
157 if (Read(key, & l))
158 {
159 *val = (l != 0);
160 return TRUE;
161 }
162 else
163 return FALSE;
164 }
165
166 bool wxConfigBase::Read(const wxString& key, bool* val, bool defVal) const
167 {
168 if (!Read(key, val))
169 {
170 *val = defVal;
171 return FALSE;
172 }
173 else
174 return TRUE;
175 }
176
177 // Convenience functions
178
179 bool wxConfigBase::Read(const wxString& key, int *pi) const
180 {
181 long l;
182 bool ret = Read(key, &l);
183 if (ret)
184 *pi = (int) l;
185 return ret;
186 }
187
188 bool wxConfigBase::Read(const wxString& key, int *pi, int defVal) const
189 {
190 long l;
191 bool ret = Read(key, &l, (long) defVal);
192 if (ret)
193 *pi = (int) l;
194 return ret;
195 }
196
197 bool wxConfigBase::Write(const wxString& key, double val)
198 {
199 wxString str;
200 str.Printf(wxT("%g"), val);
201 return Write(key, str);
202 }
203
204 bool wxConfigBase::Write(const wxString& key, bool value)
205 {
206 return Write(key, value ? 1l : 0l);
207 }
208
209 bool wxConfigBase::Write(const wxString& key, const wxChar *value)
210 {
211 // explicit cast needed, otherwise value would have been converted to bool
212 return Write(key, wxString(value));
213 }
214
215 wxString wxConfigBase::ExpandEnvVars(const wxString& str) const
216 {
217 wxString tmp; // Required for BC++
218 if (IsExpandingEnvVars())
219 tmp = wxExpandEnvVars(str);
220 else
221 tmp = str;
222 return tmp;
223 }
224
225 // ----------------------------------------------------------------------------
226 // wxConfigPathChanger
227 // ----------------------------------------------------------------------------
228
229 wxConfigPathChanger::wxConfigPathChanger(const wxConfigBase *pContainer,
230 const wxString& strEntry)
231 {
232 m_pContainer = (wxConfigBase *)pContainer;
233
234 // the path is everything which precedes the last slash
235 wxString strPath = strEntry.BeforeLast(wxCONFIG_PATH_SEPARATOR);
236
237 // except in the special case of "/keyname" when there is nothing before "/"
238 if ( strPath.IsEmpty() &&
239 ((!strEntry.IsEmpty()) && strEntry[0] == wxCONFIG_PATH_SEPARATOR) )
240 {
241 strPath = wxCONFIG_PATH_SEPARATOR;
242 }
243
244 if ( !strPath.IsEmpty() ) {
245 // do change the path
246 m_bChanged = TRUE;
247 m_strName = strEntry.AfterLast(wxCONFIG_PATH_SEPARATOR);
248 m_strOldPath = m_pContainer->GetPath();
249 if ( m_strOldPath.Len() == 0 ||
250 m_strOldPath.Last() != wxCONFIG_PATH_SEPARATOR )
251 m_strOldPath += wxCONFIG_PATH_SEPARATOR;
252 m_pContainer->SetPath(strPath);
253 }
254 else {
255 // it's a name only, without path - nothing to do
256 m_bChanged = FALSE;
257 m_strName = strEntry;
258 }
259 }
260
261 wxConfigPathChanger::~wxConfigPathChanger()
262 {
263 // only restore path if it was changed
264 if ( m_bChanged ) {
265 m_pContainer->SetPath(m_strOldPath);
266 }
267 }
268
269 #endif // wxUSE_CONFIG
270
271 // ----------------------------------------------------------------------------
272 // static & global functions
273 // ----------------------------------------------------------------------------
274
275 // understands both Unix and Windows (but only under Windows) environment
276 // variables expansion: i.e. $var, $(var) and ${var} are always understood
277 // and in addition under Windows %var% is also.
278 wxString wxExpandEnvVars(const wxString& str)
279 {
280 wxString strResult;
281 strResult.Alloc(str.Len());
282
283 // don't change the values the enum elements: they must be equal
284 // to the matching [closing] delimiter.
285 enum Bracket
286 {
287 Bracket_None,
288 Bracket_Normal = ')',
289 Bracket_Curly = '}'
290 #ifdef __WXMSW__
291 ,Bracket_Windows = '%' // yeah, Windows people are a bit strange ;-)
292 #endif
293 };
294
295 size_t m;
296 for ( size_t n = 0; n < str.Len(); n++ ) {
297 switch ( str[n] ) {
298 #ifdef __WXMSW__
299 case wxT('%'):
300 #endif //WINDOWS
301 case wxT('$'):
302 {
303 Bracket bracket;
304 #ifdef __WXMSW__
305 if ( str[n] == wxT('%') )
306 bracket = Bracket_Windows;
307 else
308 #endif //WINDOWS
309 if ( n == str.Len() - 1 ) {
310 bracket = Bracket_None;
311 }
312 else {
313 switch ( str[n + 1] ) {
314 case wxT('('):
315 bracket = Bracket_Normal;
316 n++; // skip the bracket
317 break;
318
319 case wxT('{'):
320 bracket = Bracket_Curly;
321 n++; // skip the bracket
322 break;
323
324 default:
325 bracket = Bracket_None;
326 }
327 }
328
329 m = n + 1;
330
331 while ( m < str.Len() && (wxIsalnum(str[m]) || str[m] == wxT('_')) )
332 m++;
333
334 wxString strVarName(str.c_str() + n + 1, m - n - 1);
335
336 const wxChar *pszValue = wxGetenv(strVarName);
337 if ( pszValue != NULL ) {
338 strResult += pszValue;
339 }
340 else {
341 // variable doesn't exist => don't change anything
342 #ifdef __WXMSW__
343 if ( bracket != Bracket_Windows )
344 #endif
345 if ( bracket != Bracket_None )
346 strResult << str[n - 1];
347 strResult << str[n] << strVarName;
348 }
349
350 // check the closing bracket
351 if ( bracket != Bracket_None ) {
352 if ( m == str.Len() || str[m] != (char)bracket ) {
353 wxLogWarning(_("Environment variables expansion failed: missing '%c' at position %d in '%s'."),
354 (char)bracket, m + 1, str.c_str());
355 }
356 else {
357 // skip closing bracket unless the variables wasn't expanded
358 if ( pszValue == NULL )
359 strResult << (char)bracket;
360 m++;
361 }
362 }
363
364 n = m - 1; // skip variable name
365 }
366 break;
367
368 case '\\':
369 // backslash can be used to suppress special meaning of % and $
370 if ( n != str.Len() && (str[n + 1] == wxT('%') || str[n + 1] == wxT('$')) ) {
371 strResult += str[++n];
372
373 break;
374 }
375 //else: fall through
376
377 default:
378 strResult += str[n];
379 }
380 }
381
382 return strResult;
383 }
384
385 // this function is used to properly interpret '..' in path
386 /// separates group and entry names (probably shouldn't be changed)
387
388 void wxSplitPath(wxArrayString& aParts, const wxChar *sz)
389 {
390 aParts.Empty();
391
392 wxString strCurrent;
393 const wxChar *pc = sz;
394 for ( ;; ) {
395 if ( *pc == wxT('\0') || *pc == wxCONFIG_PATH_SEPARATOR ) {
396 if ( strCurrent == wxT(".") ) {
397 // ignore
398 }
399 else if ( strCurrent == wxT("..") ) {
400 // go up one level
401 if ( aParts.IsEmpty() )
402 wxLogWarning(_("'%s' has extra '..', ignored."), sz);
403 else
404 aParts.Remove(aParts.Count() - 1);
405
406 strCurrent.Empty();
407 }
408 else if ( !strCurrent.IsEmpty() ) {
409 aParts.Add(strCurrent);
410 strCurrent.Empty();
411 }
412 //else:
413 // could log an error here, but we prefer to ignore extra '/'
414
415 if ( *pc == wxT('\0') )
416 break;
417 }
418 else
419 strCurrent += *pc;
420
421 pc++;
422 }
423 }
424
425