]>
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 license
11 ///////////////////////////////////////////////////////////////////////////////
13 // ----------------------------------------------------------------------------
15 // ----------------------------------------------------------------------------
17 #pragma implementation "confbase.h"
20 #include "wx/wxprec.h"
30 #if wxUSE_CONFIG && ((wxUSE_FILE && wxUSE_TEXTFILE) || defined(wxCONFIG_WIN32_NATIVE))
35 #include "wx/textfile.h"
41 #include "wx/config.h"
45 #include <ctype.h> // for isalnum()
47 // ----------------------------------------------------------------------------
48 // global and class static variables
49 // ----------------------------------------------------------------------------
51 wxConfigBase
*wxConfigBase::ms_pConfig
= NULL
;
52 bool wxConfigBase::ms_bAutoCreate
= TRUE
;
54 // ============================================================================
56 // ============================================================================
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 // Not all args will always be used by derived classes, but
63 // including them all in each class ensures compatibility.
64 wxConfigBase::wxConfigBase(const wxString
& appName
, const wxString
& vendorName
,
65 const wxString
& WXUNUSED(localFilename
), const wxString
& WXUNUSED(globalFilename
), long style
):
66 m_appName(appName
), m_vendorName(vendorName
), m_style(style
)
68 m_bExpandEnvVars
= TRUE
; m_bRecordDefaults
= FALSE
;
71 wxConfigBase
*wxConfigBase::Set(wxConfigBase
*pConfig
)
73 wxConfigBase
*pOld
= ms_pConfig
;
78 wxConfigBase
*wxConfigBase::Create()
80 if ( ms_bAutoCreate
&& ms_pConfig
== NULL
) {
82 #if defined(__WXMSW__) && defined(wxCONFIG_WIN32_NATIVE)
84 new wxRegConfig(wxTheApp
->GetAppName(), wxTheApp
->GetVendorName());
86 new wxIniConfig(wxTheApp
->GetAppName(), wxTheApp
->GetVendorName());
88 #else // either we're under Unix or wish to use files even under Windows
89 new wxFileConfig(wxTheApp
->GetAppName());
96 wxString
wxConfigBase::Read(const wxString
& key
, const wxString
& defVal
) const
99 Read(key
, &s
, defVal
);
103 bool wxConfigBase::Read(const wxString
& key
, wxString
*str
, const wxString
& defVal
) const
107 *str
= ExpandEnvVars(defVal
);
114 bool wxConfigBase::Read(const wxString
& key
, long *pl
, long defVal
) const
125 bool wxConfigBase::Read(const wxString
& key
, double* val
) const
128 if (Read(key
, & str
))
137 bool wxConfigBase::Read(const wxString
& key
, double* val
, double defVal
) const
148 bool wxConfigBase::Read(const wxString
& key
, bool* val
) const
160 bool wxConfigBase::Read(const wxString
& key
, bool* val
, bool defVal
) const
171 // Convenience functions
173 bool wxConfigBase::Read(const wxString
& key
, int *pi
) const
176 bool ret
= Read(key
, &l
);
182 bool wxConfigBase::Read(const wxString
& key
, int *pi
, int defVal
) const
185 bool ret
= Read(key
, &l
, (long) defVal
);
190 bool wxConfigBase::Write(const wxString
& key
, double val
)
193 str
.Printf(_T("%f"), val
);
194 return Write(key
, str
);
197 bool wxConfigBase::Write(const wxString
& key
, bool value
)
199 long l
= (value
? 1 : 0);
200 return Write(key
, l
);
203 wxString
wxConfigBase::ExpandEnvVars(const wxString
& str
) const
205 wxString tmp
; // Required for BC++
206 if (IsExpandingEnvVars())
207 tmp
= wxExpandEnvVars(str
);
213 // ----------------------------------------------------------------------------
214 // wxConfigPathChanger
215 // ----------------------------------------------------------------------------
217 wxConfigPathChanger::wxConfigPathChanger(const wxConfigBase
*pContainer
,
218 const wxString
& strEntry
)
220 m_pContainer
= (wxConfigBase
*)pContainer
;
222 // the path is everything which precedes the last slash
223 wxString strPath
= strEntry
.BeforeLast(wxCONFIG_PATH_SEPARATOR
);
225 // except in the special case of "/keyname" when there is nothing before "/"
226 if ( strPath
.IsEmpty() &&
227 ((!strEntry
.IsEmpty()) && strEntry
[0] == wxCONFIG_PATH_SEPARATOR
) )
229 strPath
= wxCONFIG_PATH_SEPARATOR
;
232 if ( !strPath
.IsEmpty() ) {
233 // do change the path
235 m_strName
= strEntry
.AfterLast(wxCONFIG_PATH_SEPARATOR
);
236 m_strOldPath
= m_pContainer
->GetPath();
237 m_strOldPath
+= wxCONFIG_PATH_SEPARATOR
;
238 m_pContainer
->SetPath(strPath
);
241 // it's a name only, without path - nothing to do
243 m_strName
= strEntry
;
247 wxConfigPathChanger::~wxConfigPathChanger()
249 // only restore path if it was changed
251 m_pContainer
->SetPath(m_strOldPath
);
255 // ----------------------------------------------------------------------------
256 // static & global functions
257 // ----------------------------------------------------------------------------
259 // understands both Unix and Windows (but only under Windows) environment
260 // variables expansion: i.e. $var, $(var) and ${var} are always understood
261 // and in addition under Windows %var% is also.
262 wxString
wxExpandEnvVars(const wxString
& str
)
265 strResult
.Alloc(str
.Len());
267 // don't change the values the enum elements: they must be equal
268 // to the matching [closing] delimiter.
272 Bracket_Normal
= ')',
275 ,Bracket_Windows
= '%' // yeah, Windows people are a bit strange ;-)
280 for ( size_t n
= 0; n
< str
.Len(); n
++ ) {
290 bracket
= Bracket_Windows
;
293 if ( n
== str
.Len() - 1 ) {
294 bracket
= Bracket_None
;
297 switch ( str
[n
+ 1] ) {
299 bracket
= Bracket_Normal
;
300 n
++; // skip the bracket
304 bracket
= Bracket_Curly
;
305 n
++; // skip the bracket
309 bracket
= Bracket_None
;
315 while ( m
< str
.Len() && (isalnum(str
[m
]) || str
[m
] == '_') )
318 wxString
strVarName(str
.c_str() + n
+ 1, m
- n
- 1);
320 const wxChar
*pszValue
= wxGetenv(strVarName
);
321 if ( pszValue
!= NULL
) {
322 strResult
+= pszValue
;
325 // variable doesn't exist => don't change anything
327 if ( bracket
!= Bracket_Windows
)
329 if ( bracket
!= Bracket_None
)
330 strResult
<< str
[n
- 1];
331 strResult
<< str
[n
] << strVarName
;
334 // check the closing bracket
335 if ( bracket
!= Bracket_None
) {
336 if ( m
== str
.Len() || str
[m
] != (char)bracket
) {
337 wxLogWarning(_("Environment variables expansion failed: "
338 "missing '%c' at position %d in '%s'."),
339 (char)bracket
, m
+ 1, str
.c_str());
342 // skip closing bracket unless the variables wasn't expanded
343 if ( pszValue
== NULL
)
344 strResult
<< (char)bracket
;
349 n
= m
- 1; // skip variable name
354 // backslash can be used to suppress special meaning of % and $
355 if ( n
!= str
.Len() && (str
[n
+ 1] == '%' || str
[n
+ 1] == '$') ) {
356 strResult
+= str
[++n
];
370 // this function is used to properly interpret '..' in path
371 void wxSplitPath(wxArrayString
& aParts
, const wxChar
*sz
)
376 const wxChar
*pc
= sz
;
378 if ( *pc
== _T('\0') || *pc
== wxCONFIG_PATH_SEPARATOR
) {
379 if ( strCurrent
== _T(".") ) {
382 else if ( strCurrent
== _T("..") ) {
384 if ( aParts
.IsEmpty() )
385 wxLogWarning(_("'%s' has extra '..', ignored."), sz
);
387 aParts
.Remove(aParts
.Count() - 1);
391 else if ( !strCurrent
.IsEmpty() ) {
392 aParts
.Add(strCurrent
);
396 // could log an error here, but we prefer to ignore extra '/'
398 if ( *pc
== _T('\0') )
408 #endif // wxUSE_CONFIG