]> git.saurik.com Git - wxWidgets.git/blob - include/wx/config.h
trace mask made static variable
[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 _wxCONFIG_H
15 #define _wxCONFIG_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
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();
98
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() { }
104
105 // path management
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;
112
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;
118 // enumerate entries
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
122 // it's subgroups
123 virtual uint GetNumberOfEntries(bool bRecursive = FALSE) const = 0;
124 virtual uint GetNumberOfGroups(bool bRecursive = FALSE) const = 0;
125
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); }
134
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;
149
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;
155
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;
166
167 // options
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
174 {
175 wxString tmp; // Required for BC++
176 if (IsExpandingEnvVars())
177 tmp = wxExpandEnvVars(str);
178 else
179 tmp = str;
180 return tmp;
181 }
182
183 protected:
184 static bool IsImmutable(const char *szKey)
185 { return *szKey == wxCONFIG_IMMUTABLE_PREFIX; }
186
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
191 class PathChanger
192 {
193 public:
194 // ctor/dtor do path changing/restorin
195 PathChanger(const wxConfigBase *pContainer, const wxString& strEntry);
196 ~PathChanger();
197
198 // get the key name
199 const wxString& Name() const { return m_strName; }
200
201 private:
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?
206 };
207
208 private:
209 // are we doing automatic environment variable expansion?
210 bool m_bExpandEnvVars;
211
212 // static variables
213 static wxConfigBase *ms_pConfig;
214 };
215
216 // ----------------------------------------------------------------------------
217 // the native wxConfigBase implementation
218 // ----------------------------------------------------------------------------
219
220 // under Windows we prefer to use the native implementation
221 #if defined(__WXMSW__) && wxCONFIG_WIN32_NATIVE
222 #ifdef __WIN32__
223 #define wxConfig wxRegConfig
224 #else //WIN16
225 #define wxConfig wxIniConfig
226 #endif
227 #else // either we're under Unix or wish to use files even under Windows
228 #define wxConfig wxFileConfig
229 #endif
230
231 #endif //_wxCONFIG_H