[gimp] app: add gimp_file_has_extension() and use it in GimpDataFactory



commit da26597467a61de06363d58814d01330bd2b56a0
Author: Michael Natterer <mitch gimp org>
Date:   Sat Aug 2 23:29:00 2014 +0200

    app: add gimp_file_has_extension() and use it in GimpDataFactory

 app/core/gimp-utils.c      |   28 ++++++++++++++++++++++++++++
 app/core/gimp-utils.h      |    2 ++
 app/core/gimpdatafactory.c |   14 ++++++--------
 3 files changed, 36 insertions(+), 8 deletions(-)
---
diff --git a/app/core/gimp-utils.c b/app/core/gimp-utils.c
index f6a931c..fb43cc0 100644
--- a/app/core/gimp-utils.c
+++ b/app/core/gimp-utils.c
@@ -947,6 +947,34 @@ gimp_file_compare (GFile *file1,
     }
 }
 
+gboolean
+gimp_file_has_extension (GFile       *file,
+                         const gchar *extension)
+{
+  gchar    *uri;
+  gint      uri_len;
+  gint      ext_len;
+  gboolean  result = FALSE;
+
+  g_return_val_if_fail (G_IS_FILE (file), FALSE);
+  g_return_val_if_fail (extension != NULL, FALSE);
+
+  uri = g_file_get_uri (file);
+
+  uri_len = strlen (uri);
+  ext_len = strlen (extension);
+
+  if (uri_len && ext_len && (uri_len > ext_len))
+    {
+      if (g_ascii_strcasecmp (uri + uri_len - ext_len, extension) == 0)
+        result = TRUE;
+    }
+
+  g_free (uri);
+
+  return result;
+}
+
 
 /*  debug stuff  */
 
diff --git a/app/core/gimp-utils.h b/app/core/gimp-utils.h
index 197eeab..33515e6 100644
--- a/app/core/gimp-utils.h
+++ b/app/core/gimp-utils.h
@@ -102,6 +102,8 @@ void         gimp_constrain_line                   (gdouble          start_x,
 
 gint         gimp_file_compare                     (GFile           *file1,
                                                     GFile           *file2);
+gboolean     gimp_file_has_extension               (GFile           *file,
+                                                    const gchar     *extension);
 
 void         gimp_create_image_from_buffer         (Gimp            *gimp,
                                                     GeglBuffer      *buffer);
diff --git a/app/core/gimpdatafactory.c b/app/core/gimpdatafactory.c
index a8ac83f..2351cae 100644
--- a/app/core/gimpdatafactory.c
+++ b/app/core/gimpdatafactory.c
@@ -833,13 +833,10 @@ gimp_data_factory_load_data (GimpDataFactory *factory,
 {
   const GimpDataFactoryLoaderEntry *loader    = NULL;
   GList                            *data_list = NULL;
-  gchar                            *uri;
   GInputStream                     *input;
   gint                              i;
   GError                           *error = NULL;
 
-  uri = g_file_get_uri (file);
-
   for (i = 0; i < factory->priv->n_loader_entries; i++)
     {
       loader = &factory->priv->loader_entries[i];
@@ -849,13 +846,12 @@ gimp_data_factory_load_data (GimpDataFactory *factory,
        * which must be last in the loader array
        */
       if (! loader->extension ||
-          gimp_datafiles_check_extension (uri, loader->extension))
+          gimp_file_has_extension (file, loader->extension))
         {
           goto insert;
         }
     }
 
-  g_free (uri);
   return;
 
  insert:
@@ -872,7 +868,6 @@ gimp_data_factory_load_data (GimpDataFactory *factory,
           for (list = cached_data; list; list = g_list_next (list))
             gimp_container_add (factory->priv->container, list->data);
 
-          g_free (uri);
           return;
         }
     }
@@ -908,12 +903,17 @@ gimp_data_factory_load_data (GimpDataFactory *factory,
   if (G_LIKELY (data_list))
     {
       GList    *list;
+      gchar    *uri;
       gboolean  obsolete;
       gboolean  writable  = FALSE;
       gboolean  deletable = FALSE;
 
+      uri = g_file_get_uri (file);
+
       obsolete = (strstr (uri, GIMP_OBSOLETE_DATA_DIR_NAME) != 0);
 
+      g_free (uri);
+
       /* obsolete files are immutable, don't check their writability */
       if (! obsolete)
         {
@@ -948,8 +948,6 @@ gimp_data_factory_load_data (GimpDataFactory *factory,
       g_list_free (data_list);
     }
 
-  g_free (uri);
-
   /*  not else { ... } because loader->load_func() can return a list
    *  of data objects *and* an error message if loading failed after
    *  something was already loaded


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