mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-08-19 15:18:55 +02:00
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:
@@ -1925,6 +1925,7 @@ g_key_file_unref
|
|||||||
g_key_file_set_list_separator
|
g_key_file_set_list_separator
|
||||||
g_key_file_load_from_file
|
g_key_file_load_from_file
|
||||||
g_key_file_load_from_data
|
g_key_file_load_from_data
|
||||||
|
g_key_file_load_from_bytes
|
||||||
g_key_file_load_from_data_dirs
|
g_key_file_load_from_data_dirs
|
||||||
g_key_file_load_from_dirs
|
g_key_file_load_from_dirs
|
||||||
g_key_file_to_data
|
g_key_file_to_data
|
||||||
|
@@ -926,6 +926,36 @@ g_key_file_load_from_data (GKeyFile *key_file,
|
|||||||
return TRUE;
|
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:
|
* g_key_file_load_from_dirs:
|
||||||
* @key_file: an empty #GKeyFile struct
|
* @key_file: an empty #GKeyFile struct
|
||||||
|
@@ -26,6 +26,7 @@
|
|||||||
#error "Only <glib.h> can be included directly."
|
#error "Only <glib.h> can be included directly."
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <glib/gbytes.h>
|
||||||
#include <glib/gerror.h>
|
#include <glib/gerror.h>
|
||||||
|
|
||||||
G_BEGIN_DECLS
|
G_BEGIN_DECLS
|
||||||
@@ -76,6 +77,11 @@ gboolean g_key_file_load_from_data (GKeyFile *key_file,
|
|||||||
gsize length,
|
gsize length,
|
||||||
GKeyFileFlags flags,
|
GKeyFileFlags flags,
|
||||||
GError **error);
|
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
|
GLIB_AVAILABLE_IN_ALL
|
||||||
gboolean g_key_file_load_from_dirs (GKeyFile *key_file,
|
gboolean g_key_file_load_from_dirs (GKeyFile *key_file,
|
||||||
const gchar *file,
|
const gchar *file,
|
||||||
|
@@ -1645,6 +1645,42 @@ test_roundtrip (void)
|
|||||||
g_key_file_free (kf);
|
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
|
int
|
||||||
main (int argc, char *argv[])
|
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/limbo", test_limbo);
|
||||||
g_test_add_func ("/keyfile/utf8", test_utf8);
|
g_test_add_func ("/keyfile/utf8", test_utf8);
|
||||||
g_test_add_func ("/keyfile/roundtrip", test_roundtrip);
|
g_test_add_func ("/keyfile/roundtrip", test_roundtrip);
|
||||||
|
g_test_add_func ("/keyfile/bytes", test_bytes);
|
||||||
|
|
||||||
return g_test_run ();
|
return g_test_run ();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user