]>
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>
43 #include <ctype.h> // for isalnum()
45 // ----------------------------------------------------------------------------
46 // global and class static variables
47 // ----------------------------------------------------------------------------
49 wxConfig
*wxConfig::ms_pConfig
= NULL
;
51 // ============================================================================
53 // ============================================================================
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
62 wxConfig
*wxConfig::Set(wxConfig
*pConfig
)
64 wxConfig
*pOld
= ms_pConfig
;
69 void wxConfig::Create()
71 ms_pConfig
= wxTheApp
->CreateConfig();
74 const char *wxConfig::Read(const char *szKey
, const char *szDefault
) const
76 static char s_szBuf
[1024];
78 Read(&s
, szKey
, szDefault
);
79 strncpy(s_szBuf
, s
, WXSIZEOF(s_szBuf
));
84 // ----------------------------------------------------------------------------
85 // Config::PathChanger
86 // ----------------------------------------------------------------------------
88 wxConfig::PathChanger::PathChanger(const wxConfig
*pContainer
,
89 const wxString
& strEntry
)
91 m_pContainer
= (wxConfig
*)pContainer
;
92 wxString strPath
= strEntry
.Before(APPCONF_PATH_SEPARATOR
);
94 // special case of "/keyname" when there is nothing before "/"
95 if ( strPath
.IsEmpty() && strEntry
[0] == APPCONF_PATH_SEPARATOR
)
96 strPath
= APPCONF_PATH_SEPARATOR
;
98 if ( !strPath
.IsEmpty() ) {
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
);
107 // it's a name only, without path - nothing to do
109 m_strName
= strEntry
;
113 wxConfig::PathChanger::~PathChanger()
115 // only restore path if it was changed
117 m_pContainer
->SetPath(m_strOldPath
);
121 // ----------------------------------------------------------------------------
122 // static & global functions
123 // ----------------------------------------------------------------------------
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
)
132 // don't change the values the enum elements: they must be equal
133 // to the matching [closing] delimiter.
137 Bracket_Normal
= ')',
140 Bracket_Windows
= '%' // yeah, Windows people are a bit strange ;-)
145 for ( uint n
= 0; n
< str
.Len(); n
++ ) {
155 bracket
= Bracket_Windows
;
158 if ( n
== str
.Len() - 1 ) {
159 bracket
= Bracket_None
;
162 switch ( str
[n
+ 1] ) {
164 bracket
= Bracket_Normal
;
165 n
++; // skip the bracket
169 bracket
= Bracket_Curly
;
170 n
++; // skip the bracket
174 bracket
= Bracket_None
;
180 while ( m
< str
.Len() && (isalnum(str
[m
]) || str
[m
] == '_') )
183 wxString
strVarName(str
.c_str() + n
+ 1, m
- n
- 1);
185 const char *pszValue
= getenv(strVarName
);
186 if ( pszValue
!= NULL
) {
187 strResult
+= pszValue
;
190 // variable doesn't exist => don't change anything
192 if ( bracket
!= Bracket_Windows
)
194 if ( bracket
!= Bracket_None
)
195 strResult
<< str
[n
- 1];
196 strResult
<< str
[n
] << strVarName
;
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());
206 // skip closing bracket
207 if ( pszValue
== NULL
)
208 strResult
<< (char)bracket
;
213 n
= m
- 1; // skip variable name
229 // this function is used to properly interpret '..' in path
230 void SplitPath(wxArrayString
& aParts
, const char *sz
)
237 if ( *pc
== '\0' || *pc
== APPCONF_PATH_SEPARATOR
) {
238 if ( strCurrent
== "." ) {
241 else if ( strCurrent
== ".." ) {
243 if ( aParts
.IsEmpty() )
244 wxLogWarning("'%s' has extra '..', ignored.", sz
);
246 aParts
.Remove(aParts
.Count() - 1);
250 else if ( !strCurrent
.IsEmpty() ) {
251 aParts
.Add(strCurrent
);
255 // could log an error here, but we prefer to ignore extra '/'