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
This commit is contained in:
Philip Withnall 2017-10-19 15:48:45 +01:00
parent 61ea1e7ca4
commit a71251dc40

View File

@ -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);
* ]|
*/
/**