]>
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 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
21 #pragma implementation "confbase.h"
24 #include "wx/wxprec.h"
31 #include <wx/string.h>
38 #include <wx/textfile.h>
39 #include <wx/confbase.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>
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 wxConfigBase
*wxConfigBase::Set(wxConfigBase
*pConfig
)
72 wxConfigBase
*pOld
= ms_pConfig
;
77 wxConfigBase
*wxConfigBase::Create()
79 if ( ms_bAutoCreate
&& ms_pConfig
== NULL
) {
81 #if defined(__WXMSW__) && defined(wxCONFIG_WIN32_NATIVE)
83 new wxRegConfig(wxTheApp
->GetVendorName() + '\\'
84 + wxTheApp
->GetAppName());
86 new wxIniConfig(wxTheApp
->GetAppName(), wxTheApp
->GetVendorName());
88 #else // either we're under Unix or wish to use files even under Windows
89 new wxFileConfig(wxTheApp
->GetAppName());
96 const char *wxConfigBase::Read(const char *szKey
, const char *szDefault
) const
98 static char s_szBuf
[1024];
100 Read(&s
, szKey
, szDefault
);
101 strncpy(s_szBuf
, s
, WXSIZEOF(s_szBuf
));
106 // ----------------------------------------------------------------------------
107 // Config::PathChanger
108 // ----------------------------------------------------------------------------
110 wxConfigBase::PathChanger::PathChanger(const wxConfigBase
*pContainer
,
111 const wxString
& strEntry
)
113 m_pContainer
= (wxConfigBase
*)pContainer
;
114 wxString strPath
= strEntry
.Before(wxCONFIG_PATH_SEPARATOR
);
116 // special case of "/keyname" when there is nothing before "/"
117 if ( strPath
.IsEmpty() && ((!strEntry
.IsEmpty()) && strEntry
[0] == wxCONFIG_PATH_SEPARATOR
))
118 strPath
= wxCONFIG_PATH_SEPARATOR
;
120 if ( !strPath
.IsEmpty() ) {
121 // do change the path
123 m_strName
= strEntry
.Right(wxCONFIG_PATH_SEPARATOR
);
124 m_strOldPath
= m_pContainer
->GetPath();
125 m_strOldPath
+= wxCONFIG_PATH_SEPARATOR
;
126 m_pContainer
->SetPath(strPath
);
129 // it's a name only, without path - nothing to do
131 m_strName
= strEntry
;
135 wxConfigBase::PathChanger::~PathChanger()
137 // only restore path if it was changed
139 m_pContainer
->SetPath(m_strOldPath
);
143 // ----------------------------------------------------------------------------
144 // static & global functions
145 // ----------------------------------------------------------------------------
147 // understands both Unix and Windows (but only under Windows) environment
148 // variables expansion: i.e. $var, $(var) and ${var} are always understood
149 // and in addition under Windows %var% is also.
150 wxString
wxExpandEnvVars(const wxString
& str
)
153 strResult
.Alloc(str
.Len());
155 // don't change the values the enum elements: they must be equal
156 // to the matching [closing] delimiter.
160 Bracket_Normal
= ')',
163 Bracket_Windows
= '%' // yeah, Windows people are a bit strange ;-)
168 for ( size_t n
= 0; n
< str
.Len(); n
++ ) {
178 bracket
= Bracket_Windows
;
181 if ( n
== str
.Len() - 1 ) {
182 bracket
= Bracket_None
;
185 switch ( str
[n
+ 1] ) {
187 bracket
= Bracket_Normal
;
188 n
++; // skip the bracket
192 bracket
= Bracket_Curly
;
193 n
++; // skip the bracket
197 bracket
= Bracket_None
;
203 while ( m
< str
.Len() && (isalnum(str
[m
]) || str
[m
] == '_') )
206 wxString
strVarName(str
.c_str() + n
+ 1, m
- n
- 1);
208 const char *pszValue
= getenv(strVarName
);
209 if ( pszValue
!= NULL
) {
210 strResult
+= pszValue
;
213 // variable doesn't exist => don't change anything
215 if ( bracket
!= Bracket_Windows
)
217 if ( bracket
!= Bracket_None
)
218 strResult
<< str
[n
- 1];
219 strResult
<< str
[n
] << strVarName
;
222 // check the closing bracket
223 if ( bracket
!= Bracket_None
) {
224 if ( m
== str
.Len() || str
[m
] != (char)bracket
) {
225 wxLogWarning(_("Environment variables expansion failed: "
226 "missing '%c' at position %d in '%s'."),
227 (char)bracket
, m
+ 1, str
.c_str());
230 // skip closing bracket unless the variables wasn't expanded
231 if ( pszValue
== NULL
)
232 strResult
<< (char)bracket
;
237 n
= m
- 1; // skip variable name
242 // backslash can be used to suppress special meaning of % and $
243 if ( n
!= str
.Len() && (str
[n
+ 1] == '%' || str
[n
+ 1] == '$') ) {
244 strResult
+= str
[++n
];
258 // this function is used to properly interpret '..' in path
259 void wxSplitPath(wxArrayString
& aParts
, const char *sz
)
266 if ( *pc
== '\0' || *pc
== wxCONFIG_PATH_SEPARATOR
) {
267 if ( strCurrent
== "." ) {
270 else if ( strCurrent
== ".." ) {
272 if ( aParts
.IsEmpty() )
273 wxLogWarning(_("'%s' has extra '..', ignored."), sz
);
275 aParts
.Remove(aParts
.Count() - 1);
279 else if ( !strCurrent
.IsEmpty() ) {
280 aParts
.Add(strCurrent
);
284 // could log an error here, but we prefer to ignore extra '/'