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