[gimp] app: port gimp_palette_load_detect_format() to GIO



commit 3213fe04ef3d61c5a0573ff6165c348ab155c0ad
Author: Michael Natterer <mitch gimp org>
Date:   Thu Jul 3 16:27:05 2014 +0200

    app: port gimp_palette_load_detect_format() to GIO

 app/core/gimppalette-import.c |    2 +-
 app/core/gimppalette-load.c   |   34 +++++++++++++++++++++-------------
 app/core/gimppalette-load.h   |    2 +-
 3 files changed, 23 insertions(+), 15 deletions(-)
---
diff --git a/app/core/gimppalette-import.c b/app/core/gimppalette-import.c
index 1ba64ed..526abd6 100644
--- a/app/core/gimppalette-import.c
+++ b/app/core/gimppalette-import.c
@@ -532,7 +532,7 @@ gimp_palette_import_from_file (GimpContext  *context,
       return NULL;
     }
 
-  switch (gimp_palette_load_detect_format (file, f))
+  switch (gimp_palette_load_detect_format (file, input))
     {
     case GIMP_PALETTE_FILE_FORMAT_GPL:
       palette_list = gimp_palette_load_gpl (context, file, input, error);
diff --git a/app/core/gimppalette-load.c b/app/core/gimppalette-load.c
index 862641b..c06e5cc 100644
--- a/app/core/gimppalette-load.c
+++ b/app/core/gimppalette-load.c
@@ -640,14 +640,16 @@ gimp_palette_load_css (GimpContext  *context,
 }
 
 GimpPaletteFileFormat
-gimp_palette_load_detect_format (GFile *file,
-                                 FILE  *f)
+gimp_palette_load_detect_format (GFile        *file,
+                                 GInputStream *input)
 {
   GimpPaletteFileFormat format = GIMP_PALETTE_FILE_FORMAT_UNKNOWN;
-  gint                  fd = fileno (f);
   gchar                 header[16];
+  gsize                 bytes_read;
 
-  if (fread (header, 1, sizeof (header), f) == sizeof (header))
+  if (g_input_stream_read_all (input, &header, sizeof (header),
+                               &bytes_read, NULL, NULL) &&
+      bytes_read == sizeof (header))
     {
       if (g_str_has_prefix (header + 0, "RIFF") &&
           g_str_has_prefix (header + 8, "PAL data"))
@@ -666,33 +668,39 @@ gimp_palette_load_detect_format (GFile *file,
 
   if (format == GIMP_PALETTE_FILE_FORMAT_UNKNOWN)
     {
-      gchar *lower_filename =
-        g_ascii_strdown (gimp_file_get_utf8_name (file), -1);
+      gchar *lower = g_ascii_strdown (gimp_file_get_utf8_name (file), -1);
 
-      if (g_str_has_suffix (lower_filename, ".aco"))
+      if (g_str_has_suffix (lower, ".aco"))
         {
           format = GIMP_PALETTE_FILE_FORMAT_ACO;
         }
-      else if (g_str_has_suffix (lower_filename, ".css"))
+      else if (g_str_has_suffix (lower, ".css"))
         {
           format = GIMP_PALETTE_FILE_FORMAT_CSS;
         }
 
-      g_free (lower_filename);
+      g_free (lower);
     }
 
   if (format == GIMP_PALETTE_FILE_FORMAT_UNKNOWN)
     {
-      struct stat file_stat;
+      GFileInfo *info = g_file_query_info (file,
+                                           G_FILE_ATTRIBUTE_STANDARD_SIZE,
+                                           G_FILE_QUERY_INFO_NONE,
+                                           NULL, NULL);
 
-      if (fstat (fd, &file_stat) >= 0)
+      if (info)
         {
-          if (file_stat.st_size == 768)
+          goffset size = g_file_info_get_size (info);
+
+          if (size == 768)
             format = GIMP_PALETTE_FILE_FORMAT_ACT;
+
+          g_object_unref (info);
         }
     }
 
-  rewind (f);
+  g_seekable_seek (G_SEEKABLE (input), 0, G_SEEK_SET, NULL, NULL);
 
   return format;
 }
diff --git a/app/core/gimppalette-load.h b/app/core/gimppalette-load.h
index 0291c61..487b43c 100644
--- a/app/core/gimppalette-load.h
+++ b/app/core/gimppalette-load.h
@@ -63,7 +63,7 @@ GList               * gimp_palette_load_css           (GimpContext   *context,
                                                        GError       **error);
 
 GimpPaletteFileFormat gimp_palette_load_detect_format (GFile         *file,
-                                                       FILE          *f);
+                                                       GInputStream  *input);
 
 
 #endif /* __GIMP_PALETTE_H__ */


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