[gegl] Add gegl_extension_handler_register/get_saver()



commit 3cd1d00b3cea01961af27e7af2ffd86580ed2885
Author: Martin Nordholts <martinn src gnome org>
Date:   Wed May 12 07:34:32 2010 +0200

    Add gegl_extension_handler_register/get_saver()
    
    Add gegl_extension_handler_register/get_saver(), which are analogous
    to gegl_extension_handler_register/get() except they are for output
    instead of input. Also add a warning when an extension handler is not
    found.
    
    Add a simple test for it and add a TODO about cleaning this up when
    cleaning up gegl-plugin.h

 docs/todo.txt                           |    6 +++
 gegl/gegl-plugin.h                      |    4 ++
 gegl/operation/gegl-extension-handler.c |   73 +++++++++++++++++++++++++------
 gegl/operation/gegl-extension-handler.h |   11 +++--
 tests/test-misc.c                       |   19 ++++++++
 5 files changed, 96 insertions(+), 17 deletions(-)
---
diff --git a/docs/todo.txt b/docs/todo.txt
new file mode 100644
index 0000000..6001934
--- /dev/null
+++ b/docs/todo.txt
@@ -0,0 +1,6 @@
+TODO
+====
+
+* Rename gegl_extension_handler_register() to
+  gegl_extension_handler_register_loader() when looking over
+  gegl-plugin.h
diff --git a/gegl/gegl-plugin.h b/gegl/gegl-plugin.h
index cb8c18b..8529696 100644
--- a/gegl/gegl-plugin.h
+++ b/gegl/gegl-plugin.h
@@ -83,7 +83,11 @@ GObject        *gegl_operation_context_get_object (GeglOperationContext *context
 
 void            gegl_extension_handler_register    (const gchar         *extension,
                                                     const gchar         *handler);
+void            gegl_extension_handler_register_saver
+                                                   (const gchar         *extension,
+                                                    const gchar         *handler);
 const gchar   * gegl_extension_handler_get         (const gchar         *extension);
+const gchar   * gegl_extension_handler_get_saver   (const gchar         *extension);
 
 
 #include <glib-object.h>
diff --git a/gegl/operation/gegl-extension-handler.c b/gegl/operation/gegl-extension-handler.c
index 9b15dbc..0150911 100644
--- a/gegl/operation/gegl-extension-handler.c
+++ b/gegl/operation/gegl-extension-handler.c
@@ -20,23 +20,43 @@
 #include <glib.h>
 #include "gegl-extension-handler.h"
 
-static GHashTable *handlers = NULL;
+static GHashTable *load_handlers = NULL;
+static GHashTable *save_handlers = NULL;
 
-void
-gegl_extension_handler_register (const gchar *extension,
-                                 const gchar *handler)
+static void
+gegl_extension_handler_register_util (GHashTable  **handlers,
+                                      const gchar  *extension,
+                                      const gchar  *handler)
 {
   /* Case fold so we get case insensitive extension comparisions */
   gchar *ext = g_utf8_casefold (extension, -1);
 
-  if (!handlers)
-    handlers = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
+  if (!*handlers)
+    *handlers = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
 
-  g_hash_table_insert (handlers, ext, g_strdup (handler));
+  g_hash_table_insert (*handlers, ext, g_strdup (handler));
 }
+                                      
 
-const gchar *
-gegl_extension_handler_get (const gchar *extension)
+void
+gegl_extension_handler_register (const gchar *extension,
+                                 const gchar *handler)
+{
+  gegl_extension_handler_register_util (&load_handlers, extension, handler);
+}
+
+void
+gegl_extension_handler_register_saver (const gchar *extension,
+                                       const gchar *handler)
+{
+  gegl_extension_handler_register_util (&save_handlers, extension, handler);
+}
+
+static const gchar *
+gegl_extension_handler_get_util (GHashTable *handlers,
+                                 const gchar *extension,
+                                 const gchar *handler_name,
+                                 const gchar *def)
 {
   const gchar *handler = NULL;
   gchar *ext = NULL;
@@ -52,15 +72,42 @@ gegl_extension_handler_get (const gchar *extension)
   if (handler)
     return handler;
 
-  return "gegl:magick-load";
+  g_warning ("No %s for extension \"%s\", falling back to \"%s\"",
+             handler_name, extension, def);
+
+  return def;
+}
+
+const gchar *
+gegl_extension_handler_get (const gchar *extension)
+{
+  return gegl_extension_handler_get_util (load_handlers,
+                                          extension,
+                                          "loader",
+                                          "gegl:magick-load");
+}
+
+const gchar *
+gegl_extension_handler_get_saver (const gchar *extension)
+{
+  return gegl_extension_handler_get_util (save_handlers,
+                                          extension,
+                                          "saver",
+                                          "gegl:png-save");
 }
 
 void
 gegl_extension_handler_cleanup (void)
 {
-  if (handlers)
+  if (load_handlers)
+    {
+      g_hash_table_destroy (load_handlers);
+      load_handlers = NULL;
+    }
+
+  if (save_handlers)
     {
-      g_hash_table_destroy (handlers);
-      handlers = NULL;
+      g_hash_table_destroy (save_handlers);
+      save_handlers = NULL;
     }
 }
diff --git a/gegl/operation/gegl-extension-handler.h b/gegl/operation/gegl-extension-handler.h
index 118ee54..bb6219f 100644
--- a/gegl/operation/gegl-extension-handler.h
+++ b/gegl/operation/gegl-extension-handler.h
@@ -21,9 +21,12 @@
 
 #include <glib.h>
 
-void          gegl_extension_handler_register (const gchar *extension,
-                                               const gchar *handler);
-const gchar * gegl_extension_handler_get      (const gchar *extension);
-void          gegl_extension_handler_cleanup  (void);
+void          gegl_extension_handler_register       (const gchar *extension,
+                                                     const gchar *handler);
+void          gegl_extension_handler_register_saver (const gchar *extension,
+                                                     const gchar *handler);
+const gchar * gegl_extension_handler_get            (const gchar *extension);
+const gchar * gegl_extension_handler_get_saver      (const gchar *extension);
+void          gegl_extension_handler_cleanup        (void);
 
 #endif
diff --git a/tests/test-misc.c b/tests/test-misc.c
index 5a660bd..09afce1 100644
--- a/tests/test-misc.c
+++ b/tests/test-misc.c
@@ -42,6 +42,22 @@ test_misc_case_insensitive_extension_handler (void)
   return result;
 }
 
+static int
+test_misc_save_handler (void)
+{
+  gint result = SUCCESS;
+  const gchar *handler = "gegl:bar-handler";
+  const gchar *ext = "barext";
+  const gchar *received_handler = NULL;
+
+  gegl_extension_handler_register_saver (ext, handler);
+  received_handler = gegl_extension_handler_get_saver (ext);
+  if (! strcmp (received_handler, handler) == 0)
+    result = FAILURE;
+  
+  return result;
+}
+
 
 int main(int argc, char *argv[])
 {
@@ -50,5 +66,8 @@ int main(int argc, char *argv[])
   if (result == SUCCESS)
     result = test_misc_case_insensitive_extension_handler ();
 
+  if (result == SUCCESS)
+    result = test_misc_save_handler ();
+
   return result;
 }



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