]>
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"
22 #include <wx/confbase.h>
31 #include <wx/string.h>
38 #include <wx/textfile.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
& WXUNUSED(localFilename
), const wxString
& WXUNUSED(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
137 if (Read(key
, & str
))
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
182 bool wxConfigBase::Read(const wxString
& key
, int *pi
) const
185 bool ret
= Read(key
, &l
);
191 bool wxConfigBase::Read(const wxString
& key
, int *pi
, int defVal
) const
194 bool ret
= Read(key
, &l
, (long) defVal
);
199 bool wxConfigBase::Write(const wxString
& key
, double val
)
202 str
.Printf("%f", val
);
203 return Write(key
, str
);
206 bool wxConfigBase::Write(const wxString
& key
, bool value
)
208 long l
= (value
? 1 : 0);
209 return Write(key
, l
);
212 wxString
wxConfigBase::ExpandEnvVars(const wxString
& str
) const
214 wxString tmp
; // Required for BC++
215 if (IsExpandingEnvVars())
216 tmp
= wxExpandEnvVars(str
);
222 // ----------------------------------------------------------------------------
223 // wxConfigPathChanger
224 // ----------------------------------------------------------------------------
226 wxConfigPathChanger::wxConfigPathChanger(const wxConfigBase
*pContainer
,
227 const wxString
& strEntry
)
229 m_pContainer
= (wxConfigBase
*)pContainer
;
230 wxString strPath
= strEntry
.Before(wxCONFIG_PATH_SEPARATOR
);
232 // special case of "/keyname" when there is nothing before "/"
233 if ( strPath
.IsEmpty() && ((!strEntry
.IsEmpty()) && strEntry
[0] == wxCONFIG_PATH_SEPARATOR
))
234 strPath
= wxCONFIG_PATH_SEPARATOR
;
236 if ( !strPath
.IsEmpty() ) {
237 // do change the path
239 m_strName
= strEntry
.Right(wxCONFIG_PATH_SEPARATOR
);
240 m_strOldPath
= m_pContainer
->GetPath();
241 m_strOldPath
+= wxCONFIG_PATH_SEPARATOR
;
242 m_pContainer
->SetPath(strPath
);
245 // it's a name only, without path - nothing to do
247 m_strName
= strEntry
;
251 wxConfigPathChanger::~wxConfigPathChanger()
253 // only restore path if it was changed
255 m_pContainer
->SetPath(m_strOldPath
);
259 // ----------------------------------------------------------------------------
260 // static & global functions
261 // ----------------------------------------------------------------------------
263 // understands both Unix and Windows (but only under Windows) environment
264 // variables expansion: i.e. $var, $(var) and ${var} are always understood
265 // and in addition under Windows %var% is also.
266 wxString
wxExpandEnvVars(const wxString
& str
)
269 strResult
.Alloc(str
.Len());
271 // don't change the values the enum elements: they must be equal
272 // to the matching [closing] delimiter.
276 Bracket_Normal
= ')',
279 Bracket_Windows
= '%' // yeah, Windows people are a bit strange ;-)
284 for ( size_t n
= 0; n
< str
.Len(); n
++ ) {
294 bracket
= Bracket_Windows
;
297 if ( n
== str
.Len() - 1 ) {
298 bracket
= Bracket_None
;
301 switch ( str
[n
+ 1] ) {
303 bracket
= Bracket_Normal
;
304 n
++; // skip the bracket
308 bracket
= Bracket_Curly
;
309 n
++; // skip the bracket
313 bracket
= Bracket_None
;
319 while ( m
< str
.Len() && (isalnum(str
[m
]) || str
[m
] == '_') )
322 wxString
strVarName(str
.c_str() + n
+ 1, m
- n
- 1);
324 const char *pszValue
= getenv(strVarName
);
325 if ( pszValue
!= NULL
) {
326 strResult
+= pszValue
;
329 // variable doesn't exist => don't change anything
331 if ( bracket
!= Bracket_Windows
)
333 if ( bracket
!= Bracket_None
)
334 strResult
<< str
[n
- 1];
335 strResult
<< str
[n
] << strVarName
;
338 // check the closing bracket
339 if ( bracket
!= Bracket_None
) {
340 if ( m
== str
.Len() || str
[m
] != (char)bracket
) {
341 wxLogWarning(_("Environment variables expansion failed: "
342 "missing '%c' at position %d in '%s'."),
343 (char)bracket
, m
+ 1, str
.c_str());
346 // skip closing bracket unless the variables wasn't expanded
347 if ( pszValue
== NULL
)
348 strResult
<< (char)bracket
;
353 n
= m
- 1; // skip variable name
358 // backslash can be used to suppress special meaning of % and $
359 if ( n
!= str
.Len() && (str
[n
+ 1] == '%' || str
[n
+ 1] == '$') ) {
360 strResult
+= str
[++n
];
374 // this function is used to properly interpret '..' in path
375 void wxSplitPath(wxArrayString
& aParts
, const char *sz
)
382 if ( *pc
== '\0' || *pc
== wxCONFIG_PATH_SEPARATOR
) {
383 if ( strCurrent
== "." ) {
386 else if ( strCurrent
== ".." ) {
388 if ( aParts
.IsEmpty() )
389 wxLogWarning(_("'%s' has extra '..', ignored."), sz
);
391 aParts
.Remove(aParts
.Count() - 1);
395 else if ( !strCurrent
.IsEmpty() ) {
396 aParts
.Add(strCurrent
);
400 // could log an error here, but we prefer to ignore extra '/'