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