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