Key-value file parser

Key-value file parser — parses .ini-like config files

Synopsis


#include <glib.h>

                    GKeyFile;
#define             G_KEY_FILE_ERROR
enum                GKeyFileError;
enum                GKeyFileFlags;

GKeyFile *          g_key_file_new                      (void);
void                g_key_file_free                     (GKeyFile *key_file);
void                g_key_file_set_list_separator       (GKeyFile *key_file,
                                                         gchar separator);
gboolean            g_key_file_load_from_file           (GKeyFile *key_file,
                                                         const gchar *file,
                                                         GKeyFileFlags flags,
                                                         GError **error);
gboolean            g_key_file_load_from_data           (GKeyFile *key_file,
                                                         const gchar *data,
                                                         gsize length,
                                                         GKeyFileFlags flags,
                                                         GError **error);
gboolean            g_key_file_load_from_data_dirs      (GKeyFile *key_file,
                                                         const gchar *file,
                                                         gchar **full_path,
                                                         GKeyFileFlags flags,
                                                         GError **error);
gboolean            g_key_file_load_from_dirs           (GKeyFile *key_file,
                                                         const gchar *file,
                                                         const gchar **search_dirs,
                                                         gchar **full_path,
                                                         GKeyFileFlags flags,
                                                         GError **error);
gchar *             g_key_file_to_data                  (GKeyFile *key_file,
                                                         gsize *length,
                                                         GError **error);
gchar *             g_key_file_get_start_group          (GKeyFile *key_file);
gchar **            g_key_file_get_groups               (GKeyFile *key_file,
                                                         gsize *length);
gchar **            g_key_file_get_keys                 (GKeyFile *key_file,
                                                         const gchar *group_name,
                                                         gsize *length,
                                                         GError **error);
gboolean            g_key_file_has_group                (GKeyFile *key_file,
                                                         const gchar *group_name);
gboolean            g_key_file_has_key                  (GKeyFile *key_file,
                                                         const gchar *group_name,
                                                         const gchar *key,
                                                         GError **error);

gchar *             g_key_file_get_value                (GKeyFile *key_file,
                                                         const gchar *group_name,
                                                         const gchar *key,
                                                         GError **error);
gchar *             g_key_file_get_string               (GKeyFile *key_file,
                                                         const gchar *group_name,
                                                         const gchar *key,
                                                         GError **error);
gchar *             g_key_file_get_locale_string        (GKeyFile *key_file,
                                                         const gchar *group_name,
                                                         const gchar *key,
                                                         const gchar *locale,
                                                         GError **error);
gboolean            g_key_file_get_boolean              (GKeyFile *key_file,
                                                         const gchar *group_name,
                                                         const gchar *key,
                                                         GError **error);
gint                g_key_file_get_integer              (GKeyFile *key_file,
                                                         const gchar *group_name,
                                                         const gchar *key,
                                                         GError **error);
gdouble             g_key_file_get_double               (GKeyFile *key_file,
                                                         const gchar *group_name,
                                                         const gchar *key,
                                                         GError **error);
gchar **            g_key_file_get_string_list          (GKeyFile *key_file,
                                                         const gchar *group_name,
                                                         const gchar *key,
                                                         gsize *length,
                                                         GError **error);
gchar **            g_key_file_get_locale_string_list   (GKeyFile *key_file,
                                                         const gchar *group_name,
                                                         const gchar *key,
                                                         const gchar *locale,
                                                         gsize *length,
                                                         GError **error);
gboolean *          g_key_file_get_boolean_list         (GKeyFile *key_file,
                                                         const gchar *group_name,
                                                         const gchar *key,
                                                         gsize *length,
                                                         GError **error);
gint *              g_key_file_get_integer_list         (GKeyFile *key_file,
                                                         const gchar *group_name,
                                                         const gchar *key,
                                                         gsize *length,
                                                         GError **error);
gdouble *           g_key_file_get_double_list          (GKeyFile *key_file,
                                                         const gchar *group_name,
                                                         const gchar *key,
                                                         gsize *length,
                                                         GError **error);
gchar *             g_key_file_get_comment              (GKeyFile *key_file,
                                                         const gchar *group_name,
                                                         const gchar *key,
                                                         GError **error);

void                g_key_file_set_value                (GKeyFile *key_file,
                                                         const gchar *group_name,
                                                         const gchar *key,
                                                         const gchar *value);
void                g_key_file_set_string               (GKeyFile *key_file,
                                                         const gchar *group_name,
                                                         const gchar *key,
                                                         const gchar *string);
void                g_key_file_set_locale_string        (GKeyFile *key_file,
                                                         const gchar *group_name,
                                                         const gchar *key,
                                                         const gchar *locale,
                                                         const gchar *string);
void                g_key_file_set_boolean              (GKeyFile *key_file,
                                                         const gchar *group_name,
                                                         const gchar *key,
                                                         gboolean value);
void                g_key_file_set_integer              (GKeyFile *key_file,
                                                         const gchar *group_name,
                                                         const gchar *key,
                                                         gint value);
void                g_key_file_set_double               (GKeyFile *key_file,
                                                         const gchar *group_name,
                                                         const gchar *key,
                                                         gdouble value);
void                g_key_file_set_string_list          (GKeyFile *key_file,
                                                         const gchar *group_name,
                                                         const gchar *key,
                                                         const gchar * const list[],
                                                         gsize length);
void                g_key_file_set_locale_string_list   (GKeyFile *key_file,
                                                         const gchar *group_name,
                                                         const gchar *key,
                                                         const gchar *locale,
                                                         const gchar * const list[],
                                                         gsize length);
void                g_key_file_set_boolean_list         (GKeyFile *key_file,
                                                         const gchar *group_name,
                                                         const gchar *key,
                                                         gboolean list[],
                                                         gsize length);
void                g_key_file_set_integer_list         (GKeyFile *key_file,
                                                         const gchar *group_name,
                                                         const gchar *key,
                                                         gint list[],
                                                         gsize length);
void                g_key_file_set_double_list          (GKeyFile *key_file,
                                                         const gchar *group_name,
                                                         const gchar *key,
                                                         gdouble list[],
                                                         gsize length);
gboolean            g_key_file_set_comment              (GKeyFile *key_file,
                                                         const gchar *group_name,
                                                         const gchar *key,
                                                         const gchar *comment,
                                                         GError **error);
gboolean            g_key_file_remove_group             (GKeyFile *key_file,
                                                         const gchar *group_name,
                                                         GError **error);
gboolean            g_key_file_remove_key               (GKeyFile *key_file,
                                                         const gchar *group_name,
                                                         const gchar *key,
                                                         GError **error);
gboolean            g_key_file_remove_comment           (GKeyFile *key_file,
                                                         const gchar *group_name,
                                                         const gchar *key,
                                                         GError **error);

#define             G_KEY_FILE_DESKTOP_GROUP
#define             G_KEY_FILE_DESKTOP_KEY_TYPE
#define             G_KEY_FILE_DESKTOP_KEY_VERSION
#define             G_KEY_FILE_DESKTOP_KEY_NAME
#define             G_KEY_FILE_DESKTOP_KEY_GENERIC_NAME
#define             G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY
#define             G_KEY_FILE_DESKTOP_KEY_COMMENT
#define             G_KEY_FILE_DESKTOP_KEY_ICON
#define             G_KEY_FILE_DESKTOP_KEY_HIDDEN
#define             G_KEY_FILE_DESKTOP_KEY_ONLY_SHOW_IN
#define             G_KEY_FILE_DESKTOP_KEY_NOT_SHOW_IN
#define             G_KEY_FILE_DESKTOP_KEY_TRY_EXEC
#define             G_KEY_FILE_DESKTOP_KEY_EXEC
#define             G_KEY_FILE_DESKTOP_KEY_PATH
#define             G_KEY_FILE_DESKTOP_KEY_TERMINAL
#define             G_KEY_FILE_DESKTOP_KEY_MIME_TYPE
#define             G_KEY_FILE_DESKTOP_KEY_CATEGORIES
#define             G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY
#define             G_KEY_FILE_DESKTOP_KEY_STARTUP_WM_CLASS
#define             G_KEY_FILE_DESKTOP_KEY_URL
#define             G_KEY_FILE_DESKTOP_TYPE_APPLICATION
#define             G_KEY_FILE_DESKTOP_TYPE_LINK
#define             G_KEY_FILE_DESKTOP_TYPE_DIRECTORY

Description

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.

# 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
Welcome[be@latin]=Hello
[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_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 group from [group].

  • .ini files don't have a strongly typed boolean entry type, they only have GetProfileInt. In GKeyFile 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.

Details

GKeyFile

typedef struct _GKeyFile GKeyFile;

The GKeyFile struct contains only private fields and should not be used directly.


G_KEY_FILE_ERROR

#define G_KEY_FILE_ERROR g_key_file_error_quark()

Error domain for key file parsing. Errors in this domain will be from the GKeyFileError enumeration. See GError for information on error domains.


enum GKeyFileError

typedef enum
{
  G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
  G_KEY_FILE_ERROR_PARSE,
  G_KEY_FILE_ERROR_NOT_FOUND,
  G_KEY_FILE_ERROR_KEY_NOT_FOUND,
  G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
  G_KEY_FILE_ERROR_INVALID_VALUE
} GKeyFileError;

Error codes returned by key file parsing.

G_KEY_FILE_ERROR_UNKNOWN_ENCODING

the text being parsed was in an unknown encoding

G_KEY_FILE_ERROR_PARSE

document was ill-formed

G_KEY_FILE_ERROR_NOT_FOUND

the file was not found

G_KEY_FILE_ERROR_KEY_NOT_FOUND

a requested key was not found

G_KEY_FILE_ERROR_GROUP_NOT_FOUND

a requested group was not found

G_KEY_FILE_ERROR_INVALID_VALUE

a value could not be parsed

enum GKeyFileFlags

typedef enum
{
  G_KEY_FILE_NONE              = 0,
  G_KEY_FILE_KEEP_COMMENTS     = 1 << 0,
  G_KEY_FILE_KEEP_TRANSLATIONS = 1 << 1
} GKeyFileFlags;

Flags which influence the parsing.

G_KEY_FILE_NONE

No flags, default behaviour

G_KEY_FILE_KEEP_COMMENTS

Use this flag if you plan to write the (possibly modified) contents of the key file back to a file; otherwise all comments will be lost when the key file is written back.

G_KEY_FILE_KEEP_TRANSLATIONS

Use this flag if you plan to write the (possibly modified) contents of the key file back to a file; otherwise only the translations for the current language will be written back.

g_key_file_new ()

GKeyFile *          g_key_file_new                      (void);

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.

Returns :

an empty GKeyFile.

Since 2.6


g_key_file_free ()

void                g_key_file_free                     (GKeyFile *key_file);

Frees a GKeyFile.

key_file :

a GKeyFile

Since 2.6


g_key_file_set_list_separator ()

void                g_key_file_set_list_separator       (GKeyFile *key_file,
                                                         gchar separator);

Sets the character which is used to separate values in lists. Typically ';' or ',' are used as separators. The default list separator is ';'.

key_file :

a GKeyFile

separator :

the separator

Since 2.6


g_key_file_load_from_file ()

gboolean            g_key_file_load_from_file           (GKeyFile *key_file,
                                                         const gchar *file,
                                                         GKeyFileFlags flags,
                                                         GError **error);

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.

key_file :

an empty GKeyFile struct

file :

the path of a filename to load, in the GLib filename encoding

flags :

flags from GKeyFileFlags

error :

return location for a GError, or NULL

Returns :

TRUE if a key file could be loaded, FALSE otherwise

Since 2.6


g_key_file_load_from_data ()

gboolean            g_key_file_load_from_data           (GKeyFile *key_file,
                                                         const gchar *data,
                                                         gsize length,
                                                         GKeyFileFlags flags,
                                                         GError **error);

Loads a key file from memory into an empty GKeyFile structure. If the object cannot be created then error is set to a GKeyFileError.

key_file :

an empty GKeyFile struct

data :

key file loaded in memory

length :

the length of data in bytes

flags :

flags from GKeyFileFlags

error :

return location for a GError, or NULL

Returns :

TRUE if a key file could be loaded, FALSE otherwise

Since 2.6


g_key_file_load_from_data_dirs ()

gboolean            g_key_file_load_from_data_dirs      (GKeyFile *key_file,
                                                         const gchar *file,
                                                         gchar **full_path,
                                                         GKeyFileFlags flags,
                                                         GError **error);

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.

key_file :

an empty GKeyFile struct

file :

a relative path to a filename to open and parse

full_path :

return location for a string containing the full path of the file, or NULL

flags :

flags from GKeyFileFlags

error :

return location for a GError, or NULL

Returns :

TRUE if a key file could be loaded, FALSE othewise

Since 2.6


g_key_file_load_from_dirs ()

gboolean            g_key_file_load_from_dirs           (GKeyFile *key_file,
                                                         const gchar *file,
                                                         const gchar **search_dirs,
                                                         gchar **full_path,
                                                         GKeyFileFlags flags,
                                                         GError **error);

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.

key_file :

an empty GKeyFile struct

file :

a relative path to a filename to open and parse

search_dirs :

NULL-terminated array of directories to search

full_path :

return location for a string containing the full path of the file, or NULL

flags :

flags from GKeyFileFlags

error :

return location for a GError, or NULL

Returns :

TRUE if a key file could be loaded, FALSE otherwise

Since 2.14


g_key_file_to_data ()

gchar *             g_key_file_to_data                  (GKeyFile *key_file,
                                                         gsize *length,
                                                         GError **error);

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.

key_file :

a GKeyFile

length :

return location for the length of the returned string, or NULL

error :

return location for a GError, or NULL

Returns :

a newly allocated string holding the contents of the GKeyFile

Since 2.6


g_key_file_get_start_group ()

gchar *             g_key_file_get_start_group          (GKeyFile *key_file);

Returns the name of the start group of the file.

key_file :

a GKeyFile

Returns :

The start group of the key file.

Since 2.6


g_key_file_get_groups ()

gchar **            g_key_file_get_groups               (GKeyFile *key_file,
                                                         gsize *length);

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.

key_file :

a GKeyFile

length :

return location for the number of returned groups, or NULL

Returns :

a newly-allocated NULL-terminated array of strings. Use g_strfreev() to free it.

Since 2.6


g_key_file_get_keys ()

gchar **            g_key_file_get_keys                 (GKeyFile *key_file,
                                                         const gchar *group_name,
                                                         gsize *length,
                                                         GError **error);

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.

key_file :

a GKeyFile

group_name :

a group name

length :

return location for the number of keys returned, or NULL

error :

return location for a GError, or NULL

Returns :

a newly-allocated NULL-terminated array of strings. Use g_strfreev() to free it.

Since 2.6


g_key_file_has_group ()

gboolean            g_key_file_has_group                (GKeyFile *key_file,
                                                         const gchar *group_name);

Looks whether the key file has the group group_name.

key_file :

a GKeyFile

group_name :

a group name

Returns :

TRUE if group_name is a part of key_file, FALSE otherwise.

Since 2.6


g_key_file_has_key ()

gboolean            g_key_file_has_key                  (GKeyFile *key_file,
                                                         const gchar *group_name,
                                                         const gchar *key,
                                                         GError **error);

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

key_file :

a GKeyFile

group_name :

a group name

key :

a key name

error :

return location for a GError

Returns :

TRUE if key is a part of group_name, FALSE otherwise.