]> git.saurik.com Git - wxWidgets.git/blob - src/common/config.cpp
* Fixed two memory leaks.
[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 // declarations
15 // ============================================================================
16
17 // ----------------------------------------------------------------------------
18 // headers
19 // ----------------------------------------------------------------------------
20 #ifdef __GNUG__
21 #pragma implementation "confbase.h"
22 #endif
23
24 #include "wx/wxprec.h"
25
26 #ifdef __BORLANDC__
27 #pragma hdrstop
28 #endif //__BORLANDC__
29
30 #ifndef WX_PRECOMP
31 #include <wx/string.h>
32 #include <wx/intl.h>
33 #endif //WX_PRECOMP
34
35 #include <wx/app.h>
36 #include <wx/file.h>
37 #include <wx/log.h>
38 #include <wx/textfile.h>
39 #include <wx/confbase.h>
40
41 // we must include (one of) these files for wxConfigBase::Create
42 #if defined(__WXMSW__) && defined(wxCONFIG_WIN32_NATIVE)
43 #ifdef __WIN32__
44 #include <wx/msw/regconf.h>
45 #else //WIN16
46 #include <wx/msw/iniconf.h>
47 #endif
48 #else // either we're under Unix or wish to use files even under Windows
49 #include <wx/fileconf.h>
50 #endif
51
52 #include <stdlib.h>
53 #include <ctype.h> // for isalnum()
54
55 // ----------------------------------------------------------------------------
56 // global and class static variables
57 // ----------------------------------------------------------------------------
58
59 wxConfigBase *wxConfigBase::ms_pConfig = NULL;
60 bool wxConfigBase::ms_bAutoCreate = TRUE;
61
62 // ============================================================================
63 // implementation
64 // ============================================================================
65
66 // ----------------------------------------------------------------------------
67 // wxConfigBase
68 // ----------------------------------------------------------------------------
69
70 wxConfigBase *wxConfigBase::Set(wxConfigBase *pConfig)
71 {
72 wxConfigBase *pOld = ms_pConfig;
73 ms_pConfig = pConfig;
74 return pOld;
75 }
76
77 wxConfigBase *wxConfigBase::Create()
78 {
79 if ( ms_bAutoCreate && ms_pConfig == NULL ) {
80 ms_pConfig =
81 #if defined(__WXMSW__) && defined(wxCONFIG_WIN32_NATIVE)
82 #ifdef __WIN32__
83 new wxRegConfig(wxTheApp->GetVendorName() + '\\'
84 + wxTheApp->GetAppName());
85 #else //WIN16
86 new wxIniConfig(wxTheApp->GetAppName(), wxTheApp->GetVendorName());
87 #endif
88 #else // either we're under Unix or wish to use files even under Windows
89 new wxFileConfig(wxTheApp->GetAppName());
90 #endif
91 }
92
93 return ms_pConfig;
94 }
95
96 const char *wxConfigBase::Read(const char *szKey, const char *szDefault) const
97 {
98 static char s_szBuf[1024];
99 wxString s;
100 Read(&s, szKey, szDefault);
101 strncpy(s_szBuf, s, WXSIZEOF(s_szBuf));
102
103 return s_szBuf;
104 }
105
106 // ----------------------------------------------------------------------------
107 // Config::PathChanger
108 // ----------------------------------------------------------------------------
109
110 wxConfigBase::PathChanger::PathChanger(const wxConfigBase *pContainer,
111 const wxString& strEntry)
112 {
113 m_pContainer = (wxConfigBase *)pContainer;
114 wxString strPath = strEntry.Before(wxCONFIG_PATH_SEPARATOR);
115
116 // special case of "/keyname" when there is nothing before "/"
117 if ( strPath.IsEmpty() && ((!strEntry.IsEmpty()) && strEntry[0] == wxCONFIG_PATH_SEPARATOR ))
118 strPath = wxCONFIG_PATH_SEPARATOR;
119
120 if ( !strPath.IsEmpty() ) {
121 // do change the path
122 m_bChanged = TRUE;
123 m_strName = strEntry.Right(wxCONFIG_PATH_SEPARATOR);
124 m_strOldPath = m_pContainer->GetPath();
125 m_strOldPath += wxCONFIG_PATH_SEPARATOR;
126 m_pContainer->SetPath(strPath);
127 }
128 else {
129 // it's a name only, without path - nothing to do
130 m_bChanged = FALSE;
131 m_strName = strEntry;
132 }
133 }
134
135 wxConfigBase::PathChanger::~PathChanger()
136 {
137 // only restore path if it was changed
138 if ( m_bChanged ) {
139 m_pContainer->SetPath(m_strOldPath);
140 }
141 }
142
143 // ----------------------------------------------------------------------------
144 // static & global functions
145 // ----------------------------------------------------------------------------
146
147 // understands both Unix and Windows (but only under Windows) environment
148 // variables expansion: i.e. $var, $(var) and ${var} are always understood
149 // and in addition under Windows %var% is also.
150 wxString wxExpandEnvVars(const wxString& str)
151 {
152 wxString strResult;
153 strResult.Alloc(str.Len());
154
155 // don't change the values the enum elements: they must be equal
156 // to the matching [closing] delimiter.
157 enum Bracket
158 {
159 Bracket_None,
160 Bracket_Normal = ')',
161 Bracket_Curly = '}',
162 #ifdef __WXMSW__
163 Bracket_Windows = '%' // yeah, Windows people are a bit strange ;-)
164 #endif
165 };
166
167 size_t m;
168 for ( size_t n = 0; n < str.Len(); n++ ) {
169 switch ( str[n] ) {
170 #ifdef __WXMSW__
171 case '%':
172 #endif //WINDOWS
173 case '$':
174 {
175 Bracket bracket;
176 #ifdef __WXMSW__
177 if ( str[n] == '%' )
178 bracket = Bracket_Windows;
179 else
180 #endif //WINDOWS
181 if ( n == str.Len() - 1 ) {
182 bracket = Bracket_None;
183 }
184 else {
185 switch ( str[n + 1] ) {
186 case '(':
187 bracket = Bracket_Normal;
188 n++; // skip the bracket
189 break;
190
191 case '{':
192 bracket = Bracket_Curly;
193 n++; // skip the bracket
194 break;
195
196 default:
197 bracket = Bracket_None;
198 }
199 }
200
201 m = n + 1;
202
203 while ( m < str.Len() && (isalnum(str[m]) || str[m] == '_') )
204 m++;
205
206 wxString strVarName(str.c_str() + n + 1, m - n - 1);
207
208 const char *pszValue = getenv(strVarName);
209 if ( pszValue != NULL ) {
210 strResult += pszValue;
211 }
212 else {
213 // variable doesn't exist => don't change anything
214 #ifdef __WXMSW__
215 if ( bracket != Bracket_Windows )
216 #endif
217 if ( bracket != Bracket_None )
218 strResult << str[n - 1];
219 strResult << str[n] << strVarName;
220 }
221
222 // check the closing bracket
223 if ( bracket != Bracket_None ) {
224 if ( m == str.Len() || str[m] != (char)bracket ) {
225 wxLogWarning(_("Environment variables expansion failed: "
226 "missing '%c' at position %d in '%s'."),
227 (char)bracket, m + 1, str.c_str());
228 }
229 else {
230 // skip closing bracket unless the variables wasn't expanded
231 if ( pszValue == NULL )
232 strResult << (char)bracket;
233 m++;
234 }
235 }
236
237 n = m - 1; // skip variable name
238 }
239 break;
240
241 case '\\':
242 // backslash can be used to suppress special meaning of % and $
243 if ( n != str.Len() && (str[n + 1] == '%' || str[n + 1] == '$') ) {
244 strResult += str[++n];
245
246 break;
247 }
248 //else: fall through
249
250 default:
251 strResult += str[n];
252 }
253 }
254
255 return strResult;
256 }
257
258 // this function is used to properly interpret '..' in path
259 void wxSplitPath(wxArrayString& aParts, const char *sz)
260 {
261 aParts.Empty();
262
263 wxString strCurrent;
264 const char *pc = sz;
265 for ( ;; ) {
266 if ( *pc == '\0' || *pc == wxCONFIG_PATH_SEPARATOR ) {
267 if ( strCurrent == "." ) {
268 // ignore
269 }
270 else if ( strCurrent == ".." ) {
271 // go up one level
272 if ( aParts.IsEmpty() )
273 wxLogWarning(_("'%s' has extra '..', ignored."), sz);
274 else
275 aParts.Remove(aParts.Count() - 1);
276
277 strCurrent.Empty();
278 }
279 else if ( !strCurrent.IsEmpty() ) {
280 aParts.Add(strCurrent);
281 strCurrent.Empty();
282 }
283 //else:
284 // could log an error here, but we prefer to ignore extra '/'
285
286 if ( *pc == '\0' )
287 return;
288 }
289 else
290 strCurrent += *pc;
291
292 pc++;
293 }
294 }
295