]>
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
wxExpandEnvVars(const wxString
& str
)
131 strResult
.Alloc(str
.Len());
133 // don't change the values the enum elements: they must be equal
134 // to the matching [closing] delimiter.
138 Bracket_Normal
= ')',
141 Bracket_Windows
= '%' // yeah, Windows people are a bit strange ;-)
146 for ( uint n
= 0; n
< str
.Len(); n
++ ) {
156 bracket
= Bracket_Windows
;
159 if ( n
== str
.Len() - 1 ) {
160 bracket
= Bracket_None
;
163 switch ( str
[n
+ 1] ) {
165 bracket
= Bracket_Normal
;
166 n
++; // skip the bracket
170 bracket
= Bracket_Curly
;
171 n
++; // skip the bracket
175 bracket
= Bracket_None
;
181 while ( m
< str
.Len() && (isalnum(str
[m
]) || str
[m
] == '_') )
184 wxString
strVarName(str
.c_str() + n
+ 1, m
- n
- 1);
186 const char *pszValue
= getenv(strVarName
);
187 if ( pszValue
!= NULL
) {
188 strResult
+= pszValue
;
191 // variable doesn't exist => don't change anything
193 if ( bracket
!= Bracket_Windows
)
195 if ( bracket
!= Bracket_None
)
196 strResult
<< str
[n
- 1];
197 strResult
<< str
[n
] << strVarName
;
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());
207 // skip closing bracket
208 if ( pszValue
== NULL
)
209 strResult
<< (char)bracket
;
214 n
= m
- 1; // skip variable name
226 // this function is used to properly interpret '..' in path
227 void wxSplitPath(wxArrayString
& aParts
, const char *sz
)
234 if ( *pc
== '\0' || *pc
== APPCONF_PATH_SEPARATOR
) {
235 if ( strCurrent
== "." ) {
238 else if ( strCurrent
== ".." ) {
240 if ( aParts
.IsEmpty() )
241 wxLogWarning(_("'%s' has extra '..', ignored."), sz
);
243 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 '/'