]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | /////////////////////////////////////////////////////////////////////////////// |
88a7a4e1 | 2 | // Name: src/common/config.cpp |
5fe256de | 3 | // Purpose: implementation of wxConfigBase class |
c801d85f | 4 | // Author: Vadim Zeitlin |
ba7f9a90 | 5 | // Modified by: |
c801d85f KB |
6 | // Created: 07.04.98 |
7 | // RCS-ID: $Id$ | |
9d55bfef | 8 | // Copyright: (c) 1997 Karsten Ballueder Ballueder@usa.net |
c801d85f | 9 | // Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> |
65571936 | 10 | // Licence: wxWindows licence |
c801d85f KB |
11 | /////////////////////////////////////////////////////////////////////////////// |
12 | ||
855e92a8 VZ |
13 | // ---------------------------------------------------------------------------- |
14 | // headers | |
15 | // ---------------------------------------------------------------------------- | |
c801d85f | 16 | |
9869734d | 17 | #include "wx/wxprec.h" |
c801d85f | 18 | |
88a7a4e1 WS |
19 | #ifdef __BORLANDC__ |
20 | #pragma hdrstop | |
c801d85f KB |
21 | #endif //__BORLANDC__ |
22 | ||
ecef04c2 VZ |
23 | #ifndef wxUSE_CONFIG_NATIVE |
24 | #define wxUSE_CONFIG_NATIVE 1 | |
25 | #endif | |
26 | ||
21709999 | 27 | #include "wx/config.h" |
88a7a4e1 WS |
28 | |
29 | #ifndef WX_PRECOMP | |
30 | #include "wx/intl.h" | |
e4db172a | 31 | #include "wx/log.h" |
670f9935 | 32 | #include "wx/app.h" |
de6185e2 | 33 | #include "wx/utils.h" |
aaa6d89a | 34 | #include "wx/arrstr.h" |
18680f86 | 35 | #include "wx/math.h" |
88a7a4e1 WS |
36 | #endif //WX_PRECOMP |
37 | ||
48c8a76b | 38 | #if wxUSE_CONFIG && ((wxUSE_FILE && wxUSE_TEXTFILE) || wxUSE_CONFIG_NATIVE) |
ce4169a4 | 39 | |
84281b92 | 40 | #include "wx/apptrait.h" |
9869734d | 41 | #include "wx/file.h" |
9869734d | 42 | |
9869734d | 43 | #include <stdlib.h> |
f6bcfd97 | 44 | #include <ctype.h> |
2ba41305 | 45 | #include <limits.h> // for INT_MAX |
855e92a8 VZ |
46 | |
47 | // ---------------------------------------------------------------------------- | |
48 | // global and class static variables | |
49 | // ---------------------------------------------------------------------------- | |
50 | ||
275bf4c1 | 51 | wxConfigBase *wxConfigBase::ms_pConfig = NULL; |
c9d59ee7 | 52 | bool wxConfigBase::ms_bAutoCreate = true; |
c801d85f KB |
53 | |
54 | // ============================================================================ | |
55 | // implementation | |
56 | // ============================================================================ | |
57 | ||
84281b92 VZ |
58 | // ---------------------------------------------------------------------------- |
59 | // wxAppTraitsBase | |
60 | // ---------------------------------------------------------------------------- | |
61 | ||
62 | wxConfigBase *wxAppTraitsBase::CreateConfig() | |
63 | { | |
64 | return new | |
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()); | |
71 | #endif | |
72 | } | |
73 | ||
c801d85f | 74 | // ---------------------------------------------------------------------------- |
5fe256de | 75 | // wxConfigBase |
c801d85f | 76 | // ---------------------------------------------------------------------------- |
c4ec0ce8 | 77 | IMPLEMENT_ABSTRACT_CLASS(wxConfigBase, wxObject) |
c801d85f | 78 | |
f6bcfd97 BP |
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), | |
85 | long style) | |
86 | : m_appName(appName), m_vendorName(vendorName), m_style(style) | |
18244936 | 87 | { |
c9d59ee7 WS |
88 | m_bExpandEnvVars = true; |
89 | m_bRecordDefaults = false; | |
18244936 JS |
90 | } |
91 | ||
5e233068 WS |
92 | wxConfigBase::~wxConfigBase() |
93 | { | |
94 | // required here for Darwin | |
95 | } | |
96 | ||
5fe256de | 97 | wxConfigBase *wxConfigBase::Set(wxConfigBase *pConfig) |
855e92a8 | 98 | { |
5fe256de | 99 | wxConfigBase *pOld = ms_pConfig; |
855e92a8 VZ |
100 | ms_pConfig = pConfig; |
101 | return pOld; | |
102 | } | |
103 | ||
5fe256de | 104 | wxConfigBase *wxConfigBase::Create() |
855e92a8 | 105 | { |
275bf4c1 | 106 | if ( ms_bAutoCreate && ms_pConfig == NULL ) { |
84281b92 VZ |
107 | wxAppTraits * const traits = wxTheApp ? wxTheApp->GetTraits() : NULL; |
108 | wxCHECK_MSG( traits, NULL, _T("create wxApp before calling this") ); | |
109 | ||
110 | ms_pConfig = traits->CreateConfig(); | |
275bf4c1 VZ |
111 | } |
112 | ||
113 | return ms_pConfig; | |
855e92a8 VZ |
114 | } |
115 | ||
2ba41305 VZ |
116 | // ---------------------------------------------------------------------------- |
117 | // wxConfigBase reading entries | |
118 | // ---------------------------------------------------------------------------- | |
18244936 | 119 | |
2ba41305 VZ |
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 \ | |
123 | { \ | |
c9d59ee7 | 124 | wxCHECK_MSG( val, false, _T("wxConfig::Read(): NULL parameter") ); \ |
2ba41305 | 125 | \ |
2b5f62a0 | 126 | if ( !DoRead##name(key, val) ) \ |
c9d59ee7 | 127 | return false; \ |
2b5f62a0 VZ |
128 | \ |
129 | *val = extra(*val); \ | |
130 | \ | |
c9d59ee7 | 131 | return true; \ |
2ba41305 VZ |
132 | } \ |
133 | \ | |
134 | bool wxConfigBase::Read(const wxString& key, \ | |
135 | type *val, \ | |
136 | deftype defVal) const \ | |
137 | { \ | |
c9d59ee7 | 138 | wxCHECK_MSG( val, false, _T("wxConfig::Read(): NULL parameter") ); \ |
2ba41305 | 139 | \ |
2b5f62a0 VZ |
140 | bool read = DoRead##name(key, val); \ |
141 | if ( !read ) \ | |
2ba41305 | 142 | { \ |
2b5f62a0 VZ |
143 | if ( IsRecordingDefaults() ) \ |
144 | { \ | |
145 | ((wxConfigBase *)this)->DoWrite##name(key, defVal); \ | |
146 | } \ | |
147 | \ | |
148 | *val = defVal; \ | |
2ba41305 VZ |
149 | } \ |
150 | \ | |
2b5f62a0 | 151 | *val = extra(*val); \ |
2ba41305 | 152 | \ |
2b5f62a0 | 153 | return read; \ |
18244936 | 154 | } |
18244936 | 155 | |
18244936 | 156 | |
2ba41305 VZ |
157 | IMPLEMENT_READ_FOR_TYPE(String, wxString, const wxString&, ExpandEnvVars) |
158 | IMPLEMENT_READ_FOR_TYPE(Long, long, long, long) | |
2ba41305 VZ |
159 | IMPLEMENT_READ_FOR_TYPE(Double, double, double, double) |
160 | IMPLEMENT_READ_FOR_TYPE(Bool, bool, bool, bool) | |
f6bcfd97 | 161 | |
2ba41305 | 162 | #undef IMPLEMENT_READ_FOR_TYPE |
18244936 | 163 | |
3e1512cd VZ |
164 | // int is stored as long |
165 | bool wxConfigBase::Read(const wxString& key, int *pi) const | |
18244936 | 166 | { |
3e1512cd VZ |
167 | long l = *pi; |
168 | bool r = Read(key, &l); | |
169 | wxASSERT_MSG( l < INT_MAX, _T("int overflow in wxConfig::Read") ); | |
2ba41305 | 170 | *pi = (int)l; |
3e1512cd VZ |
171 | return r; |
172 | } | |
dfad0599 | 173 | |
3e1512cd VZ |
174 | bool wxConfigBase::Read(const wxString& key, int *pi, int defVal) const |
175 | { | |
176 | long l = *pi; | |
177 | bool r = Read(key, &l, defVal); | |
178 | wxASSERT_MSG( l < INT_MAX, _T("int overflow in wxConfig::Read") ); | |
179 | *pi = (int)l; | |
180 | return r; | |
dfad0599 JS |
181 | } |
182 | ||
3e1512cd VZ |
183 | // the DoReadXXX() for the other types have implementation in the base class |
184 | // but can be overridden in the derived ones | |
2ba41305 | 185 | bool wxConfigBase::DoReadBool(const wxString& key, bool* val) const |
dfad0599 | 186 | { |
c9d59ee7 | 187 | wxCHECK_MSG( val, false, _T("wxConfig::Read(): NULL parameter") ); |
2ba41305 | 188 | |
dfad0599 | 189 | long l; |
2ba41305 | 190 | if ( !DoReadLong(key, &l) ) |
c9d59ee7 | 191 | return false; |
dfad0599 | 192 | |
2ba41305 | 193 | wxASSERT_MSG( l == 0 || l == 1, _T("bad bool value in wxConfig::DoReadInt") ); |
855e92a8 | 194 | |
2ba41305 VZ |
195 | *val = l != 0; |
196 | ||
c9d59ee7 | 197 | return true; |
7c74e7fe | 198 | } |
1341230e | 199 | |
2ba41305 | 200 | bool wxConfigBase::DoReadDouble(const wxString& key, double* val) const |
68275b85 | 201 | { |
2ba41305 VZ |
202 | wxString str; |
203 | if ( Read(key, &str) ) | |
204 | { | |
205 | return str.ToDouble(val); | |
206 | } | |
207 | ||
c9d59ee7 | 208 | return false; |
68275b85 VZ |
209 | } |
210 | ||
2ba41305 | 211 | // string reading helper |
6164d85e | 212 | wxString wxConfigBase::ExpandEnvVars(const wxString& str) const |
8bbe427f VZ |
213 | { |
214 | wxString tmp; // Required for BC++ | |
215 | if (IsExpandingEnvVars()) | |
216 | tmp = wxExpandEnvVars(str); | |
217 | else | |
218 | tmp = str; | |
219 | return tmp; | |
220 | } | |
18244936 | 221 | |
2ba41305 VZ |
222 | // ---------------------------------------------------------------------------- |
223 | // wxConfigBase writing | |
224 | // ---------------------------------------------------------------------------- | |
225 | ||
226 | bool wxConfigBase::DoWriteDouble(const wxString& key, double val) | |
227 | { | |
228 | return DoWriteString(key, wxString::Format(_T("%g"), val)); | |
229 | } | |
230 | ||
2ba41305 VZ |
231 | bool wxConfigBase::DoWriteBool(const wxString& key, bool value) |
232 | { | |
233 | return DoWriteLong(key, value ? 1l : 0l); | |
234 | } | |
235 | ||
855e92a8 | 236 | // ---------------------------------------------------------------------------- |
18244936 | 237 | // wxConfigPathChanger |
855e92a8 VZ |
238 | // ---------------------------------------------------------------------------- |
239 | ||
18244936 | 240 | wxConfigPathChanger::wxConfigPathChanger(const wxConfigBase *pContainer, |
6f73419b | 241 | const wxString& strEntry) |
855e92a8 | 242 | { |
6f73419b | 243 | m_bChanged = false; |
5fe256de | 244 | m_pContainer = (wxConfigBase *)pContainer; |
08159082 VZ |
245 | |
246 | // the path is everything which precedes the last slash | |
898efd1a | 247 | wxString strPath = strEntry.BeforeLast(wxCONFIG_PATH_SEPARATOR); |
855e92a8 | 248 | |
08159082 | 249 | // except in the special case of "/keyname" when there is nothing before "/" |
b713f891 WS |
250 | if ( strPath.empty() && |
251 | ((!strEntry.empty()) && strEntry[0] == wxCONFIG_PATH_SEPARATOR) ) | |
08159082 | 252 | { |
da468d38 | 253 | strPath = wxCONFIG_PATH_SEPARATOR; |
08159082 | 254 | } |
855e92a8 | 255 | |
b713f891 | 256 | if ( !strPath.empty() ) |
18860312 VZ |
257 | { |
258 | if ( m_pContainer->GetPath() != strPath ) | |
259 | { | |
6f73419b | 260 | // we do change the path so restore it later |
18860312 | 261 | m_bChanged = true; |
b9a59c91 JS |
262 | |
263 | /* JACS: work around a memory bug that causes an assert | |
264 | when using wxRegConfig, related to reference-counting. | |
86501081 | 265 | Can be reproduced by removing .wc_str() below and |
b9a59c91 JS |
266 | adding the following code to the config sample OnInit under |
267 | Windows: | |
268 | ||
269 | pConfig->SetPath(wxT("MySettings")); | |
270 | pConfig->SetPath(wxT("..")); | |
271 | int value; | |
272 | pConfig->Read(_T("MainWindowX"), & value); | |
273 | */ | |
86501081 | 274 | m_strOldPath = m_pContainer->GetPath().wc_str(); |
18860312 VZ |
275 | if ( *m_strOldPath.c_str() != wxCONFIG_PATH_SEPARATOR ) |
276 | m_strOldPath += wxCONFIG_PATH_SEPARATOR; | |
277 | m_pContainer->SetPath(strPath); | |
278 | } | |
279 | ||
280 | // in any case, use the just the name, not full path | |
898efd1a | 281 | m_strName = strEntry.AfterLast(wxCONFIG_PATH_SEPARATOR); |
855e92a8 VZ |
282 | } |
283 | else { | |
284 | // it's a name only, without path - nothing to do | |
855e92a8 VZ |
285 | m_strName = strEntry; |
286 | } | |
287 | } | |
288 | ||
41f30152 VZ |
289 | void wxConfigPathChanger::UpdateIfDeleted() |
290 | { | |
291 | // we don't have to do anything at all if we didn't change the path | |
292 | if ( !m_bChanged ) | |
293 | return; | |
294 | ||
295 | // find the deepest still existing parent path of the original path | |
296 | while ( !m_pContainer->HasGroup(m_strOldPath) ) | |
297 | { | |
298 | m_strOldPath = m_strOldPath.BeforeLast(wxCONFIG_PATH_SEPARATOR); | |
299 | if ( m_strOldPath.empty() ) | |
300 | m_strOldPath = wxCONFIG_PATH_SEPARATOR; | |
301 | } | |
302 | } | |
303 | ||
18244936 | 304 | wxConfigPathChanger::~wxConfigPathChanger() |
855e92a8 VZ |
305 | { |
306 | // only restore path if it was changed | |
307 | if ( m_bChanged ) { | |
308 | m_pContainer->SetPath(m_strOldPath); | |
309 | } | |
310 | } | |
311 | ||
f559e621 VZ |
312 | // this is a wxConfig method but it's mainly used with wxConfigPathChanger |
313 | /* static */ | |
314 | wxString wxConfigBase::RemoveTrailingSeparator(const wxString& key) | |
315 | { | |
316 | wxString path(key); | |
317 | ||
318 | // don't remove the only separator from a root group path! | |
319 | while ( path.length() > 1 ) | |
320 | { | |
321 | if ( *path.rbegin() != wxCONFIG_PATH_SEPARATOR ) | |
322 | break; | |
323 | ||
324 | path.erase(path.end() - 1); | |
325 | } | |
326 | ||
327 | return path; | |
328 | } | |
329 | ||
21709999 JS |
330 | #endif // wxUSE_CONFIG |
331 | ||
c801d85f KB |
332 | // ---------------------------------------------------------------------------- |
333 | // static & global functions | |
334 | // ---------------------------------------------------------------------------- | |
335 | ||
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. | |
1fd87ff7 SN |
339 | |
340 | // don't change the values the enum elements: they must be equal | |
341 | // to the matching [closing] delimiter. | |
342 | enum Bracket | |
343 | { | |
344 | Bracket_None, | |
345 | Bracket_Normal = ')', | |
346 | Bracket_Curly = '}', | |
347 | #ifdef __WXMSW__ | |
348 | Bracket_Windows = '%', // yeah, Windows people are a bit strange ;-) | |
349 | #endif | |
350 | Bracket_Max | |
351 | }; | |
352 | ||
f40ccee9 | 353 | wxString wxExpandEnvVars(const wxString& str) |
c801d85f KB |
354 | { |
355 | wxString strResult; | |
aaa6d89a | 356 | strResult.Alloc(str.length()); |
c801d85f | 357 | |
c86f1403 | 358 | size_t m; |
aaa6d89a | 359 | for ( size_t n = 0; n < str.length(); n++ ) { |
c9f78968 | 360 | switch ( str[n].GetValue() ) { |
2049ba38 | 361 | #ifdef __WXMSW__ |
f6bcfd97 | 362 | case wxT('%'): |
c801d85f | 363 | #endif //WINDOWS |
f6bcfd97 | 364 | case wxT('$'): |
c801d85f KB |
365 | { |
366 | Bracket bracket; | |
2049ba38 | 367 | #ifdef __WXMSW__ |
f6bcfd97 | 368 | if ( str[n] == wxT('%') ) |
c801d85f KB |
369 | bracket = Bracket_Windows; |
370 | else | |
371 | #endif //WINDOWS | |
aaa6d89a | 372 | if ( n == str.length() - 1 ) { |
c801d85f KB |
373 | bracket = Bracket_None; |
374 | } | |
375 | else { | |
c9f78968 | 376 | switch ( str[n + 1].GetValue() ) { |
f6bcfd97 | 377 | case wxT('('): |
ba7f9a90 | 378 | bracket = Bracket_Normal; |
c801d85f KB |
379 | n++; // skip the bracket |
380 | break; | |
381 | ||
f6bcfd97 | 382 | case wxT('{'): |
c801d85f KB |
383 | bracket = Bracket_Curly; |
384 | n++; // skip the bracket | |
385 | break; | |
386 | ||
387 | default: | |
388 | bracket = Bracket_None; | |
389 | } | |
390 | } | |
391 | ||
392 | m = n + 1; | |
393 | ||
aaa6d89a | 394 | while ( m < str.length() && (wxIsalnum(str[m]) || str[m] == wxT('_')) ) |
c801d85f | 395 | m++; |
1c6c52fd | 396 | |
c801d85f KB |
397 | wxString strVarName(str.c_str() + n + 1, m - n - 1); |
398 | ||
1c193821 | 399 | #ifdef __WXWINCE__ |
86501081 | 400 | const bool expanded = false; |
1c193821 | 401 | #else |
bf7f7793 RR |
402 | // NB: use wxGetEnv instead of wxGetenv as otherwise variables |
403 | // set through wxSetEnv may not be read correctly! | |
86501081 | 404 | bool expanded = false; |
bf7f7793 RR |
405 | wxString tmp; |
406 | if (wxGetEnv(strVarName, &tmp)) | |
86501081 VS |
407 | { |
408 | strResult += tmp; | |
409 | expanded = true; | |
c801d85f | 410 | } |
86501081 VS |
411 | else |
412 | #endif | |
413 | { | |
c801d85f | 414 | // variable doesn't exist => don't change anything |
2049ba38 | 415 | #ifdef __WXMSW__ |
c801d85f KB |
416 | if ( bracket != Bracket_Windows ) |
417 | #endif | |
418 | if ( bracket != Bracket_None ) | |
419 | strResult << str[n - 1]; | |
420 | strResult << str[n] << strVarName; | |
421 | } | |
422 | ||
423 | // check the closing bracket | |
424 | if ( bracket != Bracket_None ) { | |
aaa6d89a | 425 | if ( m == str.length() || str[m] != (wxChar)bracket ) { |
2b5f62a0 VZ |
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 | |
429 | // | |
430 | // under Unix, OTOH, this warning could be useful for the user to | |
431 | // understand why isn't the variable expanded as intended | |
432 | #ifndef __WXMSW__ | |
c6d60564 RN |
433 | wxLogWarning(_("Environment variables expansion failed: missing '%c' at position %u in '%s'."), |
434 | (char)bracket, (unsigned int) (m + 1), str.c_str()); | |
2b5f62a0 | 435 | #endif // __WXMSW__ |
c801d85f KB |
436 | } |
437 | else { | |
906373bf | 438 | // skip closing bracket unless the variables wasn't expanded |
86501081 | 439 | if ( !expanded ) |
f6eaf52a | 440 | strResult << (wxChar)bracket; |
c801d85f KB |
441 | m++; |
442 | } | |
443 | } | |
444 | ||
445 | n = m - 1; // skip variable name | |
446 | } | |
447 | break; | |
448 | ||
c9f78968 | 449 | case wxT('\\'): |
906373bf | 450 | // backslash can be used to suppress special meaning of % and $ |
aaa6d89a | 451 | if ( n != str.length() - 1 && |
b11dd163 | 452 | (str[n + 1] == wxT('%') || str[n + 1] == wxT('$')) ) { |
906373bf VZ |
453 | strResult += str[++n]; |
454 | ||
455 | break; | |
456 | } | |
457 | //else: fall through | |
458 | ||
c801d85f KB |
459 | default: |
460 | strResult += str[n]; | |
461 | } | |
462 | } | |
463 | ||
464 | return strResult; | |
465 | } | |
855e92a8 VZ |
466 | |
467 | // this function is used to properly interpret '..' in path | |
86501081 | 468 | void wxSplitPath(wxArrayString& aParts, const wxString& path) |
855e92a8 | 469 | { |
df5168c4 | 470 | aParts.clear(); |
855e92a8 VZ |
471 | |
472 | wxString strCurrent; | |
86501081 | 473 | wxString::const_iterator pc = path.begin(); |
855e92a8 | 474 | for ( ;; ) { |
86501081 | 475 | if ( pc == path.end() || *pc == wxCONFIG_PATH_SEPARATOR ) { |
223d09f6 | 476 | if ( strCurrent == wxT(".") ) { |
855e92a8 VZ |
477 | // ignore |
478 | } | |
223d09f6 | 479 | else if ( strCurrent == wxT("..") ) { |
855e92a8 | 480 | // go up one level |
df5168c4 | 481 | if ( aParts.size() == 0 ) |
86501081 | 482 | wxLogWarning(_("'%s' has extra '..', ignored."), path); |
855e92a8 | 483 | else |
df5168c4 | 484 | aParts.erase(aParts.end() - 1); |
937d4f4d VZ |
485 | |
486 | strCurrent.Empty(); | |
855e92a8 | 487 | } |
b713f891 | 488 | else if ( !strCurrent.empty() ) { |
df5168c4 | 489 | aParts.push_back(strCurrent); |
855e92a8 VZ |
490 | strCurrent.Empty(); |
491 | } | |
492 | //else: | |
493 | // could log an error here, but we prefer to ignore extra '/' | |
494 | ||
86501081 | 495 | if ( pc == path.end() ) |
f6bcfd97 | 496 | break; |
855e92a8 VZ |
497 | } |
498 | else | |
499 | strCurrent += *pc; | |
500 | ||
86501081 | 501 | ++pc; |
855e92a8 VZ |
502 | } |
503 | } |