]>
git.saurik.com Git - wxWidgets.git/blob - src/common/config.cpp
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::Set(wxConfigBase
*pConfig
)
78 wxConfigBase
*pOld
= ms_pConfig
;
83 wxConfigBase
*wxConfigBase::Create()
85 if ( ms_bAutoCreate
&& ms_pConfig
== NULL
) {
87 #if defined(__WXMSW__) && wxUSE_CONFIG_NATIVE
88 new wxRegConfig(wxTheApp
->GetAppName(), wxTheApp
->GetVendorName());
89 #else // either we're under Unix or wish to use files even under Windows
90 new wxFileConfig(wxTheApp
->GetAppName());
97 // ----------------------------------------------------------------------------
98 // wxConfigBase reading entries
99 // ----------------------------------------------------------------------------
101 // implement both Read() overloads for the given type in terms of DoRead()
102 #define IMPLEMENT_READ_FOR_TYPE(name, type, deftype, extra) \
103 bool wxConfigBase::Read(const wxString& key, type *val) const \
105 wxCHECK_MSG( val, false, _T("wxConfig::Read(): NULL parameter") ); \
107 if ( !DoRead##name(key, val) ) \
110 *val = extra(*val); \
115 bool wxConfigBase::Read(const wxString& key, \
117 deftype defVal) const \
119 wxCHECK_MSG( val, false, _T("wxConfig::Read(): NULL parameter") ); \
121 bool read = DoRead##name(key, val); \
124 if ( IsRecordingDefaults() ) \
126 ((wxConfigBase *)this)->DoWrite##name(key, defVal); \
132 *val = extra(*val); \
138 IMPLEMENT_READ_FOR_TYPE(String
, wxString
, const wxString
&, ExpandEnvVars
)
139 IMPLEMENT_READ_FOR_TYPE(Long
, long, long, long)
140 IMPLEMENT_READ_FOR_TYPE(Int
, int, int, int)
141 IMPLEMENT_READ_FOR_TYPE(Double
, double, double, double)
142 IMPLEMENT_READ_FOR_TYPE(Bool
, bool, bool, bool)
144 #undef IMPLEMENT_READ_FOR_TYPE
146 // the DoReadXXX() for the other types have implementation in the base class
147 // but can be overridden in the derived ones
148 bool wxConfigBase::DoReadInt(const wxString
& key
, int *pi
) const
150 wxCHECK_MSG( pi
, false, _T("wxConfig::Read(): NULL parameter") );
153 if ( !DoReadLong(key
, &l
) )
156 wxASSERT_MSG( l
< INT_MAX
, _T("overflow in wxConfig::DoReadInt") );
163 bool wxConfigBase::DoReadBool(const wxString
& key
, bool* val
) const
165 wxCHECK_MSG( val
, false, _T("wxConfig::Read(): NULL parameter") );
168 if ( !DoReadLong(key
, &l
) )
171 wxASSERT_MSG( l
== 0 || l
== 1, _T("bad bool value in wxConfig::DoReadInt") );
178 bool wxConfigBase::DoReadDouble(const wxString
& key
, double* val
) const
181 if ( Read(key
, &str
) )
183 return str
.ToDouble(val
);
189 // string reading helper
190 wxString
wxConfigBase::ExpandEnvVars(const wxString
& str
) const
192 wxString tmp
; // Required for BC++
193 if (IsExpandingEnvVars())
194 tmp
= wxExpandEnvVars(str
);
200 // ----------------------------------------------------------------------------
201 // wxConfigBase writing
202 // ----------------------------------------------------------------------------
204 bool wxConfigBase::DoWriteDouble(const wxString
& key
, double val
)
206 return DoWriteString(key
, wxString::Format(_T("%g"), val
));
209 bool wxConfigBase::DoWriteInt(const wxString
& key
, int value
)
211 return DoWriteLong(key
, (long)value
);
214 bool wxConfigBase::DoWriteBool(const wxString
& key
, bool value
)
216 return DoWriteLong(key
, value
? 1l : 0l);
219 // ----------------------------------------------------------------------------
220 // wxConfigPathChanger
221 // ----------------------------------------------------------------------------
223 wxConfigPathChanger::wxConfigPathChanger(const wxConfigBase
*pContainer
,
224 const wxString
& strEntry
)
226 m_pContainer
= (wxConfigBase
*)pContainer
;
228 // the path is everything which precedes the last slash
229 wxString strPath
= strEntry
.BeforeLast(wxCONFIG_PATH_SEPARATOR
);
231 // except in the special case of "/keyname" when there is nothing before "/"
232 if ( strPath
.IsEmpty() &&
233 ((!strEntry
.IsEmpty()) && strEntry
[0] == wxCONFIG_PATH_SEPARATOR
) )
235 strPath
= wxCONFIG_PATH_SEPARATOR
;
238 if ( !strPath
.IsEmpty() && m_pContainer
->GetPath() != strPath
) {
239 // do change the path
241 m_strName
= strEntry
.AfterLast(wxCONFIG_PATH_SEPARATOR
);
242 m_strOldPath
= m_pContainer
->GetPath();
243 if ( m_strOldPath
.Len() == 0 ||
244 m_strOldPath
.Last() != wxCONFIG_PATH_SEPARATOR
)
245 m_strOldPath
+= wxCONFIG_PATH_SEPARATOR
;
246 m_pContainer
->SetPath(strPath
);
249 // it's a name only, without path - nothing to do
251 m_strName
= strEntry
;
255 wxConfigPathChanger::~wxConfigPathChanger()
257 // only restore path if it was changed
259 m_pContainer
->SetPath(m_strOldPath
);
263 #endif // wxUSE_CONFIG
265 // ----------------------------------------------------------------------------
266 // static & global functions
267 // ----------------------------------------------------------------------------
269 // understands both Unix and Windows (but only under Windows) environment
270 // variables expansion: i.e. $var, $(var) and ${var} are always understood
271 // and in addition under Windows %var% is also.
272 wxString
wxExpandEnvVars(const wxString
& str
)
275 strResult
.Alloc(str
.Len());
277 // don't change the values the enum elements: they must be equal
278 // to the matching [closing] delimiter.
282 Bracket_Normal
= ')',
285 Bracket_Windows
= '%', // yeah, Windows people are a bit strange ;-)
291 for ( size_t n
= 0; n
< str
.Len(); n
++ ) {
300 if ( str
[n
] == wxT('%') )
301 bracket
= Bracket_Windows
;
304 if ( n
== str
.Len() - 1 ) {
305 bracket
= Bracket_None
;
308 switch ( str
[n
+ 1] ) {
310 bracket
= Bracket_Normal
;
311 n
++; // skip the bracket
315 bracket
= Bracket_Curly
;
316 n
++; // skip the bracket
320 bracket
= Bracket_None
;
326 while ( m
< str
.Len() && (wxIsalnum(str
[m
]) || str
[m
] == wxT('_')) )
329 wxString
strVarName(str
.c_str() + n
+ 1, m
- n
- 1);
332 const wxChar
*pszValue
= NULL
;
334 const wxChar
*pszValue
= wxGetenv(strVarName
);
336 if ( pszValue
!= NULL
) {
337 strResult
+= pszValue
;
340 // variable doesn't exist => don't change anything
342 if ( bracket
!= Bracket_Windows
)
344 if ( bracket
!= Bracket_None
)
345 strResult
<< str
[n
- 1];
346 strResult
<< str
[n
] << strVarName
;
349 // check the closing bracket
350 if ( bracket
!= Bracket_None
) {
351 if ( m
== str
.Len() || str
[m
] != (wxChar
)bracket
) {
352 // under MSW it's common to have '%' characters in the registry
353 // and it's annoying to have warnings about them each time, so
354 // ignroe them silently if they are not used for env vars
356 // under Unix, OTOH, this warning could be useful for the user to
357 // understand why isn't the variable expanded as intended
359 wxLogWarning(_("Environment variables expansion failed: missing '%c' at position %d in '%s'."),
360 (char)bracket
, m
+ 1, str
.c_str());
364 // skip closing bracket unless the variables wasn't expanded
365 if ( pszValue
== NULL
)
366 strResult
<< (char)bracket
;
371 n
= m
- 1; // skip variable name
376 // backslash can be used to suppress special meaning of % and $
377 if ( n
!= str
.Len() && (str
[n
+ 1] == wxT('%') || str
[n
+ 1] == wxT('$')) ) {
378 strResult
+= str
[++n
];
392 // this function is used to properly interpret '..' in path
393 void wxSplitPath(wxArrayString
& aParts
, const wxChar
*sz
)
398 const wxChar
*pc
= sz
;
400 if ( *pc
== wxT('\0') || *pc
== wxCONFIG_PATH_SEPARATOR
) {
401 if ( strCurrent
== wxT(".") ) {
404 else if ( strCurrent
== wxT("..") ) {
406 if ( aParts
.size() == 0 )
407 wxLogWarning(_("'%s' has extra '..', ignored."), sz
);
409 aParts
.erase(aParts
.end() - 1);
413 else if ( !strCurrent
.IsEmpty() ) {
414 aParts
.push_back(strCurrent
);
418 // could log an error here, but we prefer to ignore extra '/'
420 if ( *pc
== wxT('\0') )