]>
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 #include <wx/confbase.h>
37 #include <wx/textfile.h>
40 // we must include (one of) these files for wxConfigBase::Create
41 #if defined(__WXMSW__) && defined(wxCONFIG_WIN32_NATIVE)
43 #include <wx/msw/regconf.h>
45 #include <wx/msw/iniconf.h>
47 #else // either we're under Unix or wish to use files even under Windows
48 #include <wx/fileconf.h>
53 #include <ctype.h> // for isalnum()
55 // ----------------------------------------------------------------------------
56 // global and class static variables
57 // ----------------------------------------------------------------------------
59 wxConfigBase
*wxConfigBase::ms_pConfig
= NULL
;
60 bool wxConfigBase::ms_bAutoCreate
= TRUE
;
62 // ============================================================================
64 // ============================================================================
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 // Not all args will always be used by derived classes, but
71 // including them all in each class ensures compatibility.
72 wxConfigBase::wxConfigBase(const wxString
& appName
, const wxString
& vendorName
,
73 const wxString
& WXUNUSED(localFilename
), const wxString
& WXUNUSED(globalFilename
), long style
):
74 m_appName(appName
), m_vendorName(vendorName
), m_style(style
)
76 m_bExpandEnvVars
= TRUE
; m_bRecordDefaults
= FALSE
;
79 wxConfigBase
*wxConfigBase::Set(wxConfigBase
*pConfig
)
81 wxConfigBase
*pOld
= ms_pConfig
;
86 wxConfigBase
*wxConfigBase::Create()
88 if ( ms_bAutoCreate
&& ms_pConfig
== NULL
) {
90 #if defined(__WXMSW__) && defined(wxCONFIG_WIN32_NATIVE)
92 new wxRegConfig(wxTheApp
->GetAppName(), wxTheApp
->GetVendorName());
94 new wxIniConfig(wxTheApp
->GetAppName(), wxTheApp
->GetVendorName());
96 #else // either we're under Unix or wish to use files even under Windows
97 new wxFileConfig(wxTheApp
->GetAppName());
104 wxString
wxConfigBase::Read(const wxString
& key
, const wxString
& defVal
) const
107 Read(key
, &s
, defVal
);
111 bool wxConfigBase::Read(const wxString
& key
, wxString
*str
, const wxString
& defVal
) const
115 *str
= ExpandEnvVars(defVal
);
122 bool wxConfigBase::Read(const wxString
& key
, long *pl
, long defVal
) const
133 bool wxConfigBase::Read(const wxString
& key
, double* val
) const
136 if (Read(key
, & str
))
145 bool wxConfigBase::Read(const wxString
& key
, double* val
, double defVal
) const
156 bool wxConfigBase::Read(const wxString
& key
, bool* val
) const
168 bool wxConfigBase::Read(const wxString
& key
, bool* val
, bool defVal
) const
179 // Convenience functions
181 bool wxConfigBase::Read(const wxString
& key
, int *pi
) const
184 bool ret
= Read(key
, &l
);
190 bool wxConfigBase::Read(const wxString
& key
, int *pi
, int defVal
) const
193 bool ret
= Read(key
, &l
, (long) defVal
);
198 bool wxConfigBase::Write(const wxString
& key
, double val
)
201 str
.Printf("%f", val
);
202 return Write(key
, str
);
205 bool wxConfigBase::Write(const wxString
& key
, bool value
)
207 long l
= (value
? 1 : 0);
208 return Write(key
, l
);
211 wxString
wxConfigBase::ExpandEnvVars(const wxString
& str
) const
213 wxString tmp
; // Required for BC++
214 if (IsExpandingEnvVars())
215 tmp
= wxExpandEnvVars(str
);
221 // ----------------------------------------------------------------------------
222 // wxConfigPathChanger
223 // ----------------------------------------------------------------------------
225 wxConfigPathChanger::wxConfigPathChanger(const wxConfigBase
*pContainer
,
226 const wxString
& strEntry
)
228 m_pContainer
= (wxConfigBase
*)pContainer
;
230 // the path is everything which precedes the last slash
231 wxString strPath
= strEntry
.BeforeLast(wxCONFIG_PATH_SEPARATOR
);
233 // except in the special case of "/keyname" when there is nothing before "/"
234 if ( strPath
.IsEmpty() &&
235 ((!strEntry
.IsEmpty()) && strEntry
[0] == wxCONFIG_PATH_SEPARATOR
) )
237 strPath
= wxCONFIG_PATH_SEPARATOR
;
240 if ( !strPath
.IsEmpty() ) {
241 // do change the path
243 m_strName
= strEntry
.AfterLast(wxCONFIG_PATH_SEPARATOR
);
244 m_strOldPath
= m_pContainer
->GetPath();
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 // ----------------------------------------------------------------------------
264 // static & global functions
265 // ----------------------------------------------------------------------------
267 // understands both Unix and Windows (but only under Windows) environment
268 // variables expansion: i.e. $var, $(var) and ${var} are always understood
269 // and in addition under Windows %var% is also.
270 wxString
wxExpandEnvVars(const wxString
& str
)
273 strResult
.Alloc(str
.Len());
275 // don't change the values the enum elements: they must be equal
276 // to the matching [closing] delimiter.
280 Bracket_Normal
= ')',
283 ,Bracket_Windows
= '%' // yeah, Windows people are a bit strange ;-)
288 for ( size_t n
= 0; n
< str
.Len(); n
++ ) {
298 bracket
= Bracket_Windows
;
301 if ( n
== str
.Len() - 1 ) {
302 bracket
= Bracket_None
;
305 switch ( str
[n
+ 1] ) {
307 bracket
= Bracket_Normal
;
308 n
++; // skip the bracket
312 bracket
= Bracket_Curly
;
313 n
++; // skip the bracket
317 bracket
= Bracket_None
;
323 while ( m
< str
.Len() && (isalnum(str
[m
]) || str
[m
] == '_') )
326 wxString
strVarName(str
.c_str() + n
+ 1, m
- n
- 1);
328 const char *pszValue
= getenv(strVarName
);
329 if ( pszValue
!= NULL
) {
330 strResult
+= pszValue
;
333 // variable doesn't exist => don't change anything
335 if ( bracket
!= Bracket_Windows
)
337 if ( bracket
!= Bracket_None
)
338 strResult
<< str
[n
- 1];
339 strResult
<< str
[n
] << strVarName
;
342 // check the closing bracket
343 if ( bracket
!= Bracket_None
) {
344 if ( m
== str
.Len() || str
[m
] != (char)bracket
) {
345 wxLogWarning(_("Environment variables expansion failed: "
346 "missing '%c' at position %d in '%s'."),
347 (char)bracket
, m
+ 1, str
.c_str());
350 // skip closing bracket unless the variables wasn't expanded
351 if ( pszValue
== NULL
)
352 strResult
<< (char)bracket
;
357 n
= m
- 1; // skip variable name
362 // backslash can be used to suppress special meaning of % and $
363 if ( n
!= str
.Len() && (str
[n
+ 1] == '%' || str
[n
+ 1] == '$') ) {
364 strResult
+= str
[++n
];
378 // this function is used to properly interpret '..' in path
379 void wxSplitPath(wxArrayString
& aParts
, const char *sz
)
386 if ( *pc
== '\0' || *pc
== wxCONFIG_PATH_SEPARATOR
) {
387 if ( strCurrent
== "." ) {
390 else if ( strCurrent
== ".." ) {
392 if ( aParts
.IsEmpty() )
393 wxLogWarning(_("'%s' has extra '..', ignored."), sz
);
395 aParts
.Remove(aParts
.Count() - 1);
399 else if ( !strCurrent
.IsEmpty() ) {
400 aParts
.Add(strCurrent
);
404 // could log an error here, but we prefer to ignore extra '/'