]>
Commit | Line | Data |
---|---|---|
23324ae1 FM |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: config.h | |
e54c96f1 | 3 | // Purpose: interface of wxConfigBase |
23324ae1 FM |
4 | // Author: wxWidgets team |
5 | // RCS-ID: $Id$ | |
6 | // Licence: wxWindows license | |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
9 | /** | |
10 | @class wxConfigBase | |
11 | @wxheader{config.h} | |
7c913512 | 12 | |
bd0812fe BP |
13 | wxConfigBase defines the basic interface of all config classes. It can not |
14 | be used by itself (it is an abstract base class) and you will always use | |
15 | one of its derivations: wxFileConfig, wxRegConfig or any other. | |
16 | ||
17 | However, usually you don't even need to know the precise nature of the | |
18 | class you're working with but you would just use the wxConfigBase methods. | |
19 | This allows you to write the same code regardless of whether you're working | |
20 | with the registry under Win32 or text-based config files under Unix (or | |
21 | even Windows 3.1 .INI files if you're really unlucky). To make writing the | |
22 | portable code even easier, wxWidgets provides a typedef wxConfig which is | |
23 | mapped onto the native wxConfigBase implementation on the given platform: | |
24 | i.e. wxRegConfig under Win32 and wxFileConfig otherwise. | |
25 | ||
26 | See @ref overview_config for a description of all features of this class. | |
27 | ||
28 | It is highly recommended to use static functions Get() and/or Set(), so | |
29 | please have a look at them. | |
30 | ||
31 | Related Include Files: | |
32 | ||
33 | @li @c <wx/config.h> - Let wxWidgets choose a wxConfig class for your | |
34 | platform. | |
35 | @li @c <wx/confbase.h> - Base config class. | |
36 | @li @c <wx/fileconf.h> - wxFileConfig class. | |
bbc5b7f8 | 37 | @li @c <wx/msw/regconf.h> - wxRegConfig class, see also wxRegKey. |
bd0812fe BP |
38 | |
39 | ||
40 | @section configbase_example Example | |
41 | ||
42 | Here is how you would typically use this class: | |
43 | ||
44 | @code | |
45 | // using wxConfig instead of writing wxFileConfig or wxRegConfig enhances | |
46 | // portability of the code | |
47 | wxConfig *config = new wxConfig("MyAppName"); | |
48 | ||
49 | wxString str; | |
50 | if ( config->Read("LastPrompt", &str) ) { | |
51 | // last prompt was found in the config file/registry and its value is | |
52 | // now in str | |
53 | // ... | |
54 | } | |
55 | else { | |
56 | // no last prompt... | |
57 | } | |
58 | ||
59 | // another example: using default values and the full path instead of just | |
60 | // key name: if the key is not found , the value 17 is returned | |
61 | long value = config->ReadLong("/LastRun/CalculatedValues/MaxValue", 17); | |
62 | ||
63 | // at the end of the program we would save everything back | |
64 | config->Write("LastPrompt", str); | |
65 | config->Write("/LastRun/CalculatedValues/MaxValue", value); | |
66 | ||
67 | // the changes will be written back automatically | |
68 | delete config; | |
69 | @endcode | |
70 | ||
71 | This basic example, of course, doesn't show all wxConfig features, such as | |
72 | enumerating, testing for existence and deleting the entries and groups of | |
73 | entries in the config file, its abilities to automatically store the | |
74 | default values or expand the environment variables on the fly. However, the | |
75 | main idea is that using this class is easy and that it should normally do | |
76 | what you expect it to. | |
77 | ||
78 | @note In the documentation of this class, the words "config file" also mean | |
79 | "registry hive" for wxRegConfig and, generally speaking, might mean | |
80 | any physical storage where a wxConfigBase-derived class stores its | |
81 | data. | |
82 | ||
83 | ||
84 | @section configbase_static Static Functions | |
85 | ||
86 | The static functions provided deal with the "default" config object. | |
87 | Although its usage is not at all mandatory it may be convenient to use a | |
88 | global config object instead of creating and deleting the local config | |
89 | objects each time you need one (especially because creating a wxFileConfig | |
90 | object might be a time consuming operation). In this case, you may create | |
91 | this global config object in the very start of the program and Set() it as | |
92 | the default. Then, from anywhere in your program, you may access it using | |
93 | the Get() function. This global wxConfig object will be deleted by | |
94 | wxWidgets automatically if it exists. Note that this implies that if you do | |
95 | delete this object yourself (usually in wxApp::OnExit()) you must use | |
96 | Set(@NULL) to prevent wxWidgets from deleting it the second time. | |
97 | ||
98 | As it happens, you may even further simplify the procedure described above: | |
99 | you may forget about calling Set(). When Get() is called and there is no | |
100 | current object, it will create one using Create() function. To disable this | |
101 | behaviour DontCreateOnDemand() is provided. | |
102 | ||
103 | @note You should use either Set() or Get() because wxWidgets library itself | |
104 | would take advantage of it and could save various information in it. | |
105 | For example wxFontMapper or Unix version of wxFileDialog have the | |
106 | ability to use wxConfig class. | |
107 | ||
108 | ||
109 | @section configbase_paths Path Management | |
110 | ||
111 | As explained in the @ref overview_config "config overview", the config | |
112 | classes support a file system-like hierarchy of keys (files) and groups | |
113 | (directories). As in the file system case, to specify a key in the config | |
114 | class you must use a path to it. Config classes also support the notion of | |
115 | the current group, which makes it possible to use the relative paths. To | |
116 | clarify all this, here is an example (it is only for the sake of | |
117 | demonstration, it doesn't do anything sensible!): | |
118 | ||
119 | @code | |
120 | wxConfig *config = new wxConfig("FooBarApp"); | |
121 | ||
122 | // right now the current path is '/' | |
123 | conf->Write("RootEntry", 1); | |
124 | ||
125 | // go to some other place: if the group(s) don't exist, they will be created | |
126 | conf->SetPath("/Group/Subgroup"); | |
127 | ||
128 | // create an entry in subgroup | |
129 | conf->Write("SubgroupEntry", 3); | |
130 | ||
131 | // '..' is understood | |
132 | conf->Write("../GroupEntry", 2); | |
133 | conf->SetPath(".."); | |
134 | ||
135 | wxASSERT( conf->ReadLong("Subgroup/SubgroupEntry", 0) == 3 ); | |
136 | ||
137 | // use absolute path: it is allowed, too | |
138 | wxASSERT( conf->ReadLong("/RootEntry", 0) == 1 ); | |
139 | @endcode | |
140 | ||
141 | It is highly recommended that you restore the path to its old value on | |
142 | function exit: | |
143 | ||
144 | @code | |
145 | void foo(wxConfigBase *config) | |
146 | { | |
147 | wxString strOldPath = config->GetPath(); | |
148 | ||
149 | config->SetPath("/Foo/Data"); | |
150 | // ... | |
151 | ||
152 | config->SetPath(strOldPath); | |
153 | } | |
154 | @endcode | |
155 | ||
156 | Otherwise the assert in the following example will surely fail (we suppose | |
157 | here that the foo() function is the same as above except that it doesn’t | |
158 | save and restore the path): | |
159 | ||
160 | @code | |
161 | void bar(wxConfigBase *config) | |
162 | { | |
163 | config->Write("Test", 17); | |
164 | ||
165 | foo(config); | |
166 | ||
167 | // we're reading "/Foo/Data/Test" here! -1 will probably be returned... | |
168 | wxASSERT( config->ReadLong("Test", -1) == 17 ); | |
169 | } | |
170 | @endcode | |
171 | ||
172 | Finally, the path separator in wxConfigBase and derived classes is always | |
173 | "/", regardless of the platform (i.e. it is not "\\" under Windows). | |
174 | ||
175 | ||
176 | @section configbase_enumeration Enumeration | |
177 | ||
178 | The enumeration functions allow you to enumerate all entries and groups in | |
179 | the config file. All functions here return @false when there are no more | |
180 | items. | |
181 | ||
182 | You must pass the same index to GetNext() and GetFirst() (don't modify it). | |
183 | Please note that it is not the index of the current item (you will have | |
184 | some great surprises with wxRegConfig if you assume this) and you shouldn't | |
185 | even look at it: it is just a "cookie" which stores the state of the | |
186 | enumeration. It can't be stored inside the class because it would prevent | |
187 | you from running several enumerations simultaneously, that's why you must | |
188 | pass it explicitly. | |
189 | ||
190 | Having said all this, enumerating the config entries/groups is very simple: | |
191 | ||
192 | @code | |
193 | wxConfigBase *config = ...; | |
194 | wxArrayString aNames; | |
195 | ||
196 | // enumeration variables | |
197 | wxString str; | |
198 | long dummy; | |
199 | ||
200 | // first enum all entries | |
201 | bool bCont = config->GetFirstEntry(str, dummy); | |
202 | while ( bCont ) { | |
203 | aNames.Add(str); | |
204 | ||
205 | bCont = GetConfig()->GetNextEntry(str, dummy); | |
206 | } | |
207 | ||
208 | // ... we have all entry names in aNames... | |
209 | ||
210 | // now all groups... | |
211 | bCont = GetConfig()->GetFirstGroup(str, dummy); | |
212 | while ( bCont ) { | |
213 | aNames.Add(str); | |
214 | ||
215 | bCont = GetConfig()->GetNextGroup(str, dummy); | |
216 | } | |
217 | ||
218 | // ... we have all group (and entry) names in aNames... | |
219 | @endcode | |
220 | ||
221 | There are also functions to get the number of entries/subgroups without | |
222 | actually enumerating them, but you will probably never need them. | |
223 | ||
224 | ||
225 | @section configbase_keyaccess Key Access | |
226 | ||
227 | The key access functions are the core of wxConfigBase class: they allow you | |
228 | to read and write config file data. All Read() functions take a default | |
229 | value which will be returned if the specified key is not found in the | |
230 | config file. | |
231 | ||
232 | Currently, supported types of data are: wxString, @c long, @c double, | |
233 | @c bool, wxColour and any other types for which the functions | |
234 | wxToString() and wxFromString() are defined. | |
235 | ||
236 | Try not to read long values into string variables and vice versa: | |
237 | although it just might work with wxFileConfig, you will get a system | |
238 | error with wxRegConfig because in the Windows registry the different | |
239 | types of entries are indeed used. | |
240 | ||
241 | Final remark: the @a szKey parameter for all these functions can | |
242 | contain an arbitrary path (either relative or absolute), not just the | |
243 | key name. | |
244 | ||
245 | @beginWxPythonOnly | |
246 | In place of a single overloaded method name, wxPython implements the | |
247 | following methods: | |
248 | - Read(key, default="") - Returns a string. | |
249 | - ReadInt(key, default=0) - Returns an integer. | |
250 | - ReadFloat(key, default=0.0) - Returns a floating point number. | |
251 | - ReadBool(key, default=0) - Returns a boolean. | |
252 | - Write(key, value) - Writes a string. | |
253 | - WriteInt(key, value) - Writes an int. | |
254 | - WriteFloat(key, value) - Writes a floating point number. | |
255 | @endWxPythonOnly | |
256 | ||
7c913512 | 257 | |
23324ae1 FM |
258 | @library{wxbase} |
259 | @category{misc} | |
260 | */ | |
261 | class wxConfigBase : public wxObject | |
262 | { | |
263 | public: | |
264 | /** | |
23324ae1 FM |
265 | This is the default and only constructor of the wxConfigBase class, and |
266 | derived classes. | |
3c4f71cc | 267 | |
7c913512 | 268 | @param appName |
bd0812fe BP |
269 | The application name. If this is empty, the class will normally use |
270 | wxApp::GetAppName() to set it. The application name is used in the | |
271 | registry key on Windows, and can be used to deduce the local | |
272 | filename parameter if that is missing. | |
7c913512 | 273 | @param vendorName |
bd0812fe BP |
274 | The vendor name. If this is empty, it is assumed that no vendor |
275 | name is wanted, if this is optional for the current config class. | |
276 | The vendor name is appended to the application name for | |
277 | wxRegConfig. | |
7c913512 | 278 | @param localFilename |
bd0812fe BP |
279 | Some config classes require a local filename. If this is not |
280 | present, but required, the application name will be used instead. | |
7c913512 | 281 | @param globalFilename |
bd0812fe BP |
282 | Some config classes require a global filename. If this is not |
283 | present, but required, the application name will be used instead. | |
7c913512 | 284 | @param style |
bd0812fe BP |
285 | Can be one of wxCONFIG_USE_LOCAL_FILE and wxCONFIG_USE_GLOBAL_FILE. |
286 | The style interpretation depends on the config class and is ignored | |
287 | by some implementations. For wxFileConfig, these styles determine | |
288 | whether a local or global config file is created or used: if | |
289 | wxCONFIG_USE_GLOBAL_FILE is used, then settings are read from the | |
290 | global config file and if wxCONFIG_USE_LOCAL_FILE is used, settings | |
291 | are read from and written to local config file (if they are both | |
292 | set, global file is read first, then local file, overwriting global | |
293 | settings). If the flag is present but the parameter is empty, the | |
294 | parameter will be set to a default. If the parameter is present but | |
295 | the style flag not, the relevant flag will be added to the style. | |
296 | For wxRegConfig, thie GLOBAL flag refers to HKLM key while LOCAL | |
297 | one is for the usual HKCU one. | |
298 | @n For wxFileConfig you can also add wxCONFIG_USE_RELATIVE_PATH by | |
299 | logically or'ing it to either of the _FILE options to tell | |
300 | wxFileConfig to use relative instead of absolute paths. | |
301 | @n On non-VMS Unix systems, the default local configuration file is | |
302 | "~/.appname". However, this path may be also used as user data | |
303 | directory (see wxStandardPaths::GetUserDataDir()) if the | |
304 | application has several data files. In this case | |
305 | wxCONFIG_USE_SUBDIR flag, which changes the default local | |
306 | configuration file to "~/.appname/appname" should be used. Notice | |
307 | that this flag is ignored if localFilename is provided. | |
308 | wxCONFIG_USE_SUBDIR is new since wxWidgets version 2.8.2. | |
309 | @n For wxFileConfig, you can also add | |
310 | wxCONFIG_USE_NO_ESCAPE_CHARACTERS which will turn off character | |
311 | escaping for the values of entries stored in the config file: for | |
312 | example a foo key with some backslash characters will be stored as | |
313 | "foo=C:\mydir" instead of the usual storage of "foo=C:\\mydir". | |
314 | @n The wxCONFIG_USE_NO_ESCAPE_CHARACTERS style can be helpful if your | |
315 | config file must be read or written to by a non-wxWidgets program | |
316 | (which might not understand the escape characters). Note, however, | |
317 | that if wxCONFIG_USE_NO_ESCAPE_CHARACTERS style is used, it is is | |
318 | now your application's responsibility to ensure that there is no | |
319 | newline or other illegal characters in a value, before writing that | |
320 | value to the file. | |
7c913512 | 321 | @param conv |
bd0812fe BP |
322 | This parameter is only used by wxFileConfig when compiled in |
323 | Unicode mode. It specifies the encoding in which the configuration | |
324 | file is written. | |
3c4f71cc | 325 | |
23324ae1 | 326 | @remarks By default, environment variable expansion is on and recording |
4cc4bfaf | 327 | defaults is off. |
23324ae1 FM |
328 | */ |
329 | wxConfigBase(const wxString& appName = wxEmptyString, | |
330 | const wxString& vendorName = wxEmptyString, | |
331 | const wxString& localFilename = wxEmptyString, | |
332 | const wxString& globalFilename = wxEmptyString, | |
bd0812fe BP |
333 | long style = 0, |
334 | const wxMBConv& conv = wxConvAuto()); | |
23324ae1 FM |
335 | |
336 | /** | |
337 | Empty but ensures that dtor of all derived classes is virtual. | |
338 | */ | |
339 | ~wxConfigBase(); | |
340 | ||
23324ae1 FM |
341 | |
342 | /** | |
bd0812fe | 343 | @name Path Management |
23324ae1 | 344 | |
bd0812fe | 345 | See @ref configbase_paths |
23324ae1 | 346 | */ |
bd0812fe | 347 | //@{ |
23324ae1 FM |
348 | |
349 | /** | |
bd0812fe | 350 | Retrieve the current path (always as absolute path). |
23324ae1 | 351 | */ |
bd0812fe | 352 | const wxString GetPath() const; |
23324ae1 FM |
353 | |
354 | /** | |
bd0812fe BP |
355 | Set current path: if the first character is '/', it is the absolute |
356 | path, otherwise it is a relative path. '..' is supported. If @a strPath | |
357 | doesn't exist it is created. | |
23324ae1 | 358 | */ |
bd0812fe | 359 | void SetPath(const wxString& strPath); |
23324ae1 | 360 | |
bd0812fe | 361 | //@} |
23324ae1 | 362 | |
23324ae1 FM |
363 | |
364 | /** | |
bd0812fe | 365 | @name Enumeration |
23324ae1 | 366 | |
bd0812fe | 367 | See @ref configbase_enumeration |
23324ae1 | 368 | */ |
bd0812fe | 369 | //@{ |
23324ae1 FM |
370 | |
371 | /** | |
372 | Gets the first entry. | |
bd0812fe BP |
373 | |
374 | @beginWxPythonOnly | |
375 | The wxPython version of this method returns a 3-tuple consisting of the | |
376 | continue flag, the value string, and the index for the next call. | |
377 | @endWxPythonOnly | |
23324ae1 | 378 | */ |
328f5751 | 379 | bool GetFirstEntry(wxString& str, long& index) const; |
23324ae1 FM |
380 | |
381 | /** | |
382 | Gets the first group. | |
bd0812fe BP |
383 | |
384 | @beginWxPythonOnly | |
385 | The wxPython version of this method returns a 3-tuple consisting of the | |
386 | continue flag, the value string, and the index for the next call. | |
387 | @endWxPythonOnly | |
23324ae1 | 388 | */ |
328f5751 | 389 | bool GetFirstGroup(wxString& str, long& index) const; |
23324ae1 FM |
390 | |
391 | /** | |
392 | Gets the next entry. | |
bd0812fe BP |
393 | |
394 | @beginWxPythonOnly | |
395 | The wxPython version of this method returns a 3-tuple consisting of the | |
396 | continue flag, the value string, and the index for the next call. | |
397 | @endWxPythonOnly | |
23324ae1 | 398 | */ |
328f5751 | 399 | bool GetNextEntry(wxString& str, long& index) const; |
23324ae1 FM |
400 | |
401 | /** | |
402 | Gets the next group. | |
bd0812fe BP |
403 | |
404 | @beginWxPythonOnly | |
405 | The wxPython version of this method returns a 3-tuple consisting of the | |
406 | continue flag, the value string, and the index for the next call. | |
407 | @endWxPythonOnly | |
23324ae1 | 408 | */ |
328f5751 | 409 | bool GetNextGroup(wxString& str, long& index) const; |
23324ae1 FM |
410 | |
411 | /** | |
bd0812fe | 412 | Get number of entries in the current group. |
23324ae1 | 413 | */ |
328f5751 | 414 | uint GetNumberOfEntries(bool bRecursive = false) const; |
23324ae1 FM |
415 | |
416 | /** | |
bd0812fe BP |
417 | Get number of entries/subgroups in the current group, with or without |
418 | its subgroups. | |
23324ae1 | 419 | */ |
328f5751 | 420 | uint GetNumberOfGroups(bool bRecursive = false) const; |
23324ae1 | 421 | |
bd0812fe BP |
422 | //@} |
423 | ||
424 | ||
425 | enum EntryType | |
426 | { | |
427 | Type_Unknown, | |
428 | Type_String, | |
429 | Type_Boolean, | |
430 | Type_Integer, | |
431 | Type_Float | |
432 | }; | |
23324ae1 FM |
433 | |
434 | /** | |
bd0812fe | 435 | @name Tests of Existence |
23324ae1 | 436 | */ |
bd0812fe | 437 | //@{ |
23324ae1 FM |
438 | |
439 | /** | |
d29a9a8a | 440 | @return @true if either a group or an entry with a given name exists. |
23324ae1 | 441 | */ |
bd0812fe | 442 | bool Exists(wxString& strName) const; |
23324ae1 FM |
443 | |
444 | /** | |
bd0812fe BP |
445 | Returns the type of the given entry or @e Unknown if the entry doesn't |
446 | exist. This function should be used to decide which version of Read() | |
447 | should be used because some of wxConfig implementations will complain | |
448 | about type mismatch otherwise: e.g., an attempt to read a string value | |
449 | from an integer key with wxRegConfig will fail. | |
23324ae1 | 450 | */ |
bd0812fe | 451 | wxConfigBase::EntryType GetEntryType(const wxString& name) const; |
23324ae1 FM |
452 | |
453 | /** | |
d29a9a8a | 454 | @return @true if the entry by this name exists. |
23324ae1 | 455 | */ |
bd0812fe | 456 | bool HasEntry(wxString& strName) const; |
23324ae1 FM |
457 | |
458 | /** | |
d29a9a8a | 459 | @return @true if the group by this name exists. |
23324ae1 | 460 | */ |
bd0812fe | 461 | bool HasGroup(const wxString& strName) const; |
23324ae1 | 462 | |
bd0812fe | 463 | //@} |
3c4f71cc | 464 | |
3c4f71cc | 465 | |
bd0812fe BP |
466 | /** |
467 | @name Miscellaneous Functions | |
23324ae1 | 468 | */ |
bd0812fe | 469 | //@{ |
23324ae1 FM |
470 | |
471 | /** | |
bd0812fe | 472 | Returns the application name. |
23324ae1 | 473 | */ |
bd0812fe | 474 | wxString GetAppName() const; |
23324ae1 FM |
475 | |
476 | /** | |
bd0812fe BP |
477 | Returns the vendor name. |
478 | */ | |
479 | wxString GetVendorName() const; | |
3c4f71cc | 480 | |
bd0812fe | 481 | //@} |
3c4f71cc | 482 | |
3c4f71cc | 483 | |
bd0812fe BP |
484 | /** |
485 | @name Key Access | |
3c4f71cc | 486 | |
bd0812fe | 487 | See @ref configbase_keyaccess |
23324ae1 | 488 | */ |
bd0812fe | 489 | //@{ |
23324ae1 FM |
490 | |
491 | /** | |
bd0812fe BP |
492 | Permanently writes all changes (otherwise, they're only written from |
493 | object's destructor). | |
23324ae1 | 494 | */ |
bd0812fe | 495 | bool Flush(bool bCurrentOnly = false); |
23324ae1 | 496 | |
23324ae1 | 497 | /** |
bd0812fe BP |
498 | Read a string from the key, returning @true if the value was read. If |
499 | the key was not found, @a str is not changed. | |
23324ae1 | 500 | */ |
328f5751 | 501 | bool Read(const wxString& key, wxString* str) const; |
bd0812fe BP |
502 | /** |
503 | Read a string from the key. The default value is returned if the key | |
504 | was not found. | |
505 | ||
d29a9a8a | 506 | @return @true if value was really read, @false if the default was used. |
bd0812fe | 507 | */ |
328f5751 | 508 | const bool Read(const wxString& key, wxString* str, |
bd0812fe BP |
509 | const wxString& defaultVal) const; |
510 | /** | |
511 | Another version of Read(), returning the string value directly. | |
512 | */ | |
513 | const wxString Read(const wxString& key, | |
514 | const wxString& defaultVal) const; | |
515 | /** | |
516 | Reads a long value, returning @true if the value was found. If the | |
517 | value was not found, @a l is not changed. | |
518 | */ | |
328f5751 | 519 | const bool Read(const wxString& key, long* l) const; |
bd0812fe BP |
520 | /** |
521 | Reads a long value, returning @true if the value was found. If the | |
522 | value was not found, @a defaultVal is used instead. | |
523 | */ | |
328f5751 FM |
524 | const bool Read(const wxString& key, long* l, |
525 | long defaultVal) const; | |
bd0812fe BP |
526 | /** |
527 | Reads a double value, returning @true if the value was found. If the | |
528 | value was not found, @a d is not changed. | |
529 | */ | |
328f5751 | 530 | const bool Read(const wxString& key, double* d) const; |
bd0812fe BP |
531 | /** |
532 | Reads a double value, returning @true if the value was found. If the | |
533 | value was not found, @a defaultVal is used instead. | |
534 | */ | |
328f5751 | 535 | const bool Read(const wxString& key, double* d, |
bd0812fe BP |
536 | double defaultVal) const; |
537 | /** | |
538 | Reads a bool value, returning @true if the value was found. If the | |
539 | value was not found, @a b is not changed. | |
540 | */ | |
328f5751 | 541 | const bool Read(const wxString& key, bool* b) const; |
bd0812fe BP |
542 | /** |
543 | Reads a bool value, returning @true if the value was found. If the | |
544 | value was not found, @a defaultVal is used instead. | |
545 | */ | |
328f5751 | 546 | const bool Read(const wxString& key, bool* d, |
bd0812fe BP |
547 | bool defaultVal) const; |
548 | /** | |
549 | Reads a binary block, returning @true if the value was found. If the | |
550 | value was not found, @a buf is not changed. | |
551 | */ | |
328f5751 | 552 | const bool Read(const wxString& key, wxMemoryBuffer* buf) const; |
bd0812fe BP |
553 | /** |
554 | Reads a value of type T, for which function wxFromString() is defined, | |
555 | returning @true if the value was found. If the value was not found, | |
556 | @a value is not changed. | |
557 | */ | |
328f5751 | 558 | const bool Read(const wxString& key, T* value) const; |
bd0812fe BP |
559 | /** |
560 | Reads a value of type T, for which function wxFromString() is defined, | |
561 | returning @true if the value was found. If the value was not found, | |
562 | @a defaultVal is used instead. | |
563 | */ | |
328f5751 | 564 | const bool Read(const wxString& key, T* value, |
bd0812fe | 565 | const T& defaultVal) const; |
23324ae1 FM |
566 | |
567 | /** | |
bd0812fe BP |
568 | Reads a bool value from the key and returns it. @a defaultVal is |
569 | returned if the key is not found. | |
23324ae1 | 570 | */ |
328f5751 | 571 | long ReadBool(const wxString& key, bool defaultVal) const; |
23324ae1 FM |
572 | |
573 | /** | |
bd0812fe BP |
574 | Reads a double value from the key and returns it. @a defaultVal is |
575 | returned if the key is not found. | |
23324ae1 | 576 | */ |
328f5751 | 577 | long ReadDouble(const wxString& key, double defaultVal) const; |
23324ae1 FM |
578 | |
579 | /** | |
bd0812fe BP |
580 | Reads a long value from the key and returns it. @a defaultVal is |
581 | returned if the key is not found. | |
23324ae1 | 582 | */ |
328f5751 | 583 | long ReadLong(const wxString& key, long defaultVal) const; |
23324ae1 FM |
584 | |
585 | /** | |
bd0812fe BP |
586 | Reads a value of type T (for which the function wxFromString() must be |
587 | defined) from the key and returns it. @a defaultVal is returned if the | |
588 | key is not found. | |
23324ae1 | 589 | */ |
328f5751 | 590 | T ReadObject(const wxString& key, T const& defaultVal) const; |
23324ae1 FM |
591 | |
592 | /** | |
bd0812fe BP |
593 | Writes the wxString value to the config file and returns @true on |
594 | success. | |
595 | */ | |
596 | bool Write(const wxString& key, const wxString& value); | |
597 | /** | |
598 | Writes the long value to the config file and returns @true on success. | |
599 | */ | |
600 | bool Write(const wxString& key, long value); | |
601 | /** | |
602 | Writes the double value to the config file and returns @true on | |
603 | success. | |
604 | */ | |
605 | bool Write(const wxString& key, double value); | |
606 | /** | |
607 | Writes the bool value to the config file and returns @true on success. | |
23324ae1 | 608 | */ |
bd0812fe BP |
609 | bool Write(const wxString& key, bool value); |
610 | /** | |
611 | Writes the wxMemoryBuffer value to the config file and returns @true on | |
612 | success. | |
613 | */ | |
614 | bool Write(const wxString& key, const wxMemoryBuffer& buf); | |
615 | /** | |
616 | Writes the specified value to the config file and returns @true on | |
617 | success. The function wxToString() must be defined for type @e T. | |
618 | */ | |
619 | bool Write(const wxString& key, T const& buf); | |
620 | ||
621 | //@} | |
23324ae1 FM |
622 | |
623 | ||
624 | /** | |
bd0812fe BP |
625 | @name Rename Entries/Groups |
626 | ||
627 | These functions allow renaming entries or subgroups of the current | |
628 | group. They will return @false on error, typically because either the | |
629 | entry/group with the original name doesn't exist, because the | |
630 | entry/group with the new name already exists or because the function is | |
631 | not supported in this wxConfig implementation. | |
23324ae1 | 632 | */ |
bd0812fe | 633 | //@{ |
23324ae1 FM |
634 | |
635 | /** | |
bd0812fe BP |
636 | Renames an entry in the current group. The entries names (both the old |
637 | and the new one) shouldn't contain backslashes, i.e. only simple names | |
638 | and not arbitrary paths are accepted by this function. | |
639 | ||
d29a9a8a BP |
640 | @return @false if @a oldName doesn't exist or if @a newName already |
641 | exists. | |
23324ae1 | 642 | */ |
bd0812fe | 643 | bool RenameEntry(const wxString& oldName, const wxString& newName); |
23324ae1 FM |
644 | |
645 | /** | |
bd0812fe BP |
646 | Renames a subgroup of the current group. The subgroup names (both the |
647 | old and the new one) shouldn't contain backslashes, i.e. only simple | |
648 | names and not arbitrary paths are accepted by this function. | |
649 | ||
d29a9a8a BP |
650 | @return @false if @a oldName doesn't exist or if @a newName already |
651 | exists. | |
23324ae1 | 652 | */ |
bd0812fe BP |
653 | bool RenameGroup(const wxString& oldName, const wxString& newName); |
654 | ||
655 | //@} | |
656 | ||
23324ae1 FM |
657 | |
658 | /** | |
bd0812fe BP |
659 | @name Delete Entries/Groups |
660 | ||
661 | These functions delete entries and/or groups of entries from the config | |
662 | file. DeleteAll() is especially useful if you want to erase all traces | |
663 | of your program presence: for example, when you uninstall it. | |
23324ae1 | 664 | */ |
bd0812fe | 665 | //@{ |
23324ae1 FM |
666 | |
667 | /** | |
bd0812fe BP |
668 | Delete the whole underlying object (disk file, registry key, ...). |
669 | Primarly for use by uninstallation routine. | |
23324ae1 | 670 | */ |
bd0812fe | 671 | bool DeleteAll(); |
23324ae1 FM |
672 | |
673 | /** | |
bd0812fe BP |
674 | Deletes the specified entry and the group it belongs to if it was the |
675 | last key in it and the second parameter is @true. | |
23324ae1 | 676 | */ |
bd0812fe BP |
677 | bool DeleteEntry(const wxString& key, |
678 | bool bDeleteGroupIfEmpty = true); | |
23324ae1 FM |
679 | |
680 | /** | |
bd0812fe BP |
681 | Delete the group (with all subgroups). If the current path is under the |
682 | group being deleted it is changed to its deepest still existing | |
683 | component. E.g. if the current path is @c "/A/B/C/D" and the group @c C | |
684 | is deleted, the path becomes @c "/A/B". | |
685 | */ | |
686 | bool DeleteGroup(const wxString& key); | |
3c4f71cc | 687 | |
bd0812fe | 688 | //@} |
3c4f71cc | 689 | |
3c4f71cc | 690 | |
bd0812fe BP |
691 | /** |
692 | @name Options | |
23324ae1 | 693 | |
bd0812fe BP |
694 | Some aspects of wxConfigBase behaviour can be changed during run-time. |
695 | The first of them is the expansion of environment variables in the | |
696 | string values read from the config file: for example, if you have the | |
697 | following in your config file: | |
23324ae1 | 698 | |
bd0812fe BP |
699 | @code |
700 | # config file for my program | |
701 | UserData = $HOME/data | |
3c4f71cc | 702 | |
bd0812fe BP |
703 | # the following syntax is valud only under Windows |
704 | UserData = %windir%\\data.dat | |
705 | @endcode | |
3c4f71cc | 706 | |
bd0812fe BP |
707 | The call to Read("UserData") will return something like |
708 | @c "/home/zeitlin/data" on linux for example. | |
3c4f71cc | 709 | |
bd0812fe BP |
710 | Although this feature is very useful, it may be annoying if you read a |
711 | value which containts '$' or '%' symbols (% is used for environment | |
712 | variables expansion under Windows) which are not used for environment | |
713 | variable expansion. In this situation you may call | |
714 | SetExpandEnvVars(@false) just before reading this value and | |
715 | SetExpandEnvVars(@true) just after. Another solution would be to prefix | |
716 | the offending symbols with a backslash. | |
23324ae1 | 717 | */ |
bd0812fe | 718 | //@{ |
23324ae1 | 719 | |
bd0812fe BP |
720 | /** |
721 | Returns @true if we are expanding environment variables in key values. | |
722 | */ | |
723 | bool IsExpandingEnvVars() const; | |
23324ae1 | 724 | |
23324ae1 | 725 | /** |
bd0812fe BP |
726 | Returns @true if we are writing defaults back to the config file. |
727 | */ | |
728 | bool IsRecordingDefaults() const; | |
3c4f71cc | 729 | |
bd0812fe BP |
730 | /** |
731 | Determine whether we wish to expand environment variables in key | |
732 | values. | |
733 | */ | |
734 | void SetExpandEnvVars(bool bDoIt = true); | |
3c4f71cc | 735 | |
bd0812fe BP |
736 | /** |
737 | Sets whether defaults are recorded to the config file whenever an | |
738 | attempt to read the value which is not present in it is done. | |
3c4f71cc | 739 | |
bd0812fe BP |
740 | If on (default is off) all default values for the settings used by the |
741 | program are written back to the config file. This allows the user to | |
742 | see what config options may be changed and is probably useful only for | |
743 | wxFileConfig. | |
744 | */ | |
745 | void SetRecordDefaults(bool bDoIt = true); | |
3c4f71cc | 746 | |
bd0812fe | 747 | //@} |
3c4f71cc | 748 | |
3c4f71cc | 749 | |
bd0812fe BP |
750 | /** |
751 | Create a new config object: this function will create the "best" | |
752 | implementation of wxConfig available for the current platform, see | |
753 | comments near the definition of wxCONFIG_WIN32_NATIVE for details. It | |
754 | returns the created object and also sets it as the current one. | |
755 | */ | |
756 | static wxConfigBase* Create(); | |
3c4f71cc | 757 | |
bd0812fe BP |
758 | /** |
759 | Calling this function will prevent @e Get() from automatically creating | |
760 | a new config object if the current one is @NULL. It might be useful to | |
761 | call it near the program end to prevent "accidental" creation of a new | |
762 | config object. | |
763 | */ | |
764 | static void DontCreateOnDemand(); | |
3c4f71cc | 765 | |
bd0812fe BP |
766 | /** |
767 | Get the current config object. If there is no current object and | |
768 | @a CreateOnDemand is @true, this creates one (using Create()) unless | |
769 | DontCreateOnDemand() was called previously. | |
770 | */ | |
771 | static wxConfigBase* Get(bool CreateOnDemand = true); | |
3c4f71cc | 772 | |
bd0812fe BP |
773 | /** |
774 | Sets the config object as the current one, returns the pointer to the | |
775 | previous current object (both the parameter and returned value may be | |
776 | @NULL). | |
23324ae1 | 777 | */ |
bd0812fe | 778 | static wxConfigBase* Set(wxConfigBase* pConfig); |
23324ae1 | 779 | }; |
e54c96f1 | 780 |