glibmm 2.78.1
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Related Functions | List of all members
Glib::KeyFile Class Referencefinal

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...

#include <glibmm/keyfile.h>

Public Types

enum class  Flags {
  Flags::NONE = 0x0 ,
  Flags::KEEP_COMMENTS = 1 << 0 ,
  Flags::KEEP_TRANSLATIONS = 1 << 1
}
 Flags which influence the parsing. More...
 

Public Member Functions

void reference () const
 Increment the reference count for this object. More...
 
void unreference () const
 Decrement the reference count for this object. More...
 
GKeyFile * gobj ()
 Provides access to the underlying C instance. More...
 
const GKeyFile * gobj () const
 Provides access to the underlying C instance. More...
 
GKeyFile * gobj_copy () const
 Provides access to the underlying C instance. The caller is responsible for unrefing it. Use when directly setting fields in structs. More...
 
 KeyFile ()=delete
 
 KeyFile (const KeyFile &)=delete
 
KeyFileoperator= (const KeyFile &)=delete
 
bool load_from_file (const std::string & file, Flags flags=Flags::NONE)
 Loads a key file into an empty KeyFile instance. More...
 
bool load_from_data (const Glib::ustring &data, Flags flags=Flags::NONE)
 Loads a KeyFile from memory. More...
 
bool load_from_dirs (const std::string & file, const std::vector< std::string > & search_dirs, std::string & full_path, Flags flags=Flags::NONE)
 This function looks for a key file named file in the paths specified in search_dirs, loads the file into key_file and returns the file's full path in full_path. More...
 
bool load_from_data_dirs (const std::string & file, std::string & full_path, Flags flags=Flags::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. More...
 
Glib::ustring to_data ()
 Outputs the KeyFile as a string. More...
 
bool save_to_file (const std::string & filename)
 Writes the contents of key_file to filename using g_file_set_contents(). More...
 
Glib::ustring get_start_group () const
 Returns the name of the start group of the file. More...
 
std::vector< Glib::ustringget_groups () const
 Gets a list of all groups in the KeyFile. More...
 
std::vector< Glib::ustringget_keys (const Glib::ustring & group_name) const
 Gets a list of all keys from the group group_name. More...
 
bool has_group (const Glib::ustring & group_name) const
 Looks whether the key file has the group group_name. More...
 
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. More...
 
Glib::ustring get_value (const Glib::ustring & group_name, const Glib::ustring & key) const
 Returns the value associated with key under group_name. More...
 
Glib::ustring get_string (const Glib::ustring & group_name, const Glib::ustring & key) const
 Return value: a newly allocated string or nullptr. More...
 
Glib::ustring get_locale_for_key (const Glib::ustring & group_name, const Glib::ustring & key, const Glib::ustring & locale={}) const
 Returns the actual locale which the result of g_key_file_get_locale_string() or g_key_file_get_locale_string_list() came from. More...
 
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. More...
 
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 nullptr. More...
 
bool get_boolean (const Glib::ustring & key) const
 Gets the value in the first group, under key, interpreting it as a boolean. More...
 
bool get_boolean (const Glib::ustring & group_name, const Glib::ustring & key) const
 Return value: the value associated with the key as a boolean. More...
 
int get_integer (const Glib::ustring & key) const
 Gets the value in the first group, under key, interpreting it as an integer. More...
 
int get_integer (const Glib::ustring & group_name, const Glib::ustring & key) const
 Return value: the value associated with the key as an integer. More...
 
gint64 get_int64 (const Glib::ustring & key) const
 Gets the value in the first group, under key, interpreting it as a signed 64-bit integer. More...
 
gint64 get_int64 (const Glib::ustring & group_name, const Glib::ustring & key) const
 Returns the value associated with key under group_name as a signed 64-bit integer. More...
 
guint64 get_uint64 (const Glib::ustring & key) const
 Gets the value in the first group, under key, interpreting it as an unsigned 64-bit integer. More...
 
guint64 get_uint64 (const Glib::ustring & group_name, const Glib::ustring & key) const
 Returns the value associated with key under group_name as an unsigned 64-bit integer. More...
 
double get_double (const Glib::ustring & key) const
 Gets the value in the first group, under key, interpreting it as a double. More...
 
double get_double (const Glib::ustring & group_name, const Glib::ustring & key) const
 Return value: the value associated with the key as a double. More...
 
void set_double (const Glib::ustring & group_name, const Glib::ustring & key, double value)
 Associates a new double value with key under group_name. More...
 
void set_double (const Glib::ustring & key, double value)
 Associates a new double value with key under the start group. More...
 
std::vector< Glib::ustringget_string_list (const Glib::ustring & group_name, const Glib::ustring & key) const
 Returns the values associated with key under group_name. More...
 
std::vector< 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. More...
 
std::vector< 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. More...
 
std::vector< bool > get_boolean_list (const Glib::ustring & group_name, const Glib::ustring & key) const
 Returns the values associated with key under group_name. More...
 
std::vector< int > get_integer_list (const Glib::ustring & group_name, const Glib::ustring & key) const
 Returns the values associated with key under group_name. More...
 
std::vector< double > get_double_list (const Glib::ustring & group_name, const Glib::ustring & key) const
 Returns the values associated with key under group_name. More...
 
Glib::ustring get_comment () const
 Get comment from top of file. More...
 
Glib::ustring get_comment (const Glib::ustring & group_name) const
 Get comment from above a group. More...
 
Glib::ustring get_comment (const Glib::ustring & group_name, const Glib::ustring & key) const
 Retrieves a comment above key from group_name. More...
 
void set_list_separator (gchar separator)
 Sets the character which is used to separate values in lists. More...
 
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. More...
 
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. More...
 
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. More...
 
void set_boolean (const Glib::ustring & group_name, const Glib::ustring & key, bool value)
 Associates a new boolean value with key under group_name. More...
 
void set_integer (const Glib::ustring & group_name, const Glib::ustring & key, int value)
 Associates a new integer value with key under group_name. More...
 
void set_int64 (const Glib::ustring & group_name, const Glib::ustring & key, gint64 value)
 Associates a new integer value with key under group_name. More...
 
void set_uint64 (const Glib::ustring & group_name, const Glib::ustring & key, guint64 value)
 Associates a new integer value with key under group_name. More...
 
void set_string_list (const Glib::ustring & group_name, const Glib::ustring & key, const std::vector< Glib::ustring > & list)
 Sets a list of string values for key under group_name. More...
 
void set_locale_string_list (const Glib::ustring & group_name, const Glib::ustring & key, const Glib::ustring & locale, const std::vector< Glib::ustring > & list)
 Sets a list of string values for the key under group_name and marks them as being for locale. More...
 
void set_boolean_list (const Glib::ustring & group_name, const Glib::ustring & key, const std::vector< bool > & list)
 Sets a list of booleans for the key under group_name. More...
 
void set_integer_list (const Glib::ustring & group_name, const Glib::ustring & key, const std::vector< int > & list)
 Sets a list of integers for the key under group_name. More...
 
void set_double_list (const Glib::ustring & group_name, const Glib::ustring & key, const std::vector< double > & list)
 Sets a list of doubles for the key under group_name. More...
 
void set_comment (const Glib::ustring & comment)
 Places comment at the start of the file, before the first group. More...
 
void set_comment (const Glib::ustring & group_name, const Glib::ustring & comment)
 Places comment above group_name. More...
 
void set_comment (const Glib::ustring & group_name, const Glib::ustring & key, const Glib::ustring & comment)
 Places a comment above key from group_name. More...
 
void remove_comment (const Glib::ustring & group_name, const Glib::ustring & key)
 Removes a comment above key from group_name. More...
 
void remove_key (const Glib::ustring & group_name, const Glib::ustring & key)
 Removes key in group_name from the key file. More...
 
void remove_group (const Glib::ustring & group_name)
 Removes the specified group, group_name, from the key file. More...
 

Static Public Member Functions

static Glib::RefPtr< KeyFilecreate ()
 

Protected Member Functions

void operator delete (void *, std::size_t)
 

Related Functions

(Note that these are not member functions.)

Glib::RefPtr< Glib::KeyFilewrap (GKeyFile * object, bool take_copy=false)
 A Glib::wrap() method for this object. More...
 

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 \n, \t, \r, and \\, respectively. To preserve leading spaces in values, these can also be escaped as \s.

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

◆ KeyFile() [1/2]

Glib::KeyFile::KeyFile ( )
delete

◆ KeyFile() [2/2]

Glib::KeyFile::KeyFile ( const KeyFile )
delete

Member Function Documentation

◆ create()

static Glib::RefPtr< KeyFile > Glib::KeyFile::create ( )
static

◆ get_boolean() [1/2]

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.

Since glibmm 2.6:
Exceptions
Glib::KeyFileError
Parameters
group_nameA group name.
keyA key.
Returns
The value associated with the key as a boolean, or false if the key was not found or could not be parsed.

◆ get_boolean() [2/2]

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

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

Parameters
keyThe name of the key.
Returns
The value of key as a boolean.
Exceptions
Glib::KeyFileError.
Since glibmm 2.6:

◆ get_boolean_list()

std::vector< 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_nameThe name of a group
keyThe name of a key
Returns
A list of booleans
Exceptions
Glib::KeyFileError

◆ get_comment() [1/3]

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

Get comment from top of file.

Returns
The comment
Exceptions
Glib::KeyFileError

◆ get_comment() [2/3]

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

Get comment from above a group.

Parameters
group_nameThe group
Returns
The comment
Exceptions
Glib::KeyFileError

◆ get_comment() [3/3]

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

Retrieves a comment above key from group_name.

If key is nullptr then comment will be read from above group_name. If both key and group_name are nullptr, then comment will be read from above the first group in the file. Use the overrides for a nullptr key or group.

Since glibmm 2.6:
Exceptions
Glib::KeyFileError
Parameters
group_nameA group name.
keyA key.
Returns
The comment.

◆ get_double() [1/2]

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.

Since glibmm 2.14:
Exceptions
Glib::KeyFileError
Parameters
group_nameA group name.
keyA key.
Returns
The value associated with the key as a double, or 0.0 if the key was not found or could not be parsed.

◆ get_double() [2/2]

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
keyThe name of the key
Returns
The value of key as an double
Exceptions
Glib::KeyFileError
Since glibmm 2.14:

◆ get_double_list()

std::vector< 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_nameThe name of a group
keyThe name of a key
Returns
A list of doubles
Exceptions
Glib::KeyFileError

◆ get_groups()

std::vector< 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

◆ get_int64() [1/2]

gint64 Glib::KeyFile::get_int64 ( const Glib::ustring group_name,
const Glib::ustring key 
) const

Returns the value associated with key under group_name as a signed 64-bit integer.

This is similar to g_key_file_get_integer() but can return 64-bit results without truncation.

Since glibmm 2.26:
Parameters
group_nameA non-nullptr group name.
keyA non-nullptr key.
Returns
The value associated with the key as a signed 64-bit integer, or 0 if the key was not found or could not be parsed.
Exceptions
Glib::KeyFileError

◆ get_int64() [2/2]

gint64 Glib::KeyFile::get_int64 ( const Glib::ustring key) const

Gets the value in the first group, under key, interpreting it as a signed 64-bit integer.

This is similar to get_integer() but can return 64-bit results without truncation.

Parameters
keyThe name of the key.
Returns
The value of key as a signed 64-bit integer, or 0 if the key was not found or could not be parsed.
Exceptions
Glib::KeyFileError.
Since glibmm 2.28:

◆ get_integer() [1/2]

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.

Since glibmm 2.6:
Exceptions
Glib::KeyFileError
Parameters
group_nameA group name.
keyA key.
Returns
The value associated with the key as an integer, or 0 if the key was not found or could not be parsed.

◆ get_integer() [2/2]

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
keyThe name of the key
Returns
The value of key as an integer
Exceptions
Glib::KeyFileError
Since glibmm 2.6:

◆ get_integer_list()

std::vector< 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_nameThe name of a group
keyThe name of a key
Returns
A list of integers
Exceptions
Glib::KeyFileError

◆ get_keys()

std::vector< 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_nameThe name of a group
Returns
A list containing the names of the keys in group_name
Exceptions
Glib::KeyFileError

◆ get_locale_for_key()

Glib::ustring Glib::KeyFile::get_locale_for_key ( const Glib::ustring group_name,
const Glib::ustring key,
const Glib::ustring locale = {} 
) const

Returns the actual locale which the result of g_key_file_get_locale_string() or g_key_file_get_locale_string_list() came from.

If calling g_key_file_get_locale_string() or g_key_file_get_locale_string_list() with exactly the same key_file, group_name, key and locale, the result of those functions will have originally been tagged with the locale that is the result of this function.

Since glibmm 2.60:
Parameters
group_nameA group name.
keyA key.
localeA locale identifier or nullptr.
Returns
The locale from the file, or nullptr if the key was not found or the entry in the file was was untranslated.

◆ get_locale_string() [1/2]

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.

Returns
the value as a Glib::ustring
Exceptions
Glib::KeyFileError

◆ get_locale_string() [2/2]

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 nullptr.

Since glibmm 2.6:
Exceptions
Glib::KeyFileError
Parameters
group_nameA group name.
keyA key.
localeA locale identifier or nullptr.
Returns
A newly allocated string or nullptr if the specified key cannot be found.

◆ get_locale_string_list() [1/2]

std::vector< 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_nameThe name of a group
keyThe name of a key
Returns
A list containing the values requested
Exceptions
Glib::KeyFileError

◆ get_locale_string_list() [2/2]

std::vector< 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_nameThe name of a group
keyThe name of a key
localeThe name of a locale
Returns
A list containing the values requested
Exceptions
Glib::KeyFileError

◆ get_start_group()

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

Returns the name of the start group of the file.

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

◆ get_string()

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

Return value: a newly allocated string or nullptr.

Since glibmm 2.6:
Exceptions
Glib::KeyFileError
Parameters
group_nameA group name.
keyA key.
Returns
A newly allocated string or nullptr if the specified key cannot be found.

◆ get_string_list()

std::vector< 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_nameThe name of a group
keyThe name of a key
Returns
A list containing the values requested
Exceptions
Glib::KeyFileError

◆ get_uint64() [1/2]

guint64 Glib::KeyFile::get_uint64 ( const Glib::ustring group_name,
const Glib::ustring key 
) const

Returns the value associated with key under group_name as an unsigned 64-bit integer.

This is similar to g_key_file_get_integer() but can return large positive results without truncation.

Since glibmm 2.26:
Parameters
group_nameA non-nullptr group name.
keyA non-nullptr key.
Returns
The value associated with the key as an unsigned 64-bit integer, or 0 if the key was not found or could not be parsed.
Exceptions
Glib::KeyFileError

◆ get_uint64() [2/2]

guint64 Glib::KeyFile::get_uint64 ( const Glib::ustring key) const

Gets the value in the first group, under key, interpreting it as an unsigned 64-bit integer.

This is similar to get_integer() but can return large positive results without truncation.

Parameters
keyThe name of the key.
Returns
The value of key as an unsigned 64-bit integer, or 0 if the key was not found or could not be parsed.
Exceptions
Glib::KeyFileError.
Since glibmm 2.28:

◆ get_value()

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

Returns the value associated with key under group_name.

Since glibmm 2.6:
Exceptions
Glib::KeyFileErrorin the event the key cannot be found (with the Glib::KeyFileError::KEY_NOT_FOUND code).
Glib::KeyFileErrorin the event that the group_name cannot be found (with the Glib::KeyFileError::GROUP_NOT_FOUND code).
Parameters
group_nameA group name.
keyA key.
Returns
The value as a string.

◆ gobj() [1/2]

GKeyFile * Glib::KeyFile::gobj ( )

Provides access to the underlying C instance.

◆ gobj() [2/2]

const GKeyFile * Glib::KeyFile::gobj ( ) const

Provides access to the underlying C instance.

◆ gobj_copy()

GKeyFile * Glib::KeyFile::gobj_copy ( ) const

Provides access to the underlying C instance. The caller is responsible for unrefing it. Use when directly setting fields in structs.

◆ has_group()

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

Looks whether the key file has the group group_name.

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

◆ has_key()

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.

Since glibmm 2.6:
Exceptions
Glib::KeyFileError
Parameters
group_nameA group name.
keyA key name.
Returns
true if key is a part of group_name, false otherwise.

◆ load_from_data()

bool Glib::KeyFile::load_from_data ( const Glib::ustring data,
Flags  flags = Flags::NONE 
)

Loads a KeyFile from memory.

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

◆ load_from_data_dirs()

bool Glib::KeyFile::load_from_data_dirs ( const std::string file,
std::string full_path,
Flags  flags = Flags::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
fileThe file to search for
[out]full_pathReturn location for a string containing the full path of the file
flagsBitwise combination of the flags to use for the KeyFile
Returns
true if the KeyFile was successfully loaded, false otherwise
Exceptions
Glib::KeyFileError
Glib::FileError

◆ load_from_dirs()

bool Glib::KeyFile::load_from_dirs ( const std::string file,
const std::vector< std::string > &  search_dirs,
std::string full_path,
Flags  flags = Flags::NONE 
)

This function looks for a key file named file in the paths specified in search_dirs, loads the file into key_file and returns the file's full path in full_path.

If the file could not be found in any of the search_dirs, Glib::KeyFileError::NOT_FOUND is returned. If the file is found but the OS returns an error when opening or reading the file, a Glib::FileError is returned. If there is a problem parsing the file, a Glib::KeyFileError is returned.

Since glibmm 2.14:
Parameters
fileA relative path to a filename to open and parse.
search_dirsnullptr-terminated array of directories to search.
full_pathReturn location for a string containing the full path of the file, or nullptr.
flagsFlags from Glib::KeyFile::Flags.
Returns
true if a key file could be loaded, false otherwise.
Exceptions
Glib::KeyFileError
Glib::FileError

◆ load_from_file()

bool Glib::KeyFile::load_from_file ( const std::string file,
Flags  flags = Flags::NONE 
)

Loads a key file into an empty KeyFile instance.

If the file could not be loaded then a FileError or KeyFileError exception is thrown.

Since glibmm 2.6:
Exceptions
Glib::FileError
Glib::KeyFileError
Parameters
fileThe path of a filename to load, in the GLib filename encoding.
flagsFlags from Glib::KeyFile::Flags.
Returns
true if a key file could be loaded, false otherwise.

◆ operator delete()

void Glib::KeyFile::operator delete ( void *  ,
std::size_t   
)
protected

◆ operator=()

KeyFile & Glib::KeyFile::operator= ( const KeyFile )
delete

◆ reference()

void Glib::KeyFile::reference ( ) const

Increment the reference count for this object.

You should never need to do this manually - use the object via a RefPtr instead.

◆ remove_comment()

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

Removes a comment above key from group_name.

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

Exceptions
Glib::KeyFileError
Parameters
group_nameA group name, or nullptr.
keyA key.

◆ remove_group()

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

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

Exceptions
Glib::KeyFileError
Parameters
group_nameA group name.

◆ remove_key()

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

Removes key in group_name from the key file.

Exceptions
Glib::KeyFileError
Parameters
group_nameA group name.
keyA key name to remove.

◆ save_to_file()

bool Glib::KeyFile::save_to_file ( const std::string filename)

Writes the contents of key_file to filename using g_file_set_contents().

If you need stricter guarantees about durability of the written file than are provided by g_file_set_contents(), use g_file_set_contents_full() with the return value of g_key_file_to_data().

This function can fail for any of the reasons that g_file_set_contents() may fail.

Since glibmm 2.40:
Parameters
filenameThe name of the file to write to.
Returns
true if successful, else false with error set.
Exceptions
Glib::FileError

◆ set_boolean()

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_nameA group name.
keyA key.
valuetrue or false.

◆ set_boolean_list()

void Glib::KeyFile::set_boolean_list ( const Glib::ustring group_name,
const Glib::ustring key,
const std::vector< 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_nameThe name of a group
keyThe name of a key
listA list holding object of type bool

◆ set_comment() [1/3]

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

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

Parameters
commentThe Comment
Exceptions
Glib::KeyFileError

◆ set_comment() [2/3]

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

Places comment above group_name.

Parameters
group_nameThe Group the comment should be above
commentThe comment
Exceptions
Glib::KeyFileError

◆ set_comment() [3/3]

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.

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

Exceptions
Glib::KeyFileError
Parameters
group_nameA group name, or nullptr.
keyA key.
commentA comment.

◆ set_double() [1/2]

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.12:
Parameters
group_nameA group name.
keyA key.
valueA double value.

◆ set_double() [2/2]

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
keyA key.
valueAn double value.

◆ set_double_list()

void Glib::KeyFile::set_double_list ( const Glib::ustring group_name,
const Glib::ustring key,
const std::vector< 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_nameThe name of a group
keyThe name of a key
listA list holding object of type int
Since glibmm 2.14:

◆ set_int64()

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

Associates a new integer value with key under group_name.

If key cannot be found then it is created.

Since glibmm 2.26:
Parameters
group_nameA group name.
keyA key.
valueAn integer value.

◆ set_integer()

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_nameA group name.
keyA key.
valueAn integer value.

◆ set_integer_list()

void Glib::KeyFile::set_integer_list ( const Glib::ustring group_name,
const Glib::ustring key,
const std::vector< 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_nameThe name of a group
keyThe name of a key
listA list holding object of type int

◆ set_list_separator()

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
separatorThe separator.

◆ set_locale_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_nameA group name.
keyA key.
localeA locale identifier.
stringA string.

◆ set_locale_string_list()

void Glib::KeyFile::set_locale_string_list ( const Glib::ustring group_name,
const Glib::ustring key,
const Glib::ustring locale,
const std::vector< 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_nameThe name of a group
keyThe name of a key
localeA locale
listA list holding objects of type Glib::ustring

◆ set_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. Unlike g_key_file_set_value(), this function handles characters that need escaping, such as newlines.

Since glibmm 2.6:
Parameters
group_nameA group name.
keyA key.
stringA string.

◆ set_string_list()

void Glib::KeyFile::set_string_list ( const Glib::ustring group_name,
const Glib::ustring key,
const std::vector< 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_nameThe name of a group
keyThe name of a key
listA list holding objects of type Glib::ustring

◆ set_uint64()

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

Associates a new integer value with key under group_name.

If key cannot be found then it is created.

Since glibmm 2.26:
Parameters
group_nameA group name.
keyA key.
valueAn integer value.

◆ set_value()

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. To set an UTF-8 string which may contain characters that need escaping (such as newlines or spaces), use g_key_file_set_string().

Since glibmm 2.6:
Parameters
group_nameA group name.
keyA key.
valueA string.

◆ to_data()

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

Outputs the KeyFile as a string.

Returns
A string object holding the contents of KeyFile
Exceptions
Glib::KeyFileError

◆ unreference()

void Glib::KeyFile::unreference ( ) const

Decrement the reference count for this object.

You should never need to do this manually - use the object via a RefPtr instead.

Friends And Related Function Documentation

◆ wrap()

Glib::RefPtr< Glib::KeyFile > wrap ( GKeyFile *  object,
bool  take_copy = false 
)
related

A Glib::wrap() method for this object.

Parameters
objectThe C instance.
take_copyFalse if the result should take ownership of the C instance. True if it should take a new copy or ref.
Returns
A C++ instance that wraps this C instance.