]> git.saurik.com Git - wxWidgets.git/blob - src/common/config.cpp
remove unused static function (avoids a warning about it)
[wxWidgets.git] / src / common / config.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/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 licence
11 ///////////////////////////////////////////////////////////////////////////////
12
13 // ----------------------------------------------------------------------------
14 // headers
15 // ----------------------------------------------------------------------------
16
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif //__BORLANDC__
22
23 #ifndef wxUSE_CONFIG_NATIVE
24 #define wxUSE_CONFIG_NATIVE 1
25 #endif
26
27 #include "wx/config.h"
28
29 #ifndef WX_PRECOMP
30 #include "wx/intl.h"
31 #include "wx/log.h"
32 #include "wx/app.h"
33 #include "wx/utils.h"
34 #include "wx/arrstr.h"
35 #include "wx/math.h"
36 #endif //WX_PRECOMP
37
38 #if wxUSE_CONFIG && ((wxUSE_FILE && wxUSE_TEXTFILE) || wxUSE_CONFIG_NATIVE)
39
40 #include "wx/file.h"
41
42 #include <stdlib.h>
43 #include <ctype.h>
44 #include <limits.h> // for INT_MAX
45
46 // ----------------------------------------------------------------------------
47 // global and class static variables
48 // ----------------------------------------------------------------------------
49
50 wxConfigBase *wxConfigBase::ms_pConfig = NULL;
51 bool wxConfigBase::ms_bAutoCreate = true;
52
53 // ============================================================================
54 // implementation
55 // ============================================================================
56
57 // ----------------------------------------------------------------------------
58 // wxConfigBase
59 // ----------------------------------------------------------------------------
60 IMPLEMENT_ABSTRACT_CLASS(wxConfigBase, wxObject)
61
62 // Not all args will always be used by derived classes, but including them all
63 // in each class ensures compatibility.
64 wxConfigBase::wxConfigBase(const wxString& appName,
65 const wxString& vendorName,
66 const wxString& WXUNUSED(localFilename),
67 const wxString& WXUNUSED(globalFilename),
68 long style)
69 : m_appName(appName), m_vendorName(vendorName), m_style(style)
70 {
71 m_bExpandEnvVars = true;
72 m_bRecordDefaults = false;
73 }
74
75 wxConfigBase::~wxConfigBase()
76 {
77 // required here for Darwin
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__) && wxUSE_CONFIG_NATIVE
92 new wxRegConfig(wxTheApp->GetAppName(), wxTheApp->GetVendorName());
93 #elif defined(__WXPALMOS__) && wxUSE_CONFIG_NATIVE
94 new wxPrefConfig(wxTheApp->GetAppName());
95 #else // either we're under Unix or wish to use files even under Windows
96 new wxFileConfig(wxTheApp->GetAppName());
97 #endif
98 }
99
100 return ms_pConfig;
101 }
102
103 // ----------------------------------------------------------------------------
104 // wxConfigBase reading entries
105 // ----------------------------------------------------------------------------
106
107 // implement both Read() overloads for the given type in terms of DoRead()
108 #define IMPLEMENT_READ_FOR_TYPE(name, type, deftype, extra) \
109 bool wxConfigBase::Read(const wxString& key, type *val) const \
110 { \
111 wxCHECK_MSG( val, false, _T("wxConfig::Read(): NULL parameter") ); \
112 \
113 if ( !DoRead##name(key, val) ) \
114 return false; \
115 \
116 *val = extra(*val); \
117 \
118 return true; \
119 } \
120 \
121 bool wxConfigBase::Read(const wxString& key, \
122 type *val, \
123 deftype defVal) const \
124 { \
125 wxCHECK_MSG( val, false, _T("wxConfig::Read(): NULL parameter") ); \
126 \
127 bool read = DoRead##name(key, val); \
128 if ( !read ) \
129 { \
130 if ( IsRecordingDefaults() ) \
131 { \
132 ((wxConfigBase *)this)->DoWrite##name(key, defVal); \
133 } \
134 \
135 *val = defVal; \
136 } \
137 \
138 *val = extra(*val); \
139 \
140 return read; \
141 }
142
143
144 IMPLEMENT_READ_FOR_TYPE(String, wxString, const wxString&, ExpandEnvVars)
145 IMPLEMENT_READ_FOR_TYPE(Long, long, long, long)
146 IMPLEMENT_READ_FOR_TYPE(Int, int, int, int)
147 IMPLEMENT_READ_FOR_TYPE(Double, double, double, double)
148 IMPLEMENT_READ_FOR_TYPE(Bool, bool, bool, bool)
149
150 #undef IMPLEMENT_READ_FOR_TYPE
151
152 // the DoReadXXX() for the other types have implementation in the base class
153 // but can be overridden in the derived ones
154 bool wxConfigBase::DoReadInt(const wxString& key, int *pi) const
155 {
156 wxCHECK_MSG( pi, false, _T("wxConfig::Read(): NULL parameter") );
157
158 long l;
159 if ( !DoReadLong(key, &l) )
160 return false;
161
162 wxASSERT_MSG( l < INT_MAX, _T("overflow in wxConfig::DoReadInt") );
163
164 *pi = (int)l;
165
166 return true;
167 }
168
169 bool wxConfigBase::DoReadBool(const wxString& key, bool* val) const
170 {
171 wxCHECK_MSG( val, false, _T("wxConfig::Read(): NULL parameter") );
172
173 long l;
174 if ( !DoReadLong(key, &l) )
175 return false;
176
177 wxASSERT_MSG( l == 0 || l == 1, _T("bad bool value in wxConfig::DoReadInt") );
178
179 *val = l != 0;
180
181 return true;
182 }
183
184 bool wxConfigBase::DoReadDouble(const wxString& key, double* val) const
185 {
186 wxString str;
187 if ( Read(key, &str) )
188 {
189 return str.ToDouble(val);
190 }
191
192 return false;
193 }
194
195 // string reading helper
196 wxString wxConfigBase::ExpandEnvVars(const wxString& str) const
197 {
198 wxString tmp; // Required for BC++
199 if (IsExpandingEnvVars())
200 tmp = wxExpandEnvVars(str);
201 else
202 tmp = str;
203 return tmp;
204 }
205
206 // ----------------------------------------------------------------------------
207 // wxConfigBase writing
208 // ----------------------------------------------------------------------------
209
210 bool wxConfigBase::DoWriteDouble(const wxString& key, double val)
211 {
212 return DoWriteString(key, wxString::Format(_T("%g"), val));
213 }
214
215 bool wxConfigBase::DoWriteInt(const wxString& key, int value)
216 {
217 return DoWriteLong(key, (long)value);
218 }
219
220 bool wxConfigBase::DoWriteBool(const wxString& key, bool value)
221 {
222 return DoWriteLong(key, value ? 1l : 0l);
223 }
224
225 // ----------------------------------------------------------------------------
226 // wxConfigPathChanger
227 // ----------------------------------------------------------------------------
228
229 wxConfigPathChanger::wxConfigPathChanger(const wxConfigBase *pContainer,
230 const wxString& strEntry)
231 {
232 m_bChanged = false;
233 m_pContainer = (wxConfigBase *)pContainer;
234
235 // the path is everything which precedes the last slash
236 wxString strPath = strEntry.BeforeLast(wxCONFIG_PATH_SEPARATOR);
237
238 // except in the special case of "/keyname" when there is nothing before "/"
239 if ( strPath.empty() &&
240 ((!strEntry.empty()) && strEntry[0] == wxCONFIG_PATH_SEPARATOR) )
241 {
242 strPath = wxCONFIG_PATH_SEPARATOR;
243 }
244
245 if ( !strPath.empty() )
246 {
247 if ( m_pContainer->GetPath() != strPath )
248 {
249 // we do change the path so restore it later
250 m_bChanged = true;
251
252 /* JACS: work around a memory bug that causes an assert
253 when using wxRegConfig, related to reference-counting.
254 Can be reproduced by removing (const wxChar*) below and
255 adding the following code to the config sample OnInit under
256 Windows:
257
258 pConfig->SetPath(wxT("MySettings"));
259 pConfig->SetPath(wxT(".."));
260 int value;
261 pConfig->Read(_T("MainWindowX"), & value);
262 */
263 m_strOldPath = (const wxChar*) m_pContainer->GetPath();
264 if ( *m_strOldPath.c_str() != wxCONFIG_PATH_SEPARATOR )
265 m_strOldPath += wxCONFIG_PATH_SEPARATOR;
266 m_pContainer->SetPath(strPath);
267 }
268
269 // in any case, use the just the name, not full path
270 m_strName = strEntry.AfterLast(wxCONFIG_PATH_SEPARATOR);
271 }
272 else {
273 // it's a name only, without path - nothing to do
274 m_strName = strEntry;
275 }
276 }
277
278 void wxConfigPathChanger::UpdateIfDeleted()
279 {
280 // we don't have to do anything at all if we didn't change the path
281 if ( !m_bChanged )
282 return;
283
284 // find the deepest still existing parent path of the original path
285 while ( !m_pContainer->HasGroup(m_strOldPath) )
286 {
287 m_strOldPath = m_strOldPath.BeforeLast(wxCONFIG_PATH_SEPARATOR);
288 if ( m_strOldPath.empty() )
289 m_strOldPath = wxCONFIG_PATH_SEPARATOR;
290 }
291 }
292
293 wxConfigPathChanger::~wxConfigPathChanger()
294 {
295 // only restore path if it was changed
296 if ( m_bChanged ) {
297 m_pContainer->SetPath(m_strOldPath);
298 }
299 }
300
301 // this is a wxConfig method but it's mainly used with wxConfigPathChanger
302 /* static */
303 wxString wxConfigBase::RemoveTrailingSeparator(const wxString& key)
304 {
305 wxString path(key);
306
307 // don't remove the only separator from a root group path!
308 while ( path.length() > 1 )
309 {
310 if ( *path.rbegin() != wxCONFIG_PATH_SEPARATOR )
311 break;
312
313 path.erase(path.end() - 1);
314 }
315
316 return path;
317 }
318
319 #endif // wxUSE_CONFIG
320
321 // ----------------------------------------------------------------------------
322 // static & global functions
323 // ----------------------------------------------------------------------------
324
325 // understands both Unix and Windows (but only under Windows) environment
326 // variables expansion: i.e. $var, $(var) and ${var} are always understood
327 // and in addition under Windows %var% is also.
328
329 // don't change the values the enum elements: they must be equal
330 // to the matching [closing] delimiter.
331 enum Bracket
332 {
333 Bracket_None,
334 Bracket_Normal = ')',
335 Bracket_Curly = '}',
336 #ifdef __WXMSW__
337 Bracket_Windows = '%', // yeah, Windows people are a bit strange ;-)
338 #endif
339 Bracket_Max
340 };
341
342 wxString wxExpandEnvVars(const wxString& str)
343 {
344 wxString strResult;
345 strResult.Alloc(str.length());
346
347 size_t m;
348 for ( size_t n = 0; n < str.length(); n++ ) {
349 switch ( str[n].GetValue() ) {
350 #ifdef __WXMSW__
351 case wxT('%'):
352 #endif //WINDOWS
353 case wxT('$'):
354 {
355 Bracket bracket;
356 #ifdef __WXMSW__
357 if ( str[n] == wxT('%') )
358 bracket = Bracket_Windows;
359 else
360 #endif //WINDOWS
361 if ( n == str.length() - 1 ) {
362 bracket = Bracket_None;
363 }
364 else {
365 switch ( str[n + 1].GetValue() ) {
366 case wxT('('):
367 bracket = Bracket_Normal;
368 n++; // skip the bracket
369 break;
370
371 case wxT('{'):
372 bracket = Bracket_Curly;
373 n++; // skip the bracket
374 break;
375
376 default:
377 bracket = Bracket_None;
378 }
379 }
380
381 m = n + 1;
382
383 while ( m < str.length() && (wxIsalnum(str[m]) || str[m] == wxT('_')) )
384 m++;
385
386 wxString strVarName(str.c_str() + n + 1, m - n - 1);
387
388 #ifdef __WXWINCE__
389 const wxChar *pszValue = NULL;
390 #else
391 // NB: use wxGetEnv instead of wxGetenv as otherwise variables
392 // set through wxSetEnv may not be read correctly!
393 const wxChar *pszValue = NULL;
394 wxString tmp;
395 if (wxGetEnv(strVarName, &tmp))
396 pszValue = tmp;
397 #endif
398 if ( pszValue != NULL ) {
399 strResult += pszValue;
400 }
401 else {
402 // variable doesn't exist => don't change anything
403 #ifdef __WXMSW__
404 if ( bracket != Bracket_Windows )
405 #endif
406 if ( bracket != Bracket_None )
407 strResult << str[n - 1];
408 strResult << str[n] << strVarName;
409 }
410
411 // check the closing bracket
412 if ( bracket != Bracket_None ) {
413 if ( m == str.length() || str[m] != (wxChar)bracket ) {
414 // under MSW it's common to have '%' characters in the registry
415 // and it's annoying to have warnings about them each time, so
416 // ignroe them silently if they are not used for env vars
417 //
418 // under Unix, OTOH, this warning could be useful for the user to
419 // understand why isn't the variable expanded as intended
420 #ifndef __WXMSW__
421 wxLogWarning(_("Environment variables expansion failed: missing '%c' at position %u in '%s'."),
422 (char)bracket, (unsigned int) (m + 1), str.c_str());
423 #endif // __WXMSW__
424 }
425 else {
426 // skip closing bracket unless the variables wasn't expanded
427 if ( pszValue == NULL )
428 strResult << (wxChar)bracket;
429 m++;
430 }
431 }
432
433 n = m - 1; // skip variable name
434 }
435 break;
436
437 case wxT('\\'):
438 // backslash can be used to suppress special meaning of % and $
439 if ( n != str.length() - 1 &&
440 (str[n + 1] == wxT('%') || str[n + 1] == wxT('$')) ) {
441 strResult += str[++n];
442
443 break;
444 }
445 //else: fall through
446
447 default:
448 strResult += str[n];
449 }
450 }
451
452 return strResult;
453 }
454
455 // this function is used to properly interpret '..' in path
456 void wxSplitPath(wxArrayString& aParts, const wxChar *sz)
457 {
458 aParts.clear();
459
460 wxString strCurrent;
461 const wxChar *pc = sz;
462 for ( ;; ) {
463 if ( *pc == wxT('\0') || *pc == wxCONFIG_PATH_SEPARATOR ) {
464 if ( strCurrent == wxT(".") ) {
465 // ignore
466 }
467 else if ( strCurrent == wxT("..") ) {
468 // go up one level
469 if ( aParts.size() == 0 )
470 wxLogWarning(_("'%s' has extra '..', ignored."), sz);
471 else
472 aParts.erase(aParts.end() - 1);
473
474 strCurrent.Empty();
475 }
476 else if ( !strCurrent.empty() ) {
477 aParts.push_back(strCurrent);
478 strCurrent.Empty();
479 }
480 //else:
481 // could log an error here, but we prefer to ignore extra '/'
482
483 if ( *pc == wxT('\0') )
484 break;
485 }
486 else
487 strCurrent += *pc;
488
489 pc++;
490 }
491 }