]>
git.saurik.com Git - wxWidgets.git/blob - src/common/config.cpp
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)
44 #include <limits.h> // for INT_MAX
46 // ----------------------------------------------------------------------------
47 // global and class static variables
48 // ----------------------------------------------------------------------------
50 wxConfigBase
*wxConfigBase::ms_pConfig
= NULL
;
51 bool wxConfigBase::ms_bAutoCreate
= true;
53 // ============================================================================
55 // ============================================================================
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 // Not all args will always be used by derived classes, but including them all
62 // in each class ensures compatibility.
63 wxConfigBase::wxConfigBase(const wxString
& appName
,
64 const wxString
& vendorName
,
65 const wxString
& WXUNUSED(localFilename
),
66 const wxString
& WXUNUSED(globalFilename
),
68 : m_appName(appName
), m_vendorName(vendorName
), m_style(style
)
70 m_bExpandEnvVars
= true;
71 m_bRecordDefaults
= false;
74 wxConfigBase::~wxConfigBase()
76 // required here for Darwin
79 wxConfigBase
*wxConfigBase::Set(wxConfigBase
*pConfig
)
81 wxConfigBase
*pOld
= ms_pConfig
;
86 wxConfigBase
*wxConfigBase::Create()
88 if ( ms_bAutoCreate
&& ms_pConfig
== NULL
) {
90 #if defined(__WXMSW__) && wxUSE_CONFIG_NATIVE
91 new wxRegConfig(wxTheApp
->GetAppName(), wxTheApp
->GetVendorName());
92 #elif defined(__WXPALMOS__) && wxUSE_CONFIG_NATIVE
93 new wxPrefConfig(wxTheApp
->GetAppName());
94 #else // either we're under Unix or wish to use files even under Windows
95 new wxFileConfig(wxTheApp
->GetAppName());
102 // ----------------------------------------------------------------------------
103 // wxConfigBase reading entries
104 // ----------------------------------------------------------------------------
106 // implement both Read() overloads for the given type in terms of DoRead()
107 #define IMPLEMENT_READ_FOR_TYPE(name, type, deftype, extra) \
108 bool wxConfigBase::Read(const wxString& key, type *val) const \
110 wxCHECK_MSG( val, false, _T("wxConfig::Read(): NULL parameter") ); \
112 if ( !DoRead##name(key, val) ) \
115 *val = extra(*val); \
120 bool wxConfigBase::Read(const wxString& key, \
122 deftype defVal) const \
124 wxCHECK_MSG( val, false, _T("wxConfig::Read(): NULL parameter") ); \
126 bool read = DoRead##name(key, val); \
129 if ( IsRecordingDefaults() ) \
131 ((wxConfigBase *)this)->DoWrite##name(key, defVal); \
137 *val = extra(*val); \
143 IMPLEMENT_READ_FOR_TYPE(String
, wxString
, const wxString
&, ExpandEnvVars
)
144 IMPLEMENT_READ_FOR_TYPE(Long
, long, long, long)
145 IMPLEMENT_READ_FOR_TYPE(Int
, int, int, int)
146 IMPLEMENT_READ_FOR_TYPE(Double
, double, double, double)
147 IMPLEMENT_READ_FOR_TYPE(Bool
, bool, bool, bool)
149 #undef IMPLEMENT_READ_FOR_TYPE
151 // the DoReadXXX() for the other types have implementation in the base class
152 // but can be overridden in the derived ones
153 bool wxConfigBase::DoReadInt(const wxString
& key
, int *pi
) const
155 wxCHECK_MSG( pi
, false, _T("wxConfig::Read(): NULL parameter") );
158 if ( !DoReadLong(key
, &l
) )
161 wxASSERT_MSG( l
< INT_MAX
, _T("overflow in wxConfig::DoReadInt") );
168 bool wxConfigBase::DoReadBool(const wxString
& key
, bool* val
) const
170 wxCHECK_MSG( val
, false, _T("wxConfig::Read(): NULL parameter") );
173 if ( !DoReadLong(key
, &l
) )
176 wxASSERT_MSG( l
== 0 || l
== 1, _T("bad bool value in wxConfig::DoReadInt") );
183 bool wxConfigBase::DoReadDouble(const wxString
& key
, double* val
) const
186 if ( Read(key
, &str
) )
188 return str
.ToDouble(val
);
194 // string reading helper
195 wxString
wxConfigBase::ExpandEnvVars(const wxString
& str
) const
197 wxString tmp
; // Required for BC++
198 if (IsExpandingEnvVars())
199 tmp
= wxExpandEnvVars(str
);
205 // ----------------------------------------------------------------------------
206 // wxConfigBase writing
207 // ----------------------------------------------------------------------------
209 bool wxConfigBase::DoWriteDouble(const wxString
& key
, double val
)
211 return DoWriteString(key
, wxString::Format(_T("%g"), val
));
214 bool wxConfigBase::DoWriteInt(const wxString
& key
, int value
)
216 return DoWriteLong(key
, (long)value
);
219 bool wxConfigBase::DoWriteBool(const wxString
& key
, bool value
)
221 return DoWriteLong(key
, value
? 1l : 0l);
224 // ----------------------------------------------------------------------------
225 // wxConfigPathChanger
226 // ----------------------------------------------------------------------------
228 wxConfigPathChanger::wxConfigPathChanger(const wxConfigBase
*pContainer
,
229 const wxString
& strEntry
)
232 m_pContainer
= (wxConfigBase
*)pContainer
;
234 // the path is everything which precedes the last slash
235 wxString strPath
= strEntry
.BeforeLast(wxCONFIG_PATH_SEPARATOR
);
237 // except in the special case of "/keyname" when there is nothing before "/"
238 if ( strPath
.empty() &&
239 ((!strEntry
.empty()) && strEntry
[0] == wxCONFIG_PATH_SEPARATOR
) )
241 strPath
= wxCONFIG_PATH_SEPARATOR
;
244 if ( !strPath
.empty() )
246 if ( m_pContainer
->GetPath() != strPath
)
248 // we do change the path so restore it later
251 /* JACS: work around a memory bug that causes an assert
252 when using wxRegConfig, related to reference-counting.
253 Can be reproduced by removing (const wxChar*) below and
254 adding the following code to the config sample OnInit under
257 pConfig->SetPath(wxT("MySettings"));
258 pConfig->SetPath(wxT(".."));
260 pConfig->Read(_T("MainWindowX"), & value);
262 m_strOldPath
= (const wxChar
*) m_pContainer
->GetPath();
263 if ( *m_strOldPath
.c_str() != wxCONFIG_PATH_SEPARATOR
)
264 m_strOldPath
+= wxCONFIG_PATH_SEPARATOR
;
265 m_pContainer
->SetPath(strPath
);
268 // in any case, use the just the name, not full path
269 m_strName
= strEntry
.AfterLast(wxCONFIG_PATH_SEPARATOR
);
272 // it's a name only, without path - nothing to do
273 m_strName
= strEntry
;
277 void wxConfigPathChanger::UpdateIfDeleted()
279 // we don't have to do anything at all if we didn't change the path
283 // find the deepest still existing parent path of the original path
284 while ( !m_pContainer
->HasGroup(m_strOldPath
) )
286 m_strOldPath
= m_strOldPath
.BeforeLast(wxCONFIG_PATH_SEPARATOR
);
287 if ( m_strOldPath
.empty() )
288 m_strOldPath
= wxCONFIG_PATH_SEPARATOR
;
292 wxConfigPathChanger::~wxConfigPathChanger()
294 // only restore path if it was changed
296 m_pContainer
->SetPath(m_strOldPath
);
300 // this is a wxConfig method but it's mainly used with wxConfigPathChanger
302 wxString
wxConfigBase::RemoveTrailingSeparator(const wxString
& key
)
306 // don't remove the only separator from a root group path!
307 while ( path
.length() > 1 )
309 if ( *path
.rbegin() != wxCONFIG_PATH_SEPARATOR
)
312 path
.erase(path
.end() - 1);
318 #endif // wxUSE_CONFIG
320 // ----------------------------------------------------------------------------
321 // static & global functions
322 // ----------------------------------------------------------------------------
324 // understands both Unix and Windows (but only under Windows) environment
325 // variables expansion: i.e. $var, $(var) and ${var} are always understood
326 // and in addition under Windows %var% is also.
328 // don't change the values the enum elements: they must be equal
329 // to the matching [closing] delimiter.
333 Bracket_Normal
= ')',
336 Bracket_Windows
= '%', // yeah, Windows people are a bit strange ;-)
341 wxString
wxExpandEnvVars(const wxString
& str
)
344 strResult
.Alloc(str
.length());
347 for ( size_t n
= 0; n
< str
.length(); n
++ ) {
356 if ( str
[n
] == wxT('%') )
357 bracket
= Bracket_Windows
;
360 if ( n
== str
.length() - 1 ) {
361 bracket
= Bracket_None
;
364 switch ( str
[n
+ 1] ) {
366 bracket
= Bracket_Normal
;
367 n
++; // skip the bracket
371 bracket
= Bracket_Curly
;
372 n
++; // skip the bracket
376 bracket
= Bracket_None
;
382 while ( m
< str
.length() && (wxIsalnum(str
[m
]) || str
[m
] == wxT('_')) )
385 wxString
strVarName(str
.c_str() + n
+ 1, m
- n
- 1);
388 const wxChar
*pszValue
= NULL
;
390 // NB: use wxGetEnv instead of wxGetenv as otherwise variables
391 // set through wxSetEnv may not be read correctly!
392 const wxChar
*pszValue
= NULL
;
394 if (wxGetEnv(strVarName
, &tmp
))
397 if ( pszValue
!= NULL
) {
398 strResult
+= pszValue
;
401 // variable doesn't exist => don't change anything
403 if ( bracket
!= Bracket_Windows
)
405 if ( bracket
!= Bracket_None
)
406 strResult
<< str
[n
- 1];
407 strResult
<< str
[n
] << strVarName
;
410 // check the closing bracket
411 if ( bracket
!= Bracket_None
) {
412 if ( m
== str
.length() || str
[m
] != (wxChar
)bracket
) {
413 // under MSW it's common to have '%' characters in the registry
414 // and it's annoying to have warnings about them each time, so
415 // ignroe them silently if they are not used for env vars
417 // under Unix, OTOH, this warning could be useful for the user to
418 // understand why isn't the variable expanded as intended
420 wxLogWarning(_("Environment variables expansion failed: missing '%c' at position %u in '%s'."),
421 (char)bracket
, (unsigned int) (m
+ 1), str
.c_str());
425 // skip closing bracket unless the variables wasn't expanded
426 if ( pszValue
== NULL
)
427 strResult
<< (char)bracket
;
432 n
= m
- 1; // skip variable name
437 // backslash can be used to suppress special meaning of % and $
438 if ( n
!= str
.length() - 1 &&
439 (str
[n
+ 1] == wxT('%') || str
[n
+ 1] == wxT('$')) ) {
440 strResult
+= str
[++n
];
454 // this function is used to properly interpret '..' in path
455 void wxSplitPath(wxArrayString
& aParts
, const wxChar
*sz
)
460 const wxChar
*pc
= sz
;
462 if ( *pc
== wxT('\0') || *pc
== wxCONFIG_PATH_SEPARATOR
) {
463 if ( strCurrent
== wxT(".") ) {
466 else if ( strCurrent
== wxT("..") ) {
468 if ( aParts
.size() == 0 )
469 wxLogWarning(_("'%s' has extra '..', ignored."), sz
);
471 aParts
.erase(aParts
.end() - 1);
475 else if ( !strCurrent
.empty() ) {
476 aParts
.push_back(strCurrent
);
480 // could log an error here, but we prefer to ignore extra '/'
482 if ( *pc
== wxT('\0') )