]> git.saurik.com Git - wxWidgets.git/blame - src/common/config.cpp
Add a beginning of wxMenu unit test.
[wxWidgets.git] / src / common / config.cpp
CommitLineData
c801d85f 1///////////////////////////////////////////////////////////////////////////////
88a7a4e1 2// Name: src/common/config.cpp
5fe256de 3// Purpose: implementation of wxConfigBase class
c801d85f 4// Author: Vadim Zeitlin
ba7f9a90 5// Modified by:
c801d85f
KB
6// Created: 07.04.98
7// RCS-ID: $Id$
9d55bfef 8// Copyright: (c) 1997 Karsten Ballueder Ballueder@usa.net
c801d85f 9// Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
65571936 10// Licence: wxWindows licence
c801d85f
KB
11///////////////////////////////////////////////////////////////////////////////
12
855e92a8
VZ
13// ----------------------------------------------------------------------------
14// headers
15// ----------------------------------------------------------------------------
c801d85f 16
9869734d 17#include "wx/wxprec.h"
c801d85f 18
88a7a4e1
WS
19#ifdef __BORLANDC__
20 #pragma hdrstop
c801d85f
KB
21#endif //__BORLANDC__
22
ecef04c2
VZ
23#ifndef wxUSE_CONFIG_NATIVE
24 #define wxUSE_CONFIG_NATIVE 1
25#endif
26
21709999 27#include "wx/config.h"
88a7a4e1
WS
28
29#ifndef WX_PRECOMP
30 #include "wx/intl.h"
e4db172a 31 #include "wx/log.h"
670f9935 32 #include "wx/app.h"
de6185e2 33 #include "wx/utils.h"
aaa6d89a 34 #include "wx/arrstr.h"
18680f86 35 #include "wx/math.h"
88a7a4e1
WS
36#endif //WX_PRECOMP
37
48c8a76b 38#if wxUSE_CONFIG && ((wxUSE_FILE && wxUSE_TEXTFILE) || wxUSE_CONFIG_NATIVE)
ce4169a4 39
84281b92 40#include "wx/apptrait.h"
9869734d 41#include "wx/file.h"
9869734d 42
9869734d 43#include <stdlib.h>
f6bcfd97 44#include <ctype.h>
2ba41305 45#include <limits.h> // for INT_MAX
384859f8 46#include <float.h> // for FLT_MAX
855e92a8
VZ
47
48// ----------------------------------------------------------------------------
49// global and class static variables
50// ----------------------------------------------------------------------------
51
275bf4c1 52wxConfigBase *wxConfigBase::ms_pConfig = NULL;
c9d59ee7 53bool wxConfigBase::ms_bAutoCreate = true;
c801d85f
KB
54
55// ============================================================================
56// implementation
57// ============================================================================
58
84281b92
VZ
59// ----------------------------------------------------------------------------
60// wxAppTraitsBase
61// ----------------------------------------------------------------------------
62
63wxConfigBase *wxAppTraitsBase::CreateConfig()
64{
65 return new
66 #if defined(__WXMSW__) && wxUSE_CONFIG_NATIVE
67 wxRegConfig(wxTheApp->GetAppName(), wxTheApp->GetVendorName());
68 #elif defined(__WXPALMOS__) && wxUSE_CONFIG_NATIVE
69 wxPrefConfig(wxTheApp->GetAppName());
70 #else // either we're under Unix or wish to use files even under Windows
71 wxFileConfig(wxTheApp->GetAppName());
72 #endif
73}
74
c801d85f 75// ----------------------------------------------------------------------------
5fe256de 76// wxConfigBase
c801d85f 77// ----------------------------------------------------------------------------
c4ec0ce8 78IMPLEMENT_ABSTRACT_CLASS(wxConfigBase, wxObject)
c801d85f 79
f6bcfd97
BP
80// Not all args will always be used by derived classes, but including them all
81// in each class ensures compatibility.
82wxConfigBase::wxConfigBase(const wxString& appName,
83 const wxString& vendorName,
84 const wxString& WXUNUSED(localFilename),
85 const wxString& WXUNUSED(globalFilename),
86 long style)
87 : m_appName(appName), m_vendorName(vendorName), m_style(style)
18244936 88{
c9d59ee7
WS
89 m_bExpandEnvVars = true;
90 m_bRecordDefaults = false;
18244936
JS
91}
92
5e233068
WS
93wxConfigBase::~wxConfigBase()
94{
95 // required here for Darwin
96}
97
5fe256de 98wxConfigBase *wxConfigBase::Set(wxConfigBase *pConfig)
855e92a8 99{
5fe256de 100 wxConfigBase *pOld = ms_pConfig;
855e92a8
VZ
101 ms_pConfig = pConfig;
102 return pOld;
103}
104
5fe256de 105wxConfigBase *wxConfigBase::Create()
855e92a8 106{
275bf4c1 107 if ( ms_bAutoCreate && ms_pConfig == NULL ) {
84281b92 108 wxAppTraits * const traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
9a83f860 109 wxCHECK_MSG( traits, NULL, wxT("create wxApp before calling this") );
84281b92
VZ
110
111 ms_pConfig = traits->CreateConfig();
275bf4c1
VZ
112 }
113
114 return ms_pConfig;
855e92a8
VZ
115}
116
2ba41305
VZ
117// ----------------------------------------------------------------------------
118// wxConfigBase reading entries
119// ----------------------------------------------------------------------------
18244936 120
2ba41305
VZ
121// implement both Read() overloads for the given type in terms of DoRead()
122#define IMPLEMENT_READ_FOR_TYPE(name, type, deftype, extra) \
123 bool wxConfigBase::Read(const wxString& key, type *val) const \
124 { \
9a83f860 125 wxCHECK_MSG( val, false, wxT("wxConfig::Read(): NULL parameter") ); \
2ba41305 126 \
2b5f62a0 127 if ( !DoRead##name(key, val) ) \
c9d59ee7 128 return false; \
2b5f62a0
VZ
129 \
130 *val = extra(*val); \
131 \
c9d59ee7 132 return true; \
2ba41305
VZ
133 } \
134 \
135 bool wxConfigBase::Read(const wxString& key, \
136 type *val, \
137 deftype defVal) const \
138 { \
9a83f860 139 wxCHECK_MSG( val, false, wxT("wxConfig::Read(): NULL parameter") ); \
2ba41305 140 \
2b5f62a0
VZ
141 bool read = DoRead##name(key, val); \
142 if ( !read ) \
2ba41305 143 { \
2b5f62a0
VZ
144 if ( IsRecordingDefaults() ) \
145 { \
146 ((wxConfigBase *)this)->DoWrite##name(key, defVal); \
147 } \
148 \
149 *val = defVal; \
2ba41305
VZ
150 } \
151 \
2b5f62a0 152 *val = extra(*val); \
2ba41305 153 \
2b5f62a0 154 return read; \
18244936 155 }
18244936 156
18244936 157
2ba41305
VZ
158IMPLEMENT_READ_FOR_TYPE(String, wxString, const wxString&, ExpandEnvVars)
159IMPLEMENT_READ_FOR_TYPE(Long, long, long, long)
2ba41305
VZ
160IMPLEMENT_READ_FOR_TYPE(Double, double, double, double)
161IMPLEMENT_READ_FOR_TYPE(Bool, bool, bool, bool)
f6bcfd97 162
2ba41305 163#undef IMPLEMENT_READ_FOR_TYPE
18244936 164
3e1512cd
VZ
165// int is stored as long
166bool wxConfigBase::Read(const wxString& key, int *pi) const
18244936 167{
3e1512cd
VZ
168 long l = *pi;
169 bool r = Read(key, &l);
9a83f860 170 wxASSERT_MSG( l < INT_MAX, wxT("int overflow in wxConfig::Read") );
2ba41305 171 *pi = (int)l;
3e1512cd
VZ
172 return r;
173}
dfad0599 174
3e1512cd
VZ
175bool wxConfigBase::Read(const wxString& key, int *pi, int defVal) const
176{
177 long l = *pi;
178 bool r = Read(key, &l, defVal);
9a83f860 179 wxASSERT_MSG( l < INT_MAX, wxT("int overflow in wxConfig::Read") );
3e1512cd
VZ
180 *pi = (int)l;
181 return r;
dfad0599
JS
182}
183
384859f8
VZ
184// Read floats as doubles then just type cast it down.
185bool wxConfigBase::Read(const wxString& key, float* val) const
186{
187 wxCHECK_MSG( val, false, wxT("wxConfig::Read(): NULL parameter") );
188
189 double temp;
190 if ( !Read(key, &temp) )
191 return false;
192
193 wxCHECK_MSG( fabs(temp) <= FLT_MAX, false,
194 wxT("float overflow in wxConfig::Read") );
195 wxCHECK_MSG( (temp == 0.0) || (fabs(temp) >= FLT_MIN), false,
196 wxT("float underflow in wxConfig::Read") );
197
198 *val = static_cast<float>(temp);
199
200 return true;
201}
202
203bool wxConfigBase::Read(const wxString& key, float* val, float defVal) const
204{
205 wxCHECK_MSG( val, false, wxT("wxConfig::Read(): NULL parameter") );
206
207 if ( Read(key, val) )
208 return true;
209
210 *val = defVal;
211 return false;
212}
213
3e1512cd
VZ
214// the DoReadXXX() for the other types have implementation in the base class
215// but can be overridden in the derived ones
2ba41305 216bool wxConfigBase::DoReadBool(const wxString& key, bool* val) const
dfad0599 217{
9a83f860 218 wxCHECK_MSG( val, false, wxT("wxConfig::Read(): NULL parameter") );
2ba41305 219
dfad0599 220 long l;
2ba41305 221 if ( !DoReadLong(key, &l) )
c9d59ee7 222 return false;
dfad0599 223
9a83f860 224 wxASSERT_MSG( l == 0 || l == 1, wxT("bad bool value in wxConfig::DoReadInt") );
855e92a8 225
2ba41305
VZ
226 *val = l != 0;
227
c9d59ee7 228 return true;
7c74e7fe 229}
1341230e 230
2ba41305 231bool wxConfigBase::DoReadDouble(const wxString& key, double* val) const
68275b85 232{
2ba41305
VZ
233 wxString str;
234 if ( Read(key, &str) )
235 {
7e22c2bd
VZ
236 if ( str.ToCDouble(val) )
237 return true;
238
239 // Previous versions of wxFileConfig wrote the numbers out using the
240 // current locale and not the C one as now, so attempt to parse the
241 // string as a number in the current locale too, for compatibility.
242 if ( str.ToDouble(val) )
243 return true;
2ba41305
VZ
244 }
245
c9d59ee7 246 return false;
68275b85
VZ
247}
248
2ba41305 249// string reading helper
6164d85e 250wxString wxConfigBase::ExpandEnvVars(const wxString& str) const
8bbe427f
VZ
251{
252 wxString tmp; // Required for BC++
253 if (IsExpandingEnvVars())
254 tmp = wxExpandEnvVars(str);
255 else
256 tmp = str;
257 return tmp;
258}
18244936 259
2ba41305
VZ
260// ----------------------------------------------------------------------------
261// wxConfigBase writing
262// ----------------------------------------------------------------------------
263
264bool wxConfigBase::DoWriteDouble(const wxString& key, double val)
265{
7e22c2bd
VZ
266 // Notice that we always write out the numbers in C locale and not the
267 // current one. This makes the config files portable between machines using
268 // different locales.
269 return DoWriteString(key, wxString::FromCDouble(val));
2ba41305
VZ
270}
271
2ba41305
VZ
272bool wxConfigBase::DoWriteBool(const wxString& key, bool value)
273{
274 return DoWriteLong(key, value ? 1l : 0l);
275}
276
855e92a8 277// ----------------------------------------------------------------------------
18244936 278// wxConfigPathChanger
855e92a8
VZ
279// ----------------------------------------------------------------------------
280
18244936 281wxConfigPathChanger::wxConfigPathChanger(const wxConfigBase *pContainer,
6f73419b 282 const wxString& strEntry)
855e92a8 283{
6f73419b 284 m_bChanged = false;
f48a1159 285 m_pContainer = const_cast<wxConfigBase *>(pContainer);
08159082 286
1af292a6
VZ
287 // the path is everything which precedes the last slash and the name is
288 // everything after it -- and this works correctly if there is no slash too
289 wxString strPath = strEntry.BeforeLast(wxCONFIG_PATH_SEPARATOR, &m_strName);
855e92a8 290
08159082 291 // except in the special case of "/keyname" when there is nothing before "/"
b713f891
WS
292 if ( strPath.empty() &&
293 ((!strEntry.empty()) && strEntry[0] == wxCONFIG_PATH_SEPARATOR) )
08159082 294 {
da468d38 295 strPath = wxCONFIG_PATH_SEPARATOR;
08159082 296 }
855e92a8 297
b713f891 298 if ( !strPath.empty() )
18860312
VZ
299 {
300 if ( m_pContainer->GetPath() != strPath )
301 {
6f73419b 302 // we do change the path so restore it later
18860312 303 m_bChanged = true;
b9a59c91
JS
304
305 /* JACS: work around a memory bug that causes an assert
306 when using wxRegConfig, related to reference-counting.
86501081 307 Can be reproduced by removing .wc_str() below and
b9a59c91
JS
308 adding the following code to the config sample OnInit under
309 Windows:
310
311 pConfig->SetPath(wxT("MySettings"));
312 pConfig->SetPath(wxT(".."));
313 int value;
9a83f860 314 pConfig->Read(wxT("MainWindowX"), & value);
b9a59c91 315 */
86501081 316 m_strOldPath = m_pContainer->GetPath().wc_str();
18860312
VZ
317 if ( *m_strOldPath.c_str() != wxCONFIG_PATH_SEPARATOR )
318 m_strOldPath += wxCONFIG_PATH_SEPARATOR;
319 m_pContainer->SetPath(strPath);
320 }
855e92a8
VZ
321 }
322}
323
41f30152
VZ
324void wxConfigPathChanger::UpdateIfDeleted()
325{
326 // we don't have to do anything at all if we didn't change the path
327 if ( !m_bChanged )
328 return;
329
330 // find the deepest still existing parent path of the original path
331 while ( !m_pContainer->HasGroup(m_strOldPath) )
332 {
333 m_strOldPath = m_strOldPath.BeforeLast(wxCONFIG_PATH_SEPARATOR);
334 if ( m_strOldPath.empty() )
335 m_strOldPath = wxCONFIG_PATH_SEPARATOR;
336 }
337}
338
18244936 339wxConfigPathChanger::~wxConfigPathChanger()
855e92a8
VZ
340{
341 // only restore path if it was changed
342 if ( m_bChanged ) {
343 m_pContainer->SetPath(m_strOldPath);
344 }
345}
346
f559e621
VZ
347// this is a wxConfig method but it's mainly used with wxConfigPathChanger
348/* static */
349wxString wxConfigBase::RemoveTrailingSeparator(const wxString& key)
350{
351 wxString path(key);
352
353 // don't remove the only separator from a root group path!
354 while ( path.length() > 1 )
355 {
356 if ( *path.rbegin() != wxCONFIG_PATH_SEPARATOR )
357 break;
358
359 path.erase(path.end() - 1);
360 }
361
362 return path;
363}
364
21709999
JS
365#endif // wxUSE_CONFIG
366
c801d85f
KB
367// ----------------------------------------------------------------------------
368// static & global functions
369// ----------------------------------------------------------------------------
370
371// understands both Unix and Windows (but only under Windows) environment
372// variables expansion: i.e. $var, $(var) and ${var} are always understood
373// and in addition under Windows %var% is also.
1fd87ff7
SN
374
375// don't change the values the enum elements: they must be equal
376// to the matching [closing] delimiter.
377enum Bracket
378{
379 Bracket_None,
380 Bracket_Normal = ')',
381 Bracket_Curly = '}',
382#ifdef __WXMSW__
383 Bracket_Windows = '%', // yeah, Windows people are a bit strange ;-)
384#endif
385 Bracket_Max
386};
387
f40ccee9 388wxString wxExpandEnvVars(const wxString& str)
c801d85f
KB
389{
390 wxString strResult;
aaa6d89a 391 strResult.Alloc(str.length());
c801d85f 392
c86f1403 393 size_t m;
aaa6d89a 394 for ( size_t n = 0; n < str.length(); n++ ) {
c9f78968 395 switch ( str[n].GetValue() ) {
2049ba38 396#ifdef __WXMSW__
f6bcfd97 397 case wxT('%'):
c801d85f 398#endif //WINDOWS
f6bcfd97 399 case wxT('$'):
c801d85f
KB
400 {
401 Bracket bracket;
2049ba38 402 #ifdef __WXMSW__
f6bcfd97 403 if ( str[n] == wxT('%') )
c801d85f
KB
404 bracket = Bracket_Windows;
405 else
406 #endif //WINDOWS
aaa6d89a 407 if ( n == str.length() - 1 ) {
c801d85f
KB
408 bracket = Bracket_None;
409 }
410 else {
c9f78968 411 switch ( str[n + 1].GetValue() ) {
f6bcfd97 412 case wxT('('):
ba7f9a90 413 bracket = Bracket_Normal;
c801d85f
KB
414 n++; // skip the bracket
415 break;
416
f6bcfd97 417 case wxT('{'):
c801d85f
KB
418 bracket = Bracket_Curly;
419 n++; // skip the bracket
420 break;
421
422 default:
423 bracket = Bracket_None;
424 }
425 }
426
427 m = n + 1;
428
aaa6d89a 429 while ( m < str.length() && (wxIsalnum(str[m]) || str[m] == wxT('_')) )
c801d85f 430 m++;
03647350 431
c801d85f
KB
432 wxString strVarName(str.c_str() + n + 1, m - n - 1);
433
1c193821 434#ifdef __WXWINCE__
86501081 435 const bool expanded = false;
1c193821 436#else
bf7f7793
RR
437 // NB: use wxGetEnv instead of wxGetenv as otherwise variables
438 // set through wxSetEnv may not be read correctly!
86501081 439 bool expanded = false;
bf7f7793
RR
440 wxString tmp;
441 if (wxGetEnv(strVarName, &tmp))
86501081
VS
442 {
443 strResult += tmp;
444 expanded = true;
c801d85f 445 }
86501081
VS
446 else
447#endif
448 {
c801d85f 449 // variable doesn't exist => don't change anything
2049ba38 450 #ifdef __WXMSW__
c801d85f
KB
451 if ( bracket != Bracket_Windows )
452 #endif
453 if ( bracket != Bracket_None )
454 strResult << str[n - 1];
455 strResult << str[n] << strVarName;
456 }
457
458 // check the closing bracket
459 if ( bracket != Bracket_None ) {
aaa6d89a 460 if ( m == str.length() || str[m] != (wxChar)bracket ) {
2b5f62a0
VZ
461 // under MSW it's common to have '%' characters in the registry
462 // and it's annoying to have warnings about them each time, so
463 // ignroe them silently if they are not used for env vars
464 //
465 // under Unix, OTOH, this warning could be useful for the user to
466 // understand why isn't the variable expanded as intended
467 #ifndef __WXMSW__
c6d60564
RN
468 wxLogWarning(_("Environment variables expansion failed: missing '%c' at position %u in '%s'."),
469 (char)bracket, (unsigned int) (m + 1), str.c_str());
2b5f62a0 470 #endif // __WXMSW__
c801d85f
KB
471 }
472 else {
906373bf 473 // skip closing bracket unless the variables wasn't expanded
86501081 474 if ( !expanded )
f6eaf52a 475 strResult << (wxChar)bracket;
c801d85f
KB
476 m++;
477 }
478 }
479
480 n = m - 1; // skip variable name
481 }
482 break;
483
c9f78968 484 case wxT('\\'):
906373bf 485 // backslash can be used to suppress special meaning of % and $
aaa6d89a 486 if ( n != str.length() - 1 &&
b11dd163 487 (str[n + 1] == wxT('%') || str[n + 1] == wxT('$')) ) {
906373bf
VZ
488 strResult += str[++n];
489
490 break;
491 }
492 //else: fall through
493
c801d85f
KB
494 default:
495 strResult += str[n];
496 }
497 }
498
499 return strResult;
500}
855e92a8
VZ
501
502// this function is used to properly interpret '..' in path
86501081 503void wxSplitPath(wxArrayString& aParts, const wxString& path)
855e92a8 504{
df5168c4 505 aParts.clear();
855e92a8
VZ
506
507 wxString strCurrent;
86501081 508 wxString::const_iterator pc = path.begin();
855e92a8 509 for ( ;; ) {
86501081 510 if ( pc == path.end() || *pc == wxCONFIG_PATH_SEPARATOR ) {
223d09f6 511 if ( strCurrent == wxT(".") ) {
855e92a8
VZ
512 // ignore
513 }
223d09f6 514 else if ( strCurrent == wxT("..") ) {
855e92a8 515 // go up one level
df5168c4 516 if ( aParts.size() == 0 )
43b2d5e7 517 {
86501081 518 wxLogWarning(_("'%s' has extra '..', ignored."), path);
43b2d5e7 519 }
855e92a8 520 else
43b2d5e7 521 {
df5168c4 522 aParts.erase(aParts.end() - 1);
43b2d5e7 523 }
937d4f4d
VZ
524
525 strCurrent.Empty();
855e92a8 526 }
b713f891 527 else if ( !strCurrent.empty() ) {
df5168c4 528 aParts.push_back(strCurrent);
855e92a8
VZ
529 strCurrent.Empty();
530 }
531 //else:
532 // could log an error here, but we prefer to ignore extra '/'
533
86501081 534 if ( pc == path.end() )
f6bcfd97 535 break;
855e92a8
VZ
536 }
537 else
538 strCurrent += *pc;
539
86501081 540 ++pc;
855e92a8
VZ
541 }
542}