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
92 static wxConfigBase
*Get() { if ( !ms_pConfig
) Create(); return ms_pConfig
; }
93 // create a new config object: this function will create the "best"
94 // implementation of wxConfig available for the current platform, see
95 // comments near definition wxCONFIG_WIN32_NATIVE for details. It returns
96 // the created object and also sets it as ms_pConfig.
97 static wxConfigBase
*Create();
99 // ctor & virtual dtor
100 // environment variable expansion is on by default
101 wxConfigBase() { m_bExpandEnvVars
= TRUE
; }
102 // empty but ensures that dtor of all derived classes is virtual
103 virtual ~wxConfigBase() { }
106 // set current path: if the first character is '/', it's the absolute path,
107 // otherwise it's a relative path. '..' is supported. If the strPath
108 // doesn't exist it is created.
109 virtual void SetPath(const wxString
& strPath
) = 0;
110 // retrieve the current path (always as absolute path)
111 virtual const wxString
& GetPath() const = 0;
113 // enumeration: all functions here return false when there are no more items.
114 // you must pass the same lIndex to GetNext and GetFirst (don't modify it)
115 // enumerate subgroups
116 virtual bool GetFirstGroup(wxString
& str
, long& lIndex
) const = 0;
117 virtual bool GetNextGroup (wxString
& str
, long& lIndex
) const = 0;
119 virtual bool GetFirstEntry(wxString
& str
, long& lIndex
) const = 0;
120 virtual bool GetNextEntry (wxString
& str
, long& lIndex
) const = 0;
121 // get number of entries/subgroups in the current group, with or without
123 virtual uint
GetNumberOfEntries(bool bRecursive
= FALSE
) const = 0;
124 virtual uint
GetNumberOfGroups(bool bRecursive
= FALSE
) const = 0;
126 // tests of existence
127 // returns TRUE if the group by this name exists
128 virtual bool HasGroup(const wxString
& strName
) const = 0;
129 // same as above, but for an entry
130 virtual bool HasEntry(const wxString
& strName
) const = 0;
131 // returns TRUE if either a group or an entry with a given name exist
132 bool Exists(const wxString
& strName
) const
133 { return HasGroup(strName
) || HasEntry(strName
); }
135 // key access: returns TRUE if value was really read, FALSE if default used
136 // (and if the key is not found the default value is returned.)
137 // read a string from the key
138 virtual bool Read(wxString
*pStr
, const char *szKey
,
139 const char *szDefault
= NULL
) const = 0;
140 // another version using statis buffer - it means it will be overwritten
141 // after each call to this function!
142 virtual const char *Read(const char *szKey
,
143 const char *szDefault
= NULL
) const;
144 // the same for longs
145 virtual long Read(const char *szKey
, long lDefault
) const
146 { long l
; Read(&l
, szKey
, lDefault
); return l
; }
147 // and another version: returns true if default value is returned
148 virtual bool Read(long *pl
, const char *szKey
, long lDefault
= 0) const = 0;
150 // write the value (return true on success)
151 virtual bool Write(const char *szKey
, const char *szValue
) = 0;
152 virtual bool Write(const char *szKey
, long lValue
) = 0;
153 // permanently writes all changes
154 virtual bool Flush(bool bCurrentOnly
= FALSE
) = 0;
156 // delete entries/groups
157 // deletes the specified entry and the group it belongs to if
158 // it was the last key in it and the second parameter is true
159 virtual bool DeleteEntry(const char *szKey
,
160 bool bDeleteGroupIfEmpty
= TRUE
) = 0;
161 // delete the group (with all subgroups)
162 virtual bool DeleteGroup(const char *szKey
) = 0;
163 // delete the whole underlying object (disk file, registry key, ...)
164 // primarly for use by desinstallation routine.
165 virtual bool DeleteAll() = 0;
168 // we can automatically expand environment variables in the config entries
169 // (this option is on by default, you can turn it on/off at any time)
170 bool IsExpandingEnvVars() const { return m_bExpandEnvVars
; }
171 void SetExpandEnvVars(bool bDoIt
= TRUE
) { m_bExpandEnvVars
= bDoIt
; }
172 // does expansion only if needed
173 wxString
ExpandEnvVars(const wxString
& str
) const
175 wxString tmp
; // Required for BC++
176 if (IsExpandingEnvVars())
177 tmp
= wxExpandEnvVars(str
);
184 static bool IsImmutable(const char *szKey
)
185 { return *szKey
== wxCONFIG_IMMUTABLE_PREFIX
; }
187 // a handy little class which changes current path to the path of given entry
188 // and restores it in dtor: so if you declare a local variable of this type,
189 // you work in the entry directory and the path is automatically restored
190 // when the function returns
194 // ctor/dtor do path changing/restorin
195 PathChanger(const wxConfigBase
*pContainer
, const wxString
& strEntry
);
199 const wxString
& Name() const { return m_strName
; }
202 wxConfigBase
*m_pContainer
; // object we live in
203 wxString m_strName
, // name of entry (i.e. name only)
204 m_strOldPath
; // saved path
205 bool m_bChanged
; // was the path changed?
209 // are we doing automatic environment variable expansion?
210 bool m_bExpandEnvVars
;
213 static wxConfigBase
*ms_pConfig
;
216 // ----------------------------------------------------------------------------
217 // the native wxConfigBase implementation
218 // ----------------------------------------------------------------------------
220 // under Windows we prefer to use the native implementation
221 #if defined(__WXMSW__) && wxCONFIG_WIN32_NATIVE
223 #define wxConfig wxRegConfig
225 #define wxConfig wxIniConfig
227 #else // either we're under Unix or wish to use files even under Windows
228 #define wxConfig wxFileConfig