]>
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 wxASSERT_MSG( l
== 0 || l
== 1, wxT("bad bool value in wxConfig::DoReadInt") );
231 bool wxConfigBase::DoReadDouble(const wxString
& key
, double* val
) const
234 if ( Read(key
, &str
) )
236 if ( str
.ToCDouble(val
) )
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
) )
249 // string reading helper
250 wxString
wxConfigBase::ExpandEnvVars(const wxString
& str
) const
252 wxString tmp
; // Required for BC++
253 if (IsExpandingEnvVars())
254 tmp
= wxExpandEnvVars(str
);
260 // ----------------------------------------------------------------------------
261 // wxConfigBase writing
262 // ----------------------------------------------------------------------------
264 bool wxConfigBase::DoWriteDouble(const wxString
& key
, double val
)
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
));
272 bool wxConfigBase::DoWriteBool(const wxString
& key
, bool value
)
274 return DoWriteLong(key
, value
? 1l : 0l);
277 // ----------------------------------------------------------------------------
278 // wxConfigPathChanger
279 // ----------------------------------------------------------------------------
281 wxConfigPathChanger::wxConfigPathChanger(const wxConfigBase
*pContainer
,
282 const wxString
& strEntry
)
285 m_pContainer
= const_cast<wxConfigBase
*>(pContainer
);
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
);
291 // except in the special case of "/keyname" when there is nothing before "/"
292 if ( strPath
.empty() &&
293 ((!strEntry
.empty()) && strEntry
[0] == wxCONFIG_PATH_SEPARATOR
) )
295 strPath
= wxCONFIG_PATH_SEPARATOR
;
298 if ( !strPath
.empty() )
300 if ( m_pContainer
->GetPath() != strPath
)
302 // we do change the path so restore it later
305 /* JACS: work around a memory bug that causes an assert
306 when using wxRegConfig, related to reference-counting.
307 Can be reproduced by removing .wc_str() below and
308 adding the following code to the config sample OnInit under
311 pConfig->SetPath(wxT("MySettings"));
312 pConfig->SetPath(wxT(".."));
314 pConfig->Read(wxT("MainWindowX"), & value);
316 m_strOldPath
= m_pContainer
->GetPath().wc_str();
317 if ( *m_strOldPath
.c_str() != wxCONFIG_PATH_SEPARATOR
)
318 m_strOldPath
+= wxCONFIG_PATH_SEPARATOR
;
319 m_pContainer
->SetPath(strPath
);
324 void wxConfigPathChanger::UpdateIfDeleted()
326 // we don't have to do anything at all if we didn't change the path
330 // find the deepest still existing parent path of the original path
331 while ( !m_pContainer
->HasGroup(m_strOldPath
) )
333 m_strOldPath
= m_strOldPath
.BeforeLast(wxCONFIG_PATH_SEPARATOR
);
334 if ( m_strOldPath
.empty() )
335 m_strOldPath
= wxCONFIG_PATH_SEPARATOR
;
339 wxConfigPathChanger::~wxConfigPathChanger()
341 // only restore path if it was changed
343 m_pContainer
->SetPath(m_strOldPath
);
347 // this is a wxConfig method but it's mainly used with wxConfigPathChanger
349 wxString
wxConfigBase::RemoveTrailingSeparator(const wxString
& key
)
353 // don't remove the only separator from a root group path!
354 while ( path
.length() > 1 )
356 if ( *path
.rbegin() != wxCONFIG_PATH_SEPARATOR
)
359 path
.erase(path
.end() - 1);
365 #endif // wxUSE_CONFIG
367 // ----------------------------------------------------------------------------
368 // static & global functions
369 // ----------------------------------------------------------------------------
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.
375 // don't change the values the enum elements: they must be equal
376 // to the matching [closing] delimiter.
380 Bracket_Normal
= ')',
383 Bracket_Windows
= '%', // yeah, Windows people are a bit strange ;-)
388 wxString
wxExpandEnvVars(const wxString
& str
)
391 strResult
.Alloc(str
.length());
394 for ( size_t n
= 0; n
< str
.length(); n
++ ) {
395 switch ( str
[n
].GetValue() ) {
403 if ( str
[n
] == wxT('%') )
404 bracket
= Bracket_Windows
;
407 if ( n
== str
.length() - 1 ) {
408 bracket
= Bracket_None
;
411 switch ( str
[n
+ 1].GetValue() ) {
413 bracket
= Bracket_Normal
;
414 n
++; // skip the bracket
418 bracket
= Bracket_Curly
;
419 n
++; // skip the bracket
423 bracket
= Bracket_None
;
429 while ( m
< str
.length() && (wxIsalnum(str
[m
]) || str
[m
] == wxT('_')) )
432 wxString
strVarName(str
.c_str() + n
+ 1, m
- n
- 1);
435 const bool expanded
= false;
437 // NB: use wxGetEnv instead of wxGetenv as otherwise variables
438 // set through wxSetEnv may not be read correctly!
439 bool expanded
= false;
441 if (wxGetEnv(strVarName
, &tmp
))
449 // variable doesn't exist => don't change anything
451 if ( bracket
!= Bracket_Windows
)
453 if ( bracket
!= Bracket_None
)
454 strResult
<< str
[n
- 1];
455 strResult
<< str
[n
] << strVarName
;
458 // check the closing bracket
459 if ( bracket
!= Bracket_None
) {
460 if ( m
== str
.length() || str
[m
] != (wxChar
)bracket
) {
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
465 // under Unix, OTOH, this warning could be useful for the user to
466 // understand why isn't the variable expanded as intended
468 wxLogWarning(_("Environment variables expansion failed: missing '%c' at position %u in '%s'."),
469 (char)bracket
, (unsigned int) (m
+ 1), str
.c_str());
473 // skip closing bracket unless the variables wasn't expanded
475 strResult
<< (wxChar
)bracket
;
480 n
= m
- 1; // skip variable name
485 // backslash can be used to suppress special meaning of % and $
486 if ( n
!= str
.length() - 1 &&
487 (str
[n
+ 1] == wxT('%') || str
[n
+ 1] == wxT('$')) ) {
488 strResult
+= str
[++n
];
502 // this function is used to properly interpret '..' in path
503 void wxSplitPath(wxArrayString
& aParts
, const wxString
& path
)
508 wxString::const_iterator pc
= path
.begin();
510 if ( pc
== path
.end() || *pc
== wxCONFIG_PATH_SEPARATOR
) {
511 if ( strCurrent
== wxT(".") ) {
514 else if ( strCurrent
== wxT("..") ) {
516 if ( aParts
.size() == 0 )
518 wxLogWarning(_("'%s' has extra '..', ignored."), path
);
522 aParts
.erase(aParts
.end() - 1);
527 else if ( !strCurrent
.empty() ) {
528 aParts
.push_back(strCurrent
);
532 // could log an error here, but we prefer to ignore extra '/'
534 if ( pc
== path
.end() )