]>
git.saurik.com Git - wxWidgets.git/blob - src/common/config.cpp
1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: implementation of wxConfig class
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1997 Karsten Ballüder Ballueder@usa.net
9 // Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
10 // Licence: wxWindows license
11 ///////////////////////////////////////////////////////////////////////////////
13 // ============================================================================
15 // ============================================================================
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
21 #pragma implementation "config.h"
24 #include "wx/wxprec.h"
31 #include <wx/string.h>
38 #include <wx/textfile.h>
39 #include <wx/config.h>
40 #include <wx/fileconf.h>
42 #include <ctype.h> // for isalnum()
44 // ----------------------------------------------------------------------------
45 // global and class static variables
46 // ----------------------------------------------------------------------------
48 wxConfig
*wxConfig::ms_pConfig
= NULL
;
50 // ============================================================================
52 // ============================================================================
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
61 wxConfig
*wxConfig::Set(wxConfig
*pConfig
)
63 wxConfig
*pOld
= ms_pConfig
;
68 void wxConfig::Create()
70 ms_pConfig
= wxTheApp
->CreateConfig();
73 const char *wxConfig::Read(const char *szKey
, const char *szDefault
) const
75 static char s_szBuf
[1024];
77 Read(&s
, szKey
, szDefault
);
78 strncpy(s_szBuf
, s
, WXSIZEOF(s_szBuf
));
83 // ----------------------------------------------------------------------------
84 // Config::PathChanger
85 // ----------------------------------------------------------------------------
87 wxConfig::PathChanger::PathChanger(const wxConfig
*pContainer
,
88 const wxString
& strEntry
)
90 m_pContainer
= (wxConfig
*)pContainer
;
91 wxString strPath
= strEntry
.Before(APPCONF_PATH_SEPARATOR
);
93 // special case of "/keyname" when there is nothing before "/"
94 if ( strPath
.IsEmpty() && strEntry
[0] == APPCONF_PATH_SEPARATOR
)
95 strPath
= APPCONF_PATH_SEPARATOR
;
97 if ( !strPath
.IsEmpty() ) {
100 m_strName
= strEntry
.Right(APPCONF_PATH_SEPARATOR
);
101 m_strOldPath
= m_pContainer
->GetPath();
102 m_strOldPath
+= APPCONF_PATH_SEPARATOR
;
103 m_pContainer
->SetPath(strPath
);
106 // it's a name only, without path - nothing to do
108 m_strName
= strEntry
;
112 wxConfig::PathChanger::~PathChanger()
114 // only restore path if it was changed
116 m_pContainer
->SetPath(m_strOldPath
);
120 // ----------------------------------------------------------------------------
121 // static & global functions
122 // ----------------------------------------------------------------------------
124 // understands both Unix and Windows (but only under Windows) environment
125 // variables expansion: i.e. $var, $(var) and ${var} are always understood
126 // and in addition under Windows %var% is also.
127 wxString
ExpandEnvVars(const wxString
& str
)
131 // don't change the values the enum elements: they must be equal
132 // to the matching [closing] delimiter.
136 Bracket_Normal
= ')',
139 Bracket_Windows
= '%' // yeah, Windows people are a bit strange ;-)
144 for ( uint n
= 0; n
< str
.Len(); n
++ ) {
154 bracket
= Bracket_Windows
;
157 if ( n
== str
.Len() - 1 ) {
158 bracket
= Bracket_None
;
161 switch ( str
[n
+ 1] ) {
163 bracket
= Bracket_Normal
;
164 n
++; // skip the bracket
168 bracket
= Bracket_Curly
;
169 n
++; // skip the bracket
173 bracket
= Bracket_None
;
179 while ( m
< str
.Len() && (isalnum(str
[m
]) || str
[m
] == '_') )
182 wxString
strVarName(str
.c_str() + n
+ 1, m
- n
- 1);
184 const char *pszValue
= getenv(strVarName
);
185 if ( pszValue
!= NULL
) {
186 strResult
+= pszValue
;
189 // variable doesn't exist => don't change anything
191 if ( bracket
!= Bracket_Windows
)
193 if ( bracket
!= Bracket_None
)
194 strResult
<< str
[n
- 1];
195 strResult
<< str
[n
] << strVarName
;
198 // check the closing bracket
199 if ( bracket
!= Bracket_None
) {
200 if ( m
== str
.Len() || str
[m
] != (char)bracket
) {
201 wxLogWarning("missing '%c' at position %d in '%s'.",
202 (char)bracket
, m
+ 1, str
.c_str());
205 // skip closing bracket
206 if ( pszValue
== NULL
)
207 strResult
<< (char)bracket
;
212 n
= m
- 1; // skip variable name
228 // this function is used to properly interpret '..' in path
229 void SplitPath(wxArrayString
& aParts
, const char *sz
)
236 if ( *pc
== '\0' || *pc
== APPCONF_PATH_SEPARATOR
) {
237 if ( strCurrent
== "." ) {
240 else if ( strCurrent
== ".." ) {
242 if ( aParts
.IsEmpty() )
243 wxLogWarning("'%s' has extra '..', ignored.", sz
);
245 aParts
.Remove(aParts
.Count() - 1);
247 else if ( !strCurrent
.IsEmpty() ) {
248 aParts
.Add(strCurrent
);
252 // could log an error here, but we prefer to ignore extra '/'