[gimp] app: port curves cruft format saving to GIO



commit 87ecc83885fa82150f47059dea9e46fff01d2841
Author: Michael Natterer <mitch gimp org>
Date:   Wed Jul 2 23:40:26 2014 +0200

    app: port curves cruft format saving to GIO

 app/operations/gimpcurvesconfig.c |   54 ++++++++++++++++++++++++++++++-------
 app/operations/gimpcurvesconfig.h |    2 +-
 app/tools/gimpcurvestool.c        |   25 +----------------
 3 files changed, 46 insertions(+), 35 deletions(-)
---
diff --git a/app/operations/gimpcurvesconfig.c b/app/operations/gimpcurvesconfig.c
index ee90364..a8b1dd6 100644
--- a/app/operations/gimpcurvesconfig.c
+++ b/app/operations/gimpcurvesconfig.c
@@ -27,6 +27,7 @@
 #include <gdk-pixbuf/gdk-pixbuf.h>
 #include <glib/gstdio.h>
 
+#include "libgimpbase/gimpbase.h"
 #include "libgimpcolor/gimpcolor.h"
 #include "libgimpmath/gimpmath.h"
 #include "libgimpconfig/gimpconfig.h"
@@ -571,17 +572,33 @@ gimp_curves_config_load_cruft (GimpCurvesConfig  *config,
 
 gboolean
 gimp_curves_config_save_cruft (GimpCurvesConfig  *config,
-                               gpointer           fp,
+                               GFile             *file,
                                GError           **error)
 {
-  FILE *file = fp;
-  gint  i;
+  GOutputStream *output;
+  GString       *string;
+  gsize          bytes_written;
+  gint           i;
+  GError        *my_error = NULL;
 
   g_return_val_if_fail (GIMP_IS_CURVES_CONFIG (config), FALSE);
-  g_return_val_if_fail (file != NULL, FALSE);
+  g_return_val_if_fail (G_IS_FILE (file), FALSE);
   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
-  fprintf (file, "# GIMP Curves File\n");
+  output = G_OUTPUT_STREAM (g_file_replace (file,
+                                            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 (file),
+                   my_error->message);
+      g_clear_error (&my_error);
+      return FALSE;
+    }
+
+  string = g_string_new ("# GIMP Curves File\n");
 
   for (i = 0; i < 5; i++)
     {
@@ -624,18 +641,35 @@ gimp_curves_config_save_cruft (GimpCurvesConfig  *config,
 
           if (x < 0.0 || y < 0.0)
             {
-              fprintf (file, "%d %d ", -1, -1);
+              g_string_append_printf (string, "%d %d ", -1, -1);
             }
           else
             {
-              fprintf (file, "%d %d ",
-                       (gint) (x * 255.999),
-                       (gint) (y * 255.999));
+              g_string_append_printf (string, "%d %d ",
+                                      (gint) (x * 255.999),
+                                      (gint) (y * 255.999));
             }
         }
 
-      fprintf (file, "\n");
+      g_string_append_printf (string, "\n");
     }
 
+  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 (file),
+                   my_error->message);
+      g_clear_error (&my_error);
+      g_string_free (string, TRUE);
+      g_object_unref (output);
+      return FALSE;
+    }
+
+  g_string_free (string, TRUE);
+  g_object_unref (output);
+
   return TRUE;
 }
diff --git a/app/operations/gimpcurvesconfig.h b/app/operations/gimpcurvesconfig.h
index 4859dad..0f1064b 100644
--- a/app/operations/gimpcurvesconfig.h
+++ b/app/operations/gimpcurvesconfig.h
@@ -72,7 +72,7 @@ gboolean   gimp_curves_config_load_cruft          (GimpCurvesConfig  *config,
                                                    gpointer           fp,
                                                    GError           **error);
 gboolean   gimp_curves_config_save_cruft          (GimpCurvesConfig  *config,
-                                                   gpointer           fp,
+                                                   GFile             *file,
                                                    GError           **error);
 
 
diff --git a/app/tools/gimpcurvestool.c b/app/tools/gimpcurvestool.c
index 9e34213..ce0a9dc 100644
--- a/app/tools/gimpcurvestool.c
+++ b/app/tools/gimpcurvestool.c
@@ -653,30 +653,7 @@ gimp_curves_tool_settings_export (GimpImageMapTool  *image_map_tool,
   GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
 
   if (tool->export_old_format)
-    {
-      gchar    *path;
-      FILE     *f;
-      gboolean  success;
-
-      path = g_file_get_path (file);
-      f = g_fopen (path, "wt");
-      g_free (path);
-
-      if (! f)
-        {
-          g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
-                       _("Could not open '%s' for writing: %s"),
-                       gimp_file_get_utf8_name (file),
-                       g_strerror (errno));
-          return FALSE;
-        }
-
-      success = gimp_curves_config_save_cruft (tool->config, f, error);
-
-      fclose (f);
-
-      return success;
-    }
+    return gimp_curves_config_save_cruft (tool->config, file, error);
 
   return GIMP_IMAGE_MAP_TOOL_CLASS (parent_class)->settings_export (image_map_tool,
                                                                     file,


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