]>
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 #include "wx/config.h"
32 #if wxUSE_CONFIG && ((wxUSE_FILE && wxUSE_TEXTFILE) || wxUSE_CONFIG_NATIVE)
37 #include "wx/textfile.h"
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 including them all
63 // in each class ensures compatibility.
64 wxConfigBase::wxConfigBase(const wxString
& appName
,
65 const wxString
& vendorName
,
66 const wxString
& WXUNUSED(localFilename
),
67 const wxString
& WXUNUSED(globalFilename
),
69 : m_appName(appName
), m_vendorName(vendorName
), m_style(style
)
71 m_bExpandEnvVars
= TRUE
; m_bRecordDefaults
= FALSE
;
74 wxConfigBase::~wxConfigBase()
78 wxConfigBase
*wxConfigBase::Set(wxConfigBase
*pConfig
)
80 wxConfigBase
*pOld
= ms_pConfig
;
85 wxConfigBase
*wxConfigBase::Create()
87 if ( ms_bAutoCreate
&& ms_pConfig
== NULL
) {
89 #if defined(__WXMSW__) && wxUSE_CONFIG_NATIVE
91 new wxRegConfig(wxTheApp
->GetAppName(), wxTheApp
->GetVendorName());
93 new wxIniConfig(wxTheApp
->GetAppName(), wxTheApp
->GetVendorName());
95 #else // either we're under Unix or wish to use files even under Windows
96 new wxFileConfig(wxTheApp
->GetAppName());
103 wxString
wxConfigBase::Read(const wxString
& key
, const wxString
& defVal
) const
106 Read(key
, &s
, defVal
);
110 bool wxConfigBase::Read(const wxString
& key
, wxString
*str
, const wxString
& defVal
) const
114 *str
= ExpandEnvVars(defVal
);
121 bool wxConfigBase::Read(const wxString
& key
, long *pl
, long defVal
) const
132 bool wxConfigBase::Read(const wxString
& key
, double* val
) const
135 if ( Read(key
, &str
) )
137 return str
.ToDouble(val
);
143 bool wxConfigBase::Read(const wxString
& key
, double* val
, double defVal
) const
154 bool wxConfigBase::Read(const wxString
& key
, bool* val
) const
166 bool wxConfigBase::Read(const wxString
& key
, bool* val
, bool defVal
) const
177 // Convenience functions
179 bool wxConfigBase::Read(const wxString
& key
, int *pi
) const
182 bool ret
= Read(key
, &l
);
188 bool wxConfigBase::Read(const wxString
& key
, int *pi
, int defVal
) const
191 bool ret
= Read(key
, &l
, (long) defVal
);
197 bool wxConfigBase::Write(const wxString
& key
, double val
)
200 str
.Printf(wxT("%g"), val
);
201 return Write(key
, str
);
204 bool wxConfigBase::Write(const wxString
& key
, bool value
)
206 return Write(key
, value
? 1l : 0l);
209 bool wxConfigBase::Write(const wxString
& key
, const wxChar
*value
)
211 // explicit cast needed, otherwise value would have been converted to bool
212 return Write(key
, wxString(value
));
215 wxString
wxConfigBase::ExpandEnvVars(const wxString
& str
) const
217 wxString tmp
; // Required for BC++
218 if (IsExpandingEnvVars())
219 tmp
= wxExpandEnvVars(str
);
225 // ----------------------------------------------------------------------------
226 // wxConfigPathChanger
227 // ----------------------------------------------------------------------------
229 wxConfigPathChanger::wxConfigPathChanger(const wxConfigBase
*pContainer
,
230 const wxString
& strEntry
)
232 m_pContainer
= (wxConfigBase
*)pContainer
;
234 // the path is everything which precedes the last slash
235 wxString strPath
= strEntry
.BeforeLast(wxCONFIG_PATH_SEPARATOR
);
237 // except in the special case of "/keyname" when there is nothing before "/"
238 if ( strPath
.IsEmpty() &&
239 ((!strEntry
.IsEmpty()) && strEntry
[0] == wxCONFIG_PATH_SEPARATOR
) )
241 strPath
= wxCONFIG_PATH_SEPARATOR
;
244 if ( !strPath
.IsEmpty() ) {
245 // do change the path
247 m_strName
= strEntry
.AfterLast(wxCONFIG_PATH_SEPARATOR
);
248 m_strOldPath
= m_pContainer
->GetPath();
249 if ( m_strOldPath
.Len() == 0 ||
250 m_strOldPath
.Last() != wxCONFIG_PATH_SEPARATOR
)
251 m_strOldPath
+= wxCONFIG_PATH_SEPARATOR
;
252 m_pContainer
->SetPath(strPath
);
255 // it's a name only, without path - nothing to do
257 m_strName
= strEntry
;
261 wxConfigPathChanger::~wxConfigPathChanger()
263 // only restore path if it was changed
265 m_pContainer
->SetPath(m_strOldPath
);
269 #endif // wxUSE_CONFIG
271 // ----------------------------------------------------------------------------
272 // static & global functions
273 // ----------------------------------------------------------------------------
275 // understands both Unix and Windows (but only under Windows) environment
276 // variables expansion: i.e. $var, $(var) and ${var} are always understood
277 // and in addition under Windows %var% is also.
278 wxString
wxExpandEnvVars(const wxString
& str
)
281 strResult
.Alloc(str
.Len());
283 // don't change the values the enum elements: they must be equal
284 // to the matching [closing] delimiter.
288 Bracket_Normal
= ')',
291 ,Bracket_Windows
= '%' // yeah, Windows people are a bit strange ;-)
296 for ( size_t n
= 0; n
< str
.Len(); n
++ ) {
305 if ( str
[n
] == wxT('%') )
306 bracket
= Bracket_Windows
;
309 if ( n
== str
.Len() - 1 ) {
310 bracket
= Bracket_None
;
313 switch ( str
[n
+ 1] ) {
315 bracket
= Bracket_Normal
;
316 n
++; // skip the bracket
320 bracket
= Bracket_Curly
;
321 n
++; // skip the bracket
325 bracket
= Bracket_None
;
331 while ( m
< str
.Len() && (wxIsalnum(str
[m
]) || str
[m
] == wxT('_')) )
334 wxString
strVarName(str
.c_str() + n
+ 1, m
- n
- 1);
336 const wxChar
*pszValue
= wxGetenv(strVarName
);
337 if ( pszValue
!= NULL
) {
338 strResult
+= pszValue
;
341 // variable doesn't exist => don't change anything
343 if ( bracket
!= Bracket_Windows
)
345 if ( bracket
!= Bracket_None
)
346 strResult
<< str
[n
- 1];
347 strResult
<< str
[n
] << strVarName
;
350 // check the closing bracket
351 if ( bracket
!= Bracket_None
) {
352 if ( m
== str
.Len() || str
[m
] != (char)bracket
) {
353 wxLogWarning(_("Environment variables expansion failed: missing '%c' at position %d in '%s'."),
354 (char)bracket
, m
+ 1, str
.c_str());
357 // skip closing bracket unless the variables wasn't expanded
358 if ( pszValue
== NULL
)
359 strResult
<< (char)bracket
;
364 n
= m
- 1; // skip variable name
369 // backslash can be used to suppress special meaning of % and $
370 if ( n
!= str
.Len() && (str
[n
+ 1] == wxT('%') || str
[n
+ 1] == wxT('$')) ) {
371 strResult
+= str
[++n
];
385 // this function is used to properly interpret '..' in path
386 /// separates group and entry names (probably shouldn't be changed)
388 void wxSplitPath(wxArrayString
& aParts
, const wxChar
*sz
)
393 const wxChar
*pc
= sz
;
395 if ( *pc
== wxT('\0') || *pc
== wxCONFIG_PATH_SEPARATOR
) {
396 if ( strCurrent
== wxT(".") ) {
399 else if ( strCurrent
== wxT("..") ) {
401 if ( aParts
.IsEmpty() )
402 wxLogWarning(_("'%s' has extra '..', ignored."), sz
);
404 aParts
.Remove(aParts
.Count() - 1);
408 else if ( !strCurrent
.IsEmpty() ) {
409 aParts
.Add(strCurrent
);
413 // could log an error here, but we prefer to ignore extra '/'
415 if ( *pc
== wxT('\0') )