[gimp] app: port brush loading to GIO



commit add91b70b9cada7b3a3c0adf64d8fffce56e5a97
Author: Michael Natterer <mitch gimp org>
Date:   Wed Jul 2 21:11:49 2014 +0200

    app: port brush loading to GIO

 app/core/gimpbrush-load.c     |   71 +++++++++++++++++++++++-----------------
 app/core/gimpbrush-load.h     |   22 ++++++------
 app/core/gimpbrushpipe-load.c |   64 +++++++++++++++----------------------
 3 files changed, 78 insertions(+), 79 deletions(-)
---
diff --git a/app/core/gimpbrush-load.c b/app/core/gimpbrush-load.c
index 4e18940..ccd0a2c 100644
--- a/app/core/gimpbrush-load.c
+++ b/app/core/gimpbrush-load.c
@@ -128,31 +128,26 @@ gimp_brush_load (GimpContext  *context,
                  GFile        *file,
                  GError      **error)
 {
-  GimpBrush *brush;
-  gchar     *path;
-  gint       fd;
+  GimpBrush    *brush;
+  GInputStream *input;
+  GError       *my_error = NULL;
 
   g_return_val_if_fail (G_IS_FILE (file), NULL);
-
-  path = g_file_get_path (file);
-
-  g_return_val_if_fail (g_path_is_absolute (path), NULL);
   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
 
-  fd = g_open (path, O_RDONLY | _O_BINARY, 0);
-  g_free (path);
-
-  if (fd == -1)
+  input = G_INPUT_STREAM (g_file_read (file, NULL, &my_error));
+  if (! input)
     {
       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));
+                   gimp_file_get_utf8_name (file), my_error->message);
+      g_clear_error (&my_error);
       return NULL;
     }
 
-  brush = gimp_brush_load_brush (context, file, fd, error);
+  brush = gimp_brush_load_brush (context, file, input, error);
 
-  close (fd);
+  g_object_unref (input);
 
   if (! brush)
     return NULL;
@@ -161,10 +156,10 @@ gimp_brush_load (GimpContext  *context,
 }
 
 GimpBrush *
-gimp_brush_load_brush (GimpContext  *context,
-                       GFile        *file,
-                       gint          fd,
-                       GError      **error)
+gimp_brush_load_brush (GimpContext   *context,
+                       GFile         *file,
+                       GInputStream  *input,
+                       GError       **error)
 {
   GimpBrush   *brush;
   gint         bn_size;
@@ -172,22 +167,27 @@ gimp_brush_load_brush (GimpContext  *context,
   gchar       *name = NULL;
   guchar      *pixmap;
   guchar      *mask;
+  gsize        bytes_read;
   gssize       i, size;
-  gboolean     success = TRUE;
+  GError      *my_error = NULL;
+  gboolean     success  = TRUE;
 
   g_return_val_if_fail (G_IS_FILE (file), NULL);
-  g_return_val_if_fail (fd != -1, NULL);
+  g_return_val_if_fail (G_IS_INPUT_STREAM (input), NULL);
   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
 
-  /*  Read in the header size  */
-  if (read (fd, &header, sizeof (header)) != sizeof (header))
+  /*  read the header  */
+  if (! g_input_stream_read_all (input, &header, sizeof (header),
+                                 &bytes_read, NULL, &my_error) ||
+      bytes_read != sizeof (header))
     {
       g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
                    ngettext ("Could not read %d byte from '%s': %s",
                              "Could not read %d bytes from '%s': %s",
                              (gint) sizeof (header)),
                    (gint) sizeof (header),
-                   gimp_file_get_utf8_name (file), g_strerror (errno));
+                   gimp_file_get_utf8_name (file), my_error->message);
+      g_clear_error (&my_error);
       return NULL;
     }
 
@@ -233,7 +233,10 @@ gimp_brush_load_brush (GimpContext  *context,
     {
     case 1:
       /*  If this is a version 1 brush, set the fp back 8 bytes  */
-      lseek (fd, -8, SEEK_CUR);
+      if (! g_seekable_seek (G_SEEKABLE (input), -8, G_SEEK_CUR,
+                             NULL, error))
+        return NULL;
+
       header.header_size += 8;
       /*  spacing is not defined in version 1  */
       header.spacing = 25;
@@ -273,7 +276,9 @@ gimp_brush_load_brush (GimpContext  *context,
 
       name = g_new (gchar, bn_size);
 
-      if ((read (fd, name, bn_size)) < bn_size)
+      if (! g_input_stream_read_all (input, name, bn_size,
+                                     &bytes_read, NULL, NULL) ||
+          bytes_read != bn_size)
         {
           g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
                        _("Fatal parse error in brush file '%s': "
@@ -308,7 +313,9 @@ gimp_brush_load_brush (GimpContext  *context,
   switch (header.bytes)
     {
     case 1:
-      success = (read (fd, mask, size) == size);
+      success = (g_input_stream_read_all (input, mask, size,
+                                         &bytes_read, NULL, NULL) &&
+                 bytes_read == size);
       break;
 
     case 2:  /*  cinepaint brush, 16 bit floats  */
@@ -317,9 +324,11 @@ gimp_brush_load_brush (GimpContext  *context,
 
         for (i = 0; success && i < size;)
           {
-            gssize  bytes = MIN (size - i, sizeof (buf));
+            gssize bytes = MIN (size - i, sizeof (buf));
 
-            success = (read (fd, buf, bytes) == bytes);
+            success = (g_input_stream_read_all (input, buf, bytes,
+                                                &bytes_read, NULL, NULL) &&
+                       bytes_read == bytes);
 
             if (success)
               {
@@ -377,9 +386,11 @@ gimp_brush_load_brush (GimpContext  *context,
 
         for (i = 0; success && i < size;)
           {
-            gssize  bytes = MIN (size - i, sizeof (buf));
+            gssize bytes = MIN (size - i, sizeof (buf));
 
-            success = (read (fd, buf, bytes) == bytes);
+            success = (g_input_stream_read_all (input, buf, bytes,
+                                                &bytes_read, NULL, NULL) &&
+                       bytes_read == bytes);
 
             if (success)
               {
diff --git a/app/core/gimpbrush-load.h b/app/core/gimpbrush-load.h
index 9865811..afe9264 100644
--- a/app/core/gimpbrush-load.h
+++ b/app/core/gimpbrush-load.h
@@ -25,17 +25,17 @@
 #define GIMP_BRUSH_PSP_FILE_EXTENSION    ".jbr"
 
 
-GList     * gimp_brush_load        (GimpContext  *context,
-                                    GFile        *file,
-                                    GError      **error);
-GimpBrush * gimp_brush_load_brush  (GimpContext  *context,
-                                    GFile        *file,
-                                    gint          fd,
-                                    GError      **error);
-
-GList     * gimp_brush_load_abr    (GimpContext  *context,
-                                    GFile        *file,
-                                    GError      **error);
+GList     * gimp_brush_load        (GimpContext   *context,
+                                    GFile         *file,
+                                    GError       **error);
+GimpBrush * gimp_brush_load_brush  (GimpContext   *context,
+                                    GFile         *file,
+                                    GInputStream  *input,
+                                    GError       **error);
+
+GList     * gimp_brush_load_abr    (GimpContext   *context,
+                                    GFile         *file,
+                                    GError       **error);
 
 
 #endif /* __GIMP_BRUSH_LOAD_H__ */
diff --git a/app/core/gimpbrushpipe-load.c b/app/core/gimpbrushpipe-load.c
index 39ddbc2..7708b26 100644
--- a/app/core/gimpbrushpipe-load.c
+++ b/app/core/gimpbrushpipe-load.c
@@ -19,28 +19,10 @@
 #include "config.h"
 
 #include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-
-#include <sys/types.h>
-
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-#include <fcntl.h>
-
-#ifndef _O_BINARY
-#define _O_BINARY 0
-#endif
 
 #include <gdk-pixbuf/gdk-pixbuf.h>
-#include <glib/gstdio.h>
 #include <gegl.h>
 
-#ifdef G_OS_WIN32
-#include <io.h>
-#endif
-
 #include "libgimpbase/gimpbase.h"
 #include "libgimpbase/gimpparasiteio.h"
 
@@ -59,6 +41,7 @@ gimp_brush_pipe_load (GimpContext  *context,
                       GError      **error)
 {
   GimpBrushPipe     *pipe = NULL;
+  GInputStream      *input;
   GimpPixPipeParams  params;
   gint               i;
   gint               num_of_brushes = 0;
@@ -66,24 +49,19 @@ gimp_brush_pipe_load (GimpContext  *context,
   gchar             *paramstring;
   GString           *buffer;
   gchar              c;
-  gchar             *path;
-  gint               fd;
+  gsize              bytes_read;
+  GError            *my_error = NULL;
 
   g_return_val_if_fail (G_IS_FILE (file), NULL);
-
-  path = g_file_get_path (file);
-
-  g_return_val_if_fail (g_path_is_absolute (path), NULL);
   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
 
-  fd = g_open (path, O_RDONLY | _O_BINARY, 0);
-  g_free (path);
-
-  if (fd == -1)
+  input = G_INPUT_STREAM (g_file_read (file, NULL, &my_error));
+  if (! input)
     {
       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));
+                   gimp_file_get_utf8_name (file), my_error->message);
+      g_clear_error (&my_error);
       return NULL;
     }
 
@@ -91,8 +69,13 @@ gimp_brush_pipe_load (GimpContext  *context,
 
   /*  get the name  */
   buffer = g_string_new (NULL);
-  while (read (fd, &c, 1) == 1 && c != '\n' && buffer->len < 1024)
-    g_string_append_c (buffer, c);
+  while (g_input_stream_read_all (input, &c, 1, &bytes_read, NULL, NULL) &&
+         bytes_read == 1 &&
+         c != '\n'       &&
+         buffer->len < 1024)
+    {
+      g_string_append_c (buffer, c);
+    }
 
   if (buffer->len > 0 && buffer->len < 1024)
     {
@@ -117,14 +100,19 @@ gimp_brush_pipe_load (GimpContext  *context,
                    _("Fatal parse error in brush file '%s': "
                      "File is corrupt."),
                    gimp_file_get_utf8_name (file));
-      close (fd);
+      g_object_unref (input);
       return NULL;
     }
 
   /*  get the number of brushes  */
   buffer = g_string_new (NULL);
-  while (read (fd, &c, 1) == 1 && c != '\n' && buffer->len < 1024)
-    g_string_append_c (buffer, c);
+  while (g_input_stream_read_all (input, &c, 1, &bytes_read, NULL, NULL) &&
+         bytes_read == 1 &&
+         c != '\n'       &&
+         buffer->len < 1024)
+    {
+      g_string_append_c (buffer, c);
+    }
 
   if (buffer->len > 0 && buffer->len < 1024)
     {
@@ -137,7 +125,7 @@ gimp_brush_pipe_load (GimpContext  *context,
                    _("Fatal parse error in brush file '%s': "
                      "File is corrupt."),
                    gimp_file_get_utf8_name (file));
-      close (fd);
+      g_object_unref (input);
       g_object_unref (pipe);
       g_string_free (buffer, TRUE);
       return NULL;
@@ -217,7 +205,7 @@ gimp_brush_pipe_load (GimpContext  *context,
       GError *my_error = NULL;
 
       pipe->brushes[pipe->n_brushes] = gimp_brush_load_brush (context,
-                                                              file, fd,
+                                                              file, input,
                                                               &my_error);
 
       if (pipe->brushes[pipe->n_brushes])
@@ -228,7 +216,7 @@ gimp_brush_pipe_load (GimpContext  *context,
       else
         {
           g_propagate_error (error, my_error);
-          close (fd);
+          g_object_unref (input);
           g_object_unref (pipe);
           return NULL;
         }
@@ -236,7 +224,7 @@ gimp_brush_pipe_load (GimpContext  *context,
       pipe->n_brushes++;
     }
 
-  close (fd);
+  g_object_unref (input);
 
   /* Current brush is the first one. */
   pipe->current = pipe->brushes[0];


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