]>
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
8 // Copyright: (c) 1997 Karsten Ballueder Ballueder@usa.net
9 // Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
10 // Licence: wxWindows licence
11 ///////////////////////////////////////////////////////////////////////////////
13 // ----------------------------------------------------------------------------
15 // ----------------------------------------------------------------------------
17 #include "wx/wxprec.h"
23 #ifndef wxUSE_CONFIG_NATIVE
24 #define wxUSE_CONFIG_NATIVE 1
27 #include "wx/config.h"
34 #include "wx/arrstr.h"
38 #if wxUSE_CONFIG && ((wxUSE_FILE && wxUSE_TEXTFILE) || wxUSE_CONFIG_NATIVE)
40 #include "wx/apptrait.h"
45 #include <limits.h> // for INT_MAX
46 #include <float.h> // for FLT_MAX
48 // ----------------------------------------------------------------------------
49 // global and class static variables
50 // ----------------------------------------------------------------------------
52 wxConfigBase
*wxConfigBase::ms_pConfig
= NULL
;
53 bool wxConfigBase::ms_bAutoCreate
= true;
55 // ============================================================================
57 // ============================================================================
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 wxConfigBase
*wxAppTraitsBase::CreateConfig()
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());
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
78 IMPLEMENT_ABSTRACT_CLASS(wxConfigBase
, wxObject
)
80 // Not all args will always be used by derived classes, but including them all
81 // in each class ensures compatibility.
82 wxConfigBase::wxConfigBase(const wxString
& appName
,
83 const wxString
& vendorName
,
84 const wxString
& WXUNUSED(localFilename
),
85 const wxString
& WXUNUSED(globalFilename
),
87 : m_appName(appName
), m_vendorName(vendorName
), m_style(style
)
89 m_bExpandEnvVars
= true;
90 m_bRecordDefaults
= false;
93 wxConfigBase::~wxConfigBase()
95 // required here for Darwin
98 wxConfigBase
*wxConfigBase::Set(wxConfigBase
*pConfig
)
100 wxConfigBase
*pOld
= ms_pConfig
;
101 ms_pConfig
= pConfig
;
105 wxConfigBase
*wxConfigBase::Create()
107 if ( ms_bAutoCreate
&& ms_pConfig
== NULL
) {
108 wxAppTraits
* const traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
109 wxCHECK_MSG( traits
, NULL
, wxT("create wxApp before calling this") );
111 ms_pConfig
= traits
->CreateConfig();
117 // ----------------------------------------------------------------------------
118 // wxConfigBase reading entries
119 // ----------------------------------------------------------------------------
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 \
125 wxCHECK_MSG( val, false, wxT("wxConfig::Read(): NULL parameter") ); \
127 if ( !DoRead##name(key, val) ) \
130 *val = extra(*val); \
135 bool wxConfigBase::Read(const wxString& key, \
137 deftype defVal) const \
139 wxCHECK_MSG( val, false, wxT("wxConfig::Read(): NULL parameter") ); \
141 bool read = DoRead##name(key, val); \
144 if ( IsRecordingDefaults() ) \
146 ((wxConfigBase *)this)->DoWrite##name(key, defVal); \
152 *val = extra(*val); \
158 IMPLEMENT_READ_FOR_TYPE(String
, wxString
, const wxString
&, ExpandEnvVars
)
159 IMPLEMENT_READ_FOR_TYPE(Long
, long, long, long)
160 IMPLEMENT_READ_FOR_TYPE(Double
, double, double, double)
161 IMPLEMENT_READ_FOR_TYPE(Bool
, bool, bool, bool)
163 #undef IMPLEMENT_READ_FOR_TYPE
165 // int is stored as long
166 bool wxConfigBase::Read(const wxString
& key
, int *pi
) const
169 bool r
= Read(key
, &l
);
170 wxASSERT_MSG( l
< INT_MAX
, wxT("int overflow in wxConfig::Read") );
175 bool wxConfigBase::Read(const wxString
& key
, int *pi
, int defVal
) const
178 bool r
= Read(key
, &l
, defVal
);
179 wxASSERT_MSG( l
< INT_MAX
, wxT("int overflow in wxConfig::Read") );
184 // Read floats as doubles then just type cast it down.
185 bool wxConfigBase::Read(const wxString
& key
, float* val
) const
187 wxCHECK_MSG( val
, false, wxT("wxConfig::Read(): NULL parameter") );
190 if ( !Read(key
, &temp
) )
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") );
198 *val
= static_cast<float>(temp
);
203 bool wxConfigBase::Read(const wxString
& key
, float* val
, float defVal
) const
205 wxCHECK_MSG( val
, false, wxT("wxConfig::Read(): NULL parameter") );
207 if ( Read(key
, val
) )
214 // the DoReadXXX() for the other types have implementation in the base class
215 // but can be overridden in the derived ones
216 bool wxConfigBase::DoReadBool(const wxString
& key
, bool* val
) const
218 wxCHECK_MSG( val
, false, wxT("wxConfig::Read(): NULL parameter") );
221 if ( !DoReadLong(key
, &l
) )
224 if ( l
!= 0 && l
!= 1 )
226 // Don't assert here as this could happen in the result of user editing
227 // the file directly and this not indicate a bug in the program but
228 // still complain that something is wrong.
229 wxLogWarning(_("Invalid value %ld for a boolean key \"%s\" in "
239 bool wxConfigBase::DoReadDouble(const wxString
& key
, double* val
) const
242 if ( Read(key
, &str
) )
244 if ( str
.ToCDouble(val
) )
247 // Previous versions of wxFileConfig wrote the numbers out using the
248 // current locale and not the C one as now, so attempt to parse the
249 // string as a number in the current locale too, for compatibility.
250 if ( str
.ToDouble(val
) )
257 // string reading helper
258 wxString
wxConfigBase::ExpandEnvVars(const wxString
& str
) const
260 wxString tmp
; // Required for BC++
261 if (IsExpandingEnvVars())
262 tmp
= wxExpandEnvVars(str
);
268 // ----------------------------------------------------------------------------
269 // wxConfigBase writing
270 // ----------------------------------------------------------------------------
272 bool wxConfigBase::DoWriteDouble(const wxString
& key
, double val
)
274 // Notice that we always write out the numbers in C locale and not the
275 // current one. This makes the config files portable between machines using
276 // different locales.
277 return DoWriteString(key
, wxString::FromCDouble(val
));
280 bool wxConfigBase::DoWriteBool(const wxString
& key
, bool value
)
282 return DoWriteLong(key
, value
? 1l : 0l);
285 // ----------------------------------------------------------------------------
286 // wxConfigPathChanger
287 // ----------------------------------------------------------------------------
289 wxConfigPathChanger::wxConfigPathChanger(const wxConfigBase
*pContainer
,
290 const wxString
& strEntry
)
293 m_pContainer
= const_cast<wxConfigBase
*>(pContainer
);
295 // the path is everything which precedes the last slash and the name is
296 // everything after it -- and this works correctly if there is no slash too
297 wxString strPath
= strEntry
.BeforeLast(wxCONFIG_PATH_SEPARATOR
, &m_strName
);
299 // except in the special case of "/keyname" when there is nothing before "/"
300 if ( strPath
.empty() &&
301 ((!strEntry
.empty()) && strEntry
[0] == wxCONFIG_PATH_SEPARATOR
) )
303 strPath
= wxCONFIG_PATH_SEPARATOR
;
306 if ( !strPath
.empty() )
308 if ( m_pContainer
->GetPath() != strPath
)
310 // we do change the path so restore it later
313 /* JACS: work around a memory bug that causes an assert
314 when using wxRegConfig, related to reference-counting.
315 Can be reproduced by removing .wc_str() below and
316 adding the following code to the config sample OnInit under
319 pConfig->SetPath(wxT("MySettings"));
320 pConfig->SetPath(wxT(".."));
322 pConfig->Read(wxT("MainWindowX"), & value);
324 m_strOldPath
= m_pContainer
->GetPath().wc_str();
325 if ( *m_strOldPath
.c_str() != wxCONFIG_PATH_SEPARATOR
)
326 m_strOldPath
+= wxCONFIG_PATH_SEPARATOR
;
327 m_pContainer
->SetPath(strPath
);
332 void wxConfigPathChanger::UpdateIfDeleted()
334 // we don't have to do anything at all if we didn't change the path
338 // find the deepest still existing parent path of the original path
339 while ( !m_pContainer
->HasGroup(m_strOldPath
) )
341 m_strOldPath
= m_strOldPath
.BeforeLast(wxCONFIG_PATH_SEPARATOR
);
342 if ( m_strOldPath
.empty() )
343 m_strOldPath
= wxCONFIG_PATH_SEPARATOR
;
347 wxConfigPathChanger::~wxConfigPathChanger()
349 // only restore path if it was changed
351 m_pContainer
->SetPath(m_strOldPath
);
355 // this is a wxConfig method but it's mainly used with wxConfigPathChanger
357 wxString
wxConfigBase::RemoveTrailingSeparator(const wxString
& key
)
361 // don't remove the only separator from a root group path!
362 while ( path
.length() > 1 )
364 if ( *path
.rbegin() != wxCONFIG_PATH_SEPARATOR
)
367 path
.erase(path
.end() - 1);
373 #endif // wxUSE_CONFIG
375 // ----------------------------------------------------------------------------
376 // static & global functions
377 // ----------------------------------------------------------------------------
379 // understands both Unix and Windows (but only under Windows) environment
380 // variables expansion: i.e. $var, $(var) and ${var} are always understood
381 // and in addition under Windows %var% is also.
383 // don't change the values the enum elements: they must be equal
384 // to the matching [closing] delimiter.
388 Bracket_Normal
= ')',
391 Bracket_Windows
= '%', // yeah, Windows people are a bit strange ;-)
396 wxString
wxExpandEnvVars(const wxString
& str
)
399 strResult
.Alloc(str
.length());
402 for ( size_t n
= 0; n
< str
.length(); n
++ ) {
403 switch ( str
[n
].GetValue() ) {
411 if ( str
[n
] == wxT('%') )
412 bracket
= Bracket_Windows
;
415 if ( n
== str
.length() - 1 ) {
416 bracket
= Bracket_None
;
419 switch ( str
[n
+ 1].GetValue() ) {
421 bracket
= Bracket_Normal
;
422 n
++; // skip the bracket
426 bracket
= Bracket_Curly
;
427 n
++; // skip the bracket
431 bracket
= Bracket_None
;
437 while ( m
< str
.length() && (wxIsalnum(str
[m
]) || str
[m
] == wxT('_')) )
440 wxString
strVarName(str
.c_str() + n
+ 1, m
- n
- 1);
443 const bool expanded
= false;
445 // NB: use wxGetEnv instead of wxGetenv as otherwise variables
446 // set through wxSetEnv may not be read correctly!
447 bool expanded
= false;
449 if (wxGetEnv(strVarName
, &tmp
))
457 // variable doesn't exist => don't change anything
459 if ( bracket
!= Bracket_Windows
)
461 if ( bracket
!= Bracket_None
)
462 strResult
<< str
[n
- 1];
463 strResult
<< str
[n
] << strVarName
;
466 // check the closing bracket
467 if ( bracket
!= Bracket_None
) {
468 if ( m
== str
.length() || str
[m
] != (wxChar
)bracket
) {
469 // under MSW it's common to have '%' characters in the registry
470 // and it's annoying to have warnings about them each time, so
471 // ignroe them silently if they are not used for env vars
473 // under Unix, OTOH, this warning could be useful for the user to
474 // understand why isn't the variable expanded as intended
476 wxLogWarning(_("Environment variables expansion failed: missing '%c' at position %u in '%s'."),
477 (char)bracket
, (unsigned int) (m
+ 1), str
.c_str());
481 // skip closing bracket unless the variables wasn't expanded
483 strResult
<< (wxChar
)bracket
;
488 n
= m
- 1; // skip variable name
493 // backslash can be used to suppress special meaning of % and $
494 if ( n
!= str
.length() - 1 &&
495 (str
[n
+ 1] == wxT('%') || str
[n
+ 1] == wxT('$')) ) {
496 strResult
+= str
[++n
];
510 // this function is used to properly interpret '..' in path
511 void wxSplitPath(wxArrayString
& aParts
, const wxString
& path
)
516 wxString::const_iterator pc
= path
.begin();
518 if ( pc
== path
.end() || *pc
== wxCONFIG_PATH_SEPARATOR
) {
519 if ( strCurrent
== wxT(".") ) {
522 else if ( strCurrent
== wxT("..") ) {
524 if ( aParts
.size() == 0 )
526 wxLogWarning(_("'%s' has extra '..', ignored."), path
);
530 aParts
.erase(aParts
.end() - 1);
535 else if ( !strCurrent
.empty() ) {
536 aParts
.push_back(strCurrent
);
540 // could log an error here, but we prefer to ignore extra '/'
542 if ( pc
== path
.end() )