]>
git.saurik.com Git - wxWidgets.git/blob - src/common/config.cpp
488aa3b118423480617b10992f0f7e5edebd85ef
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/config.cpp
3 // Purpose: implementation of wxConfigBase class
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1997 Karsten Ballueder 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
, wxT("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, wxT("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, wxT("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(Double
, double, double, double)
160 IMPLEMENT_READ_FOR_TYPE(Bool
, bool, bool, bool)
162 #undef IMPLEMENT_READ_FOR_TYPE
164 // int is stored as long
165 bool wxConfigBase::Read(const wxString
& key
, int *pi
) const
168 bool r
= Read(key
, &l
);
169 wxASSERT_MSG( l
< INT_MAX
, wxT("int overflow in wxConfig::Read") );
174 bool wxConfigBase::Read(const wxString
& key
, int *pi
, int defVal
) const
177 bool r
= Read(key
, &l
, defVal
);
178 wxASSERT_MSG( l
< INT_MAX
, wxT("int overflow in wxConfig::Read") );
183 // the DoReadXXX() for the other types have implementation in the base class
184 // but can be overridden in the derived ones
185 bool wxConfigBase::DoReadBool(const wxString
& key
, bool* val
) const
187 wxCHECK_MSG( val
, false, wxT("wxConfig::Read(): NULL parameter") );
190 if ( !DoReadLong(key
, &l
) )
193 wxASSERT_MSG( l
== 0 || l
== 1, wxT("bad bool value in wxConfig::DoReadInt") );
200 bool wxConfigBase::DoReadDouble(const wxString
& key
, double* val
) const
203 if ( Read(key
, &str
) )
205 return str
.ToDouble(val
);
211 // string reading helper
212 wxString
wxConfigBase::ExpandEnvVars(const wxString
& str
) const
214 wxString tmp
; // Required for BC++
215 if (IsExpandingEnvVars())
216 tmp
= wxExpandEnvVars(str
);
222 // ----------------------------------------------------------------------------
223 // wxConfigBase writing
224 // ----------------------------------------------------------------------------
226 bool wxConfigBase::DoWriteDouble(const wxString
& key
, double val
)
228 return DoWriteString(key
, wxString::Format(wxT("%g"), val
));
231 bool wxConfigBase::DoWriteBool(const wxString
& key
, bool value
)
233 return DoWriteLong(key
, value
? 1l : 0l);
236 // ----------------------------------------------------------------------------
237 // wxConfigPathChanger
238 // ----------------------------------------------------------------------------
240 wxConfigPathChanger::wxConfigPathChanger(const wxConfigBase
*pContainer
,
241 const wxString
& strEntry
)
244 m_pContainer
= (wxConfigBase
*)pContainer
;
246 // the path is everything which precedes the last slash
247 wxString strPath
= strEntry
.BeforeLast(wxCONFIG_PATH_SEPARATOR
);
249 // except in the special case of "/keyname" when there is nothing before "/"
250 if ( strPath
.empty() &&
251 ((!strEntry
.empty()) && strEntry
[0] == wxCONFIG_PATH_SEPARATOR
) )
253 strPath
= wxCONFIG_PATH_SEPARATOR
;
256 if ( !strPath
.empty() )
258 if ( m_pContainer
->GetPath() != strPath
)
260 // we do change the path so restore it later
263 /* JACS: work around a memory bug that causes an assert
264 when using wxRegConfig, related to reference-counting.
265 Can be reproduced by removing .wc_str() below and
266 adding the following code to the config sample OnInit under
269 pConfig->SetPath(wxT("MySettings"));
270 pConfig->SetPath(wxT(".."));
272 pConfig->Read(wxT("MainWindowX"), & value);
274 m_strOldPath
= m_pContainer
->GetPath().wc_str();
275 if ( *m_strOldPath
.c_str() != wxCONFIG_PATH_SEPARATOR
)
276 m_strOldPath
+= wxCONFIG_PATH_SEPARATOR
;
277 m_pContainer
->SetPath(strPath
);
280 // in any case, use the just the name, not full path
281 m_strName
= strEntry
.AfterLast(wxCONFIG_PATH_SEPARATOR
);
284 // it's a name only, without path - nothing to do
285 m_strName
= strEntry
;
289 void wxConfigPathChanger::UpdateIfDeleted()
291 // we don't have to do anything at all if we didn't change the path
295 // find the deepest still existing parent path of the original path
296 while ( !m_pContainer
->HasGroup(m_strOldPath
) )
298 m_strOldPath
= m_strOldPath
.BeforeLast(wxCONFIG_PATH_SEPARATOR
);
299 if ( m_strOldPath
.empty() )
300 m_strOldPath
= wxCONFIG_PATH_SEPARATOR
;
304 wxConfigPathChanger::~wxConfigPathChanger()
306 // only restore path if it was changed
308 m_pContainer
->SetPath(m_strOldPath
);
312 // this is a wxConfig method but it's mainly used with wxConfigPathChanger
314 wxString
wxConfigBase::RemoveTrailingSeparator(const wxString
& key
)
318 // don't remove the only separator from a root group path!
319 while ( path
.length() > 1 )
321 if ( *path
.rbegin() != wxCONFIG_PATH_SEPARATOR
)
324 path
.erase(path
.end() - 1);
330 #endif // wxUSE_CONFIG
332 // ----------------------------------------------------------------------------
333 // static & global functions
334 // ----------------------------------------------------------------------------
336 // understands both Unix and Windows (but only under Windows) environment
337 // variables expansion: i.e. $var, $(var) and ${var} are always understood
338 // and in addition under Windows %var% is also.
340 // don't change the values the enum elements: they must be equal
341 // to the matching [closing] delimiter.
345 Bracket_Normal
= ')',
348 Bracket_Windows
= '%', // yeah, Windows people are a bit strange ;-)
353 wxString
wxExpandEnvVars(const wxString
& str
)
356 strResult
.Alloc(str
.length());
359 for ( size_t n
= 0; n
< str
.length(); n
++ ) {
360 switch ( str
[n
].GetValue() ) {
368 if ( str
[n
] == wxT('%') )
369 bracket
= Bracket_Windows
;
372 if ( n
== str
.length() - 1 ) {
373 bracket
= Bracket_None
;
376 switch ( str
[n
+ 1].GetValue() ) {
378 bracket
= Bracket_Normal
;
379 n
++; // skip the bracket
383 bracket
= Bracket_Curly
;
384 n
++; // skip the bracket
388 bracket
= Bracket_None
;
394 while ( m
< str
.length() && (wxIsalnum(str
[m
]) || str
[m
] == wxT('_')) )
397 wxString
strVarName(str
.c_str() + n
+ 1, m
- n
- 1);
400 const bool expanded
= false;
402 // NB: use wxGetEnv instead of wxGetenv as otherwise variables
403 // set through wxSetEnv may not be read correctly!
404 bool expanded
= false;
406 if (wxGetEnv(strVarName
, &tmp
))
414 // variable doesn't exist => don't change anything
416 if ( bracket
!= Bracket_Windows
)
418 if ( bracket
!= Bracket_None
)
419 strResult
<< str
[n
- 1];
420 strResult
<< str
[n
] << strVarName
;
423 // check the closing bracket
424 if ( bracket
!= Bracket_None
) {
425 if ( m
== str
.length() || str
[m
] != (wxChar
)bracket
) {
426 // under MSW it's common to have '%' characters in the registry
427 // and it's annoying to have warnings about them each time, so
428 // ignroe them silently if they are not used for env vars
430 // under Unix, OTOH, this warning could be useful for the user to
431 // understand why isn't the variable expanded as intended
433 wxLogWarning(_("Environment variables expansion failed: missing '%c' at position %u in '%s'."),
434 (char)bracket
, (unsigned int) (m
+ 1), str
.c_str());
438 // skip closing bracket unless the variables wasn't expanded
440 strResult
<< (wxChar
)bracket
;
445 n
= m
- 1; // skip variable name
450 // backslash can be used to suppress special meaning of % and $
451 if ( n
!= str
.length() - 1 &&
452 (str
[n
+ 1] == wxT('%') || str
[n
+ 1] == wxT('$')) ) {
453 strResult
+= str
[++n
];
467 // this function is used to properly interpret '..' in path
468 void wxSplitPath(wxArrayString
& aParts
, const wxString
& path
)
473 wxString::const_iterator pc
= path
.begin();
475 if ( pc
== path
.end() || *pc
== wxCONFIG_PATH_SEPARATOR
) {
476 if ( strCurrent
== wxT(".") ) {
479 else if ( strCurrent
== wxT("..") ) {
481 if ( aParts
.size() == 0 )
483 wxLogWarning(_("'%s' has extra '..', ignored."), path
);
487 aParts
.erase(aParts
.end() - 1);
492 else if ( !strCurrent
.empty() ) {
493 aParts
.push_back(strCurrent
);
497 // could log an error here, but we prefer to ignore extra '/'
499 if ( pc
== path
.end() )