]>
git.saurik.com Git - wxWidgets.git/blob - src/common/config.cpp
80b43e4b9a5c5a38cfdf2278c727aabc370193c2
1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: implementation of wxConfigBase class
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1997 Karsten Ballüder Ballueder@usa.net
9 // Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
10 // Licence: wxWindows licence
11 ///////////////////////////////////////////////////////////////////////////////
13 // ----------------------------------------------------------------------------
15 // ----------------------------------------------------------------------------
16 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
17 #pragma implementation "confbase.h"
20 #include "wx/wxprec.h"
26 #ifndef wxUSE_CONFIG_NATIVE
27 #define wxUSE_CONFIG_NATIVE 1
30 #include "wx/config.h"
33 #include "wx/arrstr.h"
35 #if wxUSE_CONFIG && ((wxUSE_FILE && wxUSE_TEXTFILE) || wxUSE_CONFIG_NATIVE)
39 #include "wx/textfile.h"
46 #include <limits.h> // for INT_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 // Not all args will always be used by derived classes, but including them all
64 // in each class ensures compatibility.
65 wxConfigBase::wxConfigBase(const wxString
& appName
,
66 const wxString
& vendorName
,
67 const wxString
& WXUNUSED(localFilename
),
68 const wxString
& WXUNUSED(globalFilename
),
70 : m_appName(appName
), m_vendorName(vendorName
), m_style(style
)
72 m_bExpandEnvVars
= true;
73 m_bRecordDefaults
= false;
76 wxConfigBase::~wxConfigBase()
80 wxConfigBase
*wxConfigBase::Set(wxConfigBase
*pConfig
)
82 wxConfigBase
*pOld
= ms_pConfig
;
87 wxConfigBase
*wxConfigBase::Create()
89 if ( ms_bAutoCreate
&& ms_pConfig
== NULL
) {
91 #if defined(__WXMSW__) && wxUSE_CONFIG_NATIVE
92 new wxRegConfig(wxTheApp
->GetAppName(), wxTheApp
->GetVendorName());
93 #else // either we're under Unix or wish to use files even under Windows
94 new wxFileConfig(wxTheApp
->GetAppName());
101 // ----------------------------------------------------------------------------
102 // wxConfigBase reading entries
103 // ----------------------------------------------------------------------------
105 // implement both Read() overloads for the given type in terms of DoRead()
106 #define IMPLEMENT_READ_FOR_TYPE(name, type, deftype, extra) \
107 bool wxConfigBase::Read(const wxString& key, type *val) const \
109 wxCHECK_MSG( val, false, _T("wxConfig::Read(): NULL parameter") ); \
111 if ( !DoRead##name(key, val) ) \
114 *val = extra(*val); \
119 bool wxConfigBase::Read(const wxString& key, \
121 deftype defVal) const \
123 wxCHECK_MSG( val, false, _T("wxConfig::Read(): NULL parameter") ); \
125 bool read = DoRead##name(key, val); \
128 if ( IsRecordingDefaults() ) \
130 ((wxConfigBase *)this)->DoWrite##name(key, defVal); \
136 *val = extra(*val); \
142 IMPLEMENT_READ_FOR_TYPE(String
, wxString
, const wxString
&, ExpandEnvVars
)
143 IMPLEMENT_READ_FOR_TYPE(Long
, long, long, long)
144 IMPLEMENT_READ_FOR_TYPE(Int
, int, int, int)
145 IMPLEMENT_READ_FOR_TYPE(Double
, double, double, double)
146 IMPLEMENT_READ_FOR_TYPE(Bool
, bool, bool, bool)
148 #undef IMPLEMENT_READ_FOR_TYPE
150 // the DoReadXXX() for the other types have implementation in the base class
151 // but can be overridden in the derived ones
152 bool wxConfigBase::DoReadInt(const wxString
& key
, int *pi
) const
154 wxCHECK_MSG( pi
, false, _T("wxConfig::Read(): NULL parameter") );
157 if ( !DoReadLong(key
, &l
) )
160 wxASSERT_MSG( l
< INT_MAX
, _T("overflow in wxConfig::DoReadInt") );
167 bool wxConfigBase::DoReadBool(const wxString
& key
, bool* val
) const
169 wxCHECK_MSG( val
, false, _T("wxConfig::Read(): NULL parameter") );
172 if ( !DoReadLong(key
, &l
) )
175 wxASSERT_MSG( l
== 0 || l
== 1, _T("bad bool value in wxConfig::DoReadInt") );
182 bool wxConfigBase::DoReadDouble(const wxString
& key
, double* val
) const
185 if ( Read(key
, &str
) )
187 return str
.ToDouble(val
);
193 // string reading helper
194 wxString
wxConfigBase::ExpandEnvVars(const wxString
& str
) const
196 wxString tmp
; // Required for BC++
197 if (IsExpandingEnvVars())
198 tmp
= wxExpandEnvVars(str
);
204 // ----------------------------------------------------------------------------
205 // wxConfigBase writing
206 // ----------------------------------------------------------------------------
208 bool wxConfigBase::DoWriteDouble(const wxString
& key
, double val
)
210 return DoWriteString(key
, wxString::Format(_T("%g"), val
));
213 bool wxConfigBase::DoWriteInt(const wxString
& key
, int value
)
215 return DoWriteLong(key
, (long)value
);
218 bool wxConfigBase::DoWriteBool(const wxString
& key
, bool value
)
220 return DoWriteLong(key
, value
? 1l : 0l);
223 // ----------------------------------------------------------------------------
224 // wxConfigPathChanger
225 // ----------------------------------------------------------------------------
227 wxConfigPathChanger::wxConfigPathChanger(const wxConfigBase
*pContainer
,
228 const wxString
& strEntry
)
230 m_pContainer
= (wxConfigBase
*)pContainer
;
232 // the path is everything which precedes the last slash
233 wxString strPath
= strEntry
.BeforeLast(wxCONFIG_PATH_SEPARATOR
);
235 // except in the special case of "/keyname" when there is nothing before "/"
236 if ( strPath
.IsEmpty() &&
237 ((!strEntry
.IsEmpty()) && strEntry
[0] == wxCONFIG_PATH_SEPARATOR
) )
239 strPath
= wxCONFIG_PATH_SEPARATOR
;
242 if ( !strPath
.IsEmpty() && m_pContainer
->GetPath() != strPath
) {
243 // do change the path
245 m_strName
= strEntry
.AfterLast(wxCONFIG_PATH_SEPARATOR
);
246 m_strOldPath
= m_pContainer
->GetPath();
247 if ( m_strOldPath
.Len() == 0 ||
248 m_strOldPath
.Last() != wxCONFIG_PATH_SEPARATOR
)
249 m_strOldPath
+= wxCONFIG_PATH_SEPARATOR
;
250 m_pContainer
->SetPath(strPath
);
253 // it's a name only, without path - nothing to do
255 m_strName
= strEntry
;
259 wxConfigPathChanger::~wxConfigPathChanger()
261 // only restore path if it was changed
263 m_pContainer
->SetPath(m_strOldPath
);
267 #endif // wxUSE_CONFIG
269 // ----------------------------------------------------------------------------
270 // static & global functions
271 // ----------------------------------------------------------------------------
273 // understands both Unix and Windows (but only under Windows) environment
274 // variables expansion: i.e. $var, $(var) and ${var} are always understood
275 // and in addition under Windows %var% is also.
276 wxString
wxExpandEnvVars(const wxString
& str
)
279 strResult
.Alloc(str
.Len());
281 // don't change the values the enum elements: they must be equal
282 // to the matching [closing] delimiter.
286 Bracket_Normal
= ')',
289 Bracket_Windows
= '%', // yeah, Windows people are a bit strange ;-)
295 for ( size_t n
= 0; n
< str
.Len(); n
++ ) {
304 if ( str
[n
] == wxT('%') )
305 bracket
= Bracket_Windows
;
308 if ( n
== str
.Len() - 1 ) {
309 bracket
= Bracket_None
;
312 switch ( str
[n
+ 1] ) {
314 bracket
= Bracket_Normal
;
315 n
++; // skip the bracket
319 bracket
= Bracket_Curly
;
320 n
++; // skip the bracket
324 bracket
= Bracket_None
;
330 while ( m
< str
.Len() && (wxIsalnum(str
[m
]) || str
[m
] == wxT('_')) )
333 wxString
strVarName(str
.c_str() + n
+ 1, m
- n
- 1);
336 const wxChar
*pszValue
= NULL
;
338 const wxChar
*pszValue
= wxGetenv(strVarName
);
340 if ( pszValue
!= NULL
) {
341 strResult
+= pszValue
;
344 // variable doesn't exist => don't change anything
346 if ( bracket
!= Bracket_Windows
)
348 if ( bracket
!= Bracket_None
)
349 strResult
<< str
[n
- 1];
350 strResult
<< str
[n
] << strVarName
;
353 // check the closing bracket
354 if ( bracket
!= Bracket_None
) {
355 if ( m
== str
.Len() || str
[m
] != (wxChar
)bracket
) {
356 // under MSW it's common to have '%' characters in the registry
357 // and it's annoying to have warnings about them each time, so
358 // ignroe them silently if they are not used for env vars
360 // under Unix, OTOH, this warning could be useful for the user to
361 // understand why isn't the variable expanded as intended
363 wxLogWarning(_("Environment variables expansion failed: missing '%c' at position %d in '%s'."),
364 (char)bracket
, m
+ 1, str
.c_str());
368 // skip closing bracket unless the variables wasn't expanded
369 if ( pszValue
== NULL
)
370 strResult
<< (char)bracket
;
375 n
= m
- 1; // skip variable name
380 // backslash can be used to suppress special meaning of % and $
381 if ( n
!= str
.Len() && (str
[n
+ 1] == wxT('%') || str
[n
+ 1] == wxT('$')) ) {
382 strResult
+= str
[++n
];
396 // this function is used to properly interpret '..' in path
397 void wxSplitPath(wxArrayString
& aParts
, const wxChar
*sz
)
402 const wxChar
*pc
= sz
;
404 if ( *pc
== wxT('\0') || *pc
== wxCONFIG_PATH_SEPARATOR
) {
405 if ( strCurrent
== wxT(".") ) {
408 else if ( strCurrent
== wxT("..") ) {
410 if ( aParts
.size() == 0 )
411 wxLogWarning(_("'%s' has extra '..', ignored."), sz
);
413 aParts
.erase(aParts
.end() - 1);
417 else if ( !strCurrent
.IsEmpty() ) {
418 aParts
.push_back(strCurrent
);
422 // could log an error here, but we prefer to ignore extra '/'
424 if ( *pc
== wxT('\0') )