]> git.saurik.com Git - wxWidgets.git/blob - src/common/config.cpp
Various documentation changes, makefile fixes
[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 ExpandEnvVars(const wxString& str)
129 {
130 wxString strResult;
131
132 // don't change the values the enum elements: they must be equal
133 // to the matching [closing] delimiter.
134 enum Bracket
135 {
136 Bracket_None,
137 Bracket_Normal = ')',
138 Bracket_Curly = '}',
139 #ifdef __WINDOWS__
140 Bracket_Windows = '%' // yeah, Windows people are a bit strange ;-)
141 #endif
142 };
143
144 uint m;
145 for ( uint n = 0; n < str.Len(); n++ ) {
146 switch ( str[n] ) {
147 #ifdef __WINDOWS__
148 case '%':
149 #endif //WINDOWS
150 case '$':
151 {
152 Bracket bracket;
153 #ifdef __WINDOWS__
154 if ( str[n] == '%' )
155 bracket = Bracket_Windows;
156 else
157 #endif //WINDOWS
158 if ( n == str.Len() - 1 ) {
159 bracket = Bracket_None;
160 }
161 else {
162 switch ( str[n + 1] ) {
163 case '(':
164 bracket = Bracket_Normal;
165 n++; // skip the bracket
166 break;
167
168 case '{':
169 bracket = Bracket_Curly;
170 n++; // skip the bracket
171 break;
172
173 default:
174 bracket = Bracket_None;
175 }
176 }
177
178 m = n + 1;
179
180 while ( m < str.Len() && (isalnum(str[m]) || str[m] == '_') )
181 m++;
182
183 wxString strVarName(str.c_str() + n + 1, m - n - 1);
184
185 const char *pszValue = getenv(strVarName);
186 if ( pszValue != NULL ) {
187 strResult += pszValue;
188 }
189 else {
190 // variable doesn't exist => don't change anything
191 #ifdef __WINDOWS__
192 if ( bracket != Bracket_Windows )
193 #endif
194 if ( bracket != Bracket_None )
195 strResult << str[n - 1];
196 strResult << str[n] << strVarName;
197 }
198
199 // check the closing bracket
200 if ( bracket != Bracket_None ) {
201 if ( m == str.Len() || str[m] != (char)bracket ) {
202 wxLogWarning("missing '%c' at position %d in '%s'.",
203 (char)bracket, m + 1, str.c_str());
204 }
205 else {
206 // skip closing bracket
207 if ( pszValue == NULL )
208 strResult << (char)bracket;
209 m++;
210 }
211 }
212
213 n = m - 1; // skip variable name
214 }
215 break;
216
217 case '\\':
218 n++;
219 // fall through
220
221 default:
222 strResult += str[n];
223 }
224 }
225
226 return strResult;
227 }
228
229 // this function is used to properly interpret '..' in path
230 void SplitPath(wxArrayString& aParts, const char *sz)
231 {
232 aParts.Empty();
233
234 wxString strCurrent;
235 const char *pc = sz;
236 for ( ;; ) {
237 if ( *pc == '\0' || *pc == APPCONF_PATH_SEPARATOR ) {
238 if ( strCurrent == "." ) {
239 // ignore
240 }
241 else if ( strCurrent == ".." ) {
242 // go up one level
243 if ( aParts.IsEmpty() )
244 wxLogWarning("'%s' has extra '..', ignored.", sz);
245 else
246 aParts.Remove(aParts.Count() - 1);
247
248 strCurrent.Empty();
249 }
250 else if ( !strCurrent.IsEmpty() ) {
251 aParts.Add(strCurrent);
252 strCurrent.Empty();
253 }
254 //else:
255 // could log an error here, but we prefer to ignore extra '/'
256
257 if ( *pc == '\0' )
258 return;
259 }
260 else
261 strCurrent += *pc;
262
263 pc++;
264 }
265 }
266