[glib/wip/resources] Add a flag to avoid alternatives during resource lookup
- From: Alexander Larsson <alexl src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib/wip/resources] Add a flag to avoid alternatives during resource lookup
- Date: Tue, 10 Jan 2012 14:09:00 +0000 (UTC)
commit 7cce35772e39832c6896511d5dff8c0040a03fdf
Author: Alexander Larsson <alexl redhat com>
Date: Tue Jan 10 14:26:10 2012 +0100
Add a flag to avoid alternatives during resource lookup
This is useful when an app wants to not get local-specific
files, for instance when handling a resource bundle file.
gio/gioenums.h | 5 +++
gio/gresource.c | 60 ++++++++++++++++++++++++++---------------
gio/gresource.h | 72 +++++++++++++++++++++++++++---------------------
gio/gresourcefile.c | 10 +++---
gio/tests/resources.c | 29 +++++++++++++++++++
5 files changed, 117 insertions(+), 59 deletions(-)
---
diff --git a/gio/gioenums.h b/gio/gioenums.h
index 0983225..f2971b8 100644
--- a/gio/gioenums.h
+++ b/gio/gioenums.h
@@ -641,6 +641,11 @@ typedef enum {
G_RESOURCE_FLAGS_LOCALIZED = (1<<1)
} GResourceFlags;
+typedef enum {
+ G_RESOURCE_LOOKUP_FLAGS_NONE = 0,
+ G_RESOURCE_LOOKUP_FLAGS_NO_ALTERNATIVE = (1<<0),
+} GResourceLookupFlags;
+
/**
* GSocketFamily:
* @G_SOCKET_FAMILY_INVALID: no address family
diff --git a/gio/gresource.c b/gio/gresource.c
index 22bc335..56f4902 100644
--- a/gio/gresource.c
+++ b/gio/gresource.c
@@ -120,13 +120,13 @@ g_resource_load (const gchar *filename,
}
static gboolean do_lookup (GResource *resource,
- char *path,
- gsize *size,
- guint32 *flags,
- const void **data,
- gsize *data_size,
- gboolean follow_locales,
- GError **error)
+ char *path,
+ GResourceLookupFlags lookup_flags,
+ gsize *size,
+ guint32 *flags,
+ const void **data,
+ gsize *data_size,
+ GError **error)
{
gboolean free_path = FALSE;
gsize path_len;
@@ -159,7 +159,8 @@ static gboolean do_lookup (GResource *resource,
&_flags,
&array);
- if (follow_locales && _flags & G_RESOURCE_FLAGS_LOCALIZED)
+ if (!(lookup_flags & G_RESOURCE_LOOKUP_FLAGS_NO_ALTERNATIVE) &&
+ (_flags & G_RESOURCE_FLAGS_LOCALIZED))
{
const gchar * const *langs = g_get_language_names ();
int i;
@@ -167,9 +168,16 @@ static gboolean do_lookup (GResource *resource,
for (i = 0; langs[i] != NULL; i++)
{
char *prefixed_path = g_strconcat ("/", langs[i], path, NULL);
- res = do_lookup (resource, prefixed_path, size, flags, data, data_size, FALSE, NULL);
+ res = do_lookup (resource, prefixed_path,
+ lookup_flags | G_RESOURCE_LOOKUP_FLAGS_NO_ALTERNATIVE,
+ size, flags, data, data_size, NULL);
if (res)
- break;
+ {
+ /* Mark the target as localized too if we followed the "link" */
+ if (flags)
+ *flags |= G_RESOURCE_FLAGS_LOCALIZED;
+ break;
+ }
}
}
@@ -201,6 +209,7 @@ static gboolean do_lookup (GResource *resource,
GInputStream *
g_resource_open_stream (GResource *resource,
char *path,
+ GResourceLookupFlags lookup_flags,
GError **error)
{
const void *data;
@@ -208,7 +217,7 @@ g_resource_open_stream (GResource *resource,
guint32 flags;
GInputStream *stream, *stream2;
- if (!do_lookup (resource, path, NULL, &flags, &data, &data_size, TRUE, error))
+ if (!do_lookup (resource, path, lookup_flags, NULL, &flags, &data, &data_size, error))
return NULL;
stream = g_memory_input_stream_new_from_data (data, data_size, NULL);
@@ -233,6 +242,7 @@ g_resource_open_stream (GResource *resource,
GBytes *
g_resource_lookup_data (GResource *resource,
char *path,
+ GResourceLookupFlags lookup_flags,
GError **error)
{
const void *data;
@@ -240,7 +250,7 @@ g_resource_lookup_data (GResource *resource,
gsize data_size;
gsize size;
- if (!do_lookup (resource, path, &size, &flags, &data, &data_size, TRUE, error))
+ if (!do_lookup (resource, path, lookup_flags, &size, &flags, &data, &data_size, error))
return NULL;
if (flags & G_RESOURCE_FLAGS_COMPRESSED)
@@ -255,18 +265,20 @@ g_resource_lookup_data (GResource *resource,
}
gboolean
-g_resource_get_info (GResource *resource,
- char *path,
- gsize *size,
- guint32 *flags,
- GError **error)
+g_resource_get_info (GResource *resource,
+ char *path,
+ GResourceLookupFlags lookup_flags,
+ gsize *size,
+ guint32 *flags,
+ GError **error)
{
- return do_lookup (resource, path, size, flags, NULL, NULL, TRUE, error);
+ return do_lookup (resource, path, lookup_flags, size, flags, NULL, NULL, error);
}
char **
g_resource_enumerate_children (GResource *resource,
char *path,
+ GResourceLookupFlags lookup_flags,
GError **error)
{
gchar **children;
@@ -335,6 +347,7 @@ g_resources_unregister (GResource *resource)
GInputStream *
g_resources_open_stream (char *path,
+ GResourceLookupFlags lookup_flags,
GError **error)
{
GInputStream *res = NULL;
@@ -348,7 +361,7 @@ g_resources_open_stream (char *path,
GResource *r = l->data;
GError *my_error = NULL;
- stream = g_resource_open_stream (r, path, &my_error);
+ stream = g_resource_open_stream (r, path, lookup_flags, &my_error);
if (stream == NULL &&
g_error_matches (my_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND))
{
@@ -375,6 +388,7 @@ g_resources_open_stream (char *path,
GBytes *
g_resources_lookup_data (char *path,
+ GResourceLookupFlags lookup_flags,
GError **error)
{
GBytes *res = NULL;
@@ -388,7 +402,7 @@ g_resources_lookup_data (char *path,
GResource *r = l->data;
GError *my_error = NULL;
- data = g_resource_lookup_data (r, path, &my_error);
+ data = g_resource_lookup_data (r, path, lookup_flags, &my_error);
if (data == NULL &&
g_error_matches (my_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND))
{
@@ -415,6 +429,7 @@ g_resources_lookup_data (char *path,
char **
g_resources_enumerate_children (char *path,
+ GResourceLookupFlags lookup_flags,
GError **error)
{
GHashTable *hash = NULL;
@@ -428,7 +443,7 @@ g_resources_enumerate_children (char *path,
{
GResource *r = l->data;
- children = g_resource_enumerate_children (r, path, NULL);
+ children = g_resource_enumerate_children (r, path, 0, NULL);
if (children != NULL)
{
@@ -472,6 +487,7 @@ g_resources_enumerate_children (char *path,
gboolean
g_resources_get_info (char *path,
+ GResourceLookupFlags lookup_flags,
gsize *size,
guint32 *flags,
GError **error)
@@ -487,7 +503,7 @@ g_resources_get_info (char *path,
GResource *r = l->data;
GError *my_error = NULL;
- r_res = g_resource_get_info (r, path, size, flags, &my_error);
+ r_res = g_resource_get_info (r, path, lookup_flags, size, flags, &my_error);
if (!r_res &&
g_error_matches (my_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND))
{
diff --git a/gio/gresource.h b/gio/gresource.h
index 9317152..31b5d32 100644
--- a/gio/gresource.h
+++ b/gio/gresource.h
@@ -34,40 +34,48 @@ G_BEGIN_DECLS
#define G_RESOURCE_ERROR (g_resource_error_quark ())
GQuark g_resource_error_quark (void);
-GResource * g_resource_new_from_data (GBytes *data,
- 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);
-GBytes * g_resource_lookup_data (GResource *resource,
- char *path,
- 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);
+GResource * g_resource_new_from_data (GBytes *data,
+ 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,
+ GResourceLookupFlags lookup_flags,
+ GError **error);
+GBytes * g_resource_lookup_data (GResource *resource,
+ char *path,
+ GResourceLookupFlags lookup_flags,
+ GError **error);
+char ** g_resource_enumerate_children (GResource *resource,
+ char *path,
+ GResourceLookupFlags lookup_flags,
+ GError **error);
+gboolean g_resource_get_info (GResource *resource,
+ char *path,
+ GResourceLookupFlags lookup_flags,
+ 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,
+ GResourceLookupFlags lookup_flags,
+ GError **error);
+GBytes * g_resources_lookup_data (char *path,
+ GResourceLookupFlags lookup_flags,
+ GError **error);
+char ** g_resources_enumerate_children (char *path,
+ GResourceLookupFlags lookup_flags,
+ GError **error);
+gboolean g_resources_get_info (char *path,
+ GResourceLookupFlags lookup_flags,
+ 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);
-GBytes * g_resources_lookup_data (char *path,
- 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
diff --git a/gio/gresourcefile.c b/gio/gresourcefile.c
index 9f288f0..bfa1e6e 100644
--- a/gio/gresourcefile.c
+++ b/gio/gresourcefile.c
@@ -428,7 +428,7 @@ g_resource_file_query_info (GFile *file,
char *base;
is_dir = FALSE;
- children = g_resources_enumerate_children (resource->path, NULL);
+ children = g_resources_enumerate_children (resource->path, 0, NULL);
if (children != NULL)
{
g_strfreev (children);
@@ -441,7 +441,7 @@ g_resource_file_query_info (GFile *file,
if (!is_dir)
{
- res = g_resources_get_info (resource->path, &size, &resource_flags, &my_error);
+ res = g_resources_get_info (resource->path, 0, &size, &resource_flags, &my_error);
if (!res)
{
if (g_error_matches (my_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND))
@@ -509,7 +509,7 @@ g_resource_file_read (GFile *file,
GInputStream *stream;
GFileInputStream *res;
- stream = g_resources_open_stream (resource->path, &my_error);
+ stream = g_resources_open_stream (resource->path, 0, &my_error);
if (stream == NULL)
{
@@ -607,11 +607,11 @@ _g_resource_file_enumerator_new (GResourceFile *file,
char **children;
gboolean res;
- children = g_resources_enumerate_children (file->path, NULL);
+ children = g_resources_enumerate_children (file->path, 0, NULL);
if (children == NULL &&
strcmp ("/", file->path) != 0)
{
- res = g_resources_get_info (file->path, NULL, NULL, NULL);
+ res = g_resources_get_info (file->path, 0, NULL, NULL, NULL);
if (res)
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY,
_("The resource at '%s' is not a directory"),
diff --git a/gio/tests/resources.c b/gio/tests/resources.c
index a0e1381..8b66291 100644
--- a/gio/tests/resources.c
+++ b/gio/tests/resources.c
@@ -35,6 +35,7 @@ test_resource (GResource *resource)
found = g_resource_get_info (resource,
"/not/there",
+ G_RESOURCE_LOOKUP_FLAGS_NONE,
&size, &flags, &error);
g_assert (!found);
g_assert (error != NULL);
@@ -42,6 +43,7 @@ test_resource (GResource *resource)
found = g_resource_get_info (resource,
"/test1.txt",
+ G_RESOURCE_LOOKUP_FLAGS_NONE,
&size, &flags, &error);
g_assert (found);
g_assert (error == NULL);
@@ -50,6 +52,7 @@ test_resource (GResource *resource)
found = g_resource_get_info (resource,
"/a_prefix/test2.txt",
+ G_RESOURCE_LOOKUP_FLAGS_NONE,
&size, &flags, &error);
g_assert (found);
g_assert (error == NULL);
@@ -58,6 +61,7 @@ test_resource (GResource *resource)
found = g_resource_get_info (resource,
"/a_prefix/test2-alias.txt",
+ G_RESOURCE_LOOKUP_FLAGS_NONE,
&size, &flags, &error);
g_assert (found);
g_assert (error == NULL);
@@ -66,6 +70,7 @@ test_resource (GResource *resource)
found = g_resource_get_info (resource,
"/sv/test1.txt",
+ G_RESOURCE_LOOKUP_FLAGS_NONE,
&size, &flags, &error);
g_assert (found);
g_assert (error == NULL);
@@ -75,6 +80,7 @@ test_resource (GResource *resource)
/* This will fail due to compression */
data = g_resource_lookup_data (resource,
"/test1.txt",
+ G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert (data == NULL);
g_assert (error != NULL);
@@ -82,6 +88,7 @@ test_resource (GResource *resource)
in = g_resource_open_stream (resource,
"/test1.txt",
+ G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert (in != NULL);
g_assert (error == NULL);
@@ -98,6 +105,7 @@ test_resource (GResource *resource)
data = g_resource_lookup_data (resource,
"/a_prefix/test2.txt",
+ G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert (data != NULL);
g_assert (error == NULL);
@@ -107,6 +115,7 @@ test_resource (GResource *resource)
data = g_resource_lookup_data (resource,
"/a_prefix/test2-alias.txt",
+ G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert (data != NULL);
g_assert (error == NULL);
@@ -116,6 +125,7 @@ test_resource (GResource *resource)
data = g_resource_lookup_data (resource,
"/sv/test1.txt",
+ G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert (data != NULL);
g_assert (error == NULL);
@@ -124,6 +134,7 @@ test_resource (GResource *resource)
children = g_resource_enumerate_children (resource,
"/not/here",
+ G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert (children == NULL);
g_assert (error != NULL);
@@ -131,6 +142,7 @@ test_resource (GResource *resource)
children = g_resource_enumerate_children (resource,
"/a_prefix",
+ G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert (children != NULL);
g_assert (error == NULL);
@@ -198,6 +210,7 @@ test_resource_registred (void)
g_assert (error == NULL);
found = g_resources_get_info ("/test1.txt",
+ G_RESOURCE_LOOKUP_FLAGS_NONE,
&size, &flags, &error);
g_assert (!found);
g_assert (error != NULL);
@@ -206,6 +219,7 @@ test_resource_registred (void)
g_resources_register (resource);
found = g_resources_get_info ("/test1.txt",
+ G_RESOURCE_LOOKUP_FLAGS_NONE,
&size, &flags, &error);
g_assert (found);
g_assert (error == NULL);
@@ -213,6 +227,7 @@ test_resource_registred (void)
g_assert (flags == (G_RESOURCE_FLAGS_LOCALIZED | G_RESOURCE_FLAGS_COMPRESSED));
found = g_resources_get_info ("/a_prefix/test2.txt",
+ G_RESOURCE_LOOKUP_FLAGS_NONE,
&size, &flags, &error);
g_assert (found);
g_assert (error == NULL);
@@ -220,6 +235,7 @@ test_resource_registred (void)
g_assert (flags == 0);
found = g_resources_get_info ("/a_prefix/test2-alias.txt",
+ G_RESOURCE_LOOKUP_FLAGS_NONE,
&size, &flags, &error);
g_assert (found);
g_assert (error == NULL);
@@ -227,6 +243,7 @@ test_resource_registred (void)
g_assert (flags == 0);
found = g_resources_get_info ("/sv/test1.txt",
+ G_RESOURCE_LOOKUP_FLAGS_NONE,
&size, &flags, &error);
g_assert (found);
g_assert (error == NULL);
@@ -235,12 +252,14 @@ test_resource_registred (void)
/* This will fail due to compression */
data = g_resources_lookup_data ("/test1.txt",
+ G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert (data == NULL);
g_assert (error != NULL);
g_clear_error (&error);
in = g_resources_open_stream ("/test1.txt",
+ G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert (in != NULL);
g_assert (error == NULL);
@@ -257,6 +276,7 @@ test_resource_registred (void)
data = g_resources_lookup_data ("/a_prefix/test2.txt",
+ G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert (data != NULL);
g_assert (error == NULL);
@@ -265,6 +285,7 @@ test_resource_registred (void)
g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
data = g_resources_lookup_data ("/a_prefix/test2-alias.txt",
+ G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert (data != NULL);
g_assert (error == NULL);
@@ -273,6 +294,7 @@ test_resource_registred (void)
g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test2\n");
data = g_resources_lookup_data ("/sv/test1.txt",
+ G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert (data != NULL);
g_assert (error == NULL);
@@ -281,12 +303,14 @@ test_resource_registred (void)
g_assert_cmpstr (g_bytes_get_data (data, NULL), ==, "test3\n");
children = g_resources_enumerate_children ("/not/here",
+ G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert (children == NULL);
g_assert (error != NULL);
g_clear_error (&error);
children = g_resources_enumerate_children ("/a_prefix",
+ G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert (children != NULL);
g_assert (error == NULL);
@@ -295,6 +319,7 @@ test_resource_registred (void)
g_resources_unregister (resource);
found = g_resources_get_info ("/test1.txt",
+ G_RESOURCE_LOOKUP_FLAGS_NONE,
&size, &flags, &error);
g_assert (!found);
g_assert (error != NULL);
@@ -311,6 +336,7 @@ test_resource_automatic (void)
GBytes *data;
found = g_resources_get_info ("/auto_loaded/test1.txt",
+ G_RESOURCE_LOOKUP_FLAGS_NONE,
&size, &flags, &error);
g_assert (found);
g_assert (error == NULL);
@@ -318,6 +344,7 @@ test_resource_automatic (void)
g_assert (flags == 0);
data = g_resources_lookup_data ("/auto_loaded/test1.txt",
+ G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert (data != NULL);
g_assert (error == NULL);
@@ -336,6 +363,7 @@ test_resource_manual (void)
GBytes *data;
found = g_resources_get_info ("/manual_loaded/test1.txt",
+ G_RESOURCE_LOOKUP_FLAGS_NONE,
&size, &flags, &error);
g_assert (found);
g_assert (error == NULL);
@@ -343,6 +371,7 @@ test_resource_manual (void)
g_assert (flags == 0);
data = g_resources_lookup_data ("/manual_loaded/test1.txt",
+ G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert (data != NULL);
g_assert (error == NULL);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]