[glib] gkeyfile: add g_key_file_load_from_bytes() API



commit d07e166432e0b85d64347ac30a1665d69c2cb288
Author: Cosimo Cecchi <cosimo endlessm com>
Date:   Mon Jun 20 10:01:01 2016 -0700

    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

 docs/reference/glib/glib-sections.txt |    1 +
 glib/gkeyfile.c                       |   30 ++++++++++++++++++++++++++
 glib/gkeyfile.h                       |    6 +++++
 glib/tests/keyfile.c                  |   37 +++++++++++++++++++++++++++++++++
 4 files changed, 74 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt
index 40024d1..dae86a6 100644
--- a/docs/reference/glib/glib-sections.txt
+++ b/docs/reference/glib/glib-sections.txt
@@ -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
diff --git a/glib/gkeyfile.c b/glib/gkeyfile.c
index 4b25fd7..460d483 100644
--- a/glib/gkeyfile.c
+++ b/glib/gkeyfile.c
@@ -927,6 +927,36 @@ g_key_file_load_from_data (GKeyFile       *key_file,
 }
 
 /**
+ * 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
  * @file: (type filename): a relative path to a filename to open and parse
diff --git a/glib/gkeyfile.h b/glib/gkeyfile.h
index 0c8d3e5..2e1efa4 100644
--- a/glib/gkeyfile.h
+++ b/glib/gkeyfile.h
@@ -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,
diff --git a/glib/tests/keyfile.c b/glib/tests/keyfile.c
index a5cc646..5e8ee90 100644
--- a/glib/tests/keyfile.c
+++ b/glib/tests/keyfile.c
@@ -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 ();
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]