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