KeyFile

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

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 keylocale=value, with a locale identifier of the form lang_COUNTRYMODIFIER 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.

class KeyFile {}

Constructors

this
this(GKeyFile* gKeyFile)

Sets our main struct and passes it to the parent class

this
this()

Creates a new empty GKeyFile object. Use g_key_file_load_from_file(), g_key_file_load_from_data(), g_key_file_load_from_dirs() or g_key_file_load_from_data_dirs() to read an existing key file. Since 2.6

Destructor

~this
~this()
Undocumented in source.

Members

Functions

doref
KeyFile doref()

Increases the reference count of key_file. Since 2.32

free
void free()

Clears all keys and groups from key_file, and decreases the reference count by 1. If the reference count reaches zero, frees the key file and all its allocated memory. Since 2.6

getBoolean
int getBoolean(string groupName, string key)

Returns the value associated with key under group_name as a boolean. If key cannot be found then FALSE is returned and error is set to G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated with key cannot be interpreted as a boolean then FALSE is returned and error is set to G_KEY_FILE_ERROR_INVALID_VALUE. Since 2.6

getBooleanList
int[] getBooleanList(string groupName, string key)

Returns the values associated with key under group_name as booleans. If key cannot be found then NULL is returned and error is set to G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated with key cannot be interpreted as booleans then NULL is returned and error is set to G_KEY_FILE_ERROR_INVALID_VALUE. Since 2.6

getComment
string getComment(string groupName, string key)

Retrieves a comment above key from group_name. If key is NULL then comment will be read from above group_name. If both key and group_name are NULL, then comment will be read from above the first group in the file. Since 2.6

getDouble
double getDouble(string groupName, string key)

Returns the value associated with key under group_name as a double. If group_name is NULL, the start_group is used. If key cannot be found then 0.0 is returned and error is set to G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated with key cannot be interpreted as a double then 0.0 is returned and error is set to G_KEY_FILE_ERROR_INVALID_VALUE. Since 2.12

getDoubleList
double[] getDoubleList(string groupName, string key)

Returns the values associated with key under group_name as doubles. If key cannot be found then NULL is returned and error is set to G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated with key cannot be interpreted as doubles then NULL is returned and error is set to G_KEY_FILE_ERROR_INVALID_VALUE. Since 2.12

getGroups
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. Since 2.6

getInt64
long getInt64(string groupName, string key)

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 2.26

getInteger
int getInteger(string groupName, string key)

Returns the value associated with key under group_name as an integer. If key cannot be found then 0 is returned and error is set to G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated with key cannot be interpreted as an integer then 0 is returned and error is set to G_KEY_FILE_ERROR_INVALID_VALUE. Since 2.6

getIntegerList
int[] getIntegerList(string groupName, string key)

Returns the values associated with key under group_name as integers. If key cannot be found then NULL is returned and error is set to G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated with key cannot be interpreted as integers then NULL is returned and error is set to G_KEY_FILE_ERROR_INVALID_VALUE. Since 2.6

getKeyFileStruct
GKeyFile* getKeyFileStruct()
Undocumented in source. Be warned that the author may not have intended to support it.
getKeys
string[] getKeys(string groupName)

Returns all keys for the group name group_name. The array of returned keys will be NULL-terminated, so length may optionally be NULL. In the event that the group_name cannot be found, NULL is returned and error is set to G_KEY_FILE_ERROR_GROUP_NOT_FOUND. Since 2.6

getLocaleString
string getLocaleString(string groupName, string key, string locale)

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. If key cannot be found then NULL is returned and error is set to G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated with key cannot be interpreted or no suitable translation can be found then the untranslated value is returned. Since 2.6

getLocaleStringList
string[] getLocaleStringList(string groupName, string key, string locale)

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. If key cannot be found then NULL is returned and error is set to G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated with key cannot be interpreted or no suitable translations can be found then the untranslated values are returned. The returned array is NULL-terminated, so length may optionally be NULL. Since 2.6

getStartGroup
string getStartGroup()

Returns the name of the start group of the file. Since 2.6

getString
string getString(string groupName, string key)

Returns the string value associated with key under group_name. Unlike g_key_file_get_value(), this function handles escape sequences like \s. In the event the key cannot be found, NULL is returned and error is set to G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the event that the group_name cannot be found, NULL is returned and error is set to G_KEY_FILE_ERROR_GROUP_NOT_FOUND. Since 2.6

getStringList
string[] getStringList(string groupName, string key)

Returns the values associated with key under group_name. In the event the key cannot be found, NULL is returned and error is set to G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the event that the group_name cannot be found, NULL is returned and error is set to G_KEY_FILE_ERROR_GROUP_NOT_FOUND. Since 2.6

getStruct
void* getStruct()

the main Gtk struct as a void*

getUint64
ulong getUint64(string groupName, string key)

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 2.26

getValue
string getValue(string groupName, string key)

Returns the raw value associated with key under group_name. Use g_key_file_get_string() to retrieve an unescaped UTF-8 string. In the event the key cannot be found, NULL is returned and error is set to G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the event that the group_name cannot be found, NULL is returned and error is set to G_KEY_FILE_ERROR_GROUP_NOT_FOUND. Since 2.6

hasGroup
int hasGroup(string groupName)

Looks whether the key file has the group group_name. Since 2.6

hasKey
int hasKey(string groupName, string key)

Looks whether the key file has the key key in the group group_name. Note This function does not follow the rules for GError strictly; the return value both carries meaning and signals an error. To use this function, you must pass a GError pointer in error, and check whether it is not NULL to see if an error occurred. Language bindings should use g_key_file_get_value() to test whether or not a key exists. Since 2.6

loadFromData
int loadFromData(string data, GKeyFileFlags 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. Since 2.6

loadFromDataDirs
int loadFromDataDirs(string file, string fullPath, GKeyFileFlags flags)

This function looks for a key file named file in the paths returned from g_get_user_data_dir() and g_get_system_data_dirs(), loads the file into key_file and returns the file's full path in full_path. If the file could not be loaded then an error is set to either a GFileError or GKeyFileError. Since 2.6

loadFromDirs
int loadFromDirs(string file, string[] searchDirs, string fullPath, GKeyFileFlags flags)

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 loaded then an error is set to either a GFileError or GKeyFileError. Since 2.14

loadFromFile
int loadFromFile(string file, GKeyFileFlags flags)

Loads a key file into an empty GKeyFile structure. If the file could not be loaded then error is set to either a GFileError or GKeyFileError. Since 2.6

removeComment
int removeComment(string groupName, string key)

Removes a comment above key from group_name. If key is NULL then comment will be removed above group_name. If both key and group_name are NULL, then comment will be removed above the first group in the file. Since 2.6

removeGroup
int removeGroup(string groupName)

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

removeKey
int removeKey(string groupName, string key)

Removes key in group_name from the key file. Since 2.6

setBoolean
void setBoolean(string groupName, string key, int value)

Associates a new boolean value with key under group_name. If key cannot be found then it is created. Since 2.6

setBooleanList
void setBooleanList(string groupName, string key, int[] list)

Associates a list of boolean values with key under group_name. If key cannot be found then it is created. If group_name is NULL, the start_group is used. Since 2.6

setComment
int setComment(string groupName, string key, string comment)

Places a comment above key from group_name. If key is NULL then comment will be written above group_name. If both key and group_name are NULL, then comment will be written above the first group in the file. Since 2.6

setDouble
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. Since 2.12

setDoubleList
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. Since 2.12

setInt64
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. Since 2.26

setInteger
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. Since 2.6

setIntegerList
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. Since 2.6

setListSeparator
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 ';'. Since 2.6

setLocaleString
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. Since 2.6

setLocaleStringList
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. Since 2.6

setString
void setString(string groupName, string key, string 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 2.6

setStringList
void setStringList(string groupName, string key, string[] list)

Associates a list of string values for key under group_name. If key cannot be found then it is created. If group_name cannot be found then it is created. Since 2.6

setUint64
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. Since 2.26

setValue
void setValue(string groupName, string key, string 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 2.6

toData
string toData()

This function outputs key_file as a string. Note that this function never reports an error, so it is safe to pass NULL as error. Since 2.6

unref
void unref()

Decreases the reference count of key_file by 1. If the reference count reaches zero, frees the key file and all its allocated memory. Since 2.32

Variables

gKeyFile
GKeyFile* gKeyFile;

the main Gtk struct

Meta