glib.key_file

Module for [KeyFile] class

Types 2

[glib.key_file.KeyFile] parses .ini-like config files.

[glib.key_file.KeyFile] 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_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, with a locale identifier of the form lang_COUNTRY@MODIFIER where COUNTRY and MODIFIER are optional. 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:

  • .ini files use the ';' character to begin comments,

key files use the '#' character.

  • Key files do not allow for ungrouped keys meaning only

comments can precede the first group.

  • Key files are always encoded in UTF-8.
  • Key and Group names are case-sensitive. For example, a group called

[GROUP] is a different from [group].

  • .ini files don't have a strongly typed boolean entry type,

they only have GetProfileInt(). In key files, only true and false (in lower case) are allowed.

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.

Here is an example of loading a key file and reading a value:

g_autoptr(GError) error = NULL;
g_autoptr(GKeyFile) key_file = g_key_file_new ();

if (!g_key_file_load_from_file (key_file, "key-file.ini", flags, &error))
 {
   if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
     g_warning ("Error loading key file: %s", error->message);
   return;
 }

g_autofree gchar *val = g_key_file_get_string (key_file, "Group Name", "SomeKey", &error);
if (val == NULL &&
   !g_error_matches (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND))
 {
   g_warning ("Error finding key in key file: %s", error->message);
   return;
 }
else if (val == NULL)
 {
   // Fall back to a default value.
   val = g_strdup ("default-value");
 }

Here is an example of creating and saving a key file:

g_autoptr(GKeyFile) key_file = g_key_file_new ();
const gchar *val = …;
g_autoptr(GError) error = NULL;

g_key_file_set_string (key_file, "Group Name", "SomeKey", val);

// Save as a file.
if (!g_key_file_save_to_file (key_file, "key-file.ini", &error))
 {
   g_warning ("Error saving key file: %s", error->message);
   return;
 }

// Or store to a GBytes for use elsewhere.
gsize data_len;
g_autofree guint8 *data = (guint8 *) g_key_file_to_data (key_file, &data_len, &error);
if (data == NULL)
 {
   g_warning ("Error saving key file: %s", error->message);
   return;
 }
g_autoptr(GBytes) bytes = g_bytes_new_take (g_steal_pointer (&data), data_len);

Methods
void * _cPtr(Flag!"Dup" dup = No.Dup)
GType _gType() @property
KeyFile self()Returns `this`, for use in `with` statements.
bool getBoolean(string groupName, string key)Returns the value associated with key under group_name as a boolean.
bool[] getBooleanList(string groupName, string key)Returns the values associated with key under group_name as booleans.
string getComment(string groupName = null, string key = null)Retrieves a comment above key from groupname. If key is null then comment will be read from above groupname. If both key and group_name are null, then comment will be read from above the first grou...
double getDouble(string groupName, string key)Returns the value associated with key under groupname as a double. If groupname is null, the start_group is used.
double[] getDoubleList(string groupName, string key)Returns the values associated with key under group_name as doubles.
string[] getGroups()Returns all groups in the key file loaded with key_file. The array of returned groups will be null-terminated, so length may optionally be null. Returns: a newly-allocated null-terminated array of ...
long getInt64(string groupName, string key)Returns the value associated with key under groupname as a signed 64-bit integer. This is similar to [glib.keyfile.KeyFile.getInteger] but can return 64-bit results without truncation.
int getInteger(string groupName, string key)Returns the value associated with key under group_name as an integer.
int[] getIntegerList(string groupName, string key)Returns the values associated with key under group_name as integers.
string[] getKeys(string groupName)Returns all keys for the group name groupname. The array of returned keys will be null-terminated, so length may optionally be null. In the event that the groupname cannot be found, null is return...
string getLocaleForKey(string groupName, string key, string locale = null)Returns the actual locale which the result of [glib.keyfile.KeyFile.getLocaleString] or [glib.keyfile.KeyFile.getLocaleStringList] came from.
string getLocaleString(string groupName, string key, string locale = null)Returns the value associated with key under group_name translated in the given locale if available. If locale is null then the current locale is assumed.
string[] getLocaleStringList(string groupName, string key, string locale = null)Returns the values associated with key under group_name translated in the given locale if available. If locale is null then the current locale is assumed.
string getStartGroup()Returns the name of the start group of the file. Returns: The start group of the key file.
string getString(string groupName, string key)Returns the string value associated with key under groupname. Unlike [glib.keyfile.KeyFile.getValue], this function handles escape sequences like \s.
string[] getStringList(string groupName, string key)Returns the values associated with key under group_name.
ulong getUint64(string groupName, string key)Returns the value associated with key under groupname as an unsigned 64-bit integer. This is similar to [glib.keyfile.KeyFile.getInteger] but can return large positive results without truncation.
string getValue(string groupName, string key)Returns the raw value associated with key under groupname. Use [glib.keyfile.KeyFile.getString] to retrieve an unescaped UTF-8 string.
bool hasGroup(string groupName)Looks whether the key file has the group group_name.
bool loadFromBytes(glib.bytes.Bytes bytes, glib.types.KeyFileFlags flags)Loads a key file from the data in bytes into an empty #GKeyFile structure. If the object cannot be created then `error` is set to a #GKeyFileError.
bool loadFromData(string data, glib.types.KeyFileFlags flags)Loads a key file from memory into an empty #GKeyFile structure. If the object cannot be created then `error` is set to a #GKeyFileError.
bool loadFromDataDirs(string file, out string fullPath, glib.types.KeyFileFlags flags)This function looks for a key file named file in the paths returned from [glib.global.getUserDataDir] and [glib.global.getSystemDataDirs], loads the file into keyfile and returns the file's full pa...
bool loadFromDirs(string file, string[] searchDirs, out string fullPath, glib.types.KeyFileFlags flags)This function looks for a key file named file in the paths specified in searchdirs, loads the file into keyfile and returns the file's full path in full_path.
bool loadFromFile(string file, glib.types.KeyFileFlags flags)Loads a key file into an empty #GKeyFile structure.
bool removeComment(string groupName = null, string key = null)Removes a comment above key from groupname. If key is null then comment will be removed above groupname. If both key and group_name are null, then comment will be removed above the first group in t...
bool removeGroup(string groupName)Removes the specified group, group_name, from the key file.
bool removeKey(string groupName, string key)Removes key in group_name from the key file.
bool saveToFile(string filename)Writes the contents of keyfile to filename using [glib.global.fileSetContents]. If you need stricter guarantees about durability of the written file than are provided by [glib.global.fileSetContent...
void setBoolean(string groupName, string key, bool value)Associates a new boolean value with key under group_name. If key cannot be found then it is created.
void setBooleanList(string groupName, string key, bool[] list)Associates a list of boolean values with key under groupname. If key cannot be found then it is created. If groupname is null, the start_group is used.
bool setComment(string groupName, string key, string comment)Places a comment above key from group_name.
void setDouble(string groupName, string key, double value)Associates a new double value with key under group_name. If key cannot be found then it is created.
void setDoubleList(string groupName, string key, double[] list)Associates a list of double values with key under group_name. If key cannot be found then it is created.
void setInt64(string groupName, string key, long value)Associates a new integer value with key under group_name. If key cannot be found then it is created.
void setInteger(string groupName, string key, int value)Associates a new integer value with key under group_name. If key cannot be found then it is created.
void setIntegerList(string groupName, string key, int[] list)Associates a list of integer values with key under group_name. If key cannot be found then it is created.
void setListSeparator(char separator)Sets the character which is used to separate values in lists. Typically ';' or ',' are used as separators. The default list separator is ';'.
void setLocaleString(string groupName, string key, string locale, string string_)Associates a string value for key and locale under group_name. If the translation for key cannot be found then it is created.
void setLocaleStringList(string groupName, string key, string locale, string[] list)Associates a list of string values for key and locale under group_name. If the translation for key cannot be found then it is created.
void setString(string groupName, string key, string string_)Associates a new string value with key under groupname. If key cannot be found then it is created. If groupname cannot be found then it is created. Unlike [glib.key_file.KeyFile.setValue], this fun...
void setStringList(string groupName, string key, string[] list)Associates a list of string values for key under groupname. If key cannot be found then it is created. If groupname cannot be found then it is created.
void setUint64(string groupName, string key, ulong value)Associates a new integer value with key under group_name. If key cannot be found then it is created.
void setValue(string groupName, string key, string value)Associates a new value with key under group_name.
string toData()This function outputs key_file as a string.
Constructors
this(void * ptr, Flag!"Take" take)
this()Creates a new empty #GKeyFile object. Use [glib.keyfile.KeyFile.loadFromFile], [glib.keyfile.KeyFile.loadFromData], [glib.keyfile.KeyFile.loadFromDirs] or [glib.keyfile.KeyFile.loadFromDataDirs] to...
Constructors
this(GError * err)
this(Code code, string msg)