[gimp] app: port gimp_palette_load_css() to GIO



commit 4d50c3aafdc22073ce548252c7b8e01e45dc6b00
Author: Michael Natterer <mitch gimp org>
Date:   Thu Jul 3 17:36:01 2014 +0200

    app: port gimp_palette_load_css() to GIO
    
    which finishes palette porting. Remove all temp hack porting code
    and a ton of ugly includes.

 app/core/gimppalette-import.c |   23 +-----------------
 app/core/gimppalette-load.c   |   51 ++++++++++++++++++++---------------------
 app/core/gimppalette-load.h   |    2 +-
 3 files changed, 27 insertions(+), 49 deletions(-)
---
diff --git a/app/core/gimppalette-import.c b/app/core/gimppalette-import.c
index 15725a8..4234d3d 100644
--- a/app/core/gimppalette-import.c
+++ b/app/core/gimppalette-import.c
@@ -17,15 +17,10 @@
 
 #include "config.h"
 
-#include <errno.h>
-#include <string.h>
-
 #include <cairo.h>
 #include <gegl.h>
 #include <gdk-pixbuf/gdk-pixbuf.h>
 
-#include <glib/gstdio.h>
-
 #include "libgimpbase/gimpbase.h"
 #include "libgimpcolor/gimpcolor.h"
 
@@ -500,8 +495,6 @@ gimp_palette_import_from_file (GimpContext  *context,
 {
   GList        *palette_list = NULL;
   GInputStream *input;
-  gchar        *path;
-  FILE         *f;
   GError       *my_error = NULL;
 
   g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
@@ -519,19 +512,6 @@ gimp_palette_import_from_file (GimpContext  *context,
       return NULL;
     }
 
-  /* EEK temporary double opening of the file, until all is GIO */
-  path = g_file_get_path (file);
-  f = g_fopen (path, "rb");
-  g_free (path);
-  if (! f)
-    {
-      g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_OPEN,
-                   _("Could not open '%s' for reading: %s"),
-                   gimp_file_get_utf8_name (file), g_strerror (errno));
-      g_object_unref (input);
-      return NULL;
-    }
-
   switch (gimp_palette_load_detect_format (file, input))
     {
     case GIMP_PALETTE_FILE_FORMAT_GPL:
@@ -555,7 +535,7 @@ gimp_palette_import_from_file (GimpContext  *context,
       break;
 
     case GIMP_PALETTE_FILE_FORMAT_CSS:
-      palette_list = gimp_palette_load_css (context, file, f, error);
+      palette_list = gimp_palette_load_css (context, file, input, error);
       break;
 
     default:
@@ -566,7 +546,6 @@ gimp_palette_import_from_file (GimpContext  *context,
       break;
     }
 
-  fclose (f);
   g_object_unref (input);
 
   if (palette_list)
diff --git a/app/core/gimppalette-load.c b/app/core/gimppalette-load.c
index eb45c7f..4f60766 100644
--- a/app/core/gimppalette-load.c
+++ b/app/core/gimppalette-load.c
@@ -17,24 +17,11 @@
 
 #include "config.h"
 
-#include <errno.h>
-#include <fcntl.h>
 #include <stdlib.h>
-#include <string.h>
-#include <sys/types.h>
-
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
 
 #include <cairo.h>
 #include <gegl.h>
 #include <gdk-pixbuf/gdk-pixbuf.h>
-#include <glib/gstdio.h>
-
-#ifdef G_OS_WIN32
-#include "libgimpbase/gimpwin32-io.h"
-#endif
 
 #include "libgimpbase/gimpbase.h"
 #include "libgimpcolor/gimpcolor.h"
@@ -645,17 +632,19 @@ gimp_palette_load_aco (GimpContext   *context,
 
 
 GList *
-gimp_palette_load_css (GimpContext  *context,
-                       GFile        *file,
-                       FILE         *f,
-                       GError      **error)
+gimp_palette_load_css (GimpContext   *context,
+                       GFile         *file,
+                       GInputStream  *input,
+                       GError       **error)
 {
-  GimpPalette *palette;
-  gchar       *name;
-  GRegex      *regex;
-  GimpRGB      color;
+  GimpPalette      *palette;
+  GDataInputStream *data_input;
+  gchar            *name;
+  GRegex           *regex;
+  gchar            *buf;
 
   g_return_val_if_fail (G_IS_FILE (file), NULL);
+  g_return_val_if_fail (G_IS_INPUT_STREAM (file), NULL);
   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
 
   regex = g_regex_new (".*color.*:(?P<param>.*);", G_REGEX_CASELESS, 0, error);
@@ -666,16 +655,22 @@ gimp_palette_load_css (GimpContext  *context,
   palette = GIMP_PALETTE (gimp_palette_new (context, name));
   g_free (name);
 
+  data_input = g_data_input_stream_new (input);
+
   do
     {
-      GMatchInfo *matches;
-      gchar       buf[1024];
+      gsize  buf_len = 1024;
 
-      if (fgets (buf, sizeof (buf), f) != NULL)
+      buf = g_data_input_stream_read_line (data_input, &buf_len, NULL, NULL);
+
+      if (buf)
         {
+          GMatchInfo *matches;
+
           if (g_regex_match (regex, buf, 0, &matches))
             {
-              gchar *word = g_match_info_fetch_named (matches, "param");
+              GimpRGB  color;
+              gchar   *word = g_match_info_fetch_named (matches, "param");
 
               if (gimp_rgb_parse_css (&color, word, -1))
                 {
@@ -687,10 +682,14 @@ gimp_palette_load_css (GimpContext  *context,
 
               g_free (word);
             }
+
+          g_free (buf);
         }
-    } while (! feof (f));
+    }
+  while (buf);
 
   g_regex_unref (regex);
+  g_object_unref (data_input);
 
   return g_list_prepend (NULL, palette);
 }
diff --git a/app/core/gimppalette-load.h b/app/core/gimppalette-load.h
index 8d08ad9..6cb67ad 100644
--- a/app/core/gimppalette-load.h
+++ b/app/core/gimppalette-load.h
@@ -59,7 +59,7 @@ GList               * gimp_palette_load_aco           (GimpContext   *context,
                                                        GError       **error);
 GList               * gimp_palette_load_css           (GimpContext   *context,
                                                        GFile         *file,
-                                                       FILE          *f,
+                                                       GInputStream  *input,
                                                        GError       **error);
 
 GimpPaletteFileFormat gimp_palette_load_detect_format (GFile         *file,


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