]> git.saurik.com Git - wxWidgets.git/blob - include/wx/config.h
Small changes after Robert's edits
[wxWidgets.git] / include / wx / config.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: config.h
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
6 // Modified by:
7 // Created: 07.04.98 (adapted from appconf.h)
8 // RCS-ID: $Id$
9 // Copyright: (c) 1997 Karsten Ballüder Ballueder@usa.net
10 // Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
11 // Licence: wxWindows license
12 ///////////////////////////////////////////////////////////////////////////////
13
14 #ifndef _WX_CONFIG_H_
15 #define _WX_CONFIG_H_
16
17 #ifdef __GNUG__
18 #pragma interface "config.h"
19 #endif
20
21 // ----------------------------------------------------------------------------
22 // compile options
23 // ----------------------------------------------------------------------------
24
25 // it won't compile without it anyhow
26 #ifndef USE_WXCONFIG
27 #error "Please define USE_WXCONFIG or remove config.cpp from your makefile"
28 #endif // USE_WXCONFIG
29
30 // ----------------------------------------------------------------------------
31 // constants
32 // ----------------------------------------------------------------------------
33
34 /// shall we be case sensitive in parsing variable names?
35 #ifndef wxCONFIG_CASE_SENSITIVE
36 #define wxCONFIG_CASE_SENSITIVE FALSE
37 #endif
38
39 /// separates group and entry names (probably shouldn't be changed)
40 #ifndef wxCONFIG_PATH_SEPARATOR
41 #define wxCONFIG_PATH_SEPARATOR '/'
42 #endif
43
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 '!'
48 #endif
49
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
53 // (under Win16))
54 #ifndef wxCONFIG_WIN32_NATIVE
55 #define wxCONFIG_WIN32_NATIVE TRUE
56 #endif
57
58 // ----------------------------------------------------------------------------
59 // various helper global functions
60 // ----------------------------------------------------------------------------
61
62 /*
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.
66 */
67 extern wxString wxExpandEnvVars(const wxString &sz);
68
69 /*
70 Split path into parts removing '..' in progress
71 */
72 extern void wxSplitPath(wxArrayString& aParts, const char *sz);
73
74 // ----------------------------------------------------------------------------
75 // abstract base class wxConfigBase which defines the interface for derived
76 // classes
77 //
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.
81 //
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 // ----------------------------------------------------------------------------
85 class wxConfigBase
86 {
87 public:
88 // static functions
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
92 // was called
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; }
101
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() { }
107
108 // path management
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;
115
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;
121 // enumerate entries
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
125 // it's subgroups
126 virtual uint GetNumberOfEntries(bool bRecursive = FALSE) const = 0;
127 virtual uint GetNumberOfGroups(bool bRecursive = FALSE) const = 0;
128
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); }
137
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;
152
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;
158
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;
169
170 // options
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
180 {
181 wxString tmp; // Required for BC++
182 if (IsExpandingEnvVars())
183 tmp = wxExpandEnvVars(str);
184 else
185 tmp = str;
186 return tmp;
187 }
188
189 protected:
190 static bool IsImmutable(const char *szKey)
191 { return *szKey == wxCONFIG_IMMUTABLE_PREFIX; }
192
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
197 class PathChanger
198 {
199 public:
200 // ctor/dtor do path changing/restorin
201 PathChanger(const wxConfigBase *pContainer, const wxString& strEntry);
202 ~PathChanger();
203
204 // get the key name
205 const wxString& Name() const { return m_strName; }
206
207 private:
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?
212 };
213
214 private:
215 // are we doing automatic environment variable expansion?
216 bool m_bExpandEnvVars;
217 // do we record default values?
218 bool m_bRecordDefaults;
219
220 // static variables
221 static wxConfigBase *ms_pConfig;
222 static bool ms_bAutoCreate;
223 };
224
225 // ----------------------------------------------------------------------------
226 // the native wxConfigBase implementation
227 // ----------------------------------------------------------------------------
228
229 // under Windows we prefer to use the native implementation
230 #if defined(__WXMSW__) && wxCONFIG_WIN32_NATIVE
231 #ifdef __WIN32__
232 #define wxConfig wxRegConfig
233 #else //WIN16
234 #define wxConfig wxIniConfig
235 #endif
236 #else // either we're under Unix or wish to use files even under Windows
237 #define wxConfig wxFileConfig
238 #endif
239
240 #endif // _WX_CONFIG_H_
241