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