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