]>
git.saurik.com Git - wxWidgets.git/blob - src/common/config.cpp
286e2123aa6844bc58ee27d52af6feb2b74ed210
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"
34 #if wxUSE_CONFIG && ((wxUSE_FILE && wxUSE_TEXTFILE) || wxUSE_CONFIG_NATIVE)
38 #include "wx/textfile.h"
45 #include <limits.h> // for INT_MAX
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 // ----------------------------------------------------------------------------
104 // wxConfigBase reading entries
105 // ----------------------------------------------------------------------------
107 // implement both Read() overloads for the given type in terms of DoRead()
108 #define IMPLEMENT_READ_FOR_TYPE(name, type, deftype, extra) \
109 bool wxConfigBase::Read(const wxString& key, type *val) const \
111 wxCHECK_MSG( val, FALSE, _T("wxConfig::Read(): NULL parameter") ); \
113 return DoRead##name(key, val); \
116 bool wxConfigBase::Read(const wxString& key, \
118 deftype defVal) const \
120 wxCHECK_MSG( val, FALSE, _T("wxConfig::Read(): NULL parameter") ); \
122 if ( DoRead##name(key, val) ) \
125 if ( IsRecordingDefaults() ) \
127 ((wxConfigBase *)this)->DoWrite##name(key, defVal); \
130 *val = extra(defVal); \
136 IMPLEMENT_READ_FOR_TYPE(String
, wxString
, const wxString
&, ExpandEnvVars
)
137 IMPLEMENT_READ_FOR_TYPE(Long
, long, long, long)
138 IMPLEMENT_READ_FOR_TYPE(Int
, int, int, int)
139 IMPLEMENT_READ_FOR_TYPE(Double
, double, double, double)
140 IMPLEMENT_READ_FOR_TYPE(Bool
, bool, bool, bool)
142 #undef IMPLEMENT_READ_FOR_TYPE
144 // the DoReadXXX() for the other types have implementation in the base class
145 // but can be overridden in the derived ones
146 bool wxConfigBase::DoReadInt(const wxString
& key
, int *pi
) const
148 wxCHECK_MSG( pi
, FALSE
, _T("wxConfig::Read(): NULL parameter") );
151 if ( !DoReadLong(key
, &l
) )
154 wxASSERT_MSG( l
< INT_MAX
, _T("overflow in wxConfig::DoReadInt") );
161 bool wxConfigBase::DoReadBool(const wxString
& key
, bool* val
) const
163 wxCHECK_MSG( val
, FALSE
, _T("wxConfig::Read(): NULL parameter") );
166 if ( !DoReadLong(key
, &l
) )
169 wxASSERT_MSG( l
== 0 || l
== 1, _T("bad bool value in wxConfig::DoReadInt") );
176 bool wxConfigBase::DoReadDouble(const wxString
& key
, double* val
) const
179 if ( Read(key
, &str
) )
181 return str
.ToDouble(val
);
187 // string reading helper
188 wxString
wxConfigBase::ExpandEnvVars(const wxString
& str
) const
190 wxString tmp
; // Required for BC++
191 if (IsExpandingEnvVars())
192 tmp
= wxExpandEnvVars(str
);
198 // ----------------------------------------------------------------------------
199 // wxConfigBase writing
200 // ----------------------------------------------------------------------------
202 bool wxConfigBase::DoWriteDouble(const wxString
& key
, double val
)
204 return DoWriteString(key
, wxString::Format(_T("%g"), val
));
207 bool wxConfigBase::DoWriteInt(const wxString
& key
, int value
)
209 return DoWriteLong(key
, (long)value
);
212 bool wxConfigBase::DoWriteBool(const wxString
& key
, bool value
)
214 return DoWriteLong(key
, value
? 1l : 0l);
217 // ----------------------------------------------------------------------------
218 // wxConfigPathChanger
219 // ----------------------------------------------------------------------------
221 wxConfigPathChanger::wxConfigPathChanger(const wxConfigBase
*pContainer
,
222 const wxString
& strEntry
)
224 m_pContainer
= (wxConfigBase
*)pContainer
;
226 // the path is everything which precedes the last slash
227 wxString strPath
= strEntry
.BeforeLast(wxCONFIG_PATH_SEPARATOR
);
229 // except in the special case of "/keyname" when there is nothing before "/"
230 if ( strPath
.IsEmpty() &&
231 ((!strEntry
.IsEmpty()) && strEntry
[0] == wxCONFIG_PATH_SEPARATOR
) )
233 strPath
= wxCONFIG_PATH_SEPARATOR
;
236 if ( !strPath
.IsEmpty() ) {
237 // do change the path
239 m_strName
= strEntry
.AfterLast(wxCONFIG_PATH_SEPARATOR
);
240 m_strOldPath
= m_pContainer
->GetPath();
241 if ( m_strOldPath
.Len() == 0 ||
242 m_strOldPath
.Last() != wxCONFIG_PATH_SEPARATOR
)
243 m_strOldPath
+= wxCONFIG_PATH_SEPARATOR
;
244 m_pContainer
->SetPath(strPath
);
247 // it's a name only, without path - nothing to do
249 m_strName
= strEntry
;
253 wxConfigPathChanger::~wxConfigPathChanger()
255 // only restore path if it was changed
257 m_pContainer
->SetPath(m_strOldPath
);
261 #endif // wxUSE_CONFIG
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
++ ) {
297 if ( str
[n
] == wxT('%') )
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() && (wxIsalnum(str
[m
]) || str
[m
] == wxT('_')) )
326 wxString
strVarName(str
.c_str() + n
+ 1, m
- n
- 1);
328 const wxChar
*pszValue
= wxGetenv(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: missing '%c' at position %d in '%s'."),
346 (char)bracket
, m
+ 1, str
.c_str());
349 // skip closing bracket unless the variables wasn't expanded
350 if ( pszValue
== NULL
)
351 strResult
<< (char)bracket
;
356 n
= m
- 1; // skip variable name
361 // backslash can be used to suppress special meaning of % and $
362 if ( n
!= str
.Len() && (str
[n
+ 1] == wxT('%') || str
[n
+ 1] == wxT('$')) ) {
363 strResult
+= str
[++n
];
377 // this function is used to properly interpret '..' in path
378 /// separates group and entry names (probably shouldn't be changed)
380 void wxSplitPath(wxArrayString
& aParts
, const wxChar
*sz
)
385 const wxChar
*pc
= sz
;
387 if ( *pc
== wxT('\0') || *pc
== wxCONFIG_PATH_SEPARATOR
) {
388 if ( strCurrent
== wxT(".") ) {
391 else if ( strCurrent
== wxT("..") ) {
393 if ( aParts
.IsEmpty() )
394 wxLogWarning(_("'%s' has extra '..', ignored."), sz
);
396 aParts
.Remove(aParts
.Count() - 1);
400 else if ( !strCurrent
.IsEmpty() ) {
401 aParts
.Add(strCurrent
);
405 // could log an error here, but we prefer to ignore extra '/'
407 if ( *pc
== wxT('\0') )