$szTitle = "wxconfigbase"; include "./_header.inc"; ?>
wxconfigbase class defines the basic interface of all config classes. it can not be used by itself (it is an abstract base class) and you will always use one of its derivations: wxfileconfig, wxregconfig or any other.
however, usually you don't even need to know the precise nature of the class you're working with but you would just use the wxconfigbase methods. this allows you to write the same code regardless of whether you're working with the registry under win32 or text-based config files under unix (or even windows 3.1 .ini files if you're really unlucky). to make writing the portable code even easier, wxwidgets provides a typedef wxconfig which is mapped onto the native wxconfigbase implementation on the given platform: i.e. wxregconfig under win32 and wxfileconfig otherwise.
see config overview for the descriptions of all features of this class.
it is highly recommended to use static functions get() and/or set(), so please have a look at them.
derived from
no base class
include files
<wx/config.h> (to let wxwidgets choose a wxconfig class for your platform)
<wx/confbase.h> (base config class)
<wx/fileconf.h> (wxfileconfig class)
<wx/msw/regconf.h> (wxregconfig class)
example
here is how you would typically use this class:
// using wxconfig instead of writing wxfileconfig or wxregconfig enhances // portability of the code wxconfig *config = new wxconfig("myappname"); wxstring str; if ( config->read("lastprompt", &str) ) { // last prompt was found in the config file/registry and its value is now // in str ... } else { // no last prompt... } // another example: using default values and the full path instead of just // key name: if the key is not found , the value 17 is returned long value = config->read("/lastrun/calculatedvalues/maxvalue", 17); ... ... ... // at the end of the program we would save everything back config->write("lastprompt", str); config->write("/lastrun/calculatedvalues/maxvalue", value); // the changes will be written back automatically delete config;this basic example, of course, doesn't show all wxconfig features, such as enumerating, testing for existence and deleting the entries and groups of entries in the config file, its abilities to automatically store the default values or expand the environment variables on the fly. however, the main idea is that using this class is easy and that it should normally do what you expect it to.
nb: in the documentation of this class, the words "config file" also mean "registry hive" for wxregconfig and, generally speaking, might mean any physical storage where a wxconfigbase-derived class stores its data.
function groups
static functions
constructor and destructor
path management
enumeration
tests of existence
miscellaneous functions
key access
rename entries/groups
delete entries/groups
options
wxconfigbase::wxconfigbase
wxconfigbase::~wxconfigbase
wxconfigbase::create
wxconfigbase::dontcreateondemand
wxconfigbase::deleteall
wxconfigbase::deleteentry
wxconfigbase::deletegroup
wxconfigbase::exists
wxconfigbase::flush
wxconfigbase::get
wxconfigbase::getappname
wxconfigbase::getentrytype
wxconfigbase::getfirstgroup
wxconfigbase::getfirstentry
wxconfigbase::getnextgroup
wxconfigbase::getnextentry
wxconfigbase::getnumberofentries
wxconfigbase::getnumberofgroups
wxconfigbase::getpath
wxconfigbase::getvendorname
wxconfigbase::hasentry
wxconfigbase::hasgroup
wxconfigbase::isexpandingenvvars
wxconfigbase::isrecordingdefaults
wxconfigbase::read
wxconfigbase::renameentry
wxconfigbase::renamegroup
wxconfigbase::set
wxconfigbase::setexpandenvvars
wxconfigbase::setpath
wxconfigbase::setrecorddefaults
wxconfigbase::write
these functions deal with the "default" config object. although its usage is not at all mandatory it may be convenient to use a global config object instead of creating and deleting the local config objects each time you need one (especially because creating a wxfileconfig object might be a time consuming operation). in this case, you may create this global config object in the very start of the program and set() it as the default. then, from anywhere in your program, you may access it using the get() function. this global wxconfig object will be deleted by wxwidgets automatically if it exists. note that this implies that if you do delete this object yourself (usually in wxapp::onexit) you must use set(null) to prevent wxwidgets from deleting it the second time.
as it happens, you may even further simplify the procedure described above: you may forget about calling set(). when get() is called and there is no current object, it will create one using create() function. to disable this behaviour dontcreateondemand() is provided.
note: you should use either set() or get() because wxwidgets library itself would take advantage of it and could save various information in it. for example wxfontmapper or unix version of wxfiledialog have the ability to use wxconfig class.
set
get
create
dontcreateondemand
as explained in config overview, the config classes support a file system-like hierarchy of keys (files) and groups (directories). as in the file system case, to specify a key in the config class you must use a path to it. config classes also support the notion of the current group, which makes it possible to use the relative paths. to clarify all this, here is an example (it is only for the sake of demonstration, it doesn't do anything sensible!):
wxconfig *config = new wxconfig("foobarapp"); // right now the current path is '/' conf->write("rootentry", 1); // go to some other place: if the group(s) don't exist, they will be created conf->setpath("/group/subgroup"); // create an entry in subgroup conf->write("subgroupentry", 3); // '..' is understood conf->write("../groupentry", 2); conf->setpath(".."); wxassert( conf->read("subgroup/subgroupentry", 0l) == 3 ); // use absolute path: it is allowed, too wxassert( conf->read("/rootentry", 0l) == 1 );warning: it is probably a good idea to always restore the path to its old value on function exit:
void foo(wxconfigbase *config) { wxstring stroldpath = config->getpath(); config->setpath("/foo/data"); ... config->setpath(stroldpath); }because otherwise the assert in the following example will surely fail (we suppose here that foo() function is the same as above except that it doesn't save and restore the path):
void bar(wxconfigbase *config) { config->write("test", 17); foo(config); // we're reading "/foo/data/test" here! -1 will probably be returned... wxassert( config->read("test", -1) == 17 ); }finally, the path separator in wxconfigbase and derived classes is always '/', regardless of the platform (i.e. it is not '\\' under windows).
the functions in this section allow to enumerate all entries and groups in the config file. all functions here return false when there are no more items.
you must pass the same index to getnext and getfirst (don't modify it). please note that it is not the index of the current item (you will have some great surprises with wxregconfig if you assume this) and you shouldn't even look at it: it is just a "cookie" which stores the state of the enumeration. it can't be stored inside the class because it would prevent you from running several enumerations simultaneously, that's why you must pass it explicitly.
having said all this, enumerating the config entries/groups is very simple:
wxconfigbase *config = ...; wxarraystring anames; // enumeration variables wxstring str; long dummy; // first enum all entries bool bcont = config->getfirstentry(str, dummy); while ( bcont ) { anames.add(str); bcont = getconfig()->getnextentry(str, dummy); } ... we have all entry names in anames... // now all groups... bcont = getconfig()->getfirstgroup(str, dummy); while ( bcont ) { anames.add(str); bcont = getconfig()->getnextgroup(str, dummy); } ... we have all group (and entry) names in anames...there are also functions to get the number of entries/subgroups without actually enumerating them, but you will probably never need them.
getfirstgroup
getnextgroup
getfirstentry
getnextentry
getnumberofentries
getnumberofgroups
hasgroup
hasentry
exists
getentrytype
getappname
getvendorname
setumask
these function are the core of wxconfigbase class: they allow you to read and write config file data. all read function take a default value which will be returned if the specified key is not found in the config file.
currently, only two types of data are supported: string and long (but it might change in the near future). to work with other types: for int or bool you can work with function taking/returning long and just use the casts. better yet, just use long for all variables which you're going to save in the config file: chances are that sizeof(bool) == sizeof(int) == sizeof(long) anyhow on your system. for float, double and, in general, any other type you'd have to translate them to/from string representation and use string functions.
try not to read long values into string variables and vice versa: although it just might work with wxfileconfig, you will get a system error with wxregconfig because in the windows registry the different types of entries are indeed used.
final remark: the szkey parameter for all these functions can contain an arbitrary path (either relative or absolute), not just the key name.
the functions in this section allow to rename entries or subgroups of the current group. they will return false on error. typically because either the entry/group with the original name doesn't exist, because the entry/group with the new name already exists or because the function is not supported in this wxconfig implementation.
the functions in this section delete entries and/or groups of entries from the config file. deleteall() is especially useful if you want to erase all traces of your program presence: for example, when you uninstall it.
deleteentry
deletegroup
deleteall
some aspects of wxconfigbase behaviour can be changed during run-time. the first of them is the expansion of environment variables in the string values read from the config file: for example, if you have the following in your config file:
# config file for my program userdata = $home/data # the following syntax is valud only under windows userdata = %windir%\\data.datthe call to config->read("userdata") will return something like "/home/zeitlin/data" if you're lucky enough to run a linux system ;-)
although this feature is very useful, it may be annoying if you read a value which containts '$' or '%' symbols (% is used for environment variables expansion under windows) which are not used for environment variable expansion. in this situation you may call setexpandenvvars(false) just before reading this value and setexpandenvvars(true) just after. another solution would be to prefix the offending symbols with a backslash.
the following functions control this option:
isexpandingenvvars
setexpandenvvars
setrecorddefaults
isrecordingdefaults
wxconfigbase(const wxstring& appname = wxemptystring, const wxstring& vendorname = wxemptystring, const wxstring& localfilename = wxemptystring, const wxstring& globalfilename = wxemptystring, long style = 0, wxmbconv& conv = wxconvutf8)
this is the default and only constructor of the wxconfigbase class, and derived classes.
parameters
appname
vendorname
localfilename
globalfilename
style
for wxfileconfig you can also add wxconfig_use_relative_path by logically or'ing it to either of the _file options to tell wxfileconfig to use relative instead of absolute paths.
on non-vms unix systems, the default local configuration file is ~/.appname. however, this path may be also used as user data directory (see wxstandardpaths::getuserdatadir) if the application has several data files. in this case wxconfig_use_subdir flag, which changes the default local configuration file to ~/.appname/appname should be used. notice that this flag is ignored on non-unix system, including vms, or if a non-default localfilename is provided. this function is new since wxwidgets version 2.8.2
for wxfileconfig, you can also add wxconfig_use_no_escape_characters which will turn off character escaping for the values of entries stored in the config file: for example a foo key with some backslash characters will be stored as foo=c:\mydir instead of the usual storage of foo=c:\\mydir.
the wxconfig_use_no_escape_characters style can be helpful if your config file must be read or written to by a non-wxwidgets program (which might not understand the escape characters). note, however, that if wxconfig_use_no_escape_characters style is used, it is is now your application's responsibility to ensure that there is no newline or other illegal characters in a value, before writing that value to the file.
conv
remarks
by default, environment variable expansion is on and recording defaults is off.
~wxconfigbase()
empty but ensures that dtor of all derived classes is virtual.
static wxconfigbase * create()
create a new config object: this function will create the "best" implementation of wxconfig available for the current platform, see comments near the definition of wxconfig_win32_native for details. it returns the created object and also sets it as the current one.
void dontcreateondemand()
calling this function will prevent get() from automatically creating a new config object if the current one is null. it might be useful to call it near the program end to prevent "accidental" creation of a new config object.
bool deleteall()
delete the whole underlying object (disk file, registry key, ...). primarly for use by uninstallation routine.
bool deleteentry(const wxstring& key, bool bdeletegroupifempty = true)
deletes the specified entry and the group it belongs to if it was the last key in it and the second parameter is true.
bool deletegroup(const wxstring& key)
delete the group (with all subgroups). if the current path is under the group being deleted it is changed to its deepest still existing component. e.g. if the current path is /a/b/c/d and the group c is deleted the path becomes /a/b.
bool exists(wxstring& strname) const
returns true if either a group or an entry with a given name exists
bool flush(bool bcurrentonly = false)
permanently writes all changes (otherwise, they're only written from object's destructor)
static wxconfigbase * get(bool createondemand = true)
get the current config object. if there is no current object and createondemand is true, creates one (using create) unless dontcreateondemand was called previously.
wxstring getappname() const
returns the application name.
enum wxconfigbase::entrytype getentrytype(const wxstring& name) const
returns the type of the given entry or unknown if the entry doesn't exist. this function should be used to decide which version of read() should be used because some of wxconfig implementations will complain about type mismatch otherwise: e.g., an attempt to read a string value from an integer key with wxregconfig will fail.
the result is an element of enum entrytype:
enum entrytype { type_unknown, type_string, type_boolean, type_integer, type_float };
bool getfirstgroup(wxstring& str, long& index) const
gets the first group.
wxpython note: the wxpython version of this method returns a 3-tuple consisting of the continue flag, the value string, and the index for the next call.
wxperl note: in wxperl this method takes no arguments and returns a 3-element list ( continue, str, index ).
bool getfirstentry(wxstring& str, long& index) const
gets the first entry.
wxpython note: the wxpython version of this method returns a 3-tuple consisting of the continue flag, the value string, and the index for the next call.
wxperl note: in wxperl this method takes no arguments and returns a 3-element list ( continue, str, index ).
bool getnextgroup(wxstring& str, long& index) const
gets the next group.
wxpython note: the wxpython version of this method returns a 3-tuple consisting of the continue flag, the value string, and the index for the next call.
wxperl note: in wxperl this method only takes the index parameter and returns a 3-element list ( continue, str, index ).
bool getnextentry(wxstring& str, long& index) const
gets the next entry.
wxpython note: the wxpython version of this method returns a 3-tuple consisting of the continue flag, the value string, and the index for the next call.
wxperl note: in wxperl this method only takes the index parameter and returns a 3-element list ( continue, str, index ).
uint getnumberofentries(bool brecursive = false) const
uint getnumberofgroups(bool brecursive = false) const
get number of entries/subgroups in the current group, with or without its subgroups.
const wxstring& getpath() const
retrieve the current path (always as absolute path).
wxstring getvendorname() const
returns the vendor name.
bool hasentry(wxstring& strname) const
returns true if the entry by this name exists
bool hasgroup(const wxstring& strname) const
returns true if the group by this name exists
bool isexpandingenvvars() const
returns true if we are expanding environment variables in key values.
bool isrecordingdefaults() const
returns true if we are writing defaults back to the config file.
bool read(const wxstring& key, wxstring* str) const
read a string from the key, returning true if the value was read. if the key was not found, str is not changed.
bool read(const wxstring& key, wxstring* str, const wxstring& defaultval) const
read a string from the key. the default value is returned if the key was not found.
returns true if value was really read, false if the default was used.
wxstring read(const wxstring& key, const wxstring& defaultval) const
another version of read(), returning the string value directly.
bool read(const wxstring& key, long* l) const
reads a long value, returning true if the value was found. if the value was not found, l is not changed.
bool read(const wxstring& key, long* l, long defaultval) const
reads a long value, returning true if the value was found. if the value was not found, defaultval is used instead.
long read(const wxstring& key, long defaultval) const
reads a long value from the key and returns it. defaultval is returned if the key is not found.
nb: writing
conf->read("key", 0);
won't work because the call is ambiguous: compiler can not choose between two read functions. instead, write:
conf->read("key", 0l);
bool read(const wxstring& key, double* d) const
reads a double value, returning true if the value was found. if the value was not found, d is not changed.
bool read(const wxstring& key, double* d, double defaultval) const
reads a double value, returning true if the value was found. if the value was not found, defaultval is used instead.
bool read(const wxstring& key, bool* b) const
reads a bool value, returning true if the value was found. if the value was not found, b is not changed.
bool read(const wxstring& key, bool* d, bool defaultval) const
reads a bool value, returning true if the value was found. if the value was not found, defaultval is used instead.
wxpython note: in place of a single overloaded method name, wxpython implements the following methods:
read(key, default="") | returns a string. |
readint(key, default=0) | returns an int. |
readfloat(key, default=0.0) | returns a floating point number. |
wxperl note: in place of a single overloaded method, wxperl uses:
read(key, default="") | returns a string |
readint(key, default=0) | returns an integer |
readfloat(key, default=0.0) | returns a floating point number |
readbool(key, default=0) | returns a boolean |
bool renameentry(const wxstring& oldname, const wxstring& newname)
renames an entry in the current group. the entries names (both the old and the new one) shouldn't contain backslashes, i.e. only simple names and not arbitrary paths are accepted by this function.
returns false if oldname doesn't exist or if newname already exists.
bool renamegroup(const wxstring& oldname, const wxstring& newname)
renames a subgroup of the current group. the subgroup names (both the old and the new one) shouldn't contain backslashes, i.e. only simple names and not arbitrary paths are accepted by this function.
returns false if oldname doesn't exist or if newname already exists.
static wxconfigbase * set(wxconfigbase *pconfig)
sets the config object as the current one, returns the pointer to the previous current object (both the parameter and returned value may be null)
void setexpandenvvars (bool bdoit = true)
determine whether we wish to expand environment variables in key values.
void setpath(const wxstring& strpath)
set current path: if the first character is '/', it is the absolute path, otherwise it is a relative path. '..' is supported. if strpath doesn't exist it is created.
void setrecorddefaults(bool bdoit = true)
sets whether defaults are recorded to the config file whenever an attempt to read the value which is not present in it is done.
if on (default is off) all default values for the settings used by the program are written back to the config file. this allows the user to see what config options may be changed and is probably useful only for wxfileconfig.
bool write(const wxstring& key, const wxstring& value)
bool write(const wxstring& key, long value)
bool write(const wxstring& key, double value)
bool write(const wxstring& key, bool value)
these functions write the specified value to the config file and return true on success.
wxpython note: in place of a single overloaded method name, wxpython implements the following methods:
write(key, value) | writes a string. |
writeint(key, value) | writes an int. |
writefloat(key, value) | writes a floating point number. |
wxperl note: in place of a single overloaded method, wxperl uses:
write(key, value) | writes a string |
writeint(key, value) | writes an integer |
writefloat(key, value) | writes a floating point number |
writebool(key, value) | writes a boolean |
include "./_footer.inc"; ?>