]>
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 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
21 #pragma implementation "confbase.h"
24 #include "wx/wxprec.h"
31 #include <wx/string.h>
38 #include <wx/textfile.h>
39 #include <wx/confbase.h>
41 // we must include (one of) these files for wxConfigBase::Create
42 #if defined(__WXMSW__) && defined(wxCONFIG_WIN32_NATIVE)
44 #include <wx/msw/regconf.h>
46 #include <wx/msw/iniconf.h>
48 #else // either we're under Unix or wish to use files even under Windows
49 #include <wx/fileconf.h>
54 #include <ctype.h> // for isalnum()
56 // ----------------------------------------------------------------------------
57 // global and class static variables
58 // ----------------------------------------------------------------------------
60 wxConfigBase
*wxConfigBase::ms_pConfig
= NULL
;
61 bool wxConfigBase::ms_bAutoCreate
= TRUE
;
63 // ============================================================================
65 // ============================================================================
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 // Not all args will always be used by derived classes, but
72 // including them all in each class ensures compatibility.
73 wxConfigBase::wxConfigBase(const wxString
& appName
, const wxString
& vendorName
,
74 const wxString
& localFilename
, const wxString
& globalFilename
, long style
):
75 m_appName(appName
), m_vendorName(vendorName
), m_style(style
)
77 m_bExpandEnvVars
= TRUE
; m_bRecordDefaults
= FALSE
;
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__) && defined(wxCONFIG_WIN32_NATIVE)
93 new wxRegConfig(wxTheApp
->GetAppName(), wxTheApp
->GetVendorName());
95 new wxIniConfig(wxTheApp
->GetAppName(), wxTheApp
->GetVendorName());
97 #else // either we're under Unix or wish to use files even under Windows
98 new wxFileConfig(wxTheApp
->GetAppName());
105 wxString
wxConfigBase::Read(const wxString
& key
, const wxString
& defVal
) const
108 Read(key
, &s
, defVal
);
112 bool wxConfigBase::Read(const wxString
& key
, wxString
*str
, const wxString
& defVal
) const
116 *str
= ExpandEnvVars(defVal
);
123 bool wxConfigBase::Read(const wxString
& key
, long *pl
, long defVal
) const
134 bool wxConfigBase::Read(const wxString
& key
, double* val
) const
146 bool wxConfigBase::Read(const wxString
& key
, double* val
, double defVal
) const
157 bool wxConfigBase::Read(const wxString
& key
, bool* val
) const
169 bool wxConfigBase::Read(const wxString
& key
, bool* val
, bool defVal
) const
180 // Convenience functions
181 bool wxConfigBase::Write(const wxString
& key
, double val
)
184 str
.Printf("%f", val
);
185 return Write(key
, str
);
188 bool wxConfigBase::Write(const wxString
& key
, bool value
)
190 long l
= (value
? 1 : 0);
191 return Write(key
, l
);
195 // ----------------------------------------------------------------------------
196 // wxConfigPathChanger
197 // ----------------------------------------------------------------------------
199 wxConfigPathChanger::wxConfigPathChanger(const wxConfigBase
*pContainer
,
200 const wxString
& strEntry
)
202 m_pContainer
= (wxConfigBase
*)pContainer
;
203 wxString strPath
= strEntry
.Before(wxCONFIG_PATH_SEPARATOR
);
205 // special case of "/keyname" when there is nothing before "/"
206 if ( strPath
.IsEmpty() && ((!strEntry
.IsEmpty()) && strEntry
[0] == wxCONFIG_PATH_SEPARATOR
))
207 strPath
= wxCONFIG_PATH_SEPARATOR
;
209 if ( !strPath
.IsEmpty() ) {
210 // do change the path
212 m_strName
= strEntry
.Right(wxCONFIG_PATH_SEPARATOR
);
213 m_strOldPath
= m_pContainer
->GetPath();
214 m_strOldPath
+= wxCONFIG_PATH_SEPARATOR
;
215 m_pContainer
->SetPath(strPath
);
218 // it's a name only, without path - nothing to do
220 m_strName
= strEntry
;
224 wxConfigPathChanger::~wxConfigPathChanger()
226 // only restore path if it was changed
228 m_pContainer
->SetPath(m_strOldPath
);
232 // ----------------------------------------------------------------------------
233 // static & global functions
234 // ----------------------------------------------------------------------------
236 // understands both Unix and Windows (but only under Windows) environment
237 // variables expansion: i.e. $var, $(var) and ${var} are always understood
238 // and in addition under Windows %var% is also.
239 wxString
wxExpandEnvVars(const wxString
& str
)
242 strResult
.Alloc(str
.Len());
244 // don't change the values the enum elements: they must be equal
245 // to the matching [closing] delimiter.
249 Bracket_Normal
= ')',
252 Bracket_Windows
= '%' // yeah, Windows people are a bit strange ;-)
257 for ( size_t n
= 0; n
< str
.Len(); n
++ ) {
267 bracket
= Bracket_Windows
;
270 if ( n
== str
.Len() - 1 ) {
271 bracket
= Bracket_None
;
274 switch ( str
[n
+ 1] ) {
276 bracket
= Bracket_Normal
;
277 n
++; // skip the bracket
281 bracket
= Bracket_Curly
;
282 n
++; // skip the bracket
286 bracket
= Bracket_None
;
292 while ( m
< str
.Len() && (isalnum(str
[m
]) || str
[m
] == '_') )
295 wxString
strVarName(str
.c_str() + n
+ 1, m
- n
- 1);
297 const char *pszValue
= getenv(strVarName
);
298 if ( pszValue
!= NULL
) {
299 strResult
+= pszValue
;
302 // variable doesn't exist => don't change anything
304 if ( bracket
!= Bracket_Windows
)
306 if ( bracket
!= Bracket_None
)
307 strResult
<< str
[n
- 1];
308 strResult
<< str
[n
] << strVarName
;
311 // check the closing bracket
312 if ( bracket
!= Bracket_None
) {
313 if ( m
== str
.Len() || str
[m
] != (char)bracket
) {
314 wxLogWarning(_("Environment variables expansion failed: "
315 "missing '%c' at position %d in '%s'."),
316 (char)bracket
, m
+ 1, str
.c_str());
319 // skip closing bracket unless the variables wasn't expanded
320 if ( pszValue
== NULL
)
321 strResult
<< (char)bracket
;
326 n
= m
- 1; // skip variable name
331 // backslash can be used to suppress special meaning of % and $
332 if ( n
!= str
.Len() && (str
[n
+ 1] == '%' || str
[n
+ 1] == '$') ) {
333 strResult
+= str
[++n
];
347 // this function is used to properly interpret '..' in path
348 void wxSplitPath(wxArrayString
& aParts
, const char *sz
)
355 if ( *pc
== '\0' || *pc
== wxCONFIG_PATH_SEPARATOR
) {
356 if ( strCurrent
== "." ) {
359 else if ( strCurrent
== ".." ) {
361 if ( aParts
.IsEmpty() )
362 wxLogWarning(_("'%s' has extra '..', ignored."), sz
);
364 aParts
.Remove(aParts
.Count() - 1);
368 else if ( !strCurrent
.IsEmpty() ) {
369 aParts
.Add(strCurrent
);
373 // could log an error here, but we prefer to ignore extra '/'