[tepl] File loading: write common function in FileContent to convert to UTF-8



commit 6e1137e05ff5e5fef58d9d45f382a17bbaa3a852
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sun Oct 22 11:26:36 2017 +0200

    File loading: write common function in FileContent to convert to UTF-8

 tepl/tepl-file-content.c |  101 ++++++++++++++++++++++++---------------------
 tepl/tepl-file-content.h |    8 ++++
 tepl/tepl-file-loader.c  |   51 +++--------------------
 3 files changed, 69 insertions(+), 91 deletions(-)
---
diff --git a/tepl/tepl-file-content.c b/tepl/tepl-file-content.c
index 1717f7f..8b82c8b 100644
--- a/tepl/tepl-file-content.c
+++ b/tepl/tepl-file-content.c
@@ -20,7 +20,6 @@
 #include "tepl-file-content.h"
 #include <uchardet.h>
 #include "tepl-encoding.h"
-#include "tepl-encoding-converter.h"
 
 struct _TeplFileContentPrivate
 {
@@ -167,51 +166,6 @@ determine_encoding_with_uchardet (TeplFileContent *content)
        return encoding;
 }
 
-static gboolean
-can_convert_successfully_with_encoding (TeplFileContent *content,
-                                       TeplEncoding    *from_encoding)
-{
-       TeplEncodingConverter *converter;
-       GList *l;
-       gboolean success = FALSE;
-
-       converter = _tepl_encoding_converter_new (-1);
-
-       if (!_tepl_encoding_converter_open (converter,
-                                           "UTF-8",
-                                           tepl_encoding_get_charset (from_encoding),
-                                           NULL))
-       {
-               goto out;
-       }
-
-       for (l = content->priv->chunks->head; l != NULL; l = l->next)
-       {
-               GBytes *chunk = l->data;
-
-               g_assert (chunk_is_valid (chunk));
-
-               if (!_tepl_encoding_converter_feed (converter,
-                                                   g_bytes_get_data (chunk, NULL),
-                                                   g_bytes_get_size (chunk),
-                                                   NULL))
-               {
-                       goto out;
-               }
-       }
-
-       if (!_tepl_encoding_converter_close (converter, NULL))
-       {
-               goto out;
-       }
-
-       success = TRUE;
-
-out:
-       g_object_unref (converter);
-       return success;
-}
-
 /* Try the candidate encodings one by one, taking the first without conversion
  * error.
  */
@@ -228,7 +182,7 @@ determine_encoding_with_fallback_mode (TeplFileContent *content)
        {
                TeplEncoding *cur_encoding = l->data;
 
-               if (can_convert_successfully_with_encoding (content, cur_encoding))
+               if (_tepl_file_content_convert_to_utf8 (content, cur_encoding, NULL, NULL, NULL))
                {
                        encoding = tepl_encoding_copy (cur_encoding);
                        break;
@@ -259,3 +213,56 @@ _tepl_file_content_determine_encoding (TeplFileContent *content)
 
        return encoding;
 }
+
+gboolean
+_tepl_file_content_convert_to_utf8 (TeplFileContent                 *content,
+                                   TeplEncoding                    *from_encoding,
+                                   TeplEncodingConversionCallback   callback,
+                                   gpointer                         callback_user_data,
+                                   GError                         **error)
+{
+       TeplEncodingConverter *converter;
+       GList *l;
+       gboolean success = FALSE;
+
+       g_return_val_if_fail (TEPL_IS_FILE_CONTENT (content), FALSE);
+       g_return_val_if_fail (from_encoding != NULL, FALSE);
+       g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
+       converter = _tepl_encoding_converter_new (-1);
+       _tepl_encoding_converter_set_callback (converter, callback, callback_user_data);
+
+       if (!_tepl_encoding_converter_open (converter,
+                                           "UTF-8",
+                                           tepl_encoding_get_charset (from_encoding),
+                                           error))
+       {
+               goto out;
+       }
+
+       for (l = content->priv->chunks->head; l != NULL; l = l->next)
+       {
+               GBytes *chunk = l->data;
+
+               g_assert (chunk_is_valid (chunk));
+
+               if (!_tepl_encoding_converter_feed (converter,
+                                                   g_bytes_get_data (chunk, NULL),
+                                                   g_bytes_get_size (chunk),
+                                                   error))
+               {
+                       goto out;
+               }
+       }
+
+       if (!_tepl_encoding_converter_close (converter, error))
+       {
+               goto out;
+       }
+
+       success = TRUE;
+
+out:
+       g_object_unref (converter);
+       return success;
+}
diff --git a/tepl/tepl-file-content.h b/tepl/tepl-file-content.h
index 6cdfc9e..7f1182f 100644
--- a/tepl/tepl-file-content.h
+++ b/tepl/tepl-file-content.h
@@ -22,6 +22,7 @@
 
 #include <glib-object.h>
 #include "tepl-types.h"
+#include "tepl-encoding-converter.h"
 
 G_BEGIN_DECLS
 
@@ -64,6 +65,13 @@ GQueue *             _tepl_file_content_get_chunks           (TeplFileContent *content);
 G_GNUC_INTERNAL
 TeplEncoding *         _tepl_file_content_determine_encoding   (TeplFileContent *content);
 
+G_GNUC_INTERNAL
+gboolean               _tepl_file_content_convert_to_utf8      (TeplFileContent                 *content,
+                                                                TeplEncoding                    
*from_encoding,
+                                                                TeplEncodingConversionCallback   callback,
+                                                                gpointer                         
callback_user_data,
+                                                                GError                         **error);
+
 G_END_DECLS
 
 #endif /* TEPL_FILE_CONTENT_H */
diff --git a/tepl/tepl-file-loader.c b/tepl/tepl-file-loader.c
index 3f7bd1e..4d00b80 100644
--- a/tepl/tepl-file-loader.c
+++ b/tepl/tepl-file-loader.c
@@ -793,10 +793,7 @@ convert_and_insert_content (GTask *task)
        TeplFileLoader *loader;
        TeplFileLoaderPrivate *priv;
        TaskData *task_data;
-       TeplEncodingConverter *converter = NULL;
        TeplFileContent *content;
-       GQueue *chunks;
-       GList *l;
        GError *error = NULL;
 
        loader = g_task_get_source_object (task);
@@ -810,50 +807,19 @@ convert_and_insert_content (GTask *task)
                return;
        }
 
-       converter = _tepl_encoding_converter_new (ENCODING_CONVERTER_BUFFER_SIZE);
-
-       _tepl_encoding_converter_set_callback (converter,
-                                              content_converted_cb,
-                                              task);
-
-       g_assert (priv->detected_encoding != NULL);
-       _tepl_encoding_converter_open (converter,
-                                      "UTF-8",
-                                      tepl_encoding_get_charset (priv->detected_encoding),
-                                      &error);
-       if (error != NULL)
-       {
-               g_task_return_error (task, error);
-               goto out;
-       }
-
        content = _tepl_file_content_loader_get_content (task_data->content_loader);
-       chunks = _tepl_file_content_get_chunks (content);
-
-       for (l = chunks->head; l != NULL; l = l->next)
-       {
-               GBytes *chunk = l->data;
-
-               g_assert (chunk != NULL);
-               g_assert (g_bytes_get_size (chunk) > 0);
-
-               _tepl_encoding_converter_feed (converter,
-                                              g_bytes_get_data (chunk, NULL),
-                                              g_bytes_get_size (chunk),
-                                              &error);
 
-               if (error != NULL)
-               {
-                       g_task_return_error (task, error);
-                       goto out;
-               }
-       }
+       g_assert (priv->detected_encoding != NULL);
+       _tepl_file_content_convert_to_utf8 (content,
+                                           priv->detected_encoding,
+                                           content_converted_cb,
+                                           task,
+                                           &error);
 
-       _tepl_encoding_converter_close (converter, &error);
        if (error != NULL)
        {
                g_task_return_error (task, error);
-               goto out;
+               return;
        }
 
        if (task_data->insert_carriage_return)
@@ -869,9 +835,6 @@ convert_and_insert_content (GTask *task)
        remove_trailing_newline_if_needed (loader);
 
        g_task_return_boolean (task, TRUE);
-
-out:
-       g_clear_object (&converter);
 }
 
 static void


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