]> git.saurik.com Git - wxWidgets.git/blob - include/wx/confbase.h
set eol-style
[wxWidgets.git] / include / wx / confbase.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: confbase.h
3 // Purpose: declaration of the base class of all config implementations
4 // (see also: fileconf.h and msw/regconf.h and iniconf.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 licence
12 ///////////////////////////////////////////////////////////////////////////////
13
14 #ifndef _WX_CONFBASE_H_
15 #define _WX_CONFBASE_H_
16
17 #include "wx/defs.h"
18 #include "wx/string.h"
19 #include "wx/object.h"
20 #include "wx/base64.h"
21
22 class WXDLLIMPEXP_FWD_BASE wxArrayString;
23
24 // ----------------------------------------------------------------------------
25 // constants
26 // ----------------------------------------------------------------------------
27
28 /// shall we be case sensitive in parsing variable names?
29 #ifndef wxCONFIG_CASE_SENSITIVE
30 #define wxCONFIG_CASE_SENSITIVE 0
31 #endif
32
33 /// separates group and entry names (probably shouldn't be changed)
34 #ifndef wxCONFIG_PATH_SEPARATOR
35 #define wxCONFIG_PATH_SEPARATOR _T('/')
36 #endif
37
38 /// introduces immutable entries
39 // (i.e. the ones which can't be changed from the local config file)
40 #ifndef wxCONFIG_IMMUTABLE_PREFIX
41 #define wxCONFIG_IMMUTABLE_PREFIX _T('!')
42 #endif
43
44 #if wxUSE_CONFIG
45
46 #include "wx/string.h"
47
48 /// should we use registry instead of configuration files under Windows?
49 // (i.e. whether wxConfigBase::Create() will create a wxFileConfig (if it's
50 // false) or wxRegConfig (if it's true and we're under Win32))
51 #ifndef wxUSE_CONFIG_NATIVE
52 #define wxUSE_CONFIG_NATIVE 1
53 #endif
54
55 // Style flags for constructor style parameter
56 enum
57 {
58 wxCONFIG_USE_LOCAL_FILE = 1,
59 wxCONFIG_USE_GLOBAL_FILE = 2,
60 wxCONFIG_USE_RELATIVE_PATH = 4,
61 wxCONFIG_USE_NO_ESCAPE_CHARACTERS = 8,
62 wxCONFIG_USE_SUBDIR = 16
63 };
64
65 // ----------------------------------------------------------------------------
66 // abstract base class wxConfigBase which defines the interface for derived
67 // classes
68 //
69 // wxConfig organizes the items in a tree-like structure (modeled after the
70 // Unix/Dos filesystem). There are groups (directories) and keys (files).
71 // There is always one current group given by the current path.
72 //
73 // Keys are pairs "key_name = value" where value may be of string or integer
74 // (long) type (TODO doubles and other types such as wxDate coming soon).
75 // ----------------------------------------------------------------------------
76
77 class WXDLLIMPEXP_BASE wxConfigBase : public wxObject
78 {
79 public:
80 // constants
81 // the type of an entry
82 enum EntryType
83 {
84 Type_Unknown,
85 Type_String,
86 Type_Boolean,
87 Type_Integer, // use Read(long *)
88 Type_Float // use Read(double *)
89 };
90
91 // static functions
92 // sets the config object, returns the previous pointer
93 static wxConfigBase *Set(wxConfigBase *pConfig);
94 // get the config object, creates it on demand unless DontCreateOnDemand
95 // was called
96 static wxConfigBase *Get(bool createOnDemand = true)
97 { if ( createOnDemand && (!ms_pConfig) ) Create(); return ms_pConfig; }
98 // create a new config object: this function will create the "best"
99 // implementation of wxConfig available for the current platform, see
100 // comments near definition wxUSE_CONFIG_NATIVE for details. It returns
101 // the created object and also sets it as ms_pConfig.
102 static wxConfigBase *Create();
103 // should Get() try to create a new log object if the current one is NULL?
104 static void DontCreateOnDemand() { ms_bAutoCreate = false; }
105
106 // ctor & virtual dtor
107 // ctor (can be used as default ctor too)
108 //
109 // Not all args will always be used by derived classes, but including
110 // them all in each class ensures compatibility. If appName is empty,
111 // uses wxApp name
112 wxConfigBase(const wxString& appName = wxEmptyString,
113 const wxString& vendorName = wxEmptyString,
114 const wxString& localFilename = wxEmptyString,
115 const wxString& globalFilename = wxEmptyString,
116 long style = 0);
117
118 // empty but ensures that dtor of all derived classes is virtual
119 virtual ~wxConfigBase();
120
121 // path management
122 // set current path: if the first character is '/', it's the absolute path,
123 // otherwise it's a relative path. '..' is supported. If the strPath
124 // doesn't exist it is created.
125 virtual void SetPath(const wxString& strPath) = 0;
126 // retrieve the current path (always as absolute path)
127 virtual const wxString& GetPath() const = 0;
128
129 // enumeration: all functions here return false when there are no more items.
130 // you must pass the same lIndex to GetNext and GetFirst (don't modify it)
131 // enumerate subgroups
132 virtual bool GetFirstGroup(wxString& str, long& lIndex) const = 0;
133 virtual bool GetNextGroup (wxString& str, long& lIndex) const = 0;
134 // enumerate entries
135 virtual bool GetFirstEntry(wxString& str, long& lIndex) const = 0;
136 virtual bool GetNextEntry (wxString& str, long& lIndex) const = 0;
137 // get number of entries/subgroups in the current group, with or without
138 // it's subgroups
139 virtual size_t GetNumberOfEntries(bool bRecursive = false) const = 0;
140 virtual size_t GetNumberOfGroups(bool bRecursive = false) const = 0;
141
142 // tests of existence
143 // returns true if the group by this name exists
144 virtual bool HasGroup(const wxString& strName) const = 0;
145 // same as above, but for an entry
146 virtual bool HasEntry(const wxString& strName) const = 0;
147 // returns true if either a group or an entry with a given name exist
148 bool Exists(const wxString& strName) const
149 { return HasGroup(strName) || HasEntry(strName); }
150
151 // get the entry type
152 virtual EntryType GetEntryType(const wxString& name) const
153 {
154 // by default all entries are strings
155 return HasEntry(name) ? Type_String : Type_Unknown;
156 }
157
158 // key access: returns true if value was really read, false if default used
159 // (and if the key is not found the default value is returned.)
160
161 // read a string from the key
162 bool Read(const wxString& key, wxString *pStr) const;
163 bool Read(const wxString& key, wxString *pStr, const wxString& defVal) const;
164
165 // read a number (long)
166 bool Read(const wxString& key, long *pl) const;
167 bool Read(const wxString& key, long *pl, long defVal) const;
168
169 // read an int (wrapper around `long' version)
170 bool Read(const wxString& key, int *pi) const;
171 bool Read(const wxString& key, int *pi, int defVal) const;
172
173 // read a double
174 bool Read(const wxString& key, double* val) const;
175 bool Read(const wxString& key, double* val, double defVal) const;
176
177 // read a bool
178 bool Read(const wxString& key, bool* val) const;
179 bool Read(const wxString& key, bool* val, bool defVal) const;
180
181 #if wxUSE_BASE64
182 // read a binary data block
183 bool Read(const wxString& key, wxMemoryBuffer* data) const
184 { return DoReadBinary(key, data); }
185 // no default version since it does not make sense for binary data
186 #endif // wxUSE_BASE64
187
188
189 // read other types, for which wxFromString is defined
190 template <typename T>
191 bool Read(const wxString& key, T* value) const
192 {
193 wxString s;
194 if ( !Read(key, &s) )
195 return false;
196 return wxFromString(s, value);
197 }
198
199 template <typename T>
200 bool Read(const wxString& key, T* value, const T& defVal) const
201 {
202 const bool found = Read(key, value);
203 if ( !found )
204 {
205 if (IsRecordingDefaults())
206 ((wxConfigBase *)this)->Write(key, defVal);
207 *value = defVal;
208 }
209 return found;
210 }
211
212 // convenience functions returning directly the value (we don't have them for
213 // int/double/bool as there would be ambiguities with the long one then)
214 wxString Read(const wxString& key,
215 const wxString& defVal = wxEmptyString) const
216 { wxString s; (void)Read(key, &s, defVal); return s; }
217
218 // we have to provide a separate version for C strings as otherwise the
219 // template Read() would be used
220 wxString Read(const wxString& key, const char* defVal) const
221 { return Read(key, wxString(defVal)); }
222 #if wxUSE_WCHAR_T
223 wxString Read(const wxString& key, const wchar_t* defVal) const
224 { return Read(key, wxString(defVal)); }
225 #endif
226
227 long Read(const wxString& key, long defVal) const
228 { long l; (void)Read(key, &l, defVal); return l; }
229
230 // write the value (return true on success)
231 bool Write(const wxString& key, const wxString& value)
232 { return DoWriteString(key, value); }
233
234 bool Write(const wxString& key, long value)
235 { return DoWriteLong(key, value); }
236
237 bool Write(const wxString& key, double value)
238 { return DoWriteDouble(key, value); }
239
240 bool Write(const wxString& key, bool value)
241 { return DoWriteBool(key, value); }
242
243 #if wxUSE_BASE64
244 bool Write(const wxString& key, const wxMemoryBuffer& buf)
245 { return DoWriteBinary(key, buf); }
246 #endif // wxUSE_BASE64
247
248 // we have to provide a separate version for C strings as otherwise they
249 // would be converted to bool and not to wxString as expected!
250 bool Write(const wxString& key, const char *value)
251 { return Write(key, wxString(value)); }
252 bool Write(const wxString& key, const unsigned char *value)
253 { return Write(key, wxString(value)); }
254 #if wxUSE_WCHAR_T
255 bool Write(const wxString& key, const wchar_t *value)
256 { return Write(key, wxString(value)); }
257 #endif
258
259
260 // we also have to provide specializations for other types which we want to
261 // handle using the specialized DoWriteXXX() instead of the generic template
262 // version below
263 bool Write(const wxString& key, short value)
264 { return DoWriteLong(key, value); }
265
266 bool Write(const wxString& key, unsigned short value)
267 { return DoWriteLong(key, value); }
268
269 bool Write(const wxString& key, unsigned int value)
270 { return DoWriteLong(key, value); }
271
272 bool Write(const wxString& key, int value)
273 { return DoWriteLong(key, value); }
274
275 bool Write(const wxString& key, unsigned long value)
276 { return DoWriteLong(key, value); }
277
278 bool Write(const wxString& key, float value)
279 { return DoWriteDouble(key, value); }
280
281
282 // for other types, use wxToString()
283 template <typename T>
284 bool Write(const wxString& key, T const& value)
285 { return Write(key, wxToString(value)); }
286
287 // permanently writes all changes
288 virtual bool Flush(bool bCurrentOnly = false) = 0;
289
290 // renaming, all functions return false on failure (probably because the new
291 // name is already taken by an existing entry)
292 // rename an entry
293 virtual bool RenameEntry(const wxString& oldName,
294 const wxString& newName) = 0;
295 // rename a group
296 virtual bool RenameGroup(const wxString& oldName,
297 const wxString& newName) = 0;
298
299 // delete entries/groups
300 // deletes the specified entry and the group it belongs to if
301 // it was the last key in it and the second parameter is true
302 virtual bool DeleteEntry(const wxString& key,
303 bool bDeleteGroupIfEmpty = true) = 0;
304 // delete the group (with all subgroups)
305 virtual bool DeleteGroup(const wxString& key) = 0;
306 // delete the whole underlying object (disk file, registry key, ...)
307 // primarily for use by uninstallation routine.
308 virtual bool DeleteAll() = 0;
309
310 // options
311 // we can automatically expand environment variables in the config entries
312 // (this option is on by default, you can turn it on/off at any time)
313 bool IsExpandingEnvVars() const { return m_bExpandEnvVars; }
314 void SetExpandEnvVars(bool bDoIt = true) { m_bExpandEnvVars = bDoIt; }
315 // recording of default values
316 void SetRecordDefaults(bool bDoIt = true) { m_bRecordDefaults = bDoIt; }
317 bool IsRecordingDefaults() const { return m_bRecordDefaults; }
318 // does expansion only if needed
319 wxString ExpandEnvVars(const wxString& str) const;
320
321 // misc accessors
322 wxString GetAppName() const { return m_appName; }
323 wxString GetVendorName() const { return m_vendorName; }
324
325 // Used wxIniConfig to set members in constructor
326 void SetAppName(const wxString& appName) { m_appName = appName; }
327 void SetVendorName(const wxString& vendorName) { m_vendorName = vendorName; }
328
329 void SetStyle(long style) { m_style = style; }
330 long GetStyle() const { return m_style; }
331
332 protected:
333 static bool IsImmutable(const wxString& key)
334 { return !key.IsEmpty() && key[0] == wxCONFIG_IMMUTABLE_PREFIX; }
335
336 // return the path without trailing separator, if any: this should be called
337 // to sanitize paths referring to the group names before passing them to
338 // wxConfigPathChanger as "/foo/bar/" should be the same as "/foo/bar" and it
339 // isn't interpreted in the same way by it (and this can't be changed there
340 // as it's not the same for the entries names)
341 static wxString RemoveTrailingSeparator(const wxString& key);
342
343 // do read/write the values of different types
344 virtual bool DoReadString(const wxString& key, wxString *pStr) const = 0;
345 virtual bool DoReadLong(const wxString& key, long *pl) const = 0;
346 virtual bool DoReadDouble(const wxString& key, double* val) const;
347 virtual bool DoReadBool(const wxString& key, bool* val) const;
348 #if wxUSE_BASE64
349 virtual bool DoReadBinary(const wxString& key, wxMemoryBuffer* buf) const = 0;
350 #endif // wxUSE_BASE64
351
352 virtual bool DoWriteString(const wxString& key, const wxString& value) = 0;
353 virtual bool DoWriteLong(const wxString& key, long value) = 0;
354 virtual bool DoWriteDouble(const wxString& key, double value);
355 virtual bool DoWriteBool(const wxString& key, bool value);
356 #if wxUSE_BASE64
357 virtual bool DoWriteBinary(const wxString& key, const wxMemoryBuffer& buf) = 0;
358 #endif // wxUSE_BASE64
359
360 private:
361 // are we doing automatic environment variable expansion?
362 bool m_bExpandEnvVars;
363 // do we record default values?
364 bool m_bRecordDefaults;
365
366 // static variables
367 static wxConfigBase *ms_pConfig;
368 static bool ms_bAutoCreate;
369
370 // Application name and organisation name
371 wxString m_appName;
372 wxString m_vendorName;
373
374 // Style flag
375 long m_style;
376
377 DECLARE_ABSTRACT_CLASS(wxConfigBase)
378 };
379
380 // a handy little class which changes current path to the path of given entry
381 // and restores it in dtor: so if you declare a local variable of this type,
382 // you work in the entry directory and the path is automatically restored
383 // when the function returns
384 // Taken out of wxConfig since not all compilers can cope with nested classes.
385 class WXDLLIMPEXP_BASE wxConfigPathChanger
386 {
387 public:
388 // ctor/dtor do path changing/restoring of the path
389 wxConfigPathChanger(const wxConfigBase *pContainer, const wxString& strEntry);
390 ~wxConfigPathChanger();
391
392 // get the key name
393 const wxString& Name() const { return m_strName; }
394
395 // this method must be called if the original path (i.e. the current path at
396 // the moment of creation of this object) could have been deleted to prevent
397 // us from restoring the not existing (any more) path
398 //
399 // if the original path doesn't exist any more, the path will be restored to
400 // the deepest still existing component of the old path
401 void UpdateIfDeleted();
402
403 private:
404 wxConfigBase *m_pContainer; // object we live in
405 wxString m_strName, // name of entry (i.e. name only)
406 m_strOldPath; // saved path
407 bool m_bChanged; // was the path changed?
408
409 DECLARE_NO_COPY_CLASS(wxConfigPathChanger)
410 };
411
412
413 // ----------------------------------------------------------------------------
414 // the native wxConfigBase implementation
415 // ----------------------------------------------------------------------------
416
417 // under Windows we prefer to use the native implementation
418 // wxIniConfig isn't native anywhere after droping win16 in wxWidgets 2.6
419 #if defined(__WXMSW__) && wxUSE_CONFIG_NATIVE
420 #define wxConfig wxRegConfig
421 #else // either we're under Unix or wish to use files even under Windows
422 #define wxConfig wxFileConfig
423 #endif
424
425 #endif // wxUSE_CONFIG
426
427 /*
428 Replace environment variables ($SOMETHING) with their values. The format is
429 $VARNAME or ${VARNAME} where VARNAME contains alphanumeric characters and
430 '_' only. '$' must be escaped ('\$') in order to be taken literally.
431 */
432
433 WXDLLIMPEXP_BASE wxString wxExpandEnvVars(const wxString &sz);
434
435 /*
436 Split path into parts removing '..' in progress
437 */
438 WXDLLIMPEXP_BASE void wxSplitPath(wxArrayString& aParts, const wxString& path);
439
440 #endif // _WX_CONFBASE_H_
441