]>
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>
41 // we must include (one of) these files for wxConfig::Create
42 #if defined(__MSWIN__) && defined(wxCONFIG_WIN32_NATIVE)
44 #include <wx/msw/regconf.h>
46 #include <wx/msw/iniconf.h>
48 #else // either we're under Unix or wish to use files even under Windows
49 #include <wx/fileconf.h>
53 #include <ctype.h> // for isalnum()
55 // ----------------------------------------------------------------------------
56 // global and class static variables
57 // ----------------------------------------------------------------------------
59 wxConfig
*wxConfig::ms_pConfig
= NULL
;
61 // ============================================================================
63 // ============================================================================
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 wxConfig
*wxConfig::Set(wxConfig
*pConfig
)
71 wxConfig
*pOld
= ms_pConfig
;
76 wxConfig
*wxConfig::Create()
79 #if defined(__MSWIN__) && defined(wxCONFIG_WIN32_NATIVE)
81 new wxRegConfig(wxTheApp
->GetAppName(), wxTheApp
->GetVendorName());
83 #error "Sorry, no wxIniConfig yet..."
84 //new wxIniConfig(wxTheApp->GetAppName(), wxTheApp->GetVendorName());
86 #else // either we're under Unix or wish to use files even under Windows
87 new wxFileConfig(wxTheApp
->GetAppName());
91 const char *wxConfig::Read(const char *szKey
, const char *szDefault
) const
93 static char s_szBuf
[1024];
95 Read(&s
, szKey
, szDefault
);
96 strncpy(s_szBuf
, s
, WXSIZEOF(s_szBuf
));
101 // ----------------------------------------------------------------------------
102 // Config::PathChanger
103 // ----------------------------------------------------------------------------
105 wxConfig::PathChanger::PathChanger(const wxConfig
*pContainer
,
106 const wxString
& strEntry
)
108 m_pContainer
= (wxConfig
*)pContainer
;
109 wxString strPath
= strEntry
.Before(wxCONFIG_PATH_SEPARATOR
);
111 // special case of "/keyname" when there is nothing before "/"
112 if ( strPath
.IsEmpty() && strEntry
[0] == wxCONFIG_PATH_SEPARATOR
)
113 strPath
= wxCONFIG_PATH_SEPARATOR
;
115 if ( !strPath
.IsEmpty() ) {
116 // do change the path
118 m_strName
= strEntry
.Right(wxCONFIG_PATH_SEPARATOR
);
119 m_strOldPath
= m_pContainer
->GetPath();
120 m_strOldPath
+= wxCONFIG_PATH_SEPARATOR
;
121 m_pContainer
->SetPath(strPath
);
124 // it's a name only, without path - nothing to do
126 m_strName
= strEntry
;
130 wxConfig::PathChanger::~PathChanger()
132 // only restore path if it was changed
134 m_pContainer
->SetPath(m_strOldPath
);
138 // ----------------------------------------------------------------------------
139 // static & global functions
140 // ----------------------------------------------------------------------------
142 // understands both Unix and Windows (but only under Windows) environment
143 // variables expansion: i.e. $var, $(var) and ${var} are always understood
144 // and in addition under Windows %var% is also.
145 wxString
wxExpandEnvVars(const wxString
& str
)
148 strResult
.Alloc(str
.Len());
150 // don't change the values the enum elements: they must be equal
151 // to the matching [closing] delimiter.
155 Bracket_Normal
= ')',
158 Bracket_Windows
= '%' // yeah, Windows people are a bit strange ;-)
163 for ( uint n
= 0; n
< str
.Len(); n
++ ) {
173 bracket
= Bracket_Windows
;
176 if ( n
== str
.Len() - 1 ) {
177 bracket
= Bracket_None
;
180 switch ( str
[n
+ 1] ) {
182 bracket
= Bracket_Normal
;
183 n
++; // skip the bracket
187 bracket
= Bracket_Curly
;
188 n
++; // skip the bracket
192 bracket
= Bracket_None
;
198 while ( m
< str
.Len() && (isalnum(str
[m
]) || str
[m
] == '_') )
201 wxString
strVarName(str
.c_str() + n
+ 1, m
- n
- 1);
203 const char *pszValue
= getenv(strVarName
);
204 if ( pszValue
!= NULL
) {
205 strResult
+= pszValue
;
208 // variable doesn't exist => don't change anything
210 if ( bracket
!= Bracket_Windows
)
212 if ( bracket
!= Bracket_None
)
213 strResult
<< str
[n
- 1];
214 strResult
<< str
[n
] << strVarName
;
217 // check the closing bracket
218 if ( bracket
!= Bracket_None
) {
219 if ( m
== str
.Len() || str
[m
] != (char)bracket
) {
220 wxLogWarning(_("missing '%c' at position %d in '%s'."),
221 (char)bracket
, m
+ 1, str
.c_str());
224 // skip closing bracket
225 if ( pszValue
== NULL
)
226 strResult
<< (char)bracket
;
231 n
= m
- 1; // skip variable name
243 // this function is used to properly interpret '..' in path
244 void wxSplitPath(wxArrayString
& aParts
, const char *sz
)
251 if ( *pc
== '\0' || *pc
== wxCONFIG_PATH_SEPARATOR
) {
252 if ( strCurrent
== "." ) {
255 else if ( strCurrent
== ".." ) {
257 if ( aParts
.IsEmpty() )
258 wxLogWarning(_("'%s' has extra '..', ignored."), sz
);
260 aParts
.Remove(aParts
.Count() - 1);
264 else if ( !strCurrent
.IsEmpty() ) {
265 aParts
.Add(strCurrent
);
269 // could log an error here, but we prefer to ignore extra '/'