From cba5db3899a97a827649f0071ecc1382a931c099 Mon Sep 17 00:00:00 2001 From: hiro Date: Fri, 17 Nov 2017 01:06:17 +0000 Subject: refactored UTF-16 support of compose. git-svn-id: svn://sylpheed.sraoss.jp/sylpheed/trunk@3575 ee746299-78ed-0310-b773-934348b2243d --- ChangeLog | 6 ++++++ libsylph/codeconv.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/compose.c | 22 +++------------------- 3 files changed, 54 insertions(+), 19 deletions(-) diff --git a/ChangeLog b/ChangeLog index 0b14a1a8..5ee8c5fc 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2017-11-17 + + * src/compose.c: refactored UTF-16 support. + * libsylph/codeconv.c: conv_copy_file(): supported + UTF-16/UTF-16BE/UTF-16LE. + 2017-11-16 * src/compose.c: compose_insert_file(): supported insertion of diff --git a/libsylph/codeconv.c b/libsylph/codeconv.c index 1ba52ac3..ee386591 100644 --- a/libsylph/codeconv.c +++ b/libsylph/codeconv.c @@ -2566,12 +2566,57 @@ gchar *conv_encode_filename(const gchar *src, const gchar *param_name, return g_string_free(string, FALSE); } +static gint conv_copy_file_with_gconvert(const gchar *src, const gchar *dest, + const gchar *encoding) +{ + gchar *src_s = NULL; + gsize len = 0, dlen = 0; + gchar *dest_s; + GError *error; + + g_return_val_if_fail(src != NULL, -1); + g_return_val_if_fail(dest != NULL, -1); + g_return_val_if_fail(encoding != NULL, -1); + + if (g_file_get_contents(src, &src_s, &len, &error) == FALSE) { + g_warning("conv_copy_utf16_file(): %s: %s", src, error->message); + g_error_free(error); + return -1; + } + + dest_s = g_convert(src_s, len, CS_UTF_8, encoding, NULL, &dlen, &error); + if (!dest_s) { + g_warning("conv_copy_utf16_file(): %s: %s", src, error->message); + g_error_free(error); + g_free(src_s); + return -1; + } + + if (g_file_set_contents(dest, dest_s, dlen, &error) == FALSE) { + g_warning("conv_copy_utf16_file(): %s: %s", dest, error->message); + g_error_free(error); + g_free(dest_s); + g_free(src_s); + return -1; + } + + g_free(dest_s); + g_free(src_s); + return 0; +} + gint conv_copy_file(const gchar *src, const gchar *dest, const gchar *encoding) { FILE *src_fp, *dest_fp; gchar buf[BUFFSIZE]; CodeConverter *conv; gboolean err = FALSE; + CharSet charset; + + charset = conv_get_charset_from_str(encoding); + if (charset == C_UTF_16 || charset == C_UTF_16BE || charset == C_UTF_16LE) { + return conv_copy_file_with_gconvert(src, dest, encoding); + } if ((src_fp = g_fopen(src, "rb")) == NULL) { FILE_OP_ERROR(src, "fopen"); diff --git a/src/compose.c b/src/compose.c index ea4d145c..0e5a0327 100644 --- a/src/compose.c +++ b/src/compose.c @@ -2217,22 +2217,13 @@ static void compose_insert_file(Compose *compose, const gchar *file, g_return_if_fail(file != NULL); enc = conv_check_file_encoding(file); - if (enc == C_UTF_16 || enc == C_UTF_16BE || enc == C_UTF_16LE) { - gchar *src = NULL; - gsize len = 0, dlen = 0; - gchar *dest; - - g_file_get_contents(file, &src, &len, NULL); - dest = g_convert(src, len, CS_UTF_8, conv_get_charset_str(enc), NULL, &dlen, NULL); tmp_file = get_tmp_file(); - if (g_file_set_contents(tmp_file, dest, dlen, NULL) == FALSE) { + if (conv_copy_file(file, tmp_file, conv_get_charset_str(enc)) < 0) { g_warning("compose_insert_file: Cannot convert UTF-16 file %s to UTF-8\n", file); g_free(tmp_file); tmp_file = NULL; } - g_free(dest); - g_free(src); } if (tmp_file) { @@ -4671,22 +4662,15 @@ static gint compose_write_attach(Compose *compose, FILE *fp, if (content_type == MIME_TEXT || content_type == MIME_TEXT_HTML) { CharSet enc; - gchar *src = NULL; - gsize len = 0, dlen = 0; - gchar *dest; enc = conv_check_file_encoding(ainfo->file); if (enc == C_UTF_16 || enc == C_UTF_16BE || enc == C_UTF_16LE) { - g_file_get_contents(ainfo->file, &src, &len, NULL); - dest = g_convert(src, len, CS_UTF_8, conv_get_charset_str(enc), NULL, &dlen, NULL); tmp_file = get_tmp_file(); - if (g_file_set_contents(tmp_file, dest, dlen, NULL) == FALSE) { - g_warning("Cannot convert UTF-16 file %s to UTF-8\n", ainfo->file); + if (conv_copy_file(ainfo->file, tmp_file, conv_get_charset_str(enc)) < 0) { + g_warning("compose_write_attach: Cannot convert UTF-16 file %s to UTF-8", ainfo->file); g_free(tmp_file); tmp_file = NULL; } - g_free(dest); - g_free(src); } } -- cgit v1.2.3