[glib] gkeyfile: Add some examples to the documentation
- From: Philip Withnall <pwithnall src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib] gkeyfile: Add some examples to the documentation
- Date: Thu, 26 Oct 2017 11:59:20 +0000 (UTC)
commit a71251dc402044bd80ef9ce9e47bbec7a0572386
Author: Philip Withnall <withnall endlessm com>
Date: Thu Oct 19 15:48:45 2017 +0100
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
glib/gkeyfile.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 52 insertions(+), 0 deletions(-)
---
diff --git a/glib/gkeyfile.c b/glib/gkeyfile.c
index 8a56c76..6526576 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);
+ * ]|
*/
/**
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]