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