[gimp] app: port generated brush saving to GIO



commit b0f9a42fb3ae0c44e76921a29ea89367150a1a4a
Author: Michael Natterer <mitch gimp org>
Date:   Wed Jul 2 22:16:09 2014 +0200

    app: port generated brush saving to GIO

 app/core/gimpbrushgenerated-save.c |   86 +++++++++++++++++++----------------
 1 files changed, 47 insertions(+), 39 deletions(-)
---
diff --git a/app/core/gimpbrushgenerated-save.c b/app/core/gimpbrushgenerated-save.c
index 4cd8187..d7c7fcf 100644
--- a/app/core/gimpbrushgenerated-save.c
+++ b/app/core/gimpbrushgenerated-save.c
@@ -19,16 +19,7 @@
 
 #include "config.h"
 
-#include <errno.h>
-#include <stdlib.h>
-#include <string.h>
-
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-
 #include <gdk-pixbuf/gdk-pixbuf.h>
-#include <glib/gstdio.h>
 #include <gegl.h>
 
 #include "libgimpbase/gimpbase.h"
@@ -47,42 +38,44 @@ gimp_brush_generated_save (GimpData  *data,
 {
   GimpBrushGenerated *brush = GIMP_BRUSH_GENERATED (data);
   const gchar        *name  = gimp_object_get_name (data);
-  gchar              *path;
-  FILE               *file;
+  GOutputStream      *output;
+  GString            *string;
   gchar               buf[G_ASCII_DTOSTR_BUF_SIZE];
+  gsize               bytes_written;
   gboolean            have_shape = FALSE;
+  GError             *my_error   = NULL;
 
   g_return_val_if_fail (name != NULL && *name != '\0', FALSE);
 
-  path = g_file_get_path (gimp_data_get_file (data));
-  file = g_fopen (path, "wb");
-  g_free (path);
-
-  if (! file)
+  output = G_OUTPUT_STREAM (g_file_replace (gimp_data_get_file (data),
+                                            NULL, FALSE, G_FILE_CREATE_NONE,
+                                            NULL, error));
+  if (! output)
     {
       g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_OPEN,
                    _("Could not open '%s' for writing: %s"),
                    gimp_file_get_utf8_name (gimp_data_get_file (data)),
-                   g_strerror (errno));
+                   my_error->message);
+      g_clear_error (&my_error);
       return FALSE;
     }
 
   /* write magic header */
-  fprintf (file, "GIMP-VBR\n");
+  string = g_string_new ("GIMP-VBR\n");
 
   /* write version */
   if (brush->shape != GIMP_BRUSH_GENERATED_CIRCLE || brush->spikes > 2)
     {
-      fprintf (file, "1.5\n");
+      g_string_append (string, "1.5\n");
       have_shape = TRUE;
     }
   else
     {
-      fprintf (file, "1.0\n");
+      g_string_append (string, "1.0\n");
     }
 
   /* write name */
-  fprintf (file, "%.255s\n", name);
+  g_string_append_printf (string, "%.255s\n", name);
 
   if (have_shape)
     {
@@ -93,41 +86,56 @@ gimp_brush_generated_save (GimpData  *data,
 
       /* write shape */
       shape_val = g_enum_get_value (enum_class, brush->shape);
-      fprintf (file, "%s\n", shape_val->value_nick);
+      g_string_append_printf (string, "%s\n", shape_val->value_nick);
     }
 
   /* write brush spacing */
-  fprintf (file, "%s\n",
-           g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, "%f",
-                            gimp_brush_get_spacing (GIMP_BRUSH (brush))));
+  g_string_append_printf (string, "%s\n",
+                          g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, "%f",
+                                           gimp_brush_get_spacing (GIMP_BRUSH (brush))));
 
   /* write brush radius */
-  fprintf (file, "%s\n",
-           g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, "%f",
-                            brush->radius));
+  g_string_append_printf (string, "%s\n",
+                          g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, "%f",
+                                           brush->radius));
 
   if (have_shape)
     {
       /* write brush spikes */
-      fprintf (file, "%d\n", brush->spikes);
+      g_string_append_printf (string, "%d\n", brush->spikes);
     }
 
   /* write brush hardness */
-  fprintf (file, "%s\n",
-           g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, "%f",
-                            brush->hardness));
+  g_string_append_printf (string, "%s\n",
+                          g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, "%f",
+                                           brush->hardness));
 
   /* write brush aspect_ratio */
-  fprintf (file, "%s\n",
-           g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, "%f",
-                            brush->aspect_ratio));
+  g_string_append_printf (string, "%s\n",
+                          g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, "%f",
+                                           brush->aspect_ratio));
 
   /* write brush angle */
-  fprintf (file, "%s\n",
-           g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, "%f",
-                            brush->angle));
+  g_string_append_printf (string, "%s\n",
+                          g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, "%f",
+                                           brush->angle));
+
+  if (! g_output_stream_write_all (output, string->str, string->len,
+                                   &bytes_written, NULL, &my_error) ||
+      bytes_written != string->len)
+    {
+      g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_WRITE,
+                   _("Writing brush file '%s' failed: %s"),
+                   gimp_file_get_utf8_name (gimp_data_get_file (data)),
+                   my_error->message);
+      g_clear_error (&my_error);
+      g_string_free (string, TRUE);
+      g_object_unref (output);
+      return FALSE;
+    }
 
-  fclose (file);
+  g_string_free (string, TRUE);
+  g_object_unref (output);
 
   return TRUE;
 }


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