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