]>
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"
35 #include "wx/textfile.h"
38 #include "wx/config.h"
42 #include <ctype.h> // for isalnum()
44 // ----------------------------------------------------------------------------
45 // global and class static variables
46 // ----------------------------------------------------------------------------
48 wxConfigBase
*wxConfigBase::ms_pConfig
= NULL
;
49 bool wxConfigBase::ms_bAutoCreate
= TRUE
;
51 // ============================================================================
53 // ============================================================================
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 // Not all args will always be used by derived classes, but
60 // including them all in each class ensures compatibility.
61 wxConfigBase::wxConfigBase(const wxString
& appName
, const wxString
& vendorName
,
62 const wxString
& WXUNUSED(localFilename
), const wxString
& WXUNUSED(globalFilename
), long style
):
63 m_appName(appName
), m_vendorName(vendorName
), m_style(style
)
65 m_bExpandEnvVars
= TRUE
; m_bRecordDefaults
= FALSE
;
68 wxConfigBase
*wxConfigBase::Set(wxConfigBase
*pConfig
)
70 wxConfigBase
*pOld
= ms_pConfig
;
75 wxConfigBase
*wxConfigBase::Create()
77 if ( ms_bAutoCreate
&& ms_pConfig
== NULL
) {
79 #if defined(__WXMSW__) && defined(wxCONFIG_WIN32_NATIVE)
81 new wxRegConfig(wxTheApp
->GetAppName(), wxTheApp
->GetVendorName());
83 new wxIniConfig(wxTheApp
->GetAppName(), wxTheApp
->GetVendorName());
85 #else // either we're under Unix or wish to use files even under Windows
86 new wxFileConfig(wxTheApp
->GetAppName());
93 wxString
wxConfigBase::Read(const wxString
& key
, const wxString
& defVal
) const
96 Read(key
, &s
, defVal
);
100 bool wxConfigBase::Read(const wxString
& key
, wxString
*str
, const wxString
& defVal
) const
104 *str
= ExpandEnvVars(defVal
);
111 bool wxConfigBase::Read(const wxString
& key
, long *pl
, long defVal
) const
122 bool wxConfigBase::Read(const wxString
& key
, double* val
) const
125 if (Read(key
, & str
))
134 bool wxConfigBase::Read(const wxString
& key
, double* val
, double defVal
) const
145 bool wxConfigBase::Read(const wxString
& key
, bool* val
) const
157 bool wxConfigBase::Read(const wxString
& key
, bool* val
, bool defVal
) const
168 // Convenience functions
170 bool wxConfigBase::Read(const wxString
& key
, int *pi
) const
173 bool ret
= Read(key
, &l
);
179 bool wxConfigBase::Read(const wxString
& key
, int *pi
, int defVal
) const
182 bool ret
= Read(key
, &l
, (long) defVal
);
187 bool wxConfigBase::Write(const wxString
& key
, double val
)
190 str
.Printf(_T("%f"), val
);
191 return Write(key
, str
);
194 bool wxConfigBase::Write(const wxString
& key
, bool value
)
196 long l
= (value
? 1 : 0);
197 return Write(key
, l
);
200 wxString
wxConfigBase::ExpandEnvVars(const wxString
& str
) const
202 wxString tmp
; // Required for BC++
203 if (IsExpandingEnvVars())
204 tmp
= wxExpandEnvVars(str
);
210 // ----------------------------------------------------------------------------
211 // wxConfigPathChanger
212 // ----------------------------------------------------------------------------
214 wxConfigPathChanger::wxConfigPathChanger(const wxConfigBase
*pContainer
,
215 const wxString
& strEntry
)
217 m_pContainer
= (wxConfigBase
*)pContainer
;
219 // the path is everything which precedes the last slash
220 wxString strPath
= strEntry
.BeforeLast(wxCONFIG_PATH_SEPARATOR
);
222 // except in the special case of "/keyname" when there is nothing before "/"
223 if ( strPath
.IsEmpty() &&
224 ((!strEntry
.IsEmpty()) && strEntry
[0] == wxCONFIG_PATH_SEPARATOR
) )
226 strPath
= wxCONFIG_PATH_SEPARATOR
;
229 if ( !strPath
.IsEmpty() ) {
230 // do change the path
232 m_strName
= strEntry
.AfterLast(wxCONFIG_PATH_SEPARATOR
);
233 m_strOldPath
= m_pContainer
->GetPath();
234 m_strOldPath
+= wxCONFIG_PATH_SEPARATOR
;
235 m_pContainer
->SetPath(strPath
);
238 // it's a name only, without path - nothing to do
240 m_strName
= strEntry
;
244 wxConfigPathChanger::~wxConfigPathChanger()
246 // only restore path if it was changed
248 m_pContainer
->SetPath(m_strOldPath
);
252 // ----------------------------------------------------------------------------
253 // static & global functions
254 // ----------------------------------------------------------------------------
256 // understands both Unix and Windows (but only under Windows) environment
257 // variables expansion: i.e. $var, $(var) and ${var} are always understood
258 // and in addition under Windows %var% is also.
259 wxString
wxExpandEnvVars(const wxString
& str
)
262 strResult
.Alloc(str
.Len());
264 // don't change the values the enum elements: they must be equal
265 // to the matching [closing] delimiter.
269 Bracket_Normal
= ')',
272 ,Bracket_Windows
= '%' // yeah, Windows people are a bit strange ;-)
277 for ( size_t n
= 0; n
< str
.Len(); n
++ ) {
287 bracket
= Bracket_Windows
;
290 if ( n
== str
.Len() - 1 ) {
291 bracket
= Bracket_None
;
294 switch ( str
[n
+ 1] ) {
296 bracket
= Bracket_Normal
;
297 n
++; // skip the bracket
301 bracket
= Bracket_Curly
;
302 n
++; // skip the bracket
306 bracket
= Bracket_None
;
312 while ( m
< str
.Len() && (isalnum(str
[m
]) || str
[m
] == '_') )
315 wxString
strVarName(str
.c_str() + n
+ 1, m
- n
- 1);
317 const wxChar
*pszValue
= wxGetenv(strVarName
);
318 if ( pszValue
!= NULL
) {
319 strResult
+= pszValue
;
322 // variable doesn't exist => don't change anything
324 if ( bracket
!= Bracket_Windows
)
326 if ( bracket
!= Bracket_None
)
327 strResult
<< str
[n
- 1];
328 strResult
<< str
[n
] << strVarName
;
331 // check the closing bracket
332 if ( bracket
!= Bracket_None
) {
333 if ( m
== str
.Len() || str
[m
] != (char)bracket
) {
334 wxLogWarning(_("Environment variables expansion failed: "
335 "missing '%c' at position %d in '%s'."),
336 (char)bracket
, m
+ 1, str
.c_str());
339 // skip closing bracket unless the variables wasn't expanded
340 if ( pszValue
== NULL
)
341 strResult
<< (char)bracket
;
346 n
= m
- 1; // skip variable name
351 // backslash can be used to suppress special meaning of % and $
352 if ( n
!= str
.Len() && (str
[n
+ 1] == '%' || str
[n
+ 1] == '$') ) {
353 strResult
+= str
[++n
];
367 // this function is used to properly interpret '..' in path
368 void wxSplitPath(wxArrayString
& aParts
, const char *sz
)
375 if ( *pc
== '\0' || *pc
== wxCONFIG_PATH_SEPARATOR
) {
376 if ( strCurrent
== "." ) {
379 else if ( strCurrent
== ".." ) {
381 if ( aParts
.IsEmpty() )
382 wxLogWarning(_("'%s' has extra '..', ignored."), sz
);
384 aParts
.Remove(aParts
.Count() - 1);
388 else if ( !strCurrent
.IsEmpty() ) {
389 aParts
.Add(strCurrent
);
393 // could log an error here, but we prefer to ignore extra '/'
405 #endif // wxUSE_CONFIG