GLib.KeyFile
Description: Reference for GLib.KeyFile

### Struct

# GLibKeyFile

#### Description [src]

    struct GKeyFile {
      /* No available fields */
    }

`GKeyFile` parses .ini-like config files.

`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. For example, 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. As a special case, the locale `C` is associated with the untranslated pair `key=value` (since GLib 2.84). Space before and after the `=` character is 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);

#### Constructors

###### g_key_file_new

Creates a new empty `GKeyFile` object.

since: 2.6

#### Functions

###### g_key_file_error_quark

No description available.

#### Instance methods

###### g_key_file_free

Clears all keys and groups from `key_file`, and decreases the reference count by 1.

since: 2.6

###### g_key_file_get_boolean

Returns the value associated with `key` under `group_name` as a boolean.

since: 2.6

###### g_key_file_get_boolean_list

Returns the values associated with `key` under `group_name` as booleans.

since: 2.6

###### g_key_file_get_comment

Retrieves a comment above `key` from `group_name`.

since: 2.6

###### g_key_file_get_double

Returns the value associated with `key` under `group_name` as a double.

since: 2.12

###### g_key_file_get_double_list

Returns the values associated with `key` under `group_name` as doubles.

since: 2.12

###### g_key_file_get_groups

Returns all groups in the key file loaded with `key_file`.

since: 2.6

###### g_key_file_get_int64

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

since: 2.26

###### g_key_file_get_integer

Returns the value associated with `key` under `group_name` as an integer.

since: 2.6

###### g_key_file_get_integer_list

Returns the values associated with `key` under `group_name` as integers.

since: 2.6

###### g_key_file_get_keys

Returns all keys for the group name `group_name`.

since: 2.6

###### g_key_file_get_locale_for_key

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

since: 2.56

###### g_key_file_get_locale_string

Returns the value associated with `key` under `group_name` translated in the given `locale` if available.

since: 2.6

###### g_key_file_get_locale_string_list

Returns the values associated with `key` under `group_name` translated in the given `locale` if available.

since: 2.6

###### g_key_file_get_start_group

Returns the name of the start group of the file.

since: 2.6

###### g_key_file_get_string

Returns the string value associated with `key` under `group_name`.

since: 2.6

###### g_key_file_get_string_list

Returns the values associated with `key` under `group_name`.

since: 2.6

###### g_key_file_get_uint64

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

since: 2.26

###### g_key_file_get_value

Returns the raw value associated with `key` under `group_name`.

since: 2.6

###### g_key_file_has_group

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

since: 2.6

###### g_key_file_has_key

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

since: 2.6

###### g_key_file_load_from_bytes

Loads a key file from the data in `bytes` into an empty `GKeyFile` structure.

since: 2.50

###### g_key_file_load_from_data

Loads a key file from memory into an empty `GKeyFile` structure.

since: 2.6

###### g_key_file_load_from_data_dirs

Looks for a key file named `file` in the paths returned from `g_get_user_data_dir()` and `g_get_system_data_dirs()`.

since: 2.6

###### g_key_file_load_from_dirs

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

since: 2.14

###### g_key_file_load_from_file

Loads a key file into an empty `GKeyFile` structure.

since: 2.6

###### g_key_file_ref

Increases the reference count of `key_file`.

since: 2.32

###### g_key_file_remove_comment

Removes a comment above `key` from `group_name`.

since: 2.6

###### g_key_file_remove_group

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

since: 2.6

###### g_key_file_remove_key

Removes `key` in `group_name` from the key file.

since: 2.6

###### g_key_file_save_to_file

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

since: 2.40

###### g_key_file_set_boolean

Associates a new boolean value with `key` under `group_name`.

since: 2.6

###### g_key_file_set_boolean_list

Associates a list of boolean values with `key` under `group_name`.

since: 2.6

###### g_key_file_set_comment

Places a comment above `key` from `group_name`.

since: 2.6

###### g_key_file_set_double

Associates a new double value with `key` under `group_name`.

since: 2.12

###### g_key_file_set_double_list

Associates a list of double values with `key` under `group_name`.

since: 2.12

###### g_key_file_set_int64

Associates a new integer value with `key` under `group_name`.

since: 2.26

###### g_key_file_set_integer

Associates a new integer value with `key` under `group_name`.

since: 2.6

###### g_key_file_set_integer_list

Associates a list of integer values with `key` under `group_name`.

since: 2.6

###### g_key_file_set_list_separator

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

since: 2.6

###### g_key_file_set_locale_string

Associates a string value for `key` and `locale` under `group_name`.

since: 2.6

###### g_key_file_set_locale_string_list

Associates a list of string values for `key` and `locale` under `group_name`.

since: 2.6

###### g_key_file_set_string

Associates a new string value with `key` under `group_name`.

since: 2.6

###### g_key_file_set_string_list

Associates a list of string values for `key` under `group_name`.

since: 2.6

###### g_key_file_set_uint64

Associates a new integer value with `key` under `group_name`.

since: 2.26

###### g_key_file_set_value

Associates a new value with `key` under `group_name`.

since: 2.6

###### g_key_file_to_data

Outputs `key_file` as a string.

since: 2.6

###### g_key_file_unref

Decreases the reference count of `key_file` by 1.

since: 2.32