gkeyfile: add g_key_file_load_from_bytes() API

This makes it easier to use GKeyFile from language bindings, and makes
the API more consistent and modern with the new data type available in
GLib.

https://bugzilla.gnome.org/show_bug.cgi?id=767880
This commit is contained in:
Cosimo Cecchi 2016-06-20 10:01:01 -07:00
parent 7563ab4734
commit d07e166432
4 changed files with 74 additions and 0 deletions

View File

@ -1925,6 +1925,7 @@ g_key_file_unref
g_key_file_set_list_separator
g_key_file_load_from_file
g_key_file_load_from_data
g_key_file_load_from_bytes
g_key_file_load_from_data_dirs
g_key_file_load_from_dirs
g_key_file_to_data

View File

@ -926,6 +926,36 @@ g_key_file_load_from_data (GKeyFile *key_file,
return TRUE;
}
/**
* g_key_file_load_from_bytes:
* @key_file: an empty #GKeyFile struct
* @bytes: a #GBytes
* @flags: flags from #GKeyFileFlags
* @error: return location for a #GError, or %NULL
*
* Loads a key file from the data in @bytes into an empty #GKeyFile structure.
* If the object cannot be created then %error is set to a #GKeyFileError.
*
* Returns: %TRUE if a key file could be loaded, %FALSE otherwise
*
* Since: 2.50
**/
gboolean
g_key_file_load_from_bytes (GKeyFile *key_file,
GBytes *bytes,
GKeyFileFlags flags,
GError **error)
{
const guchar *data;
gsize size;
g_return_val_if_fail (key_file != NULL, FALSE);
g_return_val_if_fail (bytes != NULL, FALSE);
data = g_bytes_get_data (bytes, &size);
return g_key_file_load_from_data (key_file, (const gchar *) data, size, flags, error);
}
/**
* g_key_file_load_from_dirs:
* @key_file: an empty #GKeyFile struct

View File

@ -26,6 +26,7 @@
#error "Only <glib.h> can be included directly."
#endif
#include <glib/gbytes.h>
#include <glib/gerror.h>
G_BEGIN_DECLS
@ -76,6 +77,11 @@ gboolean g_key_file_load_from_data (GKeyFile *key_file,
gsize length,
GKeyFileFlags flags,
GError **error);
GLIB_AVAILABLE_IN_2_50
gboolean g_key_file_load_from_bytes (GKeyFile *key_file,
GBytes *bytes,
GKeyFileFlags flags,
GError **error);
GLIB_AVAILABLE_IN_ALL
gboolean g_key_file_load_from_dirs (GKeyFile *key_file,
const gchar *file,

View File

@ -1645,6 +1645,42 @@ test_roundtrip (void)
g_key_file_free (kf);
}
static void
test_bytes (void)
{
const gchar data[] =
"[Group1]\n"
"key1=value1\n"
"\n"
"[Group2]\n"
"key2=value2\n";
GKeyFile *kf = g_key_file_new ();
GBytes *bytes = g_bytes_new (data, strlen (data));
GError *error = NULL;
gchar **names;
gsize len;
g_key_file_load_from_bytes (kf, bytes, 0, &error);
g_assert_no_error (error);
names = g_key_file_get_groups (kf, &len);
g_assert_nonnull (names);
check_length ("groups", g_strv_length (names), len, 2);
check_name ("group name", names[0], "Group1", 0);
check_name ("group name", names[1], "Group2", 1);
check_string_value (kf, "Group1", "key1", "value1");
check_string_value (kf, "Group2", "key2", "value2");
g_strfreev (names);
g_bytes_unref (bytes);
g_key_file_free (kf);
}
int
main (int argc, char *argv[])
{
@ -1688,6 +1724,7 @@ main (int argc, char *argv[])
g_test_add_func ("/keyfile/limbo", test_limbo);
g_test_add_func ("/keyfile/utf8", test_utf8);
g_test_add_func ("/keyfile/roundtrip", test_roundtrip);
g_test_add_func ("/keyfile/bytes", test_bytes);
return g_test_run ();
}