]>
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 #else // either we're under Unix or wish to use files even under Windows
69 wxFileConfig(wxTheApp
->GetAppName());
73 // ----------------------------------------------------------------------------
75 // ----------------------------------------------------------------------------
76 IMPLEMENT_ABSTRACT_CLASS(wxConfigBase
, wxObject
)
78 // Not all args will always be used by derived classes, but including them all
79 // in each class ensures compatibility.
80 wxConfigBase::wxConfigBase(const wxString
& appName
,
81 const wxString
& vendorName
,
82 const wxString
& WXUNUSED(localFilename
),
83 const wxString
& WXUNUSED(globalFilename
),
85 : m_appName(appName
), m_vendorName(vendorName
), m_style(style
)
87 m_bExpandEnvVars
= true;
88 m_bRecordDefaults
= false;
91 wxConfigBase::~wxConfigBase()
93 // required here for Darwin
96 wxConfigBase
*wxConfigBase::Set(wxConfigBase
*pConfig
)
98 wxConfigBase
*pOld
= ms_pConfig
;
103 wxConfigBase
*wxConfigBase::Create()
105 if ( ms_bAutoCreate
&& ms_pConfig
== NULL
) {
106 wxAppTraits
* const traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
107 wxCHECK_MSG( traits
, NULL
, wxT("create wxApp before calling this") );
109 ms_pConfig
= traits
->CreateConfig();
115 // ----------------------------------------------------------------------------
116 // wxConfigBase reading entries
117 // ----------------------------------------------------------------------------
119 // implement both Read() overloads for the given type in terms of DoRead()
120 #define IMPLEMENT_READ_FOR_TYPE(name, type, deftype, extra) \
121 bool wxConfigBase::Read(const wxString& key, type *val) const \
123 wxCHECK_MSG( val, false, wxT("wxConfig::Read(): NULL parameter") ); \
125 if ( !DoRead##name(key, val) ) \
128 *val = extra(*val); \
133 bool wxConfigBase::Read(const wxString& key, \
135 deftype defVal) const \
137 wxCHECK_MSG( val, false, wxT("wxConfig::Read(): NULL parameter") ); \
139 bool read = DoRead##name(key, val); \
142 if ( IsRecordingDefaults() ) \
144 ((wxConfigBase *)this)->DoWrite##name(key, defVal); \
150 *val = extra(*val); \
156 IMPLEMENT_READ_FOR_TYPE(String
, wxString
, const wxString
&, ExpandEnvVars
)
157 IMPLEMENT_READ_FOR_TYPE(Long
, long, long, long)
158 IMPLEMENT_READ_FOR_TYPE(Double
, double, double, double)
159 IMPLEMENT_READ_FOR_TYPE(Bool
, bool, bool, bool)
161 #undef IMPLEMENT_READ_FOR_TYPE
163 // int is stored as long
164 bool wxConfigBase::Read(const wxString
& key
, int *pi
) const
167 bool r
= Read(key
, &l
);
168 wxASSERT_MSG( l
< INT_MAX
, wxT("int overflow in wxConfig::Read") );
173 bool wxConfigBase::Read(const wxString
& key
, int *pi
, int defVal
) const
176 bool r
= Read(key
, &l
, defVal
);
177 wxASSERT_MSG( l
< INT_MAX
, wxT("int overflow in wxConfig::Read") );
182 // Read floats as doubles then just type cast it down.
183 bool wxConfigBase::Read(const wxString
& key
, float* val
) const
185 wxCHECK_MSG( val
, false, wxT("wxConfig::Read(): NULL parameter") );
188 if ( !Read(key
, &temp
) )
191 wxCHECK_MSG( fabs(temp
) <= FLT_MAX
, false,
192 wxT("float overflow in wxConfig::Read") );
193 wxCHECK_MSG( (temp
== 0.0) || (fabs(temp
) >= FLT_MIN
), false,
194 wxT("float underflow in wxConfig::Read") );
196 *val
= static_cast<float>(temp
);
201 bool wxConfigBase::Read(const wxString
& key
, float* val
, float defVal
) const
203 wxCHECK_MSG( val
, false, wxT("wxConfig::Read(): NULL parameter") );
205 if ( Read(key
, val
) )
212 // the DoReadXXX() for the other types have implementation in the base class
213 // but can be overridden in the derived ones
214 bool wxConfigBase::DoReadBool(const wxString
& key
, bool* val
) const
216 wxCHECK_MSG( val
, false, wxT("wxConfig::Read(): NULL parameter") );
219 if ( !DoReadLong(key
, &l
) )
222 if ( l
!= 0 && l
!= 1 )
224 // Don't assert here as this could happen in the result of user editing
225 // the file directly and this not indicate a bug in the program but
226 // still complain that something is wrong.
227 wxLogWarning(_("Invalid value %ld for a boolean key \"%s\" in "
237 bool wxConfigBase::DoReadDouble(const wxString
& key
, double* val
) const
240 if ( Read(key
, &str
) )
242 if ( str
.ToCDouble(val
) )
245 // Previous versions of wxFileConfig wrote the numbers out using the
246 // current locale and not the C one as now, so attempt to parse the
247 // string as a number in the current locale too, for compatibility.
248 if ( str
.ToDouble(val
) )
255 // string reading helper
256 wxString
wxConfigBase::ExpandEnvVars(const wxString
& str
) const
258 wxString tmp
; // Required for BC++
259 if (IsExpandingEnvVars())
260 tmp
= wxExpandEnvVars(str
);
266 // ----------------------------------------------------------------------------
267 // wxConfigBase writing
268 // ----------------------------------------------------------------------------
270 bool wxConfigBase::DoWriteDouble(const wxString
& key
, double val
)
272 // Notice that we always write out the numbers in C locale and not the
273 // current one. This makes the config files portable between machines using
274 // different locales.
275 return DoWriteString(key
, wxString::FromCDouble(val
));
278 bool wxConfigBase::DoWriteBool(const wxString
& key
, bool value
)
280 return DoWriteLong(key
, value
? 1l : 0l);
283 // ----------------------------------------------------------------------------
284 // wxConfigPathChanger
285 // ----------------------------------------------------------------------------
287 wxConfigPathChanger::wxConfigPathChanger(const wxConfigBase
*pContainer
,
288 const wxString
& strEntry
)
291 m_pContainer
= const_cast<wxConfigBase
*>(pContainer
);
293 // the path is everything which precedes the last slash and the name is
294 // everything after it -- and this works correctly if there is no slash too
295 wxString strPath
= strEntry
.BeforeLast(wxCONFIG_PATH_SEPARATOR
, &m_strName
);
297 // except in the special case of "/keyname" when there is nothing before "/"
298 if ( strPath
.empty() &&
299 ((!strEntry
.empty()) && strEntry
[0] == wxCONFIG_PATH_SEPARATOR
) )
301 strPath
= wxCONFIG_PATH_SEPARATOR
;
304 if ( !strPath
.empty() )
306 if ( m_pContainer
->GetPath() != strPath
)
308 // we do change the path so restore it later
311 /* JACS: work around a memory bug that causes an assert
312 when using wxRegConfig, related to reference-counting.
313 Can be reproduced by removing .wc_str() below and
314 adding the following code to the config sample OnInit under
317 pConfig->SetPath(wxT("MySettings"));
318 pConfig->SetPath(wxT(".."));
320 pConfig->Read(wxT("MainWindowX"), & value);
322 m_strOldPath
= m_pContainer
->GetPath().wc_str();
323 if ( *m_strOldPath
.c_str() != wxCONFIG_PATH_SEPARATOR
)
324 m_strOldPath
+= wxCONFIG_PATH_SEPARATOR
;
325 m_pContainer
->SetPath(strPath
);
330 void wxConfigPathChanger::UpdateIfDeleted()
332 // we don't have to do anything at all if we didn't change the path
336 // find the deepest still existing parent path of the original path
337 while ( !m_pContainer
->HasGroup(m_strOldPath
) )
339 m_strOldPath
= m_strOldPath
.BeforeLast(wxCONFIG_PATH_SEPARATOR
);
340 if ( m_strOldPath
.empty() )
341 m_strOldPath
= wxCONFIG_PATH_SEPARATOR
;
345 wxConfigPathChanger::~wxConfigPathChanger()
347 // only restore path if it was changed
349 m_pContainer
->SetPath(m_strOldPath
);
353 // this is a wxConfig method but it's mainly used with wxConfigPathChanger
355 wxString
wxConfigBase::RemoveTrailingSeparator(const wxString
& key
)
359 // don't remove the only separator from a root group path!
360 while ( path
.length() > 1 )
362 if ( *path
.rbegin() != wxCONFIG_PATH_SEPARATOR
)
365 path
.erase(path
.end() - 1);
371 #endif // wxUSE_CONFIG
373 // ----------------------------------------------------------------------------
374 // static & global functions
375 // ----------------------------------------------------------------------------
377 // understands both Unix and Windows (but only under Windows) environment
378 // variables expansion: i.e. $var, $(var) and ${var} are always understood
379 // and in addition under Windows %var% is also.
381 // don't change the values the enum elements: they must be equal
382 // to the matching [closing] delimiter.
386 Bracket_Normal
= ')',
389 Bracket_Windows
= '%', // yeah, Windows people are a bit strange ;-)
394 wxString
wxExpandEnvVars(const wxString
& str
)
397 strResult
.Alloc(str
.length());
400 for ( size_t n
= 0; n
< str
.length(); n
++ ) {
401 switch ( str
[n
].GetValue() ) {
409 if ( str
[n
] == wxT('%') )
410 bracket
= Bracket_Windows
;
413 if ( n
== str
.length() - 1 ) {
414 bracket
= Bracket_None
;
417 switch ( str
[n
+ 1].GetValue() ) {
419 bracket
= Bracket_Normal
;
420 n
++; // skip the bracket
424 bracket
= Bracket_Curly
;
425 n
++; // skip the bracket
429 bracket
= Bracket_None
;
435 while ( m
< str
.length() && (wxIsalnum(str
[m
]) || str
[m
] == wxT('_')) )
438 wxString
strVarName(str
.c_str() + n
+ 1, m
- n
- 1);
441 const bool expanded
= false;
443 // NB: use wxGetEnv instead of wxGetenv as otherwise variables
444 // set through wxSetEnv may not be read correctly!
445 bool expanded
= false;
447 if (wxGetEnv(strVarName
, &tmp
))
455 // variable doesn't exist => don't change anything
457 if ( bracket
!= Bracket_Windows
)
459 if ( bracket
!= Bracket_None
)
460 strResult
<< str
[n
- 1];
461 strResult
<< str
[n
] << strVarName
;
464 // check the closing bracket
465 if ( bracket
!= Bracket_None
) {
466 if ( m
== str
.length() || str
[m
] != (wxChar
)bracket
) {
467 // under MSW it's common to have '%' characters in the registry
468 // and it's annoying to have warnings about them each time, so
469 // ignroe them silently if they are not used for env vars
471 // under Unix, OTOH, this warning could be useful for the user to
472 // understand why isn't the variable expanded as intended
474 wxLogWarning(_("Environment variables expansion failed: missing '%c' at position %u in '%s'."),
475 (char)bracket
, (unsigned int) (m
+ 1), str
.c_str());
479 // skip closing bracket unless the variables wasn't expanded
481 strResult
<< (wxChar
)bracket
;
486 n
= m
- 1; // skip variable name
491 // backslash can be used to suppress special meaning of % and $
492 if ( n
!= str
.length() - 1 &&
493 (str
[n
+ 1] == wxT('%') || str
[n
+ 1] == wxT('$')) ) {
494 strResult
+= str
[++n
];
508 // this function is used to properly interpret '..' in path
509 void wxSplitPath(wxArrayString
& aParts
, const wxString
& path
)
514 wxString::const_iterator pc
= path
.begin();
516 if ( pc
== path
.end() || *pc
== wxCONFIG_PATH_SEPARATOR
) {
517 if ( strCurrent
== wxT(".") ) {
520 else if ( strCurrent
== wxT("..") ) {
522 if ( aParts
.size() == 0 )
524 wxLogWarning(_("'%s' has extra '..', ignored."), path
);
528 aParts
.erase(aParts
.end() - 1);
533 else if ( !strCurrent
.empty() ) {
534 aParts
.push_back(strCurrent
);
538 // could log an error here, but we prefer to ignore extra '/'
540 if ( pc
== path
.end() )