]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/common/config.cpp | |
3 | // Purpose: implementation of wxConfigBase class | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 07.04.98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1997 Karsten Ballueder Ballueder@usa.net | |
9 | // Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
10 | // Licence: wxWindows licence | |
11 | /////////////////////////////////////////////////////////////////////////////// | |
12 | ||
13 | // ---------------------------------------------------------------------------- | |
14 | // headers | |
15 | // ---------------------------------------------------------------------------- | |
16 | ||
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif //__BORLANDC__ | |
22 | ||
23 | #ifndef wxUSE_CONFIG_NATIVE | |
24 | #define wxUSE_CONFIG_NATIVE 1 | |
25 | #endif | |
26 | ||
27 | #include "wx/config.h" | |
28 | ||
29 | #ifndef WX_PRECOMP | |
30 | #include "wx/intl.h" | |
31 | #include "wx/log.h" | |
32 | #include "wx/app.h" | |
33 | #include "wx/utils.h" | |
34 | #include "wx/arrstr.h" | |
35 | #include "wx/math.h" | |
36 | #endif //WX_PRECOMP | |
37 | ||
38 | #if wxUSE_CONFIG && ((wxUSE_FILE && wxUSE_TEXTFILE) || wxUSE_CONFIG_NATIVE) | |
39 | ||
40 | #include "wx/apptrait.h" | |
41 | #include "wx/file.h" | |
42 | ||
43 | #include <stdlib.h> | |
44 | #include <ctype.h> | |
45 | #include <limits.h> // for INT_MAX | |
46 | #include <float.h> // for FLT_MAX | |
47 | ||
48 | // ---------------------------------------------------------------------------- | |
49 | // global and class static variables | |
50 | // ---------------------------------------------------------------------------- | |
51 | ||
52 | wxConfigBase *wxConfigBase::ms_pConfig = NULL; | |
53 | bool wxConfigBase::ms_bAutoCreate = true; | |
54 | ||
55 | // ============================================================================ | |
56 | // implementation | |
57 | // ============================================================================ | |
58 | ||
59 | // ---------------------------------------------------------------------------- | |
60 | // wxAppTraitsBase | |
61 | // ---------------------------------------------------------------------------- | |
62 | ||
63 | wxConfigBase *wxAppTraitsBase::CreateConfig() | |
64 | { | |
65 | return new | |
66 | #if defined(__WXMSW__) && wxUSE_CONFIG_NATIVE | |
67 | wxRegConfig(wxTheApp->GetAppName(), wxTheApp->GetVendorName()); | |
68 | #elif defined(__WXPALMOS__) && wxUSE_CONFIG_NATIVE | |
69 | wxPrefConfig(wxTheApp->GetAppName()); | |
70 | #else // either we're under Unix or wish to use files even under Windows | |
71 | wxFileConfig(wxTheApp->GetAppName()); | |
72 | #endif | |
73 | } | |
74 | ||
75 | // ---------------------------------------------------------------------------- | |
76 | // wxConfigBase | |
77 | // ---------------------------------------------------------------------------- | |
78 | IMPLEMENT_ABSTRACT_CLASS(wxConfigBase, wxObject) | |
79 | ||
80 | // Not all args will always be used by derived classes, but including them all | |
81 | // in each class ensures compatibility. | |
82 | wxConfigBase::wxConfigBase(const wxString& appName, | |
83 | const wxString& vendorName, | |
84 | const wxString& WXUNUSED(localFilename), | |
85 | const wxString& WXUNUSED(globalFilename), | |
86 | long style) | |
87 | : m_appName(appName), m_vendorName(vendorName), m_style(style) | |
88 | { | |
89 | m_bExpandEnvVars = true; | |
90 | m_bRecordDefaults = false; | |
91 | } | |
92 | ||
93 | wxConfigBase::~wxConfigBase() | |
94 | { | |
95 | // required here for Darwin | |
96 | } | |
97 | ||
98 | wxConfigBase *wxConfigBase::Set(wxConfigBase *pConfig) | |
99 | { | |
100 | wxConfigBase *pOld = ms_pConfig; | |
101 | ms_pConfig = pConfig; | |
102 | return pOld; | |
103 | } | |
104 | ||
105 | wxConfigBase *wxConfigBase::Create() | |
106 | { | |
107 | if ( ms_bAutoCreate && ms_pConfig == NULL ) { | |
108 | wxAppTraits * const traits = wxTheApp ? wxTheApp->GetTraits() : NULL; | |
109 | wxCHECK_MSG( traits, NULL, wxT("create wxApp before calling this") ); | |
110 | ||
111 | ms_pConfig = traits->CreateConfig(); | |
112 | } | |
113 | ||
114 | return ms_pConfig; | |
115 | } | |
116 | ||
117 | // ---------------------------------------------------------------------------- | |
118 | // wxConfigBase reading entries | |
119 | // ---------------------------------------------------------------------------- | |
120 | ||
121 | // implement both Read() overloads for the given type in terms of DoRead() | |
122 | #define IMPLEMENT_READ_FOR_TYPE(name, type, deftype, extra) \ | |
123 | bool wxConfigBase::Read(const wxString& key, type *val) const \ | |
124 | { \ | |
125 | wxCHECK_MSG( val, false, wxT("wxConfig::Read(): NULL parameter") ); \ | |
126 | \ | |
127 | if ( !DoRead##name(key, val) ) \ | |
128 | return false; \ | |
129 | \ | |
130 | *val = extra(*val); \ | |
131 | \ | |
132 | return true; \ | |
133 | } \ | |
134 | \ | |
135 | bool wxConfigBase::Read(const wxString& key, \ | |
136 | type *val, \ | |
137 | deftype defVal) const \ | |
138 | { \ | |
139 | wxCHECK_MSG( val, false, wxT("wxConfig::Read(): NULL parameter") ); \ | |
140 | \ | |
141 | bool read = DoRead##name(key, val); \ | |
142 | if ( !read ) \ | |
143 | { \ | |
144 | if ( IsRecordingDefaults() ) \ | |
145 | { \ | |
146 | ((wxConfigBase *)this)->DoWrite##name(key, defVal); \ | |
147 | } \ | |
148 | \ | |
149 | *val = defVal; \ | |
150 | } \ | |
151 | \ | |
152 | *val = extra(*val); \ | |
153 | \ | |
154 | return read; \ | |
155 | } | |
156 | ||
157 | ||
158 | IMPLEMENT_READ_FOR_TYPE(String, wxString, const wxString&, ExpandEnvVars) | |
159 | IMPLEMENT_READ_FOR_TYPE(Long, long, long, long) | |
160 | IMPLEMENT_READ_FOR_TYPE(Double, double, double, double) | |
161 | IMPLEMENT_READ_FOR_TYPE(Bool, bool, bool, bool) | |
162 | ||
163 | #undef IMPLEMENT_READ_FOR_TYPE | |
164 | ||
165 | // int is stored as long | |
166 | bool wxConfigBase::Read(const wxString& key, int *pi) const | |
167 | { | |
168 | long l = *pi; | |
169 | bool r = Read(key, &l); | |
170 | wxASSERT_MSG( l < INT_MAX, wxT("int overflow in wxConfig::Read") ); | |
171 | *pi = (int)l; | |
172 | return r; | |
173 | } | |
174 | ||
175 | bool wxConfigBase::Read(const wxString& key, int *pi, int defVal) const | |
176 | { | |
177 | long l = *pi; | |
178 | bool r = Read(key, &l, defVal); | |
179 | wxASSERT_MSG( l < INT_MAX, wxT("int overflow in wxConfig::Read") ); | |
180 | *pi = (int)l; | |
181 | return r; | |
182 | } | |
183 | ||
184 | // Read floats as doubles then just type cast it down. | |
185 | bool wxConfigBase::Read(const wxString& key, float* val) const | |
186 | { | |
187 | wxCHECK_MSG( val, false, wxT("wxConfig::Read(): NULL parameter") ); | |
188 | ||
189 | double temp; | |
190 | if ( !Read(key, &temp) ) | |
191 | return false; | |
192 | ||
193 | wxCHECK_MSG( fabs(temp) <= FLT_MAX, false, | |
194 | wxT("float overflow in wxConfig::Read") ); | |
195 | wxCHECK_MSG( (temp == 0.0) || (fabs(temp) >= FLT_MIN), false, | |
196 | wxT("float underflow in wxConfig::Read") ); | |
197 | ||
198 | *val = static_cast<float>(temp); | |
199 | ||
200 | return true; | |
201 | } | |
202 | ||
203 | bool wxConfigBase::Read(const wxString& key, float* val, float defVal) const | |
204 | { | |
205 | wxCHECK_MSG( val, false, wxT("wxConfig::Read(): NULL parameter") ); | |
206 | ||
207 | if ( Read(key, val) ) | |
208 | return true; | |
209 | ||
210 | *val = defVal; | |
211 | return false; | |
212 | } | |
213 | ||
214 | // the DoReadXXX() for the other types have implementation in the base class | |
215 | // but can be overridden in the derived ones | |
216 | bool wxConfigBase::DoReadBool(const wxString& key, bool* val) const | |
217 | { | |
218 | wxCHECK_MSG( val, false, wxT("wxConfig::Read(): NULL parameter") ); | |
219 | ||
220 | long l; | |
221 | if ( !DoReadLong(key, &l) ) | |
222 | return false; | |
223 | ||
224 | if ( l != 0 && l != 1 ) | |
225 | { | |
226 | // Don't assert here as this could happen in the result of user editing | |
227 | // the file directly and this not indicate a bug in the program but | |
228 | // still complain that something is wrong. | |
229 | wxLogWarning(_("Invalid value %ld for a boolean key \"%s\" in " | |
230 | "config file."), | |
231 | l, key); | |
232 | } | |
233 | ||
234 | *val = l != 0; | |
235 | ||
236 | return true; | |
237 | } | |
238 | ||
239 | bool wxConfigBase::DoReadDouble(const wxString& key, double* val) const | |
240 | { | |
241 | wxString str; | |
242 | if ( Read(key, &str) ) | |
243 | { | |
244 | if ( str.ToCDouble(val) ) | |
245 | return true; | |
246 | ||
247 | // Previous versions of wxFileConfig wrote the numbers out using the | |
248 | // current locale and not the C one as now, so attempt to parse the | |
249 | // string as a number in the current locale too, for compatibility. | |
250 | if ( str.ToDouble(val) ) | |
251 | return true; | |
252 | } | |
253 | ||
254 | return false; | |
255 | } | |
256 | ||
257 | // string reading helper | |
258 | wxString wxConfigBase::ExpandEnvVars(const wxString& str) const | |
259 | { | |
260 | wxString tmp; // Required for BC++ | |
261 | if (IsExpandingEnvVars()) | |
262 | tmp = wxExpandEnvVars(str); | |
263 | else | |
264 | tmp = str; | |
265 | return tmp; | |
266 | } | |
267 | ||
268 | // ---------------------------------------------------------------------------- | |
269 | // wxConfigBase writing | |
270 | // ---------------------------------------------------------------------------- | |
271 | ||
272 | bool wxConfigBase::DoWriteDouble(const wxString& key, double val) | |
273 | { | |
274 | // Notice that we always write out the numbers in C locale and not the | |
275 | // current one. This makes the config files portable between machines using | |
276 | // different locales. | |
277 | return DoWriteString(key, wxString::FromCDouble(val)); | |
278 | } | |
279 | ||
280 | bool wxConfigBase::DoWriteBool(const wxString& key, bool value) | |
281 | { | |
282 | return DoWriteLong(key, value ? 1l : 0l); | |
283 | } | |
284 | ||
285 | // ---------------------------------------------------------------------------- | |
286 | // wxConfigPathChanger | |
287 | // ---------------------------------------------------------------------------- | |
288 | ||
289 | wxConfigPathChanger::wxConfigPathChanger(const wxConfigBase *pContainer, | |
290 | const wxString& strEntry) | |
291 | { | |
292 | m_bChanged = false; | |
293 | m_pContainer = const_cast<wxConfigBase *>(pContainer); | |
294 | ||
295 | // the path is everything which precedes the last slash and the name is | |
296 | // everything after it -- and this works correctly if there is no slash too | |
297 | wxString strPath = strEntry.BeforeLast(wxCONFIG_PATH_SEPARATOR, &m_strName); | |
298 | ||
299 | // except in the special case of "/keyname" when there is nothing before "/" | |
300 | if ( strPath.empty() && | |
301 | ((!strEntry.empty()) && strEntry[0] == wxCONFIG_PATH_SEPARATOR) ) | |
302 | { | |
303 | strPath = wxCONFIG_PATH_SEPARATOR; | |
304 | } | |
305 | ||
306 | if ( !strPath.empty() ) | |
307 | { | |
308 | if ( m_pContainer->GetPath() != strPath ) | |
309 | { | |
310 | // we do change the path so restore it later | |
311 | m_bChanged = true; | |
312 | ||
313 | /* JACS: work around a memory bug that causes an assert | |
314 | when using wxRegConfig, related to reference-counting. | |
315 | Can be reproduced by removing .wc_str() below and | |
316 | adding the following code to the config sample OnInit under | |
317 | Windows: | |
318 | ||
319 | pConfig->SetPath(wxT("MySettings")); | |
320 | pConfig->SetPath(wxT("..")); | |
321 | int value; | |
322 | pConfig->Read(wxT("MainWindowX"), & value); | |
323 | */ | |
324 | m_strOldPath = m_pContainer->GetPath().wc_str(); | |
325 | if ( *m_strOldPath.c_str() != wxCONFIG_PATH_SEPARATOR ) | |
326 | m_strOldPath += wxCONFIG_PATH_SEPARATOR; | |
327 | m_pContainer->SetPath(strPath); | |
328 | } | |
329 | } | |
330 | } | |
331 | ||
332 | void wxConfigPathChanger::UpdateIfDeleted() | |
333 | { | |
334 | // we don't have to do anything at all if we didn't change the path | |
335 | if ( !m_bChanged ) | |
336 | return; | |
337 | ||
338 | // find the deepest still existing parent path of the original path | |
339 | while ( !m_pContainer->HasGroup(m_strOldPath) ) | |
340 | { | |
341 | m_strOldPath = m_strOldPath.BeforeLast(wxCONFIG_PATH_SEPARATOR); | |
342 | if ( m_strOldPath.empty() ) | |
343 | m_strOldPath = wxCONFIG_PATH_SEPARATOR; | |
344 | } | |
345 | } | |
346 | ||
347 | wxConfigPathChanger::~wxConfigPathChanger() | |
348 | { | |
349 | // only restore path if it was changed | |
350 | if ( m_bChanged ) { | |
351 | m_pContainer->SetPath(m_strOldPath); | |
352 | } | |
353 | } | |
354 | ||
355 | // this is a wxConfig method but it's mainly used with wxConfigPathChanger | |
356 | /* static */ | |
357 | wxString wxConfigBase::RemoveTrailingSeparator(const wxString& key) | |
358 | { | |
359 | wxString path(key); | |
360 | ||
361 | // don't remove the only separator from a root group path! | |
362 | while ( path.length() > 1 ) | |
363 | { | |
364 | if ( *path.rbegin() != wxCONFIG_PATH_SEPARATOR ) | |
365 | break; | |
366 | ||
367 | path.erase(path.end() - 1); | |
368 | } | |
369 | ||
370 | return path; | |
371 | } | |
372 | ||
373 | #endif // wxUSE_CONFIG | |
374 | ||
375 | // ---------------------------------------------------------------------------- | |
376 | // static & global functions | |
377 | // ---------------------------------------------------------------------------- | |
378 | ||
379 | // understands both Unix and Windows (but only under Windows) environment | |
380 | // variables expansion: i.e. $var, $(var) and ${var} are always understood | |
381 | // and in addition under Windows %var% is also. | |
382 | ||
383 | // don't change the values the enum elements: they must be equal | |
384 | // to the matching [closing] delimiter. | |
385 | enum Bracket | |
386 | { | |
387 | Bracket_None, | |
388 | Bracket_Normal = ')', | |
389 | Bracket_Curly = '}', | |
390 | #ifdef __WXMSW__ | |
391 | Bracket_Windows = '%', // yeah, Windows people are a bit strange ;-) | |
392 | #endif | |
393 | Bracket_Max | |
394 | }; | |
395 | ||
396 | wxString wxExpandEnvVars(const wxString& str) | |
397 | { | |
398 | wxString strResult; | |
399 | strResult.Alloc(str.length()); | |
400 | ||
401 | size_t m; | |
402 | for ( size_t n = 0; n < str.length(); n++ ) { | |
403 | switch ( str[n].GetValue() ) { | |
404 | #ifdef __WXMSW__ | |
405 | case wxT('%'): | |
406 | #endif //WINDOWS | |
407 | case wxT('$'): | |
408 | { | |
409 | Bracket bracket; | |
410 | #ifdef __WXMSW__ | |
411 | if ( str[n] == wxT('%') ) | |
412 | bracket = Bracket_Windows; | |
413 | else | |
414 | #endif //WINDOWS | |
415 | if ( n == str.length() - 1 ) { | |
416 | bracket = Bracket_None; | |
417 | } | |
418 | else { | |
419 | switch ( str[n + 1].GetValue() ) { | |
420 | case wxT('('): | |
421 | bracket = Bracket_Normal; | |
422 | n++; // skip the bracket | |
423 | break; | |
424 | ||
425 | case wxT('{'): | |
426 | bracket = Bracket_Curly; | |
427 | n++; // skip the bracket | |
428 | break; | |
429 | ||
430 | default: | |
431 | bracket = Bracket_None; | |
432 | } | |
433 | } | |
434 | ||
435 | m = n + 1; | |
436 | ||
437 | while ( m < str.length() && (wxIsalnum(str[m]) || str[m] == wxT('_')) ) | |
438 | m++; | |
439 | ||
440 | wxString strVarName(str.c_str() + n + 1, m - n - 1); | |
441 | ||
442 | #ifdef __WXWINCE__ | |
443 | const bool expanded = false; | |
444 | #else | |
445 | // NB: use wxGetEnv instead of wxGetenv as otherwise variables | |
446 | // set through wxSetEnv may not be read correctly! | |
447 | bool expanded = false; | |
448 | wxString tmp; | |
449 | if (wxGetEnv(strVarName, &tmp)) | |
450 | { | |
451 | strResult += tmp; | |
452 | expanded = true; | |
453 | } | |
454 | else | |
455 | #endif | |
456 | { | |
457 | // variable doesn't exist => don't change anything | |
458 | #ifdef __WXMSW__ | |
459 | if ( bracket != Bracket_Windows ) | |
460 | #endif | |
461 | if ( bracket != Bracket_None ) | |
462 | strResult << str[n - 1]; | |
463 | strResult << str[n] << strVarName; | |
464 | } | |
465 | ||
466 | // check the closing bracket | |
467 | if ( bracket != Bracket_None ) { | |
468 | if ( m == str.length() || str[m] != (wxChar)bracket ) { | |
469 | // under MSW it's common to have '%' characters in the registry | |
470 | // and it's annoying to have warnings about them each time, so | |
471 | // ignroe them silently if they are not used for env vars | |
472 | // | |
473 | // under Unix, OTOH, this warning could be useful for the user to | |
474 | // understand why isn't the variable expanded as intended | |
475 | #ifndef __WXMSW__ | |
476 | wxLogWarning(_("Environment variables expansion failed: missing '%c' at position %u in '%s'."), | |
477 | (char)bracket, (unsigned int) (m + 1), str.c_str()); | |
478 | #endif // __WXMSW__ | |
479 | } | |
480 | else { | |
481 | // skip closing bracket unless the variables wasn't expanded | |
482 | if ( !expanded ) | |
483 | strResult << (wxChar)bracket; | |
484 | m++; | |
485 | } | |
486 | } | |
487 | ||
488 | n = m - 1; // skip variable name | |
489 | } | |
490 | break; | |
491 | ||
492 | case wxT('\\'): | |
493 | // backslash can be used to suppress special meaning of % and $ | |
494 | if ( n != str.length() - 1 && | |
495 | (str[n + 1] == wxT('%') || str[n + 1] == wxT('$')) ) { | |
496 | strResult += str[++n]; | |
497 | ||
498 | break; | |
499 | } | |
500 | //else: fall through | |
501 | ||
502 | default: | |
503 | strResult += str[n]; | |
504 | } | |
505 | } | |
506 | ||
507 | return strResult; | |
508 | } | |
509 | ||
510 | // this function is used to properly interpret '..' in path | |
511 | void wxSplitPath(wxArrayString& aParts, const wxString& path) | |
512 | { | |
513 | aParts.clear(); | |
514 | ||
515 | wxString strCurrent; | |
516 | wxString::const_iterator pc = path.begin(); | |
517 | for ( ;; ) { | |
518 | if ( pc == path.end() || *pc == wxCONFIG_PATH_SEPARATOR ) { | |
519 | if ( strCurrent == wxT(".") ) { | |
520 | // ignore | |
521 | } | |
522 | else if ( strCurrent == wxT("..") ) { | |
523 | // go up one level | |
524 | if ( aParts.size() == 0 ) | |
525 | { | |
526 | wxLogWarning(_("'%s' has extra '..', ignored."), path); | |
527 | } | |
528 | else | |
529 | { | |
530 | aParts.erase(aParts.end() - 1); | |
531 | } | |
532 | ||
533 | strCurrent.Empty(); | |
534 | } | |
535 | else if ( !strCurrent.empty() ) { | |
536 | aParts.push_back(strCurrent); | |
537 | strCurrent.Empty(); | |
538 | } | |
539 | //else: | |
540 | // could log an error here, but we prefer to ignore extra '/' | |
541 | ||
542 | if ( pc == path.end() ) | |
543 | break; | |
544 | } | |
545 | else | |
546 | strCurrent += *pc; | |
547 | ||
548 | ++pc; | |
549 | } | |
550 | } |