]>
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"
26 #ifndef wxUSE_CONFIG_NATIVE
27 #define wxUSE_CONFIG_NATIVE 1
30 #if wxUSE_CONFIG && ((wxUSE_FILE && wxUSE_TEXTFILE) || wxUSE_CONFIG_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__) && wxUSE_CONFIG_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(wxT("%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 bool wxConfigBase::Write( const wxString
&key
, const wxChar
*text
)
205 wxString
str( text
) ;
206 return Write( key
, str
) ;
208 wxString
wxConfigBase::ExpandEnvVars(const wxString
& str
) const
210 wxString tmp
; // Required for BC++
211 if (IsExpandingEnvVars())
212 tmp
= wxExpandEnvVars(str
);
218 // ----------------------------------------------------------------------------
219 // wxConfigPathChanger
220 // ----------------------------------------------------------------------------
222 wxConfigPathChanger::wxConfigPathChanger(const wxConfigBase
*pContainer
,
223 const wxString
& strEntry
)
225 m_pContainer
= (wxConfigBase
*)pContainer
;
227 // the path is everything which precedes the last slash
228 wxString strPath
= strEntry
.BeforeLast(wxCONFIG_PATH_SEPARATOR
);
230 // except in the special case of "/keyname" when there is nothing before "/"
231 if ( strPath
.IsEmpty() &&
232 ((!strEntry
.IsEmpty()) && strEntry
[0] == wxCONFIG_PATH_SEPARATOR
) )
234 strPath
= wxCONFIG_PATH_SEPARATOR
;
237 if ( !strPath
.IsEmpty() ) {
238 // do change the path
240 m_strName
= strEntry
.AfterLast(wxCONFIG_PATH_SEPARATOR
);
241 m_strOldPath
= m_pContainer
->GetPath();
242 m_strOldPath
+= wxCONFIG_PATH_SEPARATOR
;
243 m_pContainer
->SetPath(strPath
);
246 // it's a name only, without path - nothing to do
248 m_strName
= strEntry
;
252 wxConfigPathChanger::~wxConfigPathChanger()
254 // only restore path if it was changed
256 m_pContainer
->SetPath(m_strOldPath
);
260 // ----------------------------------------------------------------------------
261 // static & global functions
262 // ----------------------------------------------------------------------------
264 // understands both Unix and Windows (but only under Windows) environment
265 // variables expansion: i.e. $var, $(var) and ${var} are always understood
266 // and in addition under Windows %var% is also.
267 wxString
wxExpandEnvVars(const wxString
& str
)
270 strResult
.Alloc(str
.Len());
272 // don't change the values the enum elements: they must be equal
273 // to the matching [closing] delimiter.
277 Bracket_Normal
= ')',
280 ,Bracket_Windows
= '%' // yeah, Windows people are a bit strange ;-)
285 for ( size_t n
= 0; n
< str
.Len(); n
++ ) {
295 bracket
= Bracket_Windows
;
298 if ( n
== str
.Len() - 1 ) {
299 bracket
= Bracket_None
;
302 switch ( str
[n
+ 1] ) {
304 bracket
= Bracket_Normal
;
305 n
++; // skip the bracket
309 bracket
= Bracket_Curly
;
310 n
++; // skip the bracket
314 bracket
= Bracket_None
;
320 while ( m
< str
.Len() && (isalnum(str
[m
]) || str
[m
] == '_') )
323 wxString
strVarName(str
.c_str() + n
+ 1, m
- n
- 1);
325 const wxChar
*pszValue
= wxGetenv(strVarName
);
326 if ( pszValue
!= NULL
) {
327 strResult
+= pszValue
;
330 // variable doesn't exist => don't change anything
332 if ( bracket
!= Bracket_Windows
)
334 if ( bracket
!= Bracket_None
)
335 strResult
<< str
[n
- 1];
336 strResult
<< str
[n
] << strVarName
;
339 // check the closing bracket
340 if ( bracket
!= Bracket_None
) {
341 if ( m
== str
.Len() || str
[m
] != (char)bracket
) {
342 wxLogWarning(_("Environment variables expansion failed: "
343 "missing '%c' at position %d in '%s'."),
344 (char)bracket
, m
+ 1, str
.c_str());
347 // skip closing bracket unless the variables wasn't expanded
348 if ( pszValue
== NULL
)
349 strResult
<< (char)bracket
;
354 n
= m
- 1; // skip variable name
359 // backslash can be used to suppress special meaning of % and $
360 if ( n
!= str
.Len() && (str
[n
+ 1] == '%' || str
[n
+ 1] == '$') ) {
361 strResult
+= str
[++n
];
375 // this function is used to properly interpret '..' in path
376 void wxSplitPath(wxArrayString
& aParts
, const wxChar
*sz
)
381 const wxChar
*pc
= sz
;
383 if ( *pc
== wxT('\0') || *pc
== wxCONFIG_PATH_SEPARATOR
) {
384 if ( strCurrent
== wxT(".") ) {
387 else if ( strCurrent
== wxT("..") ) {
389 if ( aParts
.IsEmpty() )
390 wxLogWarning(_("'%s' has extra '..', ignored."), sz
);
392 aParts
.Remove(aParts
.Count() - 1);
396 else if ( !strCurrent
.IsEmpty() ) {
397 aParts
.Add(strCurrent
);
401 // could log an error here, but we prefer to ignore extra '/'
403 if ( *pc
== wxT('\0') )
413 #endif // wxUSE_CONFIG