]> git.saurik.com Git - wxWidgets.git/blob - src/common/config.cpp
ddb5a9d71658bcb3fe50d5440a2898ad51521cf9
[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 // headers
15 // ============================================================================
16 #ifdef __GNUG__
17 #pragma implementation "app.h"
18 #endif
19
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif //__BORLANDC__
25
26 #include <wx/string.h>
27 #include <wx/intl.h>
28 #include <wx/file.h>
29 #include <wx/log.h>
30 #include <wx/textfile.h>
31 #include <wx/config.h>
32
33 #include <stdlib.h>
34 #include <ctype.h>
35
36 // ============================================================================
37 // implementation
38 // ============================================================================
39
40 // ----------------------------------------------------------------------------
41 // wxConfig
42 // ----------------------------------------------------------------------------
43 wxConfig::~wxConfig()
44 {
45 }
46
47 // ----------------------------------------------------------------------------
48 // static & global functions
49 // ----------------------------------------------------------------------------
50
51 // understands both Unix and Windows (but only under Windows) environment
52 // variables expansion: i.e. $var, $(var) and ${var} are always understood
53 // and in addition under Windows %var% is also.
54 wxString ExpandEnvVars(const wxString& str)
55 {
56 wxString strResult;
57
58 // don't change the values the enum elements: they must be equal
59 // to the matching [closing] delimiter.
60 enum Bracket
61 {
62 Bracket_None,
63 Bracket_Normal = ')',
64 Bracket_Curly = '}',
65 #ifdef __WINDOWS__
66 Bracket_Windows = '%' // yeah, Windows people are a bit strange ;-)
67 #endif
68 };
69
70 uint m;
71 for ( uint n = 0; n < str.Len(); n++ ) {
72 switch ( str[n] ) {
73 #ifdef __WINDOWS__
74 case '%':
75 #endif //WINDOWS
76 case '$':
77 {
78 Bracket bracket;
79 #ifdef __WINDOWS__
80 if ( str[n] == '%' )
81 bracket = Bracket_Windows;
82 else
83 #endif //WINDOWS
84 if ( n == str.Len() - 1 ) {
85 bracket = Bracket_None;
86 }
87 else {
88 switch ( str[n + 1] ) {
89 case '(':
90 bracket = Bracket_Normal;
91 n++; // skip the bracket
92 break;
93
94 case '{':
95 bracket = Bracket_Curly;
96 n++; // skip the bracket
97 break;
98
99 default:
100 bracket = Bracket_None;
101 }
102 }
103
104 m = n + 1;
105
106 while ( m < str.Len() && (isalnum(str[m]) || str[m] == '_') )
107 m++;
108
109 wxString strVarName(str.c_str() + n + 1, m - n - 1);
110
111 const char *pszValue = getenv(strVarName);
112 if ( pszValue != NULL ) {
113 strResult += pszValue;
114 }
115 else {
116 // variable doesn't exist => don't change anything
117 #ifdef __WINDOWS__
118 if ( bracket != Bracket_Windows )
119 #endif
120 if ( bracket != Bracket_None )
121 strResult << str[n - 1];
122 strResult << str[n] << strVarName;
123 }
124
125 // check the closing bracket
126 if ( bracket != Bracket_None ) {
127 if ( m == str.Len() || str[m] != (char)bracket ) {
128 wxLogWarning("missing '%c' at position %d in '%s'.",
129 (char)bracket, m + 1, str.c_str());
130 }
131 else {
132 // skip closing bracket
133 if ( pszValue == NULL )
134 strResult << (char)bracket;
135 m++;
136 }
137 }
138
139 n = m - 1; // skip variable name
140 }
141 break;
142
143 case '\\':
144 n++;
145 // fall through
146
147 default:
148 strResult += str[n];
149 }
150 }
151
152 return strResult;
153 }