]>
git.saurik.com Git - wxWidgets.git/blob - src/common/config.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/config.cpp
3 // Purpose: implementation of wxConfigBase class
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 1997 Karsten Ballueder Ballueder@usa.net
8 // Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ----------------------------------------------------------------------------
14 // ----------------------------------------------------------------------------
16 #include "wx/wxprec.h"
22 #ifndef wxUSE_CONFIG_NATIVE
23 #define wxUSE_CONFIG_NATIVE 1
26 #include "wx/config.h"
33 #include "wx/arrstr.h"
37 #if wxUSE_CONFIG && ((wxUSE_FILE && wxUSE_TEXTFILE) || wxUSE_CONFIG_NATIVE)
39 #include "wx/apptrait.h"
44 #include <limits.h> // for INT_MAX
45 #include <float.h> // for FLT_MAX
47 // ----------------------------------------------------------------------------
48 // global and class static variables
49 // ----------------------------------------------------------------------------
51 wxConfigBase
*wxConfigBase::ms_pConfig
= NULL
;
52 bool wxConfigBase::ms_bAutoCreate
= true;
54 // ============================================================================
56 // ============================================================================
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 wxConfigBase
*wxAppTraitsBase::CreateConfig()
65 #if defined(__WINDOWS__) && wxUSE_CONFIG_NATIVE
66 wxRegConfig(wxTheApp
->GetAppName(), wxTheApp
->GetVendorName());
67 #else // either we're under Unix or wish to use files even under Windows
68 wxFileConfig(wxTheApp
->GetAppName());
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
75 IMPLEMENT_ABSTRACT_CLASS(wxConfigBase
, wxObject
)
77 // Not all args will always be used by derived classes, but including them all
78 // in each class ensures compatibility.
79 wxConfigBase::wxConfigBase(const wxString
& appName
,
80 const wxString
& vendorName
,
81 const wxString
& WXUNUSED(localFilename
),
82 const wxString
& WXUNUSED(globalFilename
),
84 : m_appName(appName
), m_vendorName(vendorName
), m_style(style
)
86 m_bExpandEnvVars
= true;
87 m_bRecordDefaults
= false;
90 wxConfigBase::~wxConfigBase()
92 // required here for Darwin
95 wxConfigBase
*wxConfigBase::Set(wxConfigBase
*pConfig
)
97 wxConfigBase
*pOld
= ms_pConfig
;
102 wxConfigBase
*wxConfigBase::Create()
104 if ( ms_bAutoCreate
&& ms_pConfig
== NULL
) {
105 wxAppTraits
* const traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
106 wxCHECK_MSG( traits
, NULL
, wxT("create wxApp before calling this") );
108 ms_pConfig
= traits
->CreateConfig();
114 // ----------------------------------------------------------------------------
115 // wxConfigBase reading entries
116 // ----------------------------------------------------------------------------
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 \
122 wxCHECK_MSG( val, false, wxT("wxConfig::Read(): NULL parameter") ); \
124 if ( !DoRead##name(key, val) ) \
127 *val = extra(*val); \
132 bool wxConfigBase::Read(const wxString& key, \
134 deftype defVal) const \
136 wxCHECK_MSG( val, false, wxT("wxConfig::Read(): NULL parameter") ); \
138 bool read = DoRead##name(key, val); \
141 if ( IsRecordingDefaults() ) \
143 ((wxConfigBase *)this)->DoWrite##name(key, defVal); \
149 *val = extra(*val); \
155 IMPLEMENT_READ_FOR_TYPE(String
, wxString
, const wxString
&, ExpandEnvVars
)
156 IMPLEMENT_READ_FOR_TYPE(Long
, long, long, long)
157 IMPLEMENT_READ_FOR_TYPE(Double
, double, double, double)
158 IMPLEMENT_READ_FOR_TYPE(Bool
, bool, bool, bool)
160 #undef IMPLEMENT_READ_FOR_TYPE
162 // int is stored as long
163 bool wxConfigBase::Read(const wxString
& key
, int *pi
) const
166 bool r
= Read(key
, &l
);
167 wxASSERT_MSG( l
< INT_MAX
, wxT("int overflow in wxConfig::Read") );
172 bool wxConfigBase::Read(const wxString
& key
, int *pi
, int defVal
) const
175 bool r
= Read(key
, &l
, defVal
);
176 wxASSERT_MSG( l
< INT_MAX
, wxT("int overflow in wxConfig::Read") );
181 // Read floats as doubles then just type cast it down.
182 bool wxConfigBase::Read(const wxString
& key
, float* val
) const
184 wxCHECK_MSG( val
, false, wxT("wxConfig::Read(): NULL parameter") );
187 if ( !Read(key
, &temp
) )
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") );
195 *val
= static_cast<float>(temp
);
200 bool wxConfigBase::Read(const wxString
& key
, float* val
, float defVal
) const
202 wxCHECK_MSG( val
, false, wxT("wxConfig::Read(): NULL parameter") );
204 if ( Read(key
, val
) )
211 // the DoReadXXX() for the other types have implementation in the base class
212 // but can be overridden in the derived ones
213 bool wxConfigBase::DoReadBool(const wxString
& key
, bool* val
) const
215 wxCHECK_MSG( val
, false, wxT("wxConfig::Read(): NULL parameter") );
218 if ( !DoReadLong(key
, &l
) )
221 if ( l
!= 0 && l
!= 1 )
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 "
236 bool wxConfigBase::DoReadDouble(const wxString
& key
, double* val
) const
239 if ( Read(key
, &str
) )
241 if ( str
.ToCDouble(val
) )
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
) )
254 // string reading helper
255 wxString
wxConfigBase::ExpandEnvVars(const wxString
& str
) const
257 wxString tmp
; // Required for BC++
258 if (IsExpandingEnvVars())
259 tmp
= wxExpandEnvVars(str
);
265 // ----------------------------------------------------------------------------
266 // wxConfigBase writing
267 // ----------------------------------------------------------------------------
269 bool wxConfigBase::DoWriteDouble(const wxString
& key
, double val
)
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
));
277 bool wxConfigBase::DoWriteBool(const wxString
& key
, bool value
)
279 return DoWriteLong(key
, value
? 1l : 0l);
282 // ----------------------------------------------------------------------------
283 // wxConfigPathChanger
284 // ----------------------------------------------------------------------------
286 wxConfigPathChanger::wxConfigPathChanger(const wxConfigBase
*pContainer
,
287 const wxString
& strEntry
)
290 m_pContainer
= const_cast<wxConfigBase
*>(pContainer
);
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
);
296 // except in the special case of "/keyname" when there is nothing before "/"
297 if ( strPath
.empty() &&
298 ((!strEntry
.empty()) && strEntry
[0] == wxCONFIG_PATH_SEPARATOR
) )
300 strPath
= wxCONFIG_PATH_SEPARATOR
;
303 if ( !strPath
.empty() )
305 if ( m_pContainer
->GetPath() != strPath
)
307 // we do change the path so restore it later
310 /* JACS: work around a memory bug that causes an assert
311 when using wxRegConfig, related to reference-counting.
312 Can be reproduced by removing .wc_str() below and
313 adding the following code to the config sample OnInit under
316 pConfig->SetPath(wxT("MySettings"));
317 pConfig->SetPath(wxT(".."));
319 pConfig->Read(wxT("MainWindowX"), & value);
321 m_strOldPath
= m_pContainer
->GetPath().wc_str();
322 if ( *m_strOldPath
.c_str() != wxCONFIG_PATH_SEPARATOR
)
323 m_strOldPath
+= wxCONFIG_PATH_SEPARATOR
;
324 m_pContainer
->SetPath(strPath
);
329 void wxConfigPathChanger::UpdateIfDeleted()
331 // we don't have to do anything at all if we didn't change the path
335 // find the deepest still existing parent path of the original path
336 while ( !m_pContainer
->HasGroup(m_strOldPath
) )
338 m_strOldPath
= m_strOldPath
.BeforeLast(wxCONFIG_PATH_SEPARATOR
);
339 if ( m_strOldPath
.empty() )
340 m_strOldPath
= wxCONFIG_PATH_SEPARATOR
;
344 wxConfigPathChanger::~wxConfigPathChanger()
346 // only restore path if it was changed
348 m_pContainer
->SetPath(m_strOldPath
);
352 // this is a wxConfig method but it's mainly used with wxConfigPathChanger
354 wxString
wxConfigBase::RemoveTrailingSeparator(const wxString
& key
)
358 // don't remove the only separator from a root group path!
359 while ( path
.length() > 1 )
361 if ( *path
.rbegin() != wxCONFIG_PATH_SEPARATOR
)
364 path
.erase(path
.end() - 1);
370 #endif // wxUSE_CONFIG
372 // ----------------------------------------------------------------------------
373 // static & global functions
374 // ----------------------------------------------------------------------------
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.
380 // don't change the values the enum elements: they must be equal
381 // to the matching [closing] delimiter.
385 Bracket_Normal
= ')',
388 Bracket_Windows
= '%', // yeah, Windows people are a bit strange ;-)
393 wxString
wxExpandEnvVars(const wxString
& str
)
396 strResult
.Alloc(str
.length());
399 for ( size_t n
= 0; n
< str
.length(); n
++ ) {
400 switch ( str
[n
].GetValue() ) {
403 #endif // __WINDOWS__
408 if ( str
[n
] == wxT('%') )
409 bracket
= Bracket_Windows
;
411 #endif // __WINDOWS__
412 if ( n
== str
.length() - 1 ) {
413 bracket
= Bracket_None
;
416 switch ( str
[n
+ 1].GetValue() ) {
418 bracket
= Bracket_Normal
;
419 n
++; // skip the bracket
423 bracket
= Bracket_Curly
;
424 n
++; // skip the bracket
428 bracket
= Bracket_None
;
434 while ( m
< str
.length() && (wxIsalnum(str
[m
]) || str
[m
] == wxT('_')) )
437 wxString
strVarName(str
.c_str() + n
+ 1, m
- n
- 1);
440 const bool expanded
= false;
442 // NB: use wxGetEnv instead of wxGetenv as otherwise variables
443 // set through wxSetEnv may not be read correctly!
444 bool expanded
= false;
446 if (wxGetEnv(strVarName
, &tmp
))
454 // variable doesn't exist => don't change anything
456 if ( bracket
!= Bracket_Windows
)
458 if ( bracket
!= Bracket_None
)
459 strResult
<< str
[n
- 1];
460 strResult
<< str
[n
] << strVarName
;
463 // check the closing bracket
464 if ( bracket
!= Bracket_None
) {
465 if ( m
== str
.length() || str
[m
] != (wxChar
)bracket
) {
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
470 // under Unix, OTOH, this warning could be useful for the user to
471 // understand why isn't the variable expanded as intended
473 wxLogWarning(_("Environment variables expansion failed: missing '%c' at position %u in '%s'."),
474 (char)bracket
, (unsigned int) (m
+ 1), str
.c_str());
475 #endif // __WINDOWS__
478 // skip closing bracket unless the variables wasn't expanded
480 strResult
<< (wxChar
)bracket
;
485 n
= m
- 1; // skip variable name
490 // backslash can be used to suppress special meaning of % and $
491 if ( n
!= str
.length() - 1 &&
492 (str
[n
+ 1] == wxT('%') || str
[n
+ 1] == wxT('$')) ) {
493 strResult
+= str
[++n
];
507 // this function is used to properly interpret '..' in path
508 void wxSplitPath(wxArrayString
& aParts
, const wxString
& path
)
513 wxString::const_iterator pc
= path
.begin();
515 if ( pc
== path
.end() || *pc
== wxCONFIG_PATH_SEPARATOR
) {
516 if ( strCurrent
== wxT(".") ) {
519 else if ( strCurrent
== wxT("..") ) {
521 if ( aParts
.size() == 0 )
523 wxLogWarning(_("'%s' has extra '..', ignored."), path
);
527 aParts
.erase(aParts
.end() - 1);
532 else if ( !strCurrent
.empty() ) {
533 aParts
.push_back(strCurrent
);
537 // could log an error here, but we prefer to ignore extra '/'
539 if ( pc
== path
.end() )