]>
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 #if wxUSE_CONFIG && ((wxUSE_FILE && wxUSE_TEXTFILE) || defined(wxCONFIG_WIN32_NATIVE))
31 #include "wx/textfile.h"
37 #include "wx/config.h"
41 #include <ctype.h> // for isalnum()
43 // ----------------------------------------------------------------------------
44 // global and class static variables
45 // ----------------------------------------------------------------------------
47 wxConfigBase
*wxConfigBase::ms_pConfig
= NULL
;
48 bool wxConfigBase::ms_bAutoCreate
= TRUE
;
50 // ============================================================================
52 // ============================================================================
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 // Not all args will always be used by derived classes, but
59 // including them all in each class ensures compatibility.
60 wxConfigBase::wxConfigBase(const wxString
& appName
, const wxString
& vendorName
,
61 const wxString
& WXUNUSED(localFilename
), const wxString
& WXUNUSED(globalFilename
), long style
):
62 m_appName(appName
), m_vendorName(vendorName
), m_style(style
)
64 m_bExpandEnvVars
= TRUE
; m_bRecordDefaults
= FALSE
;
67 wxConfigBase
*wxConfigBase::Set(wxConfigBase
*pConfig
)
69 wxConfigBase
*pOld
= ms_pConfig
;
74 wxConfigBase
*wxConfigBase::Create()
76 if ( ms_bAutoCreate
&& ms_pConfig
== NULL
) {
78 #if defined(__WXMSW__) && defined(wxCONFIG_WIN32_NATIVE)
80 new wxRegConfig(wxTheApp
->GetAppName(), wxTheApp
->GetVendorName());
82 new wxIniConfig(wxTheApp
->GetAppName(), wxTheApp
->GetVendorName());
84 #else // either we're under Unix or wish to use files even under Windows
85 new wxFileConfig(wxTheApp
->GetAppName());
92 wxString
wxConfigBase::Read(const wxString
& key
, const wxString
& defVal
) const
95 Read(key
, &s
, defVal
);
99 bool wxConfigBase::Read(const wxString
& key
, wxString
*str
, const wxString
& defVal
) const
103 *str
= ExpandEnvVars(defVal
);
110 bool wxConfigBase::Read(const wxString
& key
, long *pl
, long defVal
) const
121 bool wxConfigBase::Read(const wxString
& key
, double* val
) const
124 if (Read(key
, & str
))
133 bool wxConfigBase::Read(const wxString
& key
, double* val
, double defVal
) const
144 bool wxConfigBase::Read(const wxString
& key
, bool* val
) const
156 bool wxConfigBase::Read(const wxString
& key
, bool* val
, bool defVal
) const
167 // Convenience functions
169 bool wxConfigBase::Read(const wxString
& key
, int *pi
) const
172 bool ret
= Read(key
, &l
);
178 bool wxConfigBase::Read(const wxString
& key
, int *pi
, int defVal
) const
181 bool ret
= Read(key
, &l
, (long) defVal
);
186 bool wxConfigBase::Write(const wxString
& key
, double val
)
189 str
.Printf(wxT("%f"), val
);
190 return Write(key
, str
);
193 bool wxConfigBase::Write(const wxString
& key
, bool value
)
195 long l
= (value
? 1 : 0);
196 return Write(key
, l
);
199 wxString
wxConfigBase::ExpandEnvVars(const wxString
& str
) const
201 wxString tmp
; // Required for BC++
202 if (IsExpandingEnvVars())
203 tmp
= wxExpandEnvVars(str
);
209 // ----------------------------------------------------------------------------
210 // wxConfigPathChanger
211 // ----------------------------------------------------------------------------
213 wxConfigPathChanger::wxConfigPathChanger(const wxConfigBase
*pContainer
,
214 const wxString
& strEntry
)
216 m_pContainer
= (wxConfigBase
*)pContainer
;
218 // the path is everything which precedes the last slash
219 wxString strPath
= strEntry
.BeforeLast(wxCONFIG_PATH_SEPARATOR
);
221 // except in the special case of "/keyname" when there is nothing before "/"
222 if ( strPath
.IsEmpty() &&
223 ((!strEntry
.IsEmpty()) && strEntry
[0] == wxCONFIG_PATH_SEPARATOR
) )
225 strPath
= wxCONFIG_PATH_SEPARATOR
;
228 if ( !strPath
.IsEmpty() ) {
229 // do change the path
231 m_strName
= strEntry
.AfterLast(wxCONFIG_PATH_SEPARATOR
);
232 m_strOldPath
= m_pContainer
->GetPath();
233 m_strOldPath
+= wxCONFIG_PATH_SEPARATOR
;
234 m_pContainer
->SetPath(strPath
);
237 // it's a name only, without path - nothing to do
239 m_strName
= strEntry
;
243 wxConfigPathChanger::~wxConfigPathChanger()
245 // only restore path if it was changed
247 m_pContainer
->SetPath(m_strOldPath
);
251 // ----------------------------------------------------------------------------
252 // static & global functions
253 // ----------------------------------------------------------------------------
255 // understands both Unix and Windows (but only under Windows) environment
256 // variables expansion: i.e. $var, $(var) and ${var} are always understood
257 // and in addition under Windows %var% is also.
258 wxString
wxExpandEnvVars(const wxString
& str
)
261 strResult
.Alloc(str
.Len());
263 // don't change the values the enum elements: they must be equal
264 // to the matching [closing] delimiter.
268 Bracket_Normal
= ')',
271 ,Bracket_Windows
= '%' // yeah, Windows people are a bit strange ;-)
276 for ( size_t n
= 0; n
< str
.Len(); n
++ ) {
286 bracket
= Bracket_Windows
;
289 if ( n
== str
.Len() - 1 ) {
290 bracket
= Bracket_None
;
293 switch ( str
[n
+ 1] ) {
295 bracket
= Bracket_Normal
;
296 n
++; // skip the bracket
300 bracket
= Bracket_Curly
;
301 n
++; // skip the bracket
305 bracket
= Bracket_None
;
311 while ( m
< str
.Len() && (isalnum(str
[m
]) || str
[m
] == '_') )
314 wxString
strVarName(str
.c_str() + n
+ 1, m
- n
- 1);
316 const wxChar
*pszValue
= wxGetenv(strVarName
);
317 if ( pszValue
!= NULL
) {
318 strResult
+= pszValue
;
321 // variable doesn't exist => don't change anything
323 if ( bracket
!= Bracket_Windows
)
325 if ( bracket
!= Bracket_None
)
326 strResult
<< str
[n
- 1];
327 strResult
<< str
[n
] << strVarName
;
330 // check the closing bracket
331 if ( bracket
!= Bracket_None
) {
332 if ( m
== str
.Len() || str
[m
] != (char)bracket
) {
333 wxLogWarning(_("Environment variables expansion failed: "
334 "missing '%c' at position %d in '%s'."),
335 (char)bracket
, m
+ 1, str
.c_str());
338 // skip closing bracket unless the variables wasn't expanded
339 if ( pszValue
== NULL
)
340 strResult
<< (char)bracket
;
345 n
= m
- 1; // skip variable name
350 // backslash can be used to suppress special meaning of % and $
351 if ( n
!= str
.Len() && (str
[n
+ 1] == '%' || str
[n
+ 1] == '$') ) {
352 strResult
+= str
[++n
];
366 // this function is used to properly interpret '..' in path
367 void wxSplitPath(wxArrayString
& aParts
, const wxChar
*sz
)
372 const wxChar
*pc
= sz
;
374 if ( *pc
== wxT('\0') || *pc
== wxCONFIG_PATH_SEPARATOR
) {
375 if ( strCurrent
== wxT(".") ) {
378 else if ( strCurrent
== wxT("..") ) {
380 if ( aParts
.IsEmpty() )
381 wxLogWarning(_("'%s' has extra '..', ignored."), sz
);
383 aParts
.Remove(aParts
.Count() - 1);
387 else if ( !strCurrent
.IsEmpty() ) {
388 aParts
.Add(strCurrent
);
392 // could log an error here, but we prefer to ignore extra '/'
394 if ( *pc
== wxT('\0') )
404 #endif // wxUSE_CONFIG