1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: declaration of the base class of all config implementations
4 // (see also: fileconf.h and msw/regconf.h)
5 // Author: Karsten Ballüder & Vadim Zeitlin
7 // Created: 07.04.98 (adapted from appconf.h)
9 // Copyright: (c) 1997 Karsten Ballüder Ballueder@usa.net
10 // Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
11 // Licence: wxWindows license
12 ///////////////////////////////////////////////////////////////////////////////
18 #pragma interface "config.h"
21 // ----------------------------------------------------------------------------
23 // ----------------------------------------------------------------------------
25 // it won't compile without it anyhow
27 #error "Please define USE_WXCONFIG or remove config.cpp from your makefile"
28 #endif // USE_WXCONFIG
30 // ----------------------------------------------------------------------------
32 // ----------------------------------------------------------------------------
34 /// shall we be case sensitive in parsing variable names?
35 #ifndef wxCONFIG_CASE_SENSITIVE
36 #define wxCONFIG_CASE_SENSITIVE FALSE
39 /// separates group and entry names (probably shouldn't be changed)
40 #ifndef wxCONFIG_PATH_SEPARATOR
41 #define wxCONFIG_PATH_SEPARATOR '/'
44 /// introduces immutable entries
45 // (i.e. the ones which can't be changed from the local config file)
46 #ifndef wxCONFIG_IMMUTABLE_PREFIX
47 #define wxCONFIG_IMMUTABLE_PREFIX '!'
50 /// should we use registry instead of configuration files under Win32?
51 // (i.e. whether wxConfigBase::Create() will create a wxFileConfig (if it's
52 // FALSE) or wxRegConfig (if it's true and we're under Win32) or wxIniConfig
54 #ifndef wxCONFIG_WIN32_NATIVE
55 #define wxCONFIG_WIN32_NATIVE TRUE
58 // ----------------------------------------------------------------------------
59 // various helper global functions
60 // ----------------------------------------------------------------------------
63 Replace environment variables ($SOMETHING) with their values. The format is
64 $VARNAME or ${VARNAME} where VARNAME contains alphanumeric characters and
65 '_' only. '$' must be escaped ('\$') in order to be taken literally.
67 extern wxString
wxExpandEnvVars(const wxString
&sz
);
70 Split path into parts removing '..' in progress
72 extern void wxSplitPath(wxArrayString
& aParts
, const char *sz
);
74 // ----------------------------------------------------------------------------
75 // abstract base class wxConfigBase which defines the interface for derived
78 // wxConfig organizes the items in a tree-like structure (modeled after the
79 // Unix/Dos filesystem). There are groups (directories) and keys (files).
80 // There is always one current group given by the current path.
82 // Keys are pairs "key_name = value" where value may be of string or integer
83 // (long) type (@@@ doubles and other types such as wxDate coming soon).
84 // ----------------------------------------------------------------------------
89 // sets the config object, returns the previous pointer
90 static wxConfigBase
*Set(wxConfigBase
*pConfig
);
91 // get the config object, creates it on demand unless DontCreateOnDemand
93 static wxConfigBase
*Get() { if ( !ms_pConfig
) Create(); return ms_pConfig
; }
94 // create a new config object: this function will create the "best"
95 // implementation of wxConfig available for the current platform, see
96 // comments near definition wxCONFIG_WIN32_NATIVE for details. It returns
97 // the created object and also sets it as ms_pConfig.
98 static wxConfigBase
*Create();
99 // should Get() try to create a new log object if the current one is NULL?
100 static void DontCreateOnDemand() { ms_bAutoCreate
= FALSE
; }
102 // ctor & virtual dtor
103 // environment variable expansion is on by default
104 wxConfigBase() { m_bExpandEnvVars
= TRUE
; m_bRecordDefaults
= FALSE
; }
105 // empty but ensures that dtor of all derived classes is virtual
106 virtual ~wxConfigBase() { }
109 // set current path: if the first character is '/', it's the absolute path,
110 // otherwise it's a relative path. '..' is supported. If the strPath
111 // doesn't exist it is created.
112 virtual void SetPath(const wxString
& strPath
) = 0;
113 // retrieve the current path (always as absolute path)
114 virtual const wxString
& GetPath() const = 0;
116 // enumeration: all functions here return false when there are no more items.
117 // you must pass the same lIndex to GetNext and GetFirst (don't modify it)
118 // enumerate subgroups
119 virtual bool GetFirstGroup(wxString
& str
, long& lIndex
) const = 0;
120 virtual bool GetNextGroup (wxString
& str
, long& lIndex
) const = 0;
122 virtual bool GetFirstEntry(wxString
& str
, long& lIndex
) const = 0;
123 virtual bool GetNextEntry (wxString
& str
, long& lIndex
) const = 0;
124 // get number of entries/subgroups in the current group, with or without
126 virtual uint
GetNumberOfEntries(bool bRecursive
= FALSE
) const = 0;
127 virtual uint
GetNumberOfGroups(bool bRecursive
= FALSE
) const = 0;
129 // tests of existence
130 // returns TRUE if the group by this name exists
131 virtual bool HasGroup(const wxString
& strName
) const = 0;
132 // same as above, but for an entry
133 virtual bool HasEntry(const wxString
& strName
) const = 0;
134 // returns TRUE if either a group or an entry with a given name exist
135 bool Exists(const wxString
& strName
) const
136 { return HasGroup(strName
) || HasEntry(strName
); }
138 // key access: returns TRUE if value was really read, FALSE if default used
139 // (and if the key is not found the default value is returned.)
140 // read a string from the key
141 virtual bool Read(wxString
*pStr
, const char *szKey
,
142 const char *szDefault
= NULL
) const = 0;
143 // another version using statis buffer - it means it will be overwritten
144 // after each call to this function!
145 virtual const char *Read(const char *szKey
,
146 const char *szDefault
= NULL
) const;
147 // the same for longs
148 virtual long Read(const char *szKey
, long lDefault
) const
149 { long l
; Read(&l
, szKey
, lDefault
); return l
; }
150 // and another version: returns true if default value is returned
151 virtual bool Read(long *pl
, const char *szKey
, long lDefault
= 0) const = 0;
153 // write the value (return true on success)
154 virtual bool Write(const char *szKey
, const char *szValue
) = 0;
155 virtual bool Write(const char *szKey
, long lValue
) = 0;
156 // permanently writes all changes
157 virtual bool Flush(bool bCurrentOnly
= FALSE
) = 0;
159 // delete entries/groups
160 // deletes the specified entry and the group it belongs to if
161 // it was the last key in it and the second parameter is true
162 virtual bool DeleteEntry(const char *szKey
,
163 bool bDeleteGroupIfEmpty
= TRUE
) = 0;
164 // delete the group (with all subgroups)
165 virtual bool DeleteGroup(const char *szKey
) = 0;
166 // delete the whole underlying object (disk file, registry key, ...)
167 // primarly for use by desinstallation routine.
168 virtual bool DeleteAll() = 0;
171 // we can automatically expand environment variables in the config entries
172 // (this option is on by default, you can turn it on/off at any time)
173 bool IsExpandingEnvVars() const { return m_bExpandEnvVars
; }
174 void SetExpandEnvVars(bool bDoIt
= TRUE
) { m_bExpandEnvVars
= bDoIt
; }
175 // recording of default values
176 void SetRecordDefaults(bool bDoIt
= TRUE
) { m_bRecordDefaults
= bDoIt
; }
177 bool IsRecordingDefaults() const { return m_bRecordDefaults
; }
178 // does expansion only if needed
179 wxString
ExpandEnvVars(const wxString
& str
) const
181 wxString tmp
; // Required for BC++
182 if (IsExpandingEnvVars())
183 tmp
= wxExpandEnvVars(str
);
190 static bool IsImmutable(const char *szKey
)
191 { return *szKey
== wxCONFIG_IMMUTABLE_PREFIX
; }
193 // a handy little class which changes current path to the path of given entry
194 // and restores it in dtor: so if you declare a local variable of this type,
195 // you work in the entry directory and the path is automatically restored
196 // when the function returns
200 // ctor/dtor do path changing/restorin
201 PathChanger(const wxConfigBase
*pContainer
, const wxString
& strEntry
);
205 const wxString
& Name() const { return m_strName
; }
208 wxConfigBase
*m_pContainer
; // object we live in
209 wxString m_strName
, // name of entry (i.e. name only)
210 m_strOldPath
; // saved path
211 bool m_bChanged
; // was the path changed?
215 // are we doing automatic environment variable expansion?
216 bool m_bExpandEnvVars
;
217 // do we record default values?
218 bool m_bRecordDefaults
;
221 static wxConfigBase
*ms_pConfig
;
222 static bool ms_bAutoCreate
;
225 // ----------------------------------------------------------------------------
226 // the native wxConfigBase implementation
227 // ----------------------------------------------------------------------------
229 // under Windows we prefer to use the native implementation
230 #if defined(__WXMSW__) && wxCONFIG_WIN32_NATIVE
232 #define wxConfig wxRegConfig
234 #define wxConfig wxIniConfig
236 #else // either we're under Unix or wish to use files even under Windows
237 #define wxConfig wxFileConfig