]>
git.saurik.com Git - wxWidgets.git/blob - src/common/config.cpp
e7e62a34bb7897ee5a56f3621265c224f08a8355
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/config.cpp
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"
34 #include "wx/arrstr.h"
38 #if wxUSE_CONFIG && ((wxUSE_FILE && wxUSE_TEXTFILE) || wxUSE_CONFIG_NATIVE)
40 #include "wx/apptrait.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 wxConfigBase
*wxAppTraitsBase::CreateConfig()
65 #if defined(__WXMSW__) && wxUSE_CONFIG_NATIVE
66 wxRegConfig(wxTheApp
->GetAppName(), wxTheApp
->GetVendorName());
67 #elif defined(__WXPALMOS__) && wxUSE_CONFIG_NATIVE
68 wxPrefConfig(wxTheApp
->GetAppName());
69 #else // either we're under Unix or wish to use files even under Windows
70 wxFileConfig(wxTheApp
->GetAppName());
74 // ----------------------------------------------------------------------------
76 // ----------------------------------------------------------------------------
77 IMPLEMENT_ABSTRACT_CLASS(wxConfigBase
, wxObject
)
79 // Not all args will always be used by derived classes, but including them all
80 // in each class ensures compatibility.
81 wxConfigBase::wxConfigBase(const wxString
& appName
,
82 const wxString
& vendorName
,
83 const wxString
& WXUNUSED(localFilename
),
84 const wxString
& WXUNUSED(globalFilename
),
86 : m_appName(appName
), m_vendorName(vendorName
), m_style(style
)
88 m_bExpandEnvVars
= true;
89 m_bRecordDefaults
= false;
92 wxConfigBase::~wxConfigBase()
94 // required here for Darwin
97 wxConfigBase
*wxConfigBase::Set(wxConfigBase
*pConfig
)
99 wxConfigBase
*pOld
= ms_pConfig
;
100 ms_pConfig
= pConfig
;
104 wxConfigBase
*wxConfigBase::Create()
106 if ( ms_bAutoCreate
&& ms_pConfig
== NULL
) {
107 wxAppTraits
* const traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
108 wxCHECK_MSG( traits
, NULL
, _T("create wxApp before calling this") );
110 ms_pConfig
= traits
->CreateConfig();
116 // ----------------------------------------------------------------------------
117 // wxConfigBase reading entries
118 // ----------------------------------------------------------------------------
120 // implement both Read() overloads for the given type in terms of DoRead()
121 #define IMPLEMENT_READ_FOR_TYPE(name, type, deftype, extra) \
122 bool wxConfigBase::Read(const wxString& key, type *val) const \
124 wxCHECK_MSG( val, false, _T("wxConfig::Read(): NULL parameter") ); \
126 if ( !DoRead##name(key, val) ) \
129 *val = extra(*val); \
134 bool wxConfigBase::Read(const wxString& key, \
136 deftype defVal) const \
138 wxCHECK_MSG( val, false, _T("wxConfig::Read(): NULL parameter") ); \
140 bool read = DoRead##name(key, val); \
143 if ( IsRecordingDefaults() ) \
145 ((wxConfigBase *)this)->DoWrite##name(key, defVal); \
151 *val = extra(*val); \
157 IMPLEMENT_READ_FOR_TYPE(String
, wxString
, const wxString
&, ExpandEnvVars
)
158 IMPLEMENT_READ_FOR_TYPE(Long
, long, long, long)
159 IMPLEMENT_READ_FOR_TYPE(Int
, int, int, int)
160 IMPLEMENT_READ_FOR_TYPE(Double
, double, double, double)
161 IMPLEMENT_READ_FOR_TYPE(Bool
, bool, bool, bool)
163 #undef IMPLEMENT_READ_FOR_TYPE
165 // the DoReadXXX() for the other types have implementation in the base class
166 // but can be overridden in the derived ones
167 bool wxConfigBase::DoReadInt(const wxString
& key
, int *pi
) const
169 wxCHECK_MSG( pi
, false, _T("wxConfig::Read(): NULL parameter") );
172 if ( !DoReadLong(key
, &l
) )
175 wxASSERT_MSG( l
< INT_MAX
, _T("overflow in wxConfig::DoReadInt") );
182 bool wxConfigBase::DoReadBool(const wxString
& key
, bool* val
) const
184 wxCHECK_MSG( val
, false, _T("wxConfig::Read(): NULL parameter") );
187 if ( !DoReadLong(key
, &l
) )
190 wxASSERT_MSG( l
== 0 || l
== 1, _T("bad bool value in wxConfig::DoReadInt") );
197 bool wxConfigBase::DoReadDouble(const wxString
& key
, double* val
) const
200 if ( Read(key
, &str
) )
202 return str
.ToDouble(val
);
208 // string reading helper
209 wxString
wxConfigBase::ExpandEnvVars(const wxString
& str
) const
211 wxString tmp
; // Required for BC++
212 if (IsExpandingEnvVars())
213 tmp
= wxExpandEnvVars(str
);
219 // ----------------------------------------------------------------------------
220 // wxConfigBase writing
221 // ----------------------------------------------------------------------------
223 bool wxConfigBase::DoWriteDouble(const wxString
& key
, double val
)
225 return DoWriteString(key
, wxString::Format(_T("%g"), val
));
228 bool wxConfigBase::DoWriteInt(const wxString
& key
, int value
)
230 return DoWriteLong(key
, (long)value
);
233 bool wxConfigBase::DoWriteBool(const wxString
& key
, bool value
)
235 return DoWriteLong(key
, value
? 1l : 0l);
238 // ----------------------------------------------------------------------------
239 // wxConfigPathChanger
240 // ----------------------------------------------------------------------------
242 wxConfigPathChanger::wxConfigPathChanger(const wxConfigBase
*pContainer
,
243 const wxString
& strEntry
)
246 m_pContainer
= (wxConfigBase
*)pContainer
;
248 // the path is everything which precedes the last slash
249 wxString strPath
= strEntry
.BeforeLast(wxCONFIG_PATH_SEPARATOR
);
251 // except in the special case of "/keyname" when there is nothing before "/"
252 if ( strPath
.empty() &&
253 ((!strEntry
.empty()) && strEntry
[0] == wxCONFIG_PATH_SEPARATOR
) )
255 strPath
= wxCONFIG_PATH_SEPARATOR
;
258 if ( !strPath
.empty() )
260 if ( m_pContainer
->GetPath() != strPath
)
262 // we do change the path so restore it later
265 /* JACS: work around a memory bug that causes an assert
266 when using wxRegConfig, related to reference-counting.
267 Can be reproduced by removing (const wxChar*) below and
268 adding the following code to the config sample OnInit under
271 pConfig->SetPath(wxT("MySettings"));
272 pConfig->SetPath(wxT(".."));
274 pConfig->Read(_T("MainWindowX"), & value);
276 m_strOldPath
= (const wxChar
*) m_pContainer
->GetPath();
277 if ( *m_strOldPath
.c_str() != wxCONFIG_PATH_SEPARATOR
)
278 m_strOldPath
+= wxCONFIG_PATH_SEPARATOR
;
279 m_pContainer
->SetPath(strPath
);
282 // in any case, use the just the name, not full path
283 m_strName
= strEntry
.AfterLast(wxCONFIG_PATH_SEPARATOR
);
286 // it's a name only, without path - nothing to do
287 m_strName
= strEntry
;
291 void wxConfigPathChanger::UpdateIfDeleted()
293 // we don't have to do anything at all if we didn't change the path
297 // find the deepest still existing parent path of the original path
298 while ( !m_pContainer
->HasGroup(m_strOldPath
) )
300 m_strOldPath
= m_strOldPath
.BeforeLast(wxCONFIG_PATH_SEPARATOR
);
301 if ( m_strOldPath
.empty() )
302 m_strOldPath
= wxCONFIG_PATH_SEPARATOR
;
306 wxConfigPathChanger::~wxConfigPathChanger()
308 // only restore path if it was changed
310 m_pContainer
->SetPath(m_strOldPath
);
314 // this is a wxConfig method but it's mainly used with wxConfigPathChanger
316 wxString
wxConfigBase::RemoveTrailingSeparator(const wxString
& key
)
320 // don't remove the only separator from a root group path!
321 while ( path
.length() > 1 )
323 if ( *path
.rbegin() != wxCONFIG_PATH_SEPARATOR
)
326 path
.erase(path
.end() - 1);
332 #endif // wxUSE_CONFIG
334 // ----------------------------------------------------------------------------
335 // static & global functions
336 // ----------------------------------------------------------------------------
338 // understands both Unix and Windows (but only under Windows) environment
339 // variables expansion: i.e. $var, $(var) and ${var} are always understood
340 // and in addition under Windows %var% is also.
342 // don't change the values the enum elements: they must be equal
343 // to the matching [closing] delimiter.
347 Bracket_Normal
= ')',
350 Bracket_Windows
= '%', // yeah, Windows people are a bit strange ;-)
355 wxString
wxExpandEnvVars(const wxString
& str
)
358 strResult
.Alloc(str
.length());
361 for ( size_t n
= 0; n
< str
.length(); n
++ ) {
362 switch ( str
[n
].GetValue() ) {
370 if ( str
[n
] == wxT('%') )
371 bracket
= Bracket_Windows
;
374 if ( n
== str
.length() - 1 ) {
375 bracket
= Bracket_None
;
378 switch ( str
[n
+ 1].GetValue() ) {
380 bracket
= Bracket_Normal
;
381 n
++; // skip the bracket
385 bracket
= Bracket_Curly
;
386 n
++; // skip the bracket
390 bracket
= Bracket_None
;
396 while ( m
< str
.length() && (wxIsalnum(str
[m
]) || str
[m
] == wxT('_')) )
399 wxString
strVarName(str
.c_str() + n
+ 1, m
- n
- 1);
402 const wxChar
*pszValue
= NULL
;
404 // NB: use wxGetEnv instead of wxGetenv as otherwise variables
405 // set through wxSetEnv may not be read correctly!
406 const wxChar
*pszValue
= NULL
;
408 if (wxGetEnv(strVarName
, &tmp
))
411 if ( pszValue
!= NULL
) {
412 strResult
+= pszValue
;
415 // variable doesn't exist => don't change anything
417 if ( bracket
!= Bracket_Windows
)
419 if ( bracket
!= Bracket_None
)
420 strResult
<< str
[n
- 1];
421 strResult
<< str
[n
] << strVarName
;
424 // check the closing bracket
425 if ( bracket
!= Bracket_None
) {
426 if ( m
== str
.length() || str
[m
] != (wxChar
)bracket
) {
427 // under MSW it's common to have '%' characters in the registry
428 // and it's annoying to have warnings about them each time, so
429 // ignroe them silently if they are not used for env vars
431 // under Unix, OTOH, this warning could be useful for the user to
432 // understand why isn't the variable expanded as intended
434 wxLogWarning(_("Environment variables expansion failed: missing '%c' at position %u in '%s'."),
435 (char)bracket
, (unsigned int) (m
+ 1), str
.c_str());
439 // skip closing bracket unless the variables wasn't expanded
440 if ( pszValue
== NULL
)
441 strResult
<< (wxChar
)bracket
;
446 n
= m
- 1; // skip variable name
451 // backslash can be used to suppress special meaning of % and $
452 if ( n
!= str
.length() - 1 &&
453 (str
[n
+ 1] == wxT('%') || str
[n
+ 1] == wxT('$')) ) {
454 strResult
+= str
[++n
];
468 // this function is used to properly interpret '..' in path
469 void wxSplitPath(wxArrayString
& aParts
, const wxChar
*sz
)
474 const wxChar
*pc
= sz
;
476 if ( *pc
== wxT('\0') || *pc
== wxCONFIG_PATH_SEPARATOR
) {
477 if ( strCurrent
== wxT(".") ) {
480 else if ( strCurrent
== wxT("..") ) {
482 if ( aParts
.size() == 0 )
483 wxLogWarning(_("'%s' has extra '..', ignored."), sz
);
485 aParts
.erase(aParts
.end() - 1);
489 else if ( !strCurrent
.empty() ) {
490 aParts
.push_back(strCurrent
);
494 // could log an error here, but we prefer to ignore extra '/'
496 if ( *pc
== wxT('\0') )