]>
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 licence
11 ///////////////////////////////////////////////////////////////////////////////
13 // ----------------------------------------------------------------------------
15 // ----------------------------------------------------------------------------
17 #include "wx/wxprec.h"
23 #ifndef wxUSE_CONFIG_NATIVE
24 #define wxUSE_CONFIG_NATIVE 1
27 #include "wx/config.h"
30 #include "wx/arrstr.h"
32 #if wxUSE_CONFIG && ((wxUSE_FILE && wxUSE_TEXTFILE) || wxUSE_CONFIG_NATIVE)
41 #include <limits.h> // for INT_MAX
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 including them all
59 // in each class ensures compatibility.
60 wxConfigBase::wxConfigBase(const wxString
& appName
,
61 const wxString
& vendorName
,
62 const wxString
& WXUNUSED(localFilename
),
63 const wxString
& WXUNUSED(globalFilename
),
65 : m_appName(appName
), m_vendorName(vendorName
), m_style(style
)
67 m_bExpandEnvVars
= true;
68 m_bRecordDefaults
= false;
71 wxConfigBase::~wxConfigBase()
73 // required here for Darwin
76 wxConfigBase
*wxConfigBase::Set(wxConfigBase
*pConfig
)
78 wxConfigBase
*pOld
= ms_pConfig
;
83 wxConfigBase
*wxConfigBase::Create()
85 if ( ms_bAutoCreate
&& ms_pConfig
== NULL
) {
87 #if defined(__WXMSW__) && wxUSE_CONFIG_NATIVE
88 new wxRegConfig(wxTheApp
->GetAppName(), wxTheApp
->GetVendorName());
89 #elif defined(__WXPALMOS__) && wxUSE_CONFIG_NATIVE
90 new wxPrefConfig(wxTheApp
->GetAppName());
91 #else // either we're under Unix or wish to use files even under Windows
92 new wxFileConfig(wxTheApp
->GetAppName());
99 // ----------------------------------------------------------------------------
100 // wxConfigBase reading entries
101 // ----------------------------------------------------------------------------
103 // implement both Read() overloads for the given type in terms of DoRead()
104 #define IMPLEMENT_READ_FOR_TYPE(name, type, deftype, extra) \
105 bool wxConfigBase::Read(const wxString& key, type *val) const \
107 wxCHECK_MSG( val, false, _T("wxConfig::Read(): NULL parameter") ); \
109 if ( !DoRead##name(key, val) ) \
112 *val = extra(*val); \
117 bool wxConfigBase::Read(const wxString& key, \
119 deftype defVal) const \
121 wxCHECK_MSG( val, false, _T("wxConfig::Read(): NULL parameter") ); \
123 bool read = DoRead##name(key, val); \
126 if ( IsRecordingDefaults() ) \
128 ((wxConfigBase *)this)->DoWrite##name(key, defVal); \
134 *val = extra(*val); \
140 IMPLEMENT_READ_FOR_TYPE(String
, wxString
, const wxString
&, ExpandEnvVars
)
141 IMPLEMENT_READ_FOR_TYPE(Long
, long, long, long)
142 IMPLEMENT_READ_FOR_TYPE(Int
, int, int, int)
143 IMPLEMENT_READ_FOR_TYPE(Double
, double, double, double)
144 IMPLEMENT_READ_FOR_TYPE(Bool
, bool, bool, bool)
146 #undef IMPLEMENT_READ_FOR_TYPE
148 // the DoReadXXX() for the other types have implementation in the base class
149 // but can be overridden in the derived ones
150 bool wxConfigBase::DoReadInt(const wxString
& key
, int *pi
) const
152 wxCHECK_MSG( pi
, false, _T("wxConfig::Read(): NULL parameter") );
155 if ( !DoReadLong(key
, &l
) )
158 wxASSERT_MSG( l
< INT_MAX
, _T("overflow in wxConfig::DoReadInt") );
165 bool wxConfigBase::DoReadBool(const wxString
& key
, bool* val
) const
167 wxCHECK_MSG( val
, false, _T("wxConfig::Read(): NULL parameter") );
170 if ( !DoReadLong(key
, &l
) )
173 wxASSERT_MSG( l
== 0 || l
== 1, _T("bad bool value in wxConfig::DoReadInt") );
180 bool wxConfigBase::DoReadDouble(const wxString
& key
, double* val
) const
183 if ( Read(key
, &str
) )
185 return str
.ToDouble(val
);
191 // string reading helper
192 wxString
wxConfigBase::ExpandEnvVars(const wxString
& str
) const
194 wxString tmp
; // Required for BC++
195 if (IsExpandingEnvVars())
196 tmp
= wxExpandEnvVars(str
);
202 // ----------------------------------------------------------------------------
203 // wxConfigBase writing
204 // ----------------------------------------------------------------------------
206 bool wxConfigBase::DoWriteDouble(const wxString
& key
, double val
)
208 return DoWriteString(key
, wxString::Format(_T("%g"), val
));
211 bool wxConfigBase::DoWriteInt(const wxString
& key
, int value
)
213 return DoWriteLong(key
, (long)value
);
216 bool wxConfigBase::DoWriteBool(const wxString
& key
, bool value
)
218 return DoWriteLong(key
, value
? 1l : 0l);
221 // ----------------------------------------------------------------------------
222 // wxConfigPathChanger
223 // ----------------------------------------------------------------------------
225 wxConfigPathChanger::wxConfigPathChanger(const wxConfigBase
*pContainer
,
226 const wxString
& strEntry
)
229 m_pContainer
= (wxConfigBase
*)pContainer
;
231 // the path is everything which precedes the last slash
232 wxString strPath
= strEntry
.BeforeLast(wxCONFIG_PATH_SEPARATOR
);
234 // except in the special case of "/keyname" when there is nothing before "/"
235 if ( strPath
.empty() &&
236 ((!strEntry
.empty()) && strEntry
[0] == wxCONFIG_PATH_SEPARATOR
) )
238 strPath
= wxCONFIG_PATH_SEPARATOR
;
241 if ( !strPath
.empty() )
243 if ( m_pContainer
->GetPath() != strPath
)
245 // we do change the path so restore it later
248 /* JACS: work around a memory bug that causes an assert
249 when using wxRegConfig, related to reference-counting.
250 Can be reproduced by removing (const wxChar*) below and
251 adding the following code to the config sample OnInit under
254 pConfig->SetPath(wxT("MySettings"));
255 pConfig->SetPath(wxT(".."));
257 pConfig->Read(_T("MainWindowX"), & value);
259 m_strOldPath
= (const wxChar
*) m_pContainer
->GetPath();
260 if ( *m_strOldPath
.c_str() != wxCONFIG_PATH_SEPARATOR
)
261 m_strOldPath
+= wxCONFIG_PATH_SEPARATOR
;
262 m_pContainer
->SetPath(strPath
);
265 // in any case, use the just the name, not full path
266 m_strName
= strEntry
.AfterLast(wxCONFIG_PATH_SEPARATOR
);
269 // it's a name only, without path - nothing to do
270 m_strName
= strEntry
;
274 wxConfigPathChanger::~wxConfigPathChanger()
276 // only restore path if it was changed
278 m_pContainer
->SetPath(m_strOldPath
);
282 #endif // wxUSE_CONFIG
284 // ----------------------------------------------------------------------------
285 // static & global functions
286 // ----------------------------------------------------------------------------
288 // understands both Unix and Windows (but only under Windows) environment
289 // variables expansion: i.e. $var, $(var) and ${var} are always understood
290 // and in addition under Windows %var% is also.
292 // don't change the values the enum elements: they must be equal
293 // to the matching [closing] delimiter.
297 Bracket_Normal
= ')',
300 Bracket_Windows
= '%', // yeah, Windows people are a bit strange ;-)
305 wxString
wxExpandEnvVars(const wxString
& str
)
308 strResult
.Alloc(str
.Len());
311 for ( size_t n
= 0; n
< str
.Len(); n
++ ) {
320 if ( str
[n
] == wxT('%') )
321 bracket
= Bracket_Windows
;
324 if ( n
== str
.Len() - 1 ) {
325 bracket
= Bracket_None
;
328 switch ( str
[n
+ 1] ) {
330 bracket
= Bracket_Normal
;
331 n
++; // skip the bracket
335 bracket
= Bracket_Curly
;
336 n
++; // skip the bracket
340 bracket
= Bracket_None
;
346 while ( m
< str
.Len() && (wxIsalnum(str
[m
]) || str
[m
] == wxT('_')) )
349 wxString
strVarName(str
.c_str() + n
+ 1, m
- n
- 1);
352 const wxChar
*pszValue
= NULL
;
354 const wxChar
*pszValue
= wxGetenv(strVarName
);
356 if ( pszValue
!= NULL
) {
357 strResult
+= pszValue
;
360 // variable doesn't exist => don't change anything
362 if ( bracket
!= Bracket_Windows
)
364 if ( bracket
!= Bracket_None
)
365 strResult
<< str
[n
- 1];
366 strResult
<< str
[n
] << strVarName
;
369 // check the closing bracket
370 if ( bracket
!= Bracket_None
) {
371 if ( m
== str
.Len() || str
[m
] != (wxChar
)bracket
) {
372 // under MSW it's common to have '%' characters in the registry
373 // and it's annoying to have warnings about them each time, so
374 // ignroe them silently if they are not used for env vars
376 // under Unix, OTOH, this warning could be useful for the user to
377 // understand why isn't the variable expanded as intended
379 wxLogWarning(_("Environment variables expansion failed: missing '%c' at position %u in '%s'."),
380 (char)bracket
, (unsigned int) (m
+ 1), str
.c_str());
384 // skip closing bracket unless the variables wasn't expanded
385 if ( pszValue
== NULL
)
386 strResult
<< (char)bracket
;
391 n
= m
- 1; // skip variable name
396 // backslash can be used to suppress special meaning of % and $
397 if ( n
!= str
.Len() - 1 &&
398 (str
[n
+ 1] == wxT('%') || str
[n
+ 1] == wxT('$')) ) {
399 strResult
+= str
[++n
];
413 // this function is used to properly interpret '..' in path
414 void wxSplitPath(wxArrayString
& aParts
, const wxChar
*sz
)
419 const wxChar
*pc
= sz
;
421 if ( *pc
== wxT('\0') || *pc
== wxCONFIG_PATH_SEPARATOR
) {
422 if ( strCurrent
== wxT(".") ) {
425 else if ( strCurrent
== wxT("..") ) {
427 if ( aParts
.size() == 0 )
428 wxLogWarning(_("'%s' has extra '..', ignored."), sz
);
430 aParts
.erase(aParts
.end() - 1);
434 else if ( !strCurrent
.empty() ) {
435 aParts
.push_back(strCurrent
);
439 // could log an error here, but we prefer to ignore extra '/'
441 if ( *pc
== wxT('\0') )