[glib/resources] Use GBytes as the return value for g_resource_lookup_data



commit 10be672b423b5e0e9ad519c8c606ba819c3914bb
Author: Alexander Larsson <alexl redhat com>
Date:   Thu Dec 22 13:13:51 2011 +0100

    Use GBytes as the return value for g_resource_lookup_data
    
    This means we can do proper ownership tracking. I.e. keep
    the GResource alive until the GByte is finalized.

 gio/gresource.c       |   17 ++++++++---------
 gio/gresource.h       |    6 ++----
 gio/tests/resources.c |   44 +++++++++++++++++++++++++-------------------
 3 files changed, 35 insertions(+), 32 deletions(-)
---
diff --git a/gio/gresource.c b/gio/gresource.c
index b5f5474..5736e67 100644
--- a/gio/gresource.c
+++ b/gio/gresource.c
@@ -234,17 +234,17 @@ g_resource_open_stream (GResource *resource,
   return stream;
 }
 
-const void *
+GBytes *
 g_resource_lookup_data (GResource *resource,
 			char *path,
-			gsize *size,
 			GError **error)
 {
   const void *data;
   guint32 flags;
   gsize data_size;
+  gsize size;
 
-  if (!do_lookup (resource, path, size, &flags, &data, &data_size, TRUE, error))
+  if (!do_lookup (resource, path, &size, &flags, &data, &data_size, TRUE, error))
     return NULL;
 
   if (flags & G_RESOURCE_FLAGS_COMPRESSED)
@@ -255,7 +255,7 @@ g_resource_lookup_data (GResource *resource,
       return NULL;
     }
 
-  return data;
+  return g_bytes_new_with_free_func (data, data_size, (GDestroyNotify)g_resource_unref, g_resource_ref (resource));
 }
 
 gboolean
@@ -377,14 +377,13 @@ g_resources_open_stream (char *path,
   return res;
 }
 
-const void *
+GBytes *
 g_resources_lookup_data (char *path,
-			 gsize *size,
 			 GError **error)
 {
-  const void *res = NULL;
+  GBytes *res = NULL;
   GList *l;
-  const void *data;
+  GBytes *data;
 
   g_rw_lock_reader_lock (&resources_lock);
 
@@ -393,7 +392,7 @@ g_resources_lookup_data (char *path,
       GResource *r = l->data;
       GError *my_error = NULL;
 
-      data = g_resource_lookup_data (r, path, size, &my_error);
+      data = g_resource_lookup_data (r, path, &my_error);
       if (data == NULL &&
 	  g_error_matches (my_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND))
 	{
diff --git a/gio/gresource.h b/gio/gresource.h
index 03b638e..9317152 100644
--- a/gio/gresource.h
+++ b/gio/gresource.h
@@ -43,9 +43,8 @@ GResource *   g_resource_load                (const gchar  *filename,
 GInputStream *g_resource_open_stream         (GResource    *resource,
 					      char         *path,
 					      GError      **error);
-const void *  g_resource_lookup_data         (GResource    *resource,
+GBytes *      g_resource_lookup_data         (GResource    *resource,
 					      char         *path,
-					      gsize        *size,
 					      GError      **error);
 char **       g_resource_enumerate_children  (GResource    *resource,
 					      char         *path,
@@ -61,8 +60,7 @@ void          g_resources_register           (GResource    *resource);
 void          g_resources_unregister         (GResource    *resource);
 GInputStream *g_resources_open_stream        (char         *path,
 					      GError      **error);
-const void *  g_resources_lookup_data        (char         *path,
-					      gsize        *size,
+GBytes *      g_resources_lookup_data        (char         *path,
 					      GError      **error);
 char **       g_resources_enumerate_children (char         *path,
 					      GError      **error);
diff --git a/gio/tests/resources.c b/gio/tests/resources.c
index 5eae960..8f3019a 100644
--- a/gio/tests/resources.c
+++ b/gio/tests/resources.c
@@ -27,7 +27,7 @@ test_resource (GResource *resource)
   gboolean found, success;
   gsize size;
   guint32 flags;
-  const void *data;
+  GBytes *data;
   char **children;
   GInputStream *in;
   char buffer[128];
@@ -74,7 +74,7 @@ test_resource (GResource *resource)
   /* This will fail due to compression */
   data = g_resource_lookup_data (resource,
 				 "/test1.txt",
-				 &size, &error);
+				 &error);
   g_assert (data == NULL);
   g_assert (error != NULL);
   g_clear_error (&error);
@@ -97,27 +97,29 @@ test_resource (GResource *resource)
 
   data = g_resource_lookup_data (resource,
 				 "/a_prefix/test2.txt",
-				 &size, &error);
+				 &error);
   g_assert (data != NULL);
   g_assert (error == NULL);
+  size = g_bytes_get_size (data);
   g_assert (size == 6);
-  g_assert_cmpstr (data, ==, "test2\n");
+  g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
 
   data = g_resource_lookup_data (resource,
 				 "/a_prefix/test2-alias.txt",
-				 &size, &error);
+				 &error);
   g_assert (data != NULL);
   g_assert (error == NULL);
+  size = g_bytes_get_size (data);
   g_assert (size == 6);
-  g_assert_cmpstr (data, ==, "test2\n");
+  g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
 
   data = g_resource_lookup_data (resource,
 				 "/sv/test1.txt",
-				 &size, &error);
+				 &error);
   g_assert (data != NULL);
   g_assert (error == NULL);
   g_assert (size == 6);
-  g_assert_cmpstr (data, ==, "test3\n");
+  g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test3\n");
 
   children = g_resource_enumerate_children  (resource,
 					     "/not/here",
@@ -185,7 +187,7 @@ test_resource_registred (void)
   gboolean found, success;
   gsize size;
   guint32 flags;
-  const void *data;
+  GBytes *data;
   char **children;
   GInputStream *in;
   char buffer[128];
@@ -232,7 +234,7 @@ test_resource_registred (void)
 
   /* This will fail due to compression */
   data = g_resources_lookup_data ("/test1.txt",
-				  &size, &error);
+				  &error);
   g_assert (data == NULL);
   g_assert (error != NULL);
   g_clear_error (&error);
@@ -254,25 +256,28 @@ test_resource_registred (void)
 
 
   data = g_resources_lookup_data ("/a_prefix/test2.txt",
-				  &size, &error);
+				  &error);
   g_assert (data != NULL);
   g_assert (error == NULL);
+  size = g_bytes_get_size (data);
   g_assert (size == 6);
-  g_assert_cmpstr (data, ==, "test2\n");
+  g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
 
   data = g_resources_lookup_data ("/a_prefix/test2-alias.txt",
-				  &size, &error);
+				  &error);
   g_assert (data != NULL);
   g_assert (error == NULL);
+  size = g_bytes_get_size (data);
   g_assert (size == 6);
-  g_assert_cmpstr (data, ==, "test2\n");
+  g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
 
   data = g_resources_lookup_data ("/sv/test1.txt",
-				  &size, &error);
+				  &error);
   g_assert (data != NULL);
   g_assert (error == NULL);
+  size = g_bytes_get_size (data);
   g_assert (size == 6);
-  g_assert_cmpstr (data, ==, "test3\n");
+  g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test3\n");
 
   children = g_resources_enumerate_children ("/not/here",
 					     &error);
@@ -302,7 +307,7 @@ test_resource_automatic (void)
   gboolean found;
   gsize size;
   guint32 flags;
-  const void *data;
+  GBytes *data;
 
   found = g_resources_get_info ("/auto_loaded/test1.txt",
 				&size, &flags, &error);
@@ -312,11 +317,12 @@ test_resource_automatic (void)
   g_assert (flags == 0);
 
   data = g_resources_lookup_data ("/auto_loaded/test1.txt",
-				  &size, &error);
+				  &error);
   g_assert (data != NULL);
   g_assert (error == NULL);
+  size = g_bytes_get_size (data);
   g_assert (size == 6);
-  g_assert_cmpstr (data, ==, "test1\n");
+  g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test1\n");
 
 }
 



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