]> git.saurik.com Git - wxWidgets.git/blob - src/common/config.cpp
More key event work. Added SetDetectableAutoRepeat.
[wxWidgets.git] / src / common / config.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: 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 Ballüder Ballueder@usa.net
9 // Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
10 // Licence: wxWindows license
11 ///////////////////////////////////////////////////////////////////////////////
12
13 // ----------------------------------------------------------------------------
14 // headers
15 // ----------------------------------------------------------------------------
16 #ifdef __GNUG__
17 #pragma implementation "confbase.h"
18 #endif
19
20 #include "wx/wxprec.h"
21
22 #ifndef WX_PRECOMP
23 #include "wx/wx.h"
24 #endif
25
26 #if wxUSE_CONFIG
27
28 #ifdef __BORLANDC__
29 #pragma hdrstop
30 #endif //__BORLANDC__
31
32 #include "wx/app.h"
33 #include "wx/file.h"
34 #include "wx/log.h"
35 #include "wx/textfile.h"
36 #include "wx/utils.h"
37
38 #include "wx/config.h"
39
40 #include <stdlib.h>
41 #include <math.h>
42 #include <ctype.h> // for isalnum()
43
44 // ----------------------------------------------------------------------------
45 // global and class static variables
46 // ----------------------------------------------------------------------------
47
48 wxConfigBase *wxConfigBase::ms_pConfig = NULL;
49 bool wxConfigBase::ms_bAutoCreate = TRUE;
50
51 // ============================================================================
52 // implementation
53 // ============================================================================
54
55 // ----------------------------------------------------------------------------
56 // wxConfigBase
57 // ----------------------------------------------------------------------------
58
59 // Not all args will always be used by derived classes, but
60 // including them all in each class ensures compatibility.
61 wxConfigBase::wxConfigBase(const wxString& appName, const wxString& vendorName,
62 const wxString& WXUNUSED(localFilename), const wxString& WXUNUSED(globalFilename), long style):
63 m_appName(appName), m_vendorName(vendorName), m_style(style)
64 {
65 m_bExpandEnvVars = TRUE; m_bRecordDefaults = FALSE;
66 }
67
68 wxConfigBase *wxConfigBase::Set(wxConfigBase *pConfig)
69 {
70 wxConfigBase *pOld = ms_pConfig;
71 ms_pConfig = pConfig;
72 return pOld;
73 }
74
75 wxConfigBase *wxConfigBase::Create()
76 {
77 if ( ms_bAutoCreate && ms_pConfig == NULL ) {
78 ms_pConfig =
79 #if defined(__WXMSW__) && defined(wxCONFIG_WIN32_NATIVE)
80 #ifdef __WIN32__
81 new wxRegConfig(wxTheApp->GetAppName(), wxTheApp->GetVendorName());
82 #else //WIN16
83 new wxIniConfig(wxTheApp->GetAppName(), wxTheApp->GetVendorName());
84 #endif
85 #else // either we're under Unix or wish to use files even under Windows
86 new wxFileConfig(wxTheApp->GetAppName());
87 #endif
88 }
89
90 return ms_pConfig;
91 }
92
93 wxString wxConfigBase::Read(const wxString& key, const wxString& defVal) const
94 {
95 wxString s;
96 Read(key, &s, defVal);
97 return s;
98 }
99
100 bool wxConfigBase::Read(const wxString& key, wxString *str, const wxString& defVal) const
101 {
102 if (!Read(key, str))
103 {
104 *str = ExpandEnvVars(defVal);
105 return FALSE;
106 }
107 else
108 return TRUE;
109 }
110
111 bool wxConfigBase::Read(const wxString& key, long *pl, long defVal) const
112 {
113 if (!Read(key, pl))
114 {
115 *pl = defVal;
116 return FALSE;
117 }
118 else
119 return TRUE;
120 }
121
122 bool wxConfigBase::Read(const wxString& key, double* val) const
123 {
124 wxString str;
125 if (Read(key, & str))
126 {
127 *val = wxAtof(str);
128 return TRUE;
129 }
130 else
131 return FALSE;
132 }
133
134 bool wxConfigBase::Read(const wxString& key, double* val, double defVal) const
135 {
136 if (!Read(key, val))
137 {
138 *val = defVal;
139 return FALSE;
140 }
141 else
142 return TRUE;
143 }
144
145 bool wxConfigBase::Read(const wxString& key, bool* val) const
146 {
147 long l;
148 if (Read(key, & l))
149 {
150 *val = (l != 0);
151 return TRUE;
152 }
153 else
154 return FALSE;
155 }
156
157 bool wxConfigBase::Read(const wxString& key, bool* val, bool defVal) const
158 {
159 if (!Read(key, val))
160 {
161 *val = defVal;
162 return FALSE;
163 }
164 else
165 return TRUE;
166 }
167
168 // Convenience functions
169
170 bool wxConfigBase::Read(const wxString& key, int *pi) const
171 {
172 long l;
173 bool ret = Read(key, &l);
174 if (ret)
175 *pi = (int) l;
176 return ret;
177 }
178
179 bool wxConfigBase::Read(const wxString& key, int *pi, int defVal) const
180 {
181 long l;
182 bool ret = Read(key, &l, (long) defVal);
183 *pi = (int) l;
184 return ret;
185 }
186
187 bool wxConfigBase::Write(const wxString& key, double val)
188 {
189 wxString str;
190 str.Printf(_T("%f"), val);
191 return Write(key, str);
192 }
193
194 bool wxConfigBase::Write(const wxString& key, bool value)
195 {
196 long l = (value ? 1 : 0);
197 return Write(key, l);
198 }
199
200 wxString wxConfigBase::ExpandEnvVars(const wxString& str) const
201 {
202 wxString tmp; // Required for BC++
203 if (IsExpandingEnvVars())
204 tmp = wxExpandEnvVars(str);
205 else
206 tmp = str;
207 return tmp;
208 }
209
210 // ----------------------------------------------------------------------------
211 // wxConfigPathChanger
212 // ----------------------------------------------------------------------------
213
214 wxConfigPathChanger::wxConfigPathChanger(const wxConfigBase *pContainer,
215 const wxString& strEntry)
216 {
217 m_pContainer = (wxConfigBase *)pContainer;
218
219 // the path is everything which precedes the last slash
220 wxString strPath = strEntry.BeforeLast(wxCONFIG_PATH_SEPARATOR);
221
222 // except in the special case of "/keyname" when there is nothing before "/"
223 if ( strPath.IsEmpty() &&
224 ((!strEntry.IsEmpty()) && strEntry[0] == wxCONFIG_PATH_SEPARATOR) )
225 {
226 strPath = wxCONFIG_PATH_SEPARATOR;
227 }
228
229 if ( !strPath.IsEmpty() ) {
230 // do change the path
231 m_bChanged = TRUE;
232 m_strName = strEntry.AfterLast(wxCONFIG_PATH_SEPARATOR);
233 m_strOldPath = m_pContainer->GetPath();
234 m_strOldPath += wxCONFIG_PATH_SEPARATOR;
235 m_pContainer->SetPath(strPath);
236 }
237 else {
238 // it's a name only, without path - nothing to do
239 m_bChanged = FALSE;
240 m_strName = strEntry;
241 }
242 }
243
244 wxConfigPathChanger::~wxConfigPathChanger()
245 {
246 // only restore path if it was changed
247 if ( m_bChanged ) {
248 m_pContainer->SetPath(m_strOldPath);
249 }
250 }
251
252 // ----------------------------------------------------------------------------
253 // static & global functions
254 // ----------------------------------------------------------------------------
255
256 // understands both Unix and Windows (but only under Windows) environment
257 // variables expansion: i.e. $var, $(var) and ${var} are always understood
258 // and in addition under Windows %var% is also.
259 wxString wxExpandEnvVars(const wxString& str)
260 {
261 wxString strResult;
262 strResult.Alloc(str.Len());
263
264 // don't change the values the enum elements: they must be equal
265 // to the matching [closing] delimiter.
266 enum Bracket
267 {
268 Bracket_None,
269 Bracket_Normal = ')',
270 Bracket_Curly = '}'
271 #ifdef __WXMSW__
272 ,Bracket_Windows = '%' // yeah, Windows people are a bit strange ;-)
273 #endif
274 };
275
276 size_t m;
277 for ( size_t n = 0; n < str.Len(); n++ ) {
278 switch ( str[n] ) {
279 #ifdef __WXMSW__
280 case '%':
281 #endif //WINDOWS
282 case '$':
283 {
284 Bracket bracket;
285 #ifdef __WXMSW__
286 if ( str[n] == '%' )
287 bracket = Bracket_Windows;
288 else
289 #endif //WINDOWS
290 if ( n == str.Len() - 1 ) {
291 bracket = Bracket_None;
292 }
293 else {
294 switch ( str[n + 1] ) {
295 case '(':
296 bracket = Bracket_Normal;
297 n++; // skip the bracket
298 break;
299
300 case '{':
301 bracket = Bracket_Curly;
302 n++; // skip the bracket
303 break;
304
305 default:
306 bracket = Bracket_None;
307 }
308 }
309
310 m = n + 1;
311
312 while ( m < str.Len() && (isalnum(str[m]) || str[m] == '_') )
313 m++;
314
315 wxString strVarName(str.c_str() + n + 1, m - n - 1);
316
317 const wxChar *pszValue = wxGetenv(strVarName);
318 if ( pszValue != NULL ) {
319 strResult += pszValue;
320 }
321 else {
322 // variable doesn't exist => don't change anything
323 #ifdef __WXMSW__
324 if ( bracket != Bracket_Windows )
325 #endif
326 if ( bracket != Bracket_None )
327 strResult << str[n - 1];
328 strResult << str[n] << strVarName;
329 }
330
331 // check the closing bracket
332 if ( bracket != Bracket_None ) {
333 if ( m == str.Len() || str[m] != (char)bracket ) {
334 wxLogWarning(_("Environment variables expansion failed: "
335 "missing '%c' at position %d in '%s'."),
336 (char)bracket, m + 1, str.c_str());
337 }
338 else {
339 // skip closing bracket unless the variables wasn't expanded
340 if ( pszValue == NULL )
341 strResult << (char)bracket;
342 m++;
343 }
344 }
345
346 n = m - 1; // skip variable name
347 }
348 break;
349
350 case '\\':
351 // backslash can be used to suppress special meaning of % and $
352 if ( n != str.Len() && (str[n + 1] == '%' || str[n + 1] == '$') ) {
353 strResult += str[++n];
354
355 break;
356 }
357 //else: fall through
358
359 default:
360 strResult += str[n];
361 }
362 }
363
364 return strResult;
365 }
366
367 // this function is used to properly interpret '..' in path
368 void wxSplitPath(wxArrayString& aParts, const wxChar *sz)
369 {
370 aParts.Empty();
371
372 wxString strCurrent;
373 const wxChar *pc = sz;
374 for ( ;; ) {
375 if ( *pc == _T('\0') || *pc == wxCONFIG_PATH_SEPARATOR ) {
376 if ( strCurrent == _T(".") ) {
377 // ignore
378 }
379 else if ( strCurrent == _T("..") ) {
380 // go up one level
381 if ( aParts.IsEmpty() )
382 wxLogWarning(_("'%s' has extra '..', ignored."), sz);
383 else
384 aParts.Remove(aParts.Count() - 1);
385
386 strCurrent.Empty();
387 }
388 else if ( !strCurrent.IsEmpty() ) {
389 aParts.Add(strCurrent);
390 strCurrent.Empty();
391 }
392 //else:
393 // could log an error here, but we prefer to ignore extra '/'
394
395 if ( *pc == _T('\0') )
396 return;
397 }
398 else
399 strCurrent += *pc;
400
401 pc++;
402 }
403 }
404
405 #endif // wxUSE_CONFIG
406