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