logo top
Main Page   Widgets   glibmm Namespaces   Book  

Glib::KeyFile Class Reference

This class lets you parse, edit or create files containing groups of key-value pairs, which we call key files for lack of a better name. More...

List of all members.

Public Member Functions

 KeyFile ()
 Creates a new, empty KeyFile object.
 ~KeyFile ()
 Destructor.
 KeyFile (GKeyFile* castitem, bool takes_ownership=false)
 Creates a glibmm KeyFile wrapper for a GKeyFile object.
bool load_from_file (const std::string& filename, KeyFileFlags flags=Glib::KEY_FILE_NONE)
 Loads a key file into an empty G::KeyFile structure.
bool load_from_data (const Glib::ustring& data, KeyFileFlags flags=Glib::KEY_FILE_NONE)
 Loads a KeyFile from memory.
bool load_from_data_dirs (const std::string& file, std::string& full_path, KeyFileFlags flags=Glib::KEY_FILE_NONE)
 Looks for a KeyFile named file in the paths returned from g_get_user_data_dir() and g_get_system_data_dirs() and loads them into the keyfile object, placing the full path to the file in full_path.
Glib::ustring to_data ()
 Outputs the KeyFile as a string.
Glib::ustring get_start_group () const
 Return value: The start group of the key file.
Glib::ArrayHandle<Glib::ustringget_groups () const
 Gets a list of all groups in the KeyFile.
Glib::ArrayHandle<Glib::ustringget_keys (const Glib::ustring& group_name) const
 Gets a list of all keys from the group group_name.
bool has_group (const Glib::ustring& group_name) const
 Looks whether the key file has the group group_name .
bool has_key (const Glib::ustring& group_name, const Glib::ustring& key) const
 Looks whether the key file has the key key in the group group_name .
Glib::ustring get_value (const Glib::ustring& group_name, const Glib::ustring& key) const
 Return value: a newly allocated string or 0 if the specified.
Glib::ustring get_string (const Glib::ustring& group_name, const Glib::ustring& key) const
 Return value: a newly allocated string or 0 if the specified.
Glib::ustring get_locale_string (const Glib::ustring& group_name, const Glib::ustring& key) const
 Gets the value associated with key under group_name translated into the current locale.
Glib::ustring get_locale_string (const Glib::ustring& group_name, const Glib::ustring& key, const Glib::ustring& locale) const
 Return value: a newly allocated string or 0 if the specified.
bool get_boolean (const Glib::ustring& group_name, const Glib::ustring& key) const
 Return value: the value associated with the key as a boolean, or.
int get_integer (const Glib::ustring& key) const
 Gets the value in the first group, under key, interpreting it as an integer.
int get_integer (const Glib::ustring& group_name, const Glib::ustring& key) const
 Return value: the value associated with the key as an integer, or.
double get_double (const Glib::ustring& key) const
 Gets the value in the first group, under key, interpreting it as a double.
double get_double (const Glib::ustring& group_name, const Glib::ustring& key) const
 Return value: the value associated with the key as a double, or.
void set_double (const Glib::ustring& group_name, const Glib::ustring& key, double value)
 Associates a new double value with key under group_name .
void set_double (const Glib::ustring& key, double value)
 Associates a new double value with key under the start group.
Glib::ArrayHandle<Glib::ustringget_string_list (const Glib::ustring& group_name, const Glib::ustring& key) const
 Returns the values associated with key under group_name.
Glib::ArrayHandle<Glib::ustringget_locale_string_list (const Glib::ustring& group_name, const Glib::ustring& key) const
 Returns the values associated with key under group_name translated into the current locale, if available.
Glib::ArrayHandle<Glib::ustringget_locale_string_list (const Glib::ustring& group_name, const Glib::ustring& key, const Glib::ustring& locale) const
 Returns the values associated with key under group_name translated into locale, if available.
Glib::ArrayHandle<bool> get_boolean_list (const Glib::ustring& group_name, const Glib::ustring& key) const
 Returns the values associated with key under group_name.
Glib::ArrayHandle<int> get_integer_list (const Glib::ustring& group_name, const Glib::ustring& key) const
 Returns the values associated with key under group_name.
Glib::ArrayHandle<double> get_double_list (const Glib::ustring& group_name, const Glib::ustring& key) const
 Returns the values associated with key under group_name.
Glib::ustring get_comment () const
 Get comment from top of file.
Glib::ustring get_comment (const Glib::ustring& group_name) const
 Get comment from above a group.
Glib::ustring get_comment (const Glib::ustring& group_name, const Glib::ustring& key) const
 Retrieves a comment above key from group_name .
void set_list_separator (gchar separator)
 Sets the character which is used to separate values in lists.
void set_value (const Glib::ustring& group_name, const Glib::ustring& key, const Glib::ustring& value)
 Associates a new value with key under group_name .
void set_string (const Glib::ustring& group_name, const Glib::ustring& key, const Glib::ustring& string)
 Associates a new string value with key under group_name .
void set_locale_string (const Glib::ustring& group_name, const Glib::ustring& key, const Glib::ustring& locale, const Glib::ustring& string)
 Associates a string value for key and locale under group_name .
void set_boolean (const Glib::ustring& group_name, const Glib::ustring& key, bool value)
 Associates a new boolean value with key under group_name .
void set_integer (const Glib::ustring& group_name, const Glib::ustring& key, int value)
 Associates a new integer value with key under group_name .
void set_string_list (const Glib::ustring& group_name, const Glib::ustring& key, const Glib::ArrayHandle<Glib::ustring>& list)
 Sets a list of string values for key under group_name.
void set_locale_string_list (const Glib::ustring& group_name, const Glib::ustring& key, const Glib::ustring& locale, const Glib::ArrayHandle<Glib::ustring>& list)
 Sets a list of string values for the key under group_name and marks them as being for locale.
void set_boolean_list (const Glib::ustring& group_name, const Glib::ustring& key, Glib::ArrayHandle<bool>& list)
 Sets a list of booleans for the key under group_name.
void set_integer_list (const Glib::ustring& group_name, const Glib::ustring& key, Glib::ArrayHandle<int>& list)
 Sets a list of integers for the key under group_name.
void set_double_list (const Glib::ustring& group_name, const Glib::ustring& key, Glib::ArrayHandle<double>& list)
 Sets a list of doubles for the key under group_name.
void set_comment (const Glib::ustring& comment)
 Places comment at the start of the file, before the first group.
void set_comment (const Glib::ustring& group_name, const Glib::ustring& comment)
 Places comment above group_name.
void set_comment (const Glib::ustring& group_name, const Glib::ustring& key, const Glib::ustring& comment)
 Places a comment above key from group_name .
void remove_comment (const Glib::ustring& group_name, const Glib::ustring& key)
 Removes a comment above key from group_name .
void remove_key (const Glib::ustring& group_name, const Glib::ustring& key)
 Removes key in group_name from the key file.
void remove_group (const Glib::ustring& group_name)
 Removes the specified group, group_name , from the key file.
GKeyFile* gobj ()
const GKeyFile* gobj () const

Protected Attributes

GKeyFile* gobject_
bool owns_gobject_


Detailed Description

This class lets you parse, edit or create files containing groups of key-value pairs, which we call key files for lack of a better name.

Several freedesktop.org specifications use key files now, e.g the Desktop Entry Specification and the Icon Theme Specification.

The syntax of key files is described in detail in the Desktop Entry Specification, here is a quick summary: Key files consists of groups of key-value pairs, interspersed with comments.

 # this is just an example
 # there can be comments before the first group

 [First Group]

 Name=Key File Example\tthis value shows\nescaping

 # localized strings are stored in multiple key-value pairs
 Welcome=Hello
 Welcome[de]=Hallo
 Welcome[fr]=Bonjour
 Welcome[it]=Ciao

 [Another Group]

 Numbers=2;20;-200;0

 Booleans=true;false;true;true

Lines beginning with a '#' and blank lines are considered comments.

Groups are started by a header line containing the group name enclosed in '[' and ']', and ended implicitly by the start of the next group or the end of the file. Each key-value pair must be contained in a group.

Key-value pairs generally have the form key=value, with the exception of localized strings, which have the form key[locale]=value. Space before and after the '=' character are ignored. Newline, tab, carriage return and backslash characters in value are escaped as
, , , and \, respectively. To preserve leading spaces in values, these can also be escaped as .

Key files can store strings (possibly with localized variants), integers, booleans and lists of these. Lists are separated by a separator character, typically ';' or ','. To use the list separator character in a value in a list, it has to be escaped by prefixing it with a backslash.

This syntax is obviously inspired by the .ini files commonly met on Windows, but there are some important differences:

Note that in contrast to the Desktop Entry Specification, groups in key files may contain the same key multiple times; the last entry wins. Key files may also contain multiple groups with the same name; they are merged together. Another difference is that keys and group names in key files are not restricted to ASCII characters.

Since glibmm 2.14:


Constructor & Destructor Documentation

Glib::KeyFile::KeyFile (  ) 

Creates a new, empty KeyFile object.

Glib::KeyFile::~KeyFile (  ) 

Destructor.

Glib::KeyFile::KeyFile ( GKeyFile *  castitem,
bool  takes_ownership = false 
)

Creates a glibmm KeyFile wrapper for a GKeyFile object.

Note, when using this that when the wrapper is deleted, it will not automatically deleted the GKeyFile unless you set the delete_c_instance boolean to true.

Parameters:
castitem The C instance to wrap
delete_c_instance If the C instance should be deleted when the wrapper is deleted.


Member Function Documentation

bool Glib::KeyFile::load_from_file ( const std::string filename,
KeyFileFlags  flags = Glib::KEY_FILE_NONE 
)

Loads a key file into an empty G::KeyFile structure.

If the file could not be loaded then error is set to either a G::FileError or G::KeyFileError.

Parameters:
file The path of a filename to load, in the GLib file name encoding.
flags Flags from G::KeyFileFlags.
error Return location for a G::Error, or 0.
Returns:
true if a key file could be loaded, false othewise
Since glibmm 2.6:

bool Glib::KeyFile::load_from_data ( const Glib::ustring data,
KeyFileFlags  flags = Glib::KEY_FILE_NONE 
)

Loads a KeyFile from memory.

Parameters:
data The data to use as a KeyFile
flags Bitwise combination of the flags to use for the KeyFile
Returns:
true if the KeyFile was successfully loaded, false otherwise
Exceptions:
Glib::KeyFileError 

bool Glib::KeyFile::load_from_data_dirs ( const std::string file,
std::string full_path,
KeyFileFlags  flags = Glib::KEY_FILE_NONE 
)

Looks for a KeyFile named file in the paths returned from g_get_user_data_dir() and g_get_system_data_dirs() and loads them into the keyfile object, placing the full path to the file in full_path.

Parameters:
file The file to search for
full_path Return location for a string containing the full path of the file
flags Bitwise combination of the flags to use for the KeyFile
Returns:
true if the KeyFile was successfully loaded, false otherwise
Exceptions:
Glib::KeyFileError 
Glib::FileError 

Glib::ustring Glib::KeyFile::to_data (  ) 

Outputs the KeyFile as a string.

Returns:
A string object holding the contents of KeyFile

Glib::ustring Glib::KeyFile::get_start_group (  )  const

Return value: The start group of the key file.

Returns:
The start group of the key file.
Since glibmm 2.6:

Glib::ArrayHandle<Glib::ustring> Glib::KeyFile::get_groups (  )  const

Gets a list of all groups in the KeyFile.

Returns:
A list containing the names of the groups

Glib::ArrayHandle<Glib::ustring> Glib::KeyFile::get_keys ( const Glib::ustring group_name  )  const

Gets a list of all keys from the group group_name.

Parameters:
group_name The name of a group
Returns:
A list containing the names of the keys in group_name

bool Glib::KeyFile::has_group ( const Glib::ustring group_name  )  const

Looks whether the key file has the group group_name .

Parameters:
group_name A group name.
Returns:
true if group_name is a part of key_file , false otherwise.
Since glibmm 2.6:

bool Glib::KeyFile::has_key ( const Glib::ustring group_name,
const Glib::ustring key 
) const

Looks whether the key file has the key key in the group group_name .

Parameters:
group_name A group name.
key A key name.
error Return location for a G::Error.
Returns:
true if key is a part of group_name , false otherwise.
Since glibmm 2.6:

Glib::ustring Glib::KeyFile::get_value ( const Glib::ustring group_name,
const Glib::ustring key 
) const

Return value: a newly allocated string or 0 if the specified.

Parameters:
group_name A group name.
key A key.
error Return location for a G::Error, or 0.
Returns:
A newly allocated string or 0 if the specified key cannot be found.
Since glibmm 2.6:

Glib::ustring Glib::KeyFile::get_string ( const Glib::ustring group_name,
const Glib::ustring key 
) const

Return value: a newly allocated string or 0 if the specified.

Parameters:
group_name A group name.
key A key.
error Return location for a G::Error, or 0.
Returns:
A newly allocated string or 0 if the specified key cannot be found.
Since glibmm 2.6:

Glib::ustring Glib::KeyFile::get_locale_string ( const Glib::ustring group_name,
const Glib::ustring key 
) const

Gets the value associated with key under group_name translated into the current locale.

Glib::ustring Glib::KeyFile::get_locale_string ( const Glib::ustring group_name,
const Glib::ustring key,
const Glib::ustring locale 
) const

Return value: a newly allocated string or 0 if the specified.

Parameters:
group_name A group name.
key A key.
locale A locale or 0.
error Return location for a G::Error, or 0.
Returns:
A newly allocated string or 0 if the specified key cannot be found.
Since glibmm 2.6:

bool Glib::KeyFile::get_boolean ( const Glib::ustring group_name,
const Glib::ustring key 
) const

Return value: the value associated with the key as a boolean, or.

Parameters:
group_name A group name.
key A key.
error Return location for a G::Error.
Returns:
The value associated with the key as a boolean, or false if the key was not found or could not be parsed.
Since glibmm 2.6:

int Glib::KeyFile::get_integer ( const Glib::ustring key  )  const

Gets the value in the first group, under key, interpreting it as an integer.

Parameters:
key The name of the key
Returns:
The value of key as an integer
Exceptions:
Glib::KeyFileError 

int Glib::KeyFile::get_integer ( const Glib::ustring group_name,
const Glib::ustring key 
) const

Return value: the value associated with the key as an integer, or.

Parameters:
group_name A group name.
key A key.
error Return location for a G::Error.
Returns:
The value associated with the key as an integer, or 0 if the key was not found or could not be parsed.
Since glibmm 2.6:

double Glib::KeyFile::get_double ( const Glib::ustring key  )  const

Gets the value in the first group, under key, interpreting it as a double.

Parameters:
key The name of the key
Returns:
The value of key as an double
Exceptions:
Glib::KeyFileError 
Since glibmm 2.14:

double Glib::KeyFile::get_double ( const Glib::ustring group_name,
const Glib::ustring key 
) const

Return value: the value associated with the key as a double, or.

Parameters:
group_name A group name.
key A key.
error Return location for a G::Error.
Returns:
The value associated with the key as a double, or 0.0 if the key was not found or could not be parsed.
Since glibmm 2.14:

void Glib::KeyFile::set_double ( const Glib::ustring group_name,
const Glib::ustring key,
double  value 
)

Associates a new double value with key under group_name .

If key cannot be found then it is created.

Since glibmm 2.14:
Parameters:
group_name A group name.
key A key.
value An double value.

void Glib::KeyFile::set_double ( const Glib::ustring key,
double  value 
)

Associates a new double value with key under the start group.

If key cannot be found then it is created.

Since glibmm 2.12:
Parameters:
key A key.
value An double value.

Glib::ArrayHandle<Glib::ustring> Glib::KeyFile::get_string_list ( const Glib::ustring group_name,
const Glib::ustring key 
) const

Returns the values associated with key under group_name.

Parameters:
group_name The name of a group
key The name of a key
Returns:
A list containing the values requested
Exceptions:
Glib::KeyFileError 

Glib::ArrayHandle<Glib::ustring> Glib::KeyFile::get_locale_string_list ( const Glib::ustring group_name,
const Glib::ustring key 
) const

Returns the values associated with key under group_name translated into the current locale, if available.

Parameters:
group_name The name of a group
key The name of a key
Returns:
A list containing the values requested
Exceptions:
Glib::KeyFileError 

Glib::ArrayHandle<Glib::ustring> Glib::KeyFile::get_locale_string_list ( const Glib::ustring group_name,
const Glib::ustring key,
const Glib::ustring locale 
) const

Returns the values associated with key under group_name translated into locale, if available.

Parameters:
group_name The name of a group
key The name of a key
locale The name of a locale
Returns:
A list containing the values requested
Exceptions:
Glib::KeyFileError 

Glib::ArrayHandle<bool> Glib::KeyFile::get_boolean_list ( const Glib::ustring group_name,
const Glib::ustring key 
) const

Returns the values associated with key under group_name.

Parameters:
group_name The name of a group
key The name of a key
Returns:
A list of booleans
Exceptions:
Glib::KeyFileError 

Glib::ArrayHandle<int> Glib::KeyFile::get_integer_list ( const Glib::ustring group_name,
const Glib::ustring key 
) const

Returns the values associated with key under group_name.

Parameters:
group_name The name of a group
key The name of a key
Returns:
A list of integers
Exceptions:
Glib::KeyFileError 

Glib::ArrayHandle<double> Glib::KeyFile::get_double_list ( const Glib::ustring group_name,
const Glib::ustring key 
) const

Returns the values associated with key under group_name.

Parameters:
group_name The name of a group
key The name of a key
Returns:
A list of doubles
Exceptions:
Glib::KeyFileError 

Glib::ustring Glib::KeyFile::get_comment (  )  const

Get comment from top of file.

Returns:
The comment

Glib::ustring Glib::KeyFile::get_comment ( const Glib::ustring group_name  )  const

Get comment from above a group.

Parameters:
group_name The group
Returns:
The comment

Glib::ustring Glib::KeyFile::get_comment ( const Glib::ustring group_name,
const Glib::ustring key 
) const

Retrieves a comment above key from group_name .

group_name . If key is 0 then comment will be read from above group_name . If both key and group_name are 0, then comment will be read from above the first group in the file.

Parameters:
group_name A group name, or 0.
key A key.
error Return location for a G::Error.
Returns:
A comment that should be freed with g_free()
Since glibmm 2.6:

void Glib::KeyFile::set_list_separator ( gchar  separator  ) 

Sets the character which is used to separate values in lists.

Typically ';' or ',' are used as separators. The default list separator is ';'.

Since glibmm 2.6:
Parameters:
separator The separator.

void Glib::KeyFile::set_value ( const Glib::ustring group_name,
const Glib::ustring key,
const Glib::ustring value 
)

Associates a new value with key under group_name .

If key cannot be found then it is created. If group_name cannot be found then it is created.

Since glibmm 2.6:
Parameters:
group_name A group name.
key A key.
value A string.

void Glib::KeyFile::set_string ( const Glib::ustring group_name,
const Glib::ustring key,
const Glib::ustring string 
)

Associates a new string value with key under group_name .

If key cannot be found then it is created. If group_name cannot be found then it is created.

Since glibmm 2.6:
Parameters:
group_name A group name.
key A key.
string A string.

void Glib::KeyFile::set_locale_string ( const Glib::ustring group_name,
const Glib::ustring key,
const Glib::ustring locale,
const Glib::ustring string 
)

Associates a string value for key and locale under group_name .

If the translation for key cannot be found then it is created.

Since glibmm 2.6:
Parameters:
group_name A group name.
key A key.
locale A locale.
string A string.

void Glib::KeyFile::set_boolean ( const Glib::ustring group_name,
const Glib::ustring key,
bool  value 
)

Associates a new boolean value with key under group_name .

If key cannot be found then it is created.

Since glibmm 2.6:
Parameters:
group_name A group name.
key A key.
value true or false.

void Glib::KeyFile::set_integer ( const Glib::ustring group_name,
const Glib::ustring key,
int  value 
)

Associates a new integer value with key under group_name .

If key cannot be found then it is created.

Since glibmm 2.6:
Parameters:
group_name A group name.
key A key.
value An integer value.

void Glib::KeyFile::set_string_list ( const Glib::ustring group_name,
const Glib::ustring key,
const Glib::ArrayHandle<Glib::ustring>&  list 
)

Sets a list of string values for key under group_name.

If key cannot be found it is created. If group_name cannot be found it is created.

Parameters:
group_name The name of a group
key The name of a key
list A list holding objects of type Glib::ustring

void Glib::KeyFile::set_locale_string_list ( const Glib::ustring group_name,
const Glib::ustring key,
const Glib::ustring locale,
const Glib::ArrayHandle<Glib::ustring>&  list 
)

Sets a list of string values for the key under group_name and marks them as being for locale.

If the key or group_name cannot be found, they are created.

Parameters:
group_name The name of a group
key The name of a key
locale A locale
list A list holding objects of type Glib::ustring

void Glib::KeyFile::set_boolean_list ( const Glib::ustring group_name,
const Glib::ustring key,
Glib::ArrayHandle<bool>&  list 
)

Sets a list of booleans for the key under group_name.

If either the key or group_name cannot be found they are created.

Parameters:
group_name The name of a group
key The name of a key
list A list holding object of type bool

void Glib::KeyFile::set_integer_list ( const Glib::ustring group_name,
const Glib::ustring key,
Glib::ArrayHandle<int>&  list 
)

Sets a list of integers for the key under group_name.

If either the key or group_name cannot be found they are created.

Parameters:
group_name The name of a group
key The name of a key
list A list holding object of type int

void Glib::KeyFile::set_double_list ( const Glib::ustring group_name,
const Glib::ustring key,
Glib::ArrayHandle<double>&  list 
)

Sets a list of doubles for the key under group_name.

If either the key or group_name cannot be found they are created.

Parameters:
group_name The name of a group
key The name of a key
list A list holding object of type int
Since glibmm 2.14:

void Glib::KeyFile::set_comment ( const Glib::ustring comment  ) 

Places comment at the start of the file, before the first group.

Parameters:
comment The Comment

void Glib::KeyFile::set_comment ( const Glib::ustring group_name,
const Glib::ustring comment 
)

Places comment above group_name.

Parameters:
group_name The Group the comment should be above
comment The comment

void Glib::KeyFile::set_comment ( const Glib::ustring group_name,
const Glib::ustring key,
const Glib::ustring comment 
)

Places a comment above key from group_name .

group_name . If key is 0 then comment will be written above group_name . If both key and group_name are 0, then comment will be written above the first group in the file.

Since glibmm 2.6:
Parameters:
group_name A group name, or 0.
key A key.
comment A comment.
error Return location for a G::Error.

void Glib::KeyFile::remove_comment ( const Glib::ustring group_name,
const Glib::ustring key 
)

Removes a comment above key from group_name .

group_name . If key is 0 then comment will be written above group_name . If both key and group_name are 0, then comment will be written above the first group in the file.

Since glibmm 2.6:
Parameters:
group_name A group name, or 0.
key A key.
error Return location for a G::Error.

void Glib::KeyFile::remove_key ( const Glib::ustring group_name,
const Glib::ustring key 
)

Removes key in group_name from the key file.

Since glibmm 2.6:
Parameters:
group_name A group name.
key A key name to remove.
error Return location for a G::Error or 0.

void Glib::KeyFile::remove_group ( const Glib::ustring group_name  ) 

Removes the specified group, group_name , from the key file.

Since glibmm 2.6:
Parameters:
group_name A group name.
error Return location for a G::Error or 0.

GKeyFile* Glib::KeyFile::gobj (  )  [inline]

const GKeyFile* Glib::KeyFile::gobj (  )  const [inline]


Member Data Documentation

GKeyFile* Glib::KeyFile::gobject_ [protected]

bool Glib::KeyFile::owns_gobject_ [protected]


The documentation for this class was generated from the following file:

Generated for glibmm 2.4 by Doxygen 1.5.1 © 1997-2001