]>
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 // ----------------------------------------------------------------------------
16 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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"
33 #include "wx/arrstr.h"
35 #if wxUSE_CONFIG && ((wxUSE_FILE && wxUSE_TEXTFILE) || wxUSE_CONFIG_NATIVE)
39 #include "wx/textfile.h"
46 #include <limits.h> // for INT_MAX
48 // ----------------------------------------------------------------------------
49 // global and class static variables
50 // ----------------------------------------------------------------------------
52 wxConfigBase
*wxConfigBase::ms_pConfig
= NULL
;
53 bool wxConfigBase::ms_bAutoCreate
= true;
55 // ============================================================================
57 // ============================================================================
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 // Not all args will always be used by derived classes, but including them all
64 // in each class ensures compatibility.
65 wxConfigBase::wxConfigBase(const wxString
& appName
,
66 const wxString
& vendorName
,
67 const wxString
& WXUNUSED(localFilename
),
68 const wxString
& WXUNUSED(globalFilename
),
70 : m_appName(appName
), m_vendorName(vendorName
), m_style(style
)
72 m_bExpandEnvVars
= true;
73 m_bRecordDefaults
= false;
76 wxConfigBase::~wxConfigBase()
78 // required here for Darwin
81 wxConfigBase
*wxConfigBase::Set(wxConfigBase
*pConfig
)
83 wxConfigBase
*pOld
= ms_pConfig
;
88 wxConfigBase
*wxConfigBase::Create()
90 if ( ms_bAutoCreate
&& ms_pConfig
== NULL
) {
92 #if defined(__WXMSW__) && wxUSE_CONFIG_NATIVE
93 new wxRegConfig(wxTheApp
->GetAppName(), wxTheApp
->GetVendorName());
94 #elif defined(__WXPALMOS__) && wxUSE_CONFIG_NATIVE
95 new wxPrefConfig(wxTheApp
->GetAppName());
96 #else // either we're under Unix or wish to use files even under Windows
97 new wxFileConfig(wxTheApp
->GetAppName());
104 // ----------------------------------------------------------------------------
105 // wxConfigBase reading entries
106 // ----------------------------------------------------------------------------
108 // implement both Read() overloads for the given type in terms of DoRead()
109 #define IMPLEMENT_READ_FOR_TYPE(name, type, deftype, extra) \
110 bool wxConfigBase::Read(const wxString& key, type *val) const \
112 wxCHECK_MSG( val, false, _T("wxConfig::Read(): NULL parameter") ); \
114 if ( !DoRead##name(key, val) ) \
117 *val = extra(*val); \
122 bool wxConfigBase::Read(const wxString& key, \
124 deftype defVal) const \
126 wxCHECK_MSG( val, false, _T("wxConfig::Read(): NULL parameter") ); \
128 bool read = DoRead##name(key, val); \
131 if ( IsRecordingDefaults() ) \
133 ((wxConfigBase *)this)->DoWrite##name(key, defVal); \
139 *val = extra(*val); \
145 IMPLEMENT_READ_FOR_TYPE(String
, wxString
, const wxString
&, ExpandEnvVars
)
146 IMPLEMENT_READ_FOR_TYPE(Long
, long, long, long)
147 IMPLEMENT_READ_FOR_TYPE(Int
, int, int, int)
148 IMPLEMENT_READ_FOR_TYPE(Double
, double, double, double)
149 IMPLEMENT_READ_FOR_TYPE(Bool
, bool, bool, bool)
151 #undef IMPLEMENT_READ_FOR_TYPE
153 // the DoReadXXX() for the other types have implementation in the base class
154 // but can be overridden in the derived ones
155 bool wxConfigBase::DoReadInt(const wxString
& key
, int *pi
) const
157 wxCHECK_MSG( pi
, false, _T("wxConfig::Read(): NULL parameter") );
160 if ( !DoReadLong(key
, &l
) )
163 wxASSERT_MSG( l
< INT_MAX
, _T("overflow in wxConfig::DoReadInt") );
170 bool wxConfigBase::DoReadBool(const wxString
& key
, bool* val
) const
172 wxCHECK_MSG( val
, false, _T("wxConfig::Read(): NULL parameter") );
175 if ( !DoReadLong(key
, &l
) )
178 wxASSERT_MSG( l
== 0 || l
== 1, _T("bad bool value in wxConfig::DoReadInt") );
185 bool wxConfigBase::DoReadDouble(const wxString
& key
, double* val
) const
188 if ( Read(key
, &str
) )
190 return str
.ToDouble(val
);
196 // string reading helper
197 wxString
wxConfigBase::ExpandEnvVars(const wxString
& str
) const
199 wxString tmp
; // Required for BC++
200 if (IsExpandingEnvVars())
201 tmp
= wxExpandEnvVars(str
);
207 // ----------------------------------------------------------------------------
208 // wxConfigBase writing
209 // ----------------------------------------------------------------------------
211 bool wxConfigBase::DoWriteDouble(const wxString
& key
, double val
)
213 return DoWriteString(key
, wxString::Format(_T("%g"), val
));
216 bool wxConfigBase::DoWriteInt(const wxString
& key
, int value
)
218 return DoWriteLong(key
, (long)value
);
221 bool wxConfigBase::DoWriteBool(const wxString
& key
, bool value
)
223 return DoWriteLong(key
, value
? 1l : 0l);
226 // ----------------------------------------------------------------------------
227 // wxConfigPathChanger
228 // ----------------------------------------------------------------------------
230 wxConfigPathChanger::wxConfigPathChanger(const wxConfigBase
*pContainer
,
231 const wxString
& strEntry
)
233 m_pContainer
= (wxConfigBase
*)pContainer
;
235 // the path is everything which precedes the last slash
236 wxString strPath
= strEntry
.BeforeLast(wxCONFIG_PATH_SEPARATOR
);
238 // except in the special case of "/keyname" when there is nothing before "/"
239 if ( strPath
.empty() &&
240 ((!strEntry
.empty()) && strEntry
[0] == wxCONFIG_PATH_SEPARATOR
) )
242 strPath
= wxCONFIG_PATH_SEPARATOR
;
245 if ( !strPath
.empty() )
247 if ( m_pContainer
->GetPath() != strPath
)
249 // do change the path
252 /* JACS: work around a memory bug that causes an assert
253 when using wxRegConfig, related to reference-counting.
254 Can be reproduced by removing (const wxChar*) below and
255 adding the following code to the config sample OnInit under
258 pConfig->SetPath(wxT("MySettings"));
259 pConfig->SetPath(wxT(".."));
261 pConfig->Read(_T("MainWindowX"), & value);
263 m_strOldPath
= (const wxChar
*) m_pContainer
->GetPath();
264 if ( *m_strOldPath
.c_str() != wxCONFIG_PATH_SEPARATOR
)
265 m_strOldPath
+= wxCONFIG_PATH_SEPARATOR
;
266 m_pContainer
->SetPath(strPath
);
269 // in any case, use the just the name, not full path
270 m_strName
= strEntry
.AfterLast(wxCONFIG_PATH_SEPARATOR
);
273 // it's a name only, without path - nothing to do
275 m_strName
= strEntry
;
279 wxConfigPathChanger::~wxConfigPathChanger()
281 // only restore path if it was changed
283 m_pContainer
->SetPath(m_strOldPath
);
287 #endif // wxUSE_CONFIG
289 // ----------------------------------------------------------------------------
290 // static & global functions
291 // ----------------------------------------------------------------------------
293 // understands both Unix and Windows (but only under Windows) environment
294 // variables expansion: i.e. $var, $(var) and ${var} are always understood
295 // and in addition under Windows %var% is also.
296 wxString
wxExpandEnvVars(const wxString
& str
)
299 strResult
.Alloc(str
.Len());
301 // don't change the values the enum elements: they must be equal
302 // to the matching [closing] delimiter.
306 Bracket_Normal
= ')',
309 Bracket_Windows
= '%', // yeah, Windows people are a bit strange ;-)
315 for ( size_t n
= 0; n
< str
.Len(); n
++ ) {
324 if ( str
[n
] == wxT('%') )
325 bracket
= Bracket_Windows
;
328 if ( n
== str
.Len() - 1 ) {
329 bracket
= Bracket_None
;
332 switch ( str
[n
+ 1] ) {
334 bracket
= Bracket_Normal
;
335 n
++; // skip the bracket
339 bracket
= Bracket_Curly
;
340 n
++; // skip the bracket
344 bracket
= Bracket_None
;
350 while ( m
< str
.Len() && (wxIsalnum(str
[m
]) || str
[m
] == wxT('_')) )
353 wxString
strVarName(str
.c_str() + n
+ 1, m
- n
- 1);
356 const wxChar
*pszValue
= NULL
;
358 const wxChar
*pszValue
= wxGetenv(strVarName
);
360 if ( pszValue
!= NULL
) {
361 strResult
+= pszValue
;
364 // variable doesn't exist => don't change anything
366 if ( bracket
!= Bracket_Windows
)
368 if ( bracket
!= Bracket_None
)
369 strResult
<< str
[n
- 1];
370 strResult
<< str
[n
] << strVarName
;
373 // check the closing bracket
374 if ( bracket
!= Bracket_None
) {
375 if ( m
== str
.Len() || str
[m
] != (wxChar
)bracket
) {
376 // under MSW it's common to have '%' characters in the registry
377 // and it's annoying to have warnings about them each time, so
378 // ignroe them silently if they are not used for env vars
380 // under Unix, OTOH, this warning could be useful for the user to
381 // understand why isn't the variable expanded as intended
383 wxLogWarning(_("Environment variables expansion failed: missing '%c' at position %u in '%s'."),
384 (char)bracket
, (unsigned int) (m
+ 1), str
.c_str());
388 // skip closing bracket unless the variables wasn't expanded
389 if ( pszValue
== NULL
)
390 strResult
<< (char)bracket
;
395 n
= m
- 1; // skip variable name
400 // backslash can be used to suppress special meaning of % and $
401 if ( n
!= str
.Len() - 1 &&
402 (str
[n
+ 1] == wxT('%') || str
[n
+ 1] == wxT('$')) ) {
403 strResult
+= str
[++n
];
417 // this function is used to properly interpret '..' in path
418 void wxSplitPath(wxArrayString
& aParts
, const wxChar
*sz
)
423 const wxChar
*pc
= sz
;
425 if ( *pc
== wxT('\0') || *pc
== wxCONFIG_PATH_SEPARATOR
) {
426 if ( strCurrent
== wxT(".") ) {
429 else if ( strCurrent
== wxT("..") ) {
431 if ( aParts
.size() == 0 )
432 wxLogWarning(_("'%s' has extra '..', ignored."), sz
);
434 aParts
.erase(aParts
.end() - 1);
438 else if ( !strCurrent
.empty() ) {
439 aParts
.push_back(strCurrent
);
443 // could log an error here, but we prefer to ignore extra '/'
445 if ( *pc
== wxT('\0') )