[glib/resources: 3/6] Initial version of GResource



commit e376c3b8794a1d5919793baa4a37571f50705031
Author: Alexander Larsson <alexl redhat com>
Date:   Wed Dec 21 21:38:23 2011 +0100

    Initial version of GResource
    
    GResource is a bundle of files combined into a single binary blog.
    The API lets you access the files the resource contains by
    using resource paths. You can also register resources with a
    global list and access these globally in a merged resource namespace.
    
    The normal way this is used is to link in the resources into your
    application/library and have it be automatically registred.

 gio/Makefile.am |    2 +
 gio/gio.h       |    1 +
 gio/gioenums.h  |   11 ++
 gio/giotypes.h  |    1 +
 gio/gresource.c |  495 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 gio/gresource.h |   78 +++++++++
 6 files changed, 588 insertions(+), 0 deletions(-)
---
diff --git a/gio/Makefile.am b/gio/Makefile.am
index ef5869e..7189724 100644
--- a/gio/Makefile.am
+++ b/gio/Makefile.am
@@ -377,6 +377,7 @@ libgio_2_0_la_SOURCES =		\
 	gpollfilemonitor.h 	\
 	gproxyresolver.c	\
 	gresolver.c		\
+	gresource.c		\
 	gseekable.c 		\
 	gsimpleasyncresult.c 	\
 	gsimplepermission.c 	\
@@ -545,6 +546,7 @@ gio_headers =			\
 	gproxyaddressenumerator.h \
 	gproxyresolver.h	\
 	gresolver.h		\
+	gresource.h		\
 	gseekable.h 		\
 	gsimpleasyncresult.h 	\
 	gsimplepermission.h 	\
diff --git a/gio/gio.h b/gio/gio.h
index 3a9f90a..6e26e50 100644
--- a/gio/gio.h
+++ b/gio/gio.h
@@ -103,6 +103,7 @@
 #include <gio/gproxyaddressenumerator.h>
 #include <gio/gproxyresolver.h>
 #include <gio/gresolver.h>
+#include <gio/gresource.h>
 #include <gio/gseekable.h>
 #include <gio/gsettingsschema.h>
 #include <gio/gsettings.h>
diff --git a/gio/gioenums.h b/gio/gioenums.h
index 47d72bd..cf58091 100644
--- a/gio/gioenums.h
+++ b/gio/gioenums.h
@@ -630,6 +630,17 @@ typedef enum {
   G_RESOLVER_ERROR_INTERNAL
 } GResolverError;
 
+typedef enum {
+  G_RESOURCE_ERROR_NOT_FOUND,
+  G_RESOURCE_ERROR_NOT_MAPPABLE
+} GResourceError;
+
+typedef enum {
+  G_RESOURCE_FLAGS_NONE       = 0,
+  G_RESOURCE_FLAGS_COMPRESSED = (1<<0),
+  G_RESOURCE_FLAGS_LOCALIZED  = (1<<1)
+} GResourceFlags;
+
 /**
  * GSocketFamily:
  * @G_SOCKET_FAMILY_INVALID: no address family
diff --git a/gio/giotypes.h b/gio/giotypes.h
index b30e77b..8537748 100644
--- a/gio/giotypes.h
+++ b/gio/giotypes.h
@@ -135,6 +135,7 @@ typedef struct _GIOStream                     GIOStream;
 typedef struct _GPollableInputStream          GPollableInputStream; /* Dummy typedef */
 typedef struct _GPollableOutputStream         GPollableOutputStream; /* Dummy typedef */
 typedef struct _GResolver                     GResolver;
+typedef struct _GResource                     GResource;
 typedef struct _GSeekable                     GSeekable;
 typedef struct _GSimpleAsyncResult            GSimpleAsyncResult;
 
diff --git a/gio/gresource.c b/gio/gresource.c
new file mode 100644
index 0000000..bcd2b99
--- /dev/null
+++ b/gio/gresource.c
@@ -0,0 +1,495 @@
+/* GIO - GLib Input, Output and Streaming Library
+ *
+ * Copyright  2011 Red Hat, Inc
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Authors: Alexander Larsson <alexl redhat com>
+ */
+
+#include "config.h"
+
+#include <string.h>
+
+#include "gresource.h"
+#include <gvdb/gvdb-reader.h>
+#include <gi18n.h>
+#include <gio/gmemoryinputstream.h>
+#include <gio/gzlibdecompressor.h>
+#include <gio/gconverterinputstream.h>
+
+struct _GResource
+{
+  int ref_count;
+
+  GvdbTable *table;
+  void *data;
+  GDestroyNotify free_data;
+};
+
+/**
+ * SECTION:gresource
+ * @short_description: Resource framework
+ * @include: gio/gio.h
+ *
+ * Since: 2.32
+ */
+
+/**
+ * g_resource_error_quark:
+ *
+ * Gets the #GResource Error Quark.
+ *
+ * Return value: a #GQuark.
+ *
+ * Since: 2.32
+ */
+GQuark
+g_resource_error_quark (void)
+{
+  return g_quark_from_static_string ("g-resource-error-quark");
+}
+
+GResource *
+g_resource_ref (GResource *resource)
+{
+  resource->ref_count++;
+  return resource;
+}
+
+void
+g_resource_unref (GResource *resource)
+{
+  if (resource->ref_count-- == 0)
+    {
+      gvdb_table_unref (resource->table);
+      if (resource->free_data)
+	resource->free_data (resource->data);
+      g_free (resource);
+    }
+}
+
+GResource *
+g_resource_new_from_data (void *data, gsize length,
+			  GDestroyNotify free_data, GError **error)
+{
+  GResource *resource;
+  GvdbTable *table;
+
+  table = gvdb_table_new_from_data (data,
+				    length,
+				    TRUE,
+				    NULL,
+				    NULL,
+				    NULL,
+				    error);
+
+  if (table == NULL)
+    return NULL;
+
+  resource = g_new0 (GResource, 1);
+  resource->table = table;
+  resource->data = data;
+  resource->free_data = free_data;
+
+  return resource;
+}
+
+GResource *
+g_resource_load (const gchar *filename,
+		 GError **error)
+{
+  GResource *resource;
+  GvdbTable *table;
+
+  table = gvdb_table_new (filename, FALSE, error);
+  if (table == NULL)
+    return NULL;
+
+  resource = g_new0 (GResource, 1);
+  resource->table = table;
+
+  return resource;
+}
+
+static gboolean do_lookup (GResource *resource,
+			   char         *path,
+			   gsize        *size,
+			   guint32      *flags,
+			   const void  **data,
+			   gsize        *data_size,
+			   GError      **error)
+{
+  gboolean free_path = FALSE;
+  gsize path_len;
+  gboolean res = FALSE;
+  GVariant *value;
+
+  path_len = strlen (path);
+  if (path[path_len-1] == '/')
+    {
+      free_path = TRUE;
+      path = g_strdup (path);
+      path[path_len-1] = 0;
+    }
+
+  value = gvdb_table_get_value (resource->table, path);
+
+  if (value == NULL)
+    {
+      g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
+		   _("The resource at '%s' does not exist"),
+		   path);
+    }
+  else
+    {
+      guint32 _size, _flags;
+      GVariant *array;
+
+      g_variant_get (value, "(uu ay)",
+		     &_size,
+		     &_flags,
+		     &array);
+      if (size)
+	*size = _size;
+      if (flags)
+	*flags = _flags;
+      if (data)
+	*data = g_variant_get_data (array);
+      if (data_size)
+	*data_size = g_variant_get_size (array);
+      res = TRUE;
+    }
+
+  if (free_path)
+    g_free (path);
+  return res;
+}
+
+GInputStream *
+g_resource_open_stream (GResource *resource,
+			char *path,
+			GError **error)
+{
+  const void *data;
+  gsize data_size;
+  guint32 flags;
+  GInputStream *stream, *stream2;
+
+  if (!do_lookup (resource, path, NULL, &flags, &data, &data_size, error))
+    return NULL;
+
+  stream = g_memory_input_stream_new_from_data (data, data_size, NULL);
+  g_object_set_data_full (G_OBJECT (stream), "g-resource",
+			  g_resource_ref (resource),
+			  (GDestroyNotify)g_resource_unref);
+
+  if (flags & G_RESOURCE_FLAGS_COMPRESSED)
+    {
+      GZlibDecompressor *decompressor =
+	g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_ZLIB);
+
+      stream2 = g_converter_input_stream_new (stream, G_CONVERTER (decompressor));
+      g_object_unref (decompressor);
+      g_object_unref (stream);
+      stream = stream2;
+    }
+
+  return stream;
+}
+
+const void *
+g_resource_lookup_data (GResource *resource,
+			char *path,
+			gsize *size,
+			GError **error)
+{
+  const void *data;
+  guint32 flags;
+  gsize data_size;
+
+  if (!do_lookup (resource, path, size, &flags, &data, &data_size, error))
+    return NULL;
+
+  if (flags & G_RESOURCE_FLAGS_COMPRESSED)
+    {
+      g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_MAPPABLE,
+		   _("The resource at '%s' is not directly mappable due to being compressed"),
+		   path);
+      return NULL;
+    }
+
+  return data;
+}
+
+gboolean
+g_resource_get_info (GResource    *resource,
+		     char         *path,
+		     gsize        *size,
+		     guint32      *flags,
+		     GError      **error)
+{
+  return do_lookup (resource, path, size, flags, NULL, NULL, error);
+}
+
+char **
+g_resource_enumerate_children (GResource *resource,
+			       char *path,
+			       GError **error)
+{
+  gchar **children;
+  gsize path_len;
+  char *path_with_slash;
+
+  if (*path == 0)
+    {
+      g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
+		   _("The resource at '%s' does not exist"),
+		   path);
+      return NULL;
+    }
+
+  path_len = strlen (path);
+  if (path[path_len-1] != '/')
+    path_with_slash = g_strconcat (path, "/", NULL);
+  else
+    path_with_slash = g_strdup (path);
+
+  children = gvdb_table_list (resource->table, path_with_slash);
+
+  if (children == NULL)
+    {
+      g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
+		   _("The resource at '%s' does not exist"),
+		   path);
+      return NULL;
+    }
+
+  return children;
+}
+
+static GRWLock resources_lock;
+static GList *registred_resources;
+
+void
+g_resources_register (GResource *resource)
+{
+  g_rw_lock_writer_lock (&resources_lock);
+
+  registred_resources = g_list_prepend (registred_resources,
+					g_resource_ref (resource));
+
+  g_rw_lock_writer_unlock (&resources_lock);
+}
+
+void
+g_resources_unregister (GResource *resource)
+{
+  g_rw_lock_writer_lock (&resources_lock);
+
+  if (g_list_find (registred_resources, resource) == NULL)
+    {
+      g_warning ("Tried to remove not registred resource");
+    }
+  else
+    {
+      registred_resources = g_list_remove (registred_resources,
+					   resource);
+      g_resource_unref (resource);
+    }
+
+  g_rw_lock_writer_unlock (&resources_lock);
+}
+
+GInputStream *
+g_resources_open_stream (char *path,
+			 GError **error)
+{
+  GInputStream *res = NULL;
+  GList *l;
+  GInputStream *stream;
+
+  g_rw_lock_reader_lock (&resources_lock);
+
+  for (l = registred_resources; l != NULL; l = l->next)
+    {
+      GResource *r = l->data;
+      GError *my_error = NULL;
+
+      stream = g_resource_open_stream (r, path, &my_error);
+      if (stream == NULL &&
+	  g_error_matches (my_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND))
+	{
+	  g_clear_error (&my_error);
+	}
+      else
+	{
+	  if (stream == NULL)
+	    g_propagate_error (error, my_error);
+	  res = stream;
+	  break;
+	}
+    }
+
+  if (l == NULL)
+    g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
+		 _("The resource at '%s' does not exist"),
+		 path);
+
+  g_rw_lock_reader_unlock (&resources_lock);
+
+  return res;
+}
+
+const void *
+g_resources_lookup_data (char *path,
+			 gsize *size,
+			 GError **error)
+{
+  const void *res = NULL;
+  GList *l;
+  const void *data;
+
+  g_rw_lock_reader_lock (&resources_lock);
+
+  for (l = registred_resources; l != NULL; l = l->next)
+    {
+      GResource *r = l->data;
+      GError *my_error = NULL;
+
+      data = g_resource_lookup_data (r, path, size, &my_error);
+      if (data == NULL &&
+	  g_error_matches (my_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND))
+	{
+	  g_clear_error (&my_error);
+	}
+      else
+	{
+	  if (data == NULL)
+	    g_propagate_error (error, my_error);
+	  res = data;
+	  break;
+	}
+    }
+
+  if (l == NULL)
+    g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
+		 _("The resource at '%s' does not exist"),
+		 path);
+
+  g_rw_lock_reader_unlock (&resources_lock);
+
+  return res;
+}
+
+char **
+g_resources_enumerate_children (char *path,
+				GError **error)
+{
+  GHashTable *hash = NULL;
+  GList *l;
+  char **children;
+  int i;
+
+  g_rw_lock_reader_lock (&resources_lock);
+
+  for (l = registred_resources; l != NULL; l = l->next)
+    {
+      GResource *r = l->data;
+
+      children = g_resource_enumerate_children (r, path, NULL);
+
+      if (children != NULL)
+	{
+	  if (hash == NULL)
+	    hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
+
+	  for (i = 0; children[i] != NULL; i++)
+	    g_hash_table_insert (hash, children[i], children[i]);
+	  g_free (children);
+	}
+    }
+
+  g_rw_lock_reader_unlock (&resources_lock);
+
+  if (hash == NULL)
+    {
+      g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
+		   _("The resource at '%s' does not exist"),
+		   path);
+      return NULL;
+    }
+  else
+    {
+      GHashTableIter iter;
+      const char *key;
+      guint n_children;
+      n_children = g_hash_table_size (hash);
+      children = g_new (char *, n_children + 1);
+      i = 0;
+
+      g_hash_table_iter_init (&iter, hash);
+      while (g_hash_table_iter_next (&iter, (gpointer *)&key, NULL))
+	children[i++] = g_strdup (key);
+      children[i++] = NULL;
+
+      g_hash_table_destroy (hash);
+
+      return children;
+    }
+}
+
+gboolean
+g_resources_get_info (char         *path,
+		      gsize        *size,
+		      guint32      *flags,
+		      GError      **error)
+{
+  gboolean res = FALSE;
+  GList *l;
+  gboolean r_res;
+
+  g_rw_lock_reader_lock (&resources_lock);
+
+  for (l = registred_resources; l != NULL; l = l->next)
+    {
+      GResource *r = l->data;
+      GError *my_error = NULL;
+
+      r_res = g_resource_get_info (r, path, size, flags, &my_error);
+      if (!r_res &&
+	  g_error_matches (my_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND))
+	{
+	  g_clear_error (&my_error);
+	}
+      else
+	{
+	  if (!r_res)
+	    g_propagate_error (error, my_error);
+	  res = r_res;
+	  break;
+	}
+    }
+
+  if (l == NULL)
+    g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND,
+		 _("The resource at '%s' does not exist"),
+		 path);
+
+  g_rw_lock_reader_unlock (&resources_lock);
+
+  return res;
+}
diff --git a/gio/gresource.h b/gio/gresource.h
new file mode 100644
index 0000000..9515d3b
--- /dev/null
+++ b/gio/gresource.h
@@ -0,0 +1,78 @@
+/* GIO - GLib Input, Output and Streaming Library
+ *
+ * Copyright (C) 2006-2007 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Author: Alexander Larsson <alexl redhat com>
+ */
+
+#if !defined (__GIO_GIO_H_INSIDE__) && !defined (GIO_COMPILATION)
+#error "Only <gio/gio.h> can be included directly."
+#endif
+
+#ifndef __G_RESOURCE_H__
+#define __G_RESOURCE_H__
+
+#include <gio/giotypes.h>
+
+G_BEGIN_DECLS
+
+#define G_RESOURCE_ERROR (g_resource_error_quark ())
+GQuark g_resource_error_quark (void);
+
+GResource *   g_resource_new_from_data       (void         *data,
+					      gsize         length,
+					      GDestroyNotify free,
+					      GError      **error);
+GResource *   g_resource_ref                 (GResource    *resource);
+void          g_resource_unref               (GResource    *resource);
+GResource *   g_resource_load                (const gchar  *filename,
+					      GError      **error);
+GInputStream *g_resource_open_stream         (GResource    *resource,
+					      char         *path,
+					      GError      **error);
+const void *  g_resource_lookup_data         (GResource    *resource,
+					      char         *path,
+					      gsize        *size,
+					      GError      **error);
+char **       g_resource_enumerate_children  (GResource    *resource,
+					      char         *path,
+					      GError      **error);
+gboolean      g_resource_get_info            (GResource    *resource,
+					      char         *path,
+					      gsize        *size,
+					      guint32      *flags,
+					      GError      **error);
+
+
+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,
+					      GError      **error);
+char **       g_resources_enumerate_children (char         *path,
+					      GError      **error);
+gboolean      g_resources_get_info           (char         *path,
+					      gsize        *size,
+					      guint32      *flags,
+					      GError      **error);
+
+G_END_DECLS
+
+#endif /* __G_RESOURCE_H__ */



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