]> git.saurik.com Git - wxWidgets.git/blob - src/common/config.cpp
9707475ac918ca0d9dc4afee91c474cbc9376c66
[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 bool wxConfigBase::Write(const wxString& key, double val)
183 {
184 wxString str;
185 str.Printf("%f", val);
186 return Write(key, str);
187 }
188
189 bool wxConfigBase::Write(const wxString& key, bool value)
190 {
191 long l = (value ? 1 : 0);
192 return Write(key, l);
193 }
194
195
196 // ----------------------------------------------------------------------------
197 // wxConfigPathChanger
198 // ----------------------------------------------------------------------------
199
200 wxConfigPathChanger::wxConfigPathChanger(const wxConfigBase *pContainer,
201 const wxString& strEntry)
202 {
203 m_pContainer = (wxConfigBase *)pContainer;
204 wxString strPath = strEntry.Before(wxCONFIG_PATH_SEPARATOR);
205
206 // special case of "/keyname" when there is nothing before "/"
207 if ( strPath.IsEmpty() && ((!strEntry.IsEmpty()) && strEntry[0] == wxCONFIG_PATH_SEPARATOR ))
208 strPath = wxCONFIG_PATH_SEPARATOR;
209
210 if ( !strPath.IsEmpty() ) {
211 // do change the path
212 m_bChanged = TRUE;
213 m_strName = strEntry.Right(wxCONFIG_PATH_SEPARATOR);
214 m_strOldPath = m_pContainer->GetPath();
215 m_strOldPath += wxCONFIG_PATH_SEPARATOR;
216 m_pContainer->SetPath(strPath);
217 }
218 else {
219 // it's a name only, without path - nothing to do
220 m_bChanged = FALSE;
221 m_strName = strEntry;
222 }
223 }
224
225 wxConfigPathChanger::~wxConfigPathChanger()
226 {
227 // only restore path if it was changed
228 if ( m_bChanged ) {
229 m_pContainer->SetPath(m_strOldPath);
230 }
231 }
232
233 // ----------------------------------------------------------------------------
234 // static & global functions
235 // ----------------------------------------------------------------------------
236
237 // understands both Unix and Windows (but only under Windows) environment
238 // variables expansion: i.e. $var, $(var) and ${var} are always understood
239 // and in addition under Windows %var% is also.
240 wxString wxExpandEnvVars(const wxString& str)
241 {
242 wxString strResult;
243 strResult.Alloc(str.Len());
244
245 // don't change the values the enum elements: they must be equal
246 // to the matching [closing] delimiter.
247 enum Bracket
248 {
249 Bracket_None,
250 Bracket_Normal = ')',
251 Bracket_Curly = '}',
252 #ifdef __WXMSW__
253 Bracket_Windows = '%' // yeah, Windows people are a bit strange ;-)
254 #endif
255 };
256
257 size_t m;
258 for ( size_t n = 0; n < str.Len(); n++ ) {
259 switch ( str[n] ) {
260 #ifdef __WXMSW__
261 case '%':
262 #endif //WINDOWS
263 case '$':
264 {
265 Bracket bracket;
266 #ifdef __WXMSW__
267 if ( str[n] == '%' )
268 bracket = Bracket_Windows;
269 else
270 #endif //WINDOWS
271 if ( n == str.Len() - 1 ) {
272 bracket = Bracket_None;
273 }
274 else {
275 switch ( str[n + 1] ) {
276 case '(':
277 bracket = Bracket_Normal;
278 n++; // skip the bracket
279 break;
280
281 case '{':
282 bracket = Bracket_Curly;
283 n++; // skip the bracket
284 break;
285
286 default:
287 bracket = Bracket_None;
288 }
289 }
290
291 m = n + 1;
292
293 while ( m < str.Len() && (isalnum(str[m]) || str[m] == '_') )
294 m++;
295
296 wxString strVarName(str.c_str() + n + 1, m - n - 1);
297
298 const char *pszValue = getenv(strVarName);
299 if ( pszValue != NULL ) {
300 strResult += pszValue;
301 }
302 else {
303 // variable doesn't exist => don't change anything
304 #ifdef __WXMSW__
305 if ( bracket != Bracket_Windows )
306 #endif
307 if ( bracket != Bracket_None )
308 strResult << str[n - 1];
309 strResult << str[n] << strVarName;
310 }
311
312 // check the closing bracket
313 if ( bracket != Bracket_None ) {
314 if ( m == str.Len() || str[m] != (char)bracket ) {
315 wxLogWarning(_("Environment variables expansion failed: "
316 "missing '%c' at position %d in '%s'."),
317 (char)bracket, m + 1, str.c_str());
318 }
319 else {
320 // skip closing bracket unless the variables wasn't expanded
321 if ( pszValue == NULL )
322 strResult << (char)bracket;
323 m++;
324 }
325 }
326
327 n = m - 1; // skip variable name
328 }
329 break;
330
331 case '\\':
332 // backslash can be used to suppress special meaning of % and $
333 if ( n != str.Len() && (str[n + 1] == '%' || str[n + 1] == '$') ) {
334 strResult += str[++n];
335
336 break;
337 }
338 //else: fall through
339
340 default:
341 strResult += str[n];
342 }
343 }
344
345 return strResult;
346 }
347
348 // this function is used to properly interpret '..' in path
349 void wxSplitPath(wxArrayString& aParts, const char *sz)
350 {
351 aParts.Empty();
352
353 wxString strCurrent;
354 const char *pc = sz;
355 for ( ;; ) {
356 if ( *pc == '\0' || *pc == wxCONFIG_PATH_SEPARATOR ) {
357 if ( strCurrent == "." ) {
358 // ignore
359 }
360 else if ( strCurrent == ".." ) {
361 // go up one level
362 if ( aParts.IsEmpty() )
363 wxLogWarning(_("'%s' has extra '..', ignored."), sz);
364 else
365 aParts.Remove(aParts.Count() - 1);
366
367 strCurrent.Empty();
368 }
369 else if ( !strCurrent.IsEmpty() ) {
370 aParts.Add(strCurrent);
371 strCurrent.Empty();
372 }
373 //else:
374 // could log an error here, but we prefer to ignore extra '/'
375
376 if ( *pc == '\0' )
377 return;
378 }
379 else
380 strCurrent += *pc;
381
382 pc++;
383 }
384 }
385
386