[gnome-autoar] private: Add helper to convert filenames to UTF-8



commit 99e5ae648d751e7b9e28f6e512f4318ddec3a123
Author: Bastien Nocera <hadess hadess net>
Date:   Tue Jul 18 19:27:57 2017 +0200

    private: Add helper to convert filenames to UTF-8
    
    Archives in general and zip files in particular contain filenames
    in legacy encodings. If a pathname is not in UTF-8 encoding already, try
    to convert it using commonly used encoding in various archive types.
    
    See also https://git.gnome.org//browse/file-roller/tree/src/fr-process.c#n245
    
    https://bugzilla.gnome.org/show_bug.cgi?id=785077

 gnome-autoar/autoar-private.c |   33 +++++++++++++++++++++++++++++++++
 gnome-autoar/autoar-private.h |    1 +
 2 files changed, 34 insertions(+), 0 deletions(-)
---
diff --git a/gnome-autoar/autoar-private.c b/gnome-autoar/autoar-private.c
index 3f740c7..1232ba3 100644
--- a/gnome-autoar/autoar-private.c
+++ b/gnome-autoar/autoar-private.c
@@ -279,3 +279,36 @@ autoar_common_g_file_get_name (GFile *file)
     name = g_file_get_uri (file);
   return name;
 }
+
+/**
+ * autoar_common_get_utf8_pathname:
+ * @pathname: a pathname with an unspecified encoding
+ *
+ * Transforms pathname into a UTF-8 filename from a variety of common
+ * legacy encodings.
+ *
+ * Returns: (transfer full): a UTF-8 filename, or %NULL if the filename
+ * could not be converted or is already in UTF-8. Free the string with
+ * g_free().
+ **/
+G_GNUC_INTERNAL char*
+autoar_common_get_utf8_pathname (const char *pathname)
+{
+  char *utf8_pathname;
+  static const char *try_charsets[] = { "CSPC8CODEPAGE437", "ISO-8859-1", "WINDOWS-1252" };
+  guint i;
+
+  if (g_utf8_validate (pathname, -1, NULL))
+    return NULL;
+  /* If pathname is not in UTF-8 encoding already, try
+   * to convert it using commonly used encoding in various archive types.
+   * See also https://git.gnome.org//browse/file-roller/tree/src/fr-process.c#n245 */
+  for (i = 0; i < G_N_ELEMENTS (try_charsets); i++) {
+    utf8_pathname = g_convert (pathname, -1, "UTF-8",
+                               try_charsets[i], NULL, NULL, NULL);
+    if (utf8_pathname != NULL)
+      break;
+  }
+
+  return utf8_pathname;
+}
diff --git a/gnome-autoar/autoar-private.h b/gnome-autoar/autoar-private.h
index 12bef25..c17c799 100644
--- a/gnome-autoar/autoar-private.h
+++ b/gnome-autoar/autoar-private.h
@@ -55,6 +55,7 @@ GError*   autoar_common_g_error_new_a_entry            (struct archive *a,
                                                         struct archive_entry *entry);
 
 char*     autoar_common_g_file_get_name                (GFile *file);
+char*     autoar_common_get_utf8_pathname              (const char *pathname);
 
 G_END_DECLS
 


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