]>
Commit | Line | Data |
---|---|---|
c4afa2cb VZ |
1 | \section{\class{wxConfigBase}}\label{wxconfigbase} |
2 | ||
3 | wxConfigBase class defines the basic interface of all config classes. It can | |
4 | not be used by itself (it's an abstract base class) and you'll always use one | |
5 | of it's derivations: \helpref{wxIniConfig}{wxiniconfig}, | |
6 | \helpref{wxFileConfig}{wxfileconfig}, \helpref{wxRegConfig}{wxregconfig} or | |
7 | any other. | |
8 | ||
9 | However, usually you don't even need to know the precise nature of the class | |
10 | you're working with but you would just use the wxConfigBase methods. This | |
11 | allows you to write the same code regardless of whether you're working with | |
12 | the registry under Win32 or text-based config files under Unix (or even | |
13 | Windows 3.1 .INI files if you're really unlucky). To make writing the portable | |
14 | code even easier, wxWindows provides a typedef \helpref{wxConfig}{wxconfig} | |
15 | which is mapped onto the native wxConfigBase implementation on the given | |
16 | platform: i.e. wxRegConfig under Win32, wxIniConfig under Win16 and | |
17 | wxFileConfig otherwise. | |
18 | ||
19 | See \helpref{config overview}{configoverview} for the descriptions of all | |
20 | features of this class. | |
21 | ||
22 | \wxheading{Derived from} | |
23 | ||
24 | No base class | |
25 | ||
26 | \wxheading{Example} | |
27 | ||
28 | Here is how you would typically use this class: | |
29 | ||
30 | \begin{verbatim} | |
31 | // using wxConfig instead of writing wxFileConfig or wxRegConfig enhances | |
32 | // portability of the code | |
33 | wxConfig *config = new wxConfig("MyAppName"); | |
34 | ||
35 | wxString str; | |
36 | if ( config->Read(&str, "LastPrompt") ) { | |
37 | // last prompt was found in the config file/registry and it's value is now | |
38 | // in str | |
39 | ... | |
40 | } | |
41 | else { | |
42 | // no last prompt... | |
43 | } | |
44 | ||
45 | // another example: using default values and the full path instead of just | |
46 | // key name: if the key is not found , the value 17 is returned | |
47 | long value = config->Read("/LastRun/CalculatedValues/MaxValue", -1); | |
48 | ... | |
49 | ... | |
50 | ... | |
51 | // at the end of the program we would save everything back | |
52 | config->Write("LastPrompt", str); | |
53 | config->Write("/LastRun/CalculatedValues/MaxValue", value); | |
54 | ||
55 | // the changes will be written back automatically | |
56 | delete config; | |
57 | \end{verbatim} | |
58 | ||
59 | This basic example, of course, doesn't show all wxConfig features, such as | |
60 | enumerating, testing for existence and deleting the entries and groups of | |
61 | entries in the config file, it's abilities to automatically store the default | |
62 | values or expand the environment variables on the fly. However, the main idea | |
63 | is that using this class is easy and that it should normally do what you | |
64 | expect it to. | |
65 | ||
66 | NB: in the documentation of this class, the words "config file" also mean | |
67 | "registry hive" for wxRegConfig and, generally speaking, might mean any | |
68 | physical storage where a wxConfigBase-derived class stores it's data. | |
69 | ||
70 | \wxheading{See also} | |
71 | ||
72 | \helpref{wxIniConfig}{wxiniconfig}, \helpref{wxFileConfig}{wxfileconfig}, | |
73 | \helpref{wxRegConfig}{wxregconfig} | |
74 | ||
75 | \latexignore{\rtfignore{\wxheading{Members}}} | |
76 | ||
77 | \membersection{static functions} | |
78 | ||
79 | These function deal with the "default" config object. Although it's usage is | |
80 | not at all mandatory it may be convenient to use a global config object | |
81 | instead of creating and deleting the local config objects each time you need | |
82 | one (especially because creating a wxFileConfig object might be a time | |
83 | consuming operation). In this case, you may create this global config object | |
84 | in the very start of the program and {\it Set()} it as the default. Then, from | |
85 | anywhere in your program, you may access it using the {\it Get()} function. Of | |
86 | course, you should delete it on the program termination (otherwise, not only a | |
87 | memory leak will result, but even more importantly the changes won't be | |
88 | written back!). | |
89 | ||
90 | As it happens, you may even further simplify the procedure described above: | |
91 | you may forget about calling {\it Set()}. When {\it Get()} is called and there | |
92 | is no current object, it will create one using {\it Create()} function. To | |
93 | disable this behaviour {\it DontCreateOnDemand()} is provided. | |
94 | ||
95 | \func{wxConfigBase *}{Set}{\param{wxConfigBase *}{pConfig}} | |
96 | ||
97 | Sets the config object as the current one, returns the pointer to the previous | |
98 | current object (both the parameter and returned value may be NULL) | |
99 | ||
100 | \func{wxConfigBase *}{Get}{\void} | |
101 | ||
102 | Get the current config object. If there is no current object, creates one | |
103 | (using {\it Create}) unless DontCreateOnDemand was called previously. | |
104 | ||
105 | \func{wxConfigBase *}{Create}{\void} | |
106 | ||
107 | Create a new config object: this function will create the "best" | |
108 | implementation of wxConfig available for the current platform, see | |
109 | comments near definition wxCONFIG\_WIN32\_NATIVE for details. It returns | |
110 | the created object and also sets it as the current one. | |
111 | ||
112 | \func{void}{DontCreateOnDemand}{\void} | |
113 | ||
114 | Calling this function will prevent {\it Get()} from automatically creating a | |
115 | new config object if the current one is NULL. It might be useful to call it | |
116 | near the program end to prevent new config object "accidental" creation. | |
117 | ||
118 | \membersection{ctor and dtor} | |
119 | ||
120 | \func{}{wxConfigBase}{\void} | |
121 | ||
122 | This is the default and only constructor of wxConfigBase class. It initializes | |
123 | it options: by default, environment variable expansion is on and recording | |
124 | defaults is off. | |
125 | ||
126 | \func{}{\destruct{wxConfigBase}}{\void} | |
127 | ||
128 | Empty but ensures that dtor of all derived classes is virtual. | |
129 | ||
130 | \membersection{path management} | |
131 | ||
132 | As explained in \helpref{config overview}{configoverview}, the config classes | |
133 | support a file system-like hierarchy of keys (files) and groups (directories). | |
134 | As in the file system case, to specify a key in the config class you must use | |
135 | a path to it. Config classes also support the notion of the current group, | |
136 | which makes it possible to use the relative paths. To clarify all this, here | |
137 | is an example (it's only for the sake of demonstration, it doesn't do anything | |
138 | sensible!): | |
139 | ||
140 | \begin{verbatim} | |
141 | wxConfig *config = new wxConfig("FooBarApp"); | |
142 | ||
143 | // right now the current path is '/' | |
144 | conf->Write("RootEntry", 1); | |
145 | ||
146 | // go to some other place: if the group(s) don't exist, they will be created | |
147 | conf->SetPath("/Group/Subgroup"); | |
148 | ||
149 | // create an entry in subgroup | |
150 | conf->Write("SubgroupEntry", 3); | |
151 | ||
152 | // '..' is understood | |
153 | conf->Write("../GroupEntry", 2); | |
154 | conf->SetPath(".."); | |
155 | ||
156 | wxASSERT( conf->Read("Subgroup/SubgroupEntry", 0l) == 3 ); | |
157 | ||
158 | // use absolute path: it's allowed, too | |
159 | wxASSERT( conf->Read("/RootEntry", 0l) == 1 ); | |
160 | \end{verbatim} | |
161 | ||
162 | {\it Warning}: it's probably a good idea to always restore the path to it's | |
163 | old value on function exit: | |
164 | \begin{verbatim} | |
165 | void foo(wxConfigBase *config) | |
166 | { | |
167 | wxString strOldPath = config->GetPath(); | |
168 | ||
169 | config->SetPath("/Foo/Data"); | |
170 | ... | |
171 | ||
172 | config->SetPath(strOldPath); | |
173 | } | |
174 | \end{verbatim} | |
175 | ||
176 | because otherwise the assert in the following example will surely fail | |
177 | (we suppose here that {\it foo()} function is the same as above except that it | |
178 | doesn't save and restore the path): | |
179 | ||
180 | \begin{verbatim} | |
181 | void bar(wxConfigBase *config) | |
182 | { | |
183 | config->Write("Test", 17); | |
184 | ||
185 | foo(config); | |
186 | ||
187 | // we're reading "/Foo/Data/Test" here! -1 will probably be returned... | |
188 | wxASSERT( config->Read("Test", -1) == 17 ); | |
189 | } | |
190 | \end{verbatim} | |
191 | ||
192 | Finally, the path separator in wxConfigBase and derived classes is always '/', | |
193 | regardless of the platform (i.e. it's {\bf not} '\\' under Windows). | |
194 | ||
195 | \func{void}{SetPath}{\param{const wxString& }{strPath}} | |
196 | ||
197 | Set current path: if the first character is '/', it's the absolute path, | |
198 | otherwise it's a relative path. '..' is supported. If the strPath | |
199 | doesn't exist it is created. | |
200 | ||
201 | \func{const wxString& }{GetPath}{\void} const | |
202 | ||
203 | Retrieve the current path (always as absolute path). | |
204 | ||
205 | \membersection{enumeration} | |
206 | ||
207 | The functions in this section allow to enumerate all entries and groups in the | |
208 | config file. All functions here return false when there are no more items. | |
209 | ||
210 | You must pass the same lIndex to GetNext and GetFirst (don't modify it). | |
211 | Please note that it's {\bf not} the index of the current item (you will have | |
212 | some great surprizes with wxRegConfig if you assume this) and you shouldn't | |
213 | even look at it: it's just a "cookie" which stores the state of the | |
214 | enumeration. It can't be stored inside the class because it would prevent you | |
215 | from running several enumerations simultaneously, that's why you must pass it | |
216 | explicitly. | |
217 | ||
218 | Having said all this, enumerating the config entries/groups is very simple: | |
219 | ||
220 | \begin{verbatim} | |
221 | wxArrayString aNames; | |
222 | ||
223 | // enumeration variables | |
224 | wxString str; | |
225 | long dummy; | |
226 | ||
227 | // first enum all entries | |
228 | bool bCont = config->GetFirstEntry(str, dummy); | |
229 | while ( bCont ) { | |
230 | aNames.Add(str); | |
231 | ||
232 | bCont = GetConfig()->GetNextEntry(str, dummy); | |
233 | } | |
234 | ||
235 | ... we have all entry names in aNames... | |
236 | ||
237 | // now all groups... | |
238 | bCont = GetConfig()->GetFirstGroup(str, dummy); | |
239 | while ( bCont ) { | |
240 | aNames.Add(str); | |
241 | ||
242 | bCont = GetConfig()->GetNextGroup(str, dummy); | |
243 | } | |
244 | ||
245 | ... we have all group (and entry) names in aNames... | |
246 | ||
247 | \end{verbatim} | |
248 | ||
249 | There are also functions to get th number of entries/subgroups without | |
250 | actually enumerating them, but you will probably never need them. | |
251 | ||
252 | \func{bool }{GetFirstGroup}{\param{wxString& }{str}, \param{long&}{lIndex}} const | |
253 | ||
254 | \func{bool }{GetNextGroup}{\param{wxString& }{str}, \param{long&}{lIndex}} const | |
255 | ||
256 | \func{bool }{GetFirstEntry}{\param{wxString& }{str}, \param{long&}{lIndex}} const | |
257 | ||
258 | \func{bool }{GetNextEntry}{\param{wxString& }{str}, \param{long&}{lIndex}} const | |
259 | ||
260 | See the example above. | |
261 | ||
262 | \func{uint }{GetNumberOfEntries}{\param{bool }{bRecursive = FALSE}} const | |
263 | ||
264 | \func{uint }{GetNumberOfGroups}{\param{bool }{bRecursive = FALSE}} const | |
265 | ||
266 | Get number of entries/subgroups in the current group, with or without | |
267 | it's subgroups. | |
268 | ||
269 | \membersection{tests of existence} | |
270 | ||
271 | \func{bool }{HasGroup}{\param{const wxString& }{strName}} const | |
272 | ||
273 | returns TRUE if the group by this name exists | |
274 | ||
275 | \func{bool }{HasEntryconst }{\param{wxString& }{strName}} const | |
276 | ||
277 | returns TRUE if the entry by this name exists | |
278 | ||
279 | \func{bool }{Existsconst }{\param{wxString& }{strName}} const | |
280 | ||
281 | returns TRUE if either a group or an entry with a given name exists | |
282 | ||
283 | \membersection{key access} | |
284 | ||
285 | These function are the core of wxConfigBase class: they allow you to read and | |
286 | write config file data. All {\it Read} function take a default value which | |
287 | will be returned if the specified key is not found in the config file. | |
288 | ||
289 | Currently, only two types of data are supported: string and long (but it might | |
290 | change in the near future). To work with other types: for {\it int} or {\it | |
291 | bool} you can work with function taking/returning {\it long} and just use the | |
292 | casts. Better yet, just use {\it long} for all variables which you're going to | |
293 | save in the config file: chances are that \verb{sizeof(bool) == sizeof(int) == | |
294 | sizeof(long)} anyhow on your system. For {\it float}, {\it double} and, in | |
295 | general, any other type you'd have to translate them to/from string | |
296 | representation and use string functions. | |
297 | ||
298 | Try not to read long values into string variables and vice versa: although it | |
299 | just might work with wxFileConfig, you will get a system error with | |
300 | wxRegConfig because in the Windows registry the different types of entries are | |
301 | indeed used. | |
302 | ||
303 | Final remark: the {\it szKey} parameter for all these functions can contain an | |
304 | arbitrary path (either relative or absolute), not just the key name. | |
305 | ||
306 | \func{bool }{Read}{\param{wxString *}{pStr}, \param{const char *}{szKey}, \param{const char *}{szDefault = NULL}} const | |
307 | ||
308 | Read a string from the key: returns TRUE if value was really read, FALSE if | |
309 | default was used (the default value is returned if the key wasn't found) | |
310 | ||
311 | \func{const char *}{Read }{\param{const char *}{szKey}, \param{const char *}{szDefault = NULL}} const | |
312 | ||
313 | Another version of {\it Read()} using static buffer. | |
314 | ||
315 | NB: although this function seems more convenient, you should only use it if | |
316 | you know what you're doing! As it uses a static buffer, it means it will be | |
317 | overwritten after each call to this function! If you do use it, you should | |
318 | save the returned value to a wxString as soon as possible. | |
319 | ||
320 | \func{long }{Read}{\param{const char *}{szKey}, \param{long}{lDefault}} const | |
321 | ||
322 | Read a long value from the key and return it (lDefault is returned if key not | |
323 | found). | |
324 | ||
325 | NB: writing | |
326 | \begin{verbatim} | |
327 | conf->Read("key", 0); | |
328 | \end{verbatim} | |
329 | won't work because the call is ambiguous: compiler can not choose between 2 | |
330 | {\it Read} functions. Instead, just write | |
331 | \begin{verbatim} | |
332 | conf->Read("key", 0l); | |
333 | \end{verbatim} | |
334 | ||
335 | \func{bool }{Read }{\param{long *}{pl}, \param{const char *}{szKey}, \param{long}{lDefault = 0}} const | |
336 | ||
337 | Another function for reading long values, it returns TRUE if the key was | |
338 | really read from config file and FALSE if the default value was used. | |
339 | ||
340 | \func{bool}{Write}{\param{const char *}{szKey}, \param{const char *}{szValue}} | |
341 | \func{bool}{Write}{\param{const char *}{szKey}, \param{long}{lValue}} | |
342 | ||
343 | Both of these functions write the specified value to the config file and | |
344 | return true on success. | |
345 | ||
346 | \func{bool }{Flush}{\param{bool }{bCurrentOnly = FALSE}} | |
347 | ||
348 | permanently writes all changes (otherwise, they're only written from object's | |
349 | destructor) | |
350 | ||
351 | \membersection{delete entries/groups} | |
352 | ||
353 | The functions in this section delete entries and/or groups of entries from the | |
354 | config file. {\it DeleteAll()} is especially useful if you want to erase all | |
355 | traces of your program presence: for example, when you uninstall it. | |
356 | ||
357 | \func{bool}{DeleteEntry}{\param{const char *}{szKey}, \param{bool}{bDeleteGroupIfEmpty = TRUE}} | |
358 | ||
359 | Deletes the specified entry and the group it belongs to if | |
360 | it was the last key in it and the second parameter is true. | |
361 | ||
362 | \func{bool }{DeleteGroup}{\param{const char *}{szKey}} | |
363 | ||
364 | Delete the group (with all subgroups) | |
365 | ||
366 | \func{bool }{DeleteAll}{\void} | |
367 | ||
368 | Delete the whole underlying object (disk file, registry key, ...). Primarly | |
369 | for use by desinstallation routine. | |
370 | ||
371 | \membersection{options} | |
372 | ||
373 | Some aspects of wxConfigBase behaviour can be changed during run-time. The | |
374 | first of them is the expansion of environment variables in the string values | |
375 | read from the config file: for example, if you have the following in your | |
376 | config file: | |
377 | ||
378 | \begin{verbatim} | |
379 | # config file for my program | |
380 | UserData = $HOME/data | |
381 | ||
382 | # the following syntax is valud only under Windows | |
383 | UserData = %windir%\\data.dat | |
384 | \end{verbatim} | |
385 | ||
386 | the call to \verb{config->Read("UserData")} will return something like | |
387 | \verb{"/home/zeitlin/data"} if you're lucky enough to run a Linux system ;-) | |
388 | ||
389 | Although this feature is very useful, it may be annoying if you read a value | |
390 | which containts '\$' or '\%' symbols (\% is used for environment variables | |
391 | expansion under Windows) which are not used for environment variable | |
392 | expansion. In this situation you may call SetExpandEnvVars(FALSE) just before | |
393 | reading this value and SetExpandEnvVars(TRUE) just after. Another solution | |
394 | would be to prefix the offending symbols with a backslash. | |
395 | ||
396 | Following functions control this option: | |
397 | ||
398 | \func{bool }{IsExpandingEnvVars}{\void} const | |
399 | ||
400 | \func{void }{SetExpandEnvVars }{\param{bool }{bDoIt = TRUE}} | |
401 | ||
402 | The second option is recording of the default values: if it's on (default | |
403 | is off) all default values are written back to the config file. This allows | |
404 | the user to see what config options may be changed and is probably useful only | |
405 | for wxFileConfig. | |
406 | ||
407 | Following functions control this option: | |
408 | ||
409 | \func{void }{SetRecordDefaults }{\param{bool }{bDoIt = TRUE}} | |
410 | ||
411 | \func{bool }{IsRecordingDefaults }{\void} const |