diff options
author | Philip Withnall <withnall@endlessm.com> | 2017-10-19 15:48:45 +0100 |
---|---|---|
committer | Philip Withnall <withnall@endlessm.com> | 2017-10-26 12:58:59 +0100 |
commit | a71251dc402044bd80ef9ce9e47bbec7a0572386 (patch) | |
tree | 5b85809c42107f1bfa46442697c00e36eebc9e59 /glib/gkeyfile.c | |
parent | 61ea1e7ca48fae358c7212b3dfdb41ce37bd0f33 (diff) | |
download | glib-a71251dc402044bd80ef9ce9e47bbec7a0572386.tar.gz |
gkeyfile: Add some examples to the documentation
Add some examples of loading and saving key files.
Signed-off-by: Philip Withnall <withnall@endlessm.com>
https://bugzilla.gnome.org/show_bug.cgi?id=330458
Diffstat (limited to 'glib/gkeyfile.c')
-rw-r--r-- | glib/gkeyfile.c | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/glib/gkeyfile.c b/glib/gkeyfile.c index 8a56c7617..65265760e 100644 --- a/glib/gkeyfile.c +++ b/glib/gkeyfile.c @@ -152,6 +152,58 @@ * 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: + * |[<!-- language="C" --> + * 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: + * |[<!-- language="C" --> + * 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); + * ]| */ /** |