diff options
author | hiro <hiro@ee746299-78ed-0310-b773-934348b2243d> | 2006-08-09 02:49:40 +0000 |
---|---|---|
committer | hiro <hiro@ee746299-78ed-0310-b773-934348b2243d> | 2006-08-09 02:49:40 +0000 |
commit | 7a15ea11b11c309884e208849fcd6597a4c12b5a (patch) | |
tree | dedb62ea28c82654e05a4aa7901c1bebad1da9da | |
parent | b536a7965390e03e8635c21d6c2d8d3d7efa8f04 (diff) |
when inserting file, check whole file to see if it is UTF-8.
git-svn-id: svn://sylpheed.sraoss.jp/sylpheed/trunk@1121 ee746299-78ed-0310-b773-934348b2243d
-rw-r--r-- | ChangeLog | 7 | ||||
-rw-r--r-- | libsylph/codeconv.c | 50 | ||||
-rw-r--r-- | libsylph/codeconv.h | 2 | ||||
-rwxr-xr-x | makewin32.sh | 2 | ||||
-rw-r--r-- | src/compose.c | 21 |
5 files changed, 74 insertions, 8 deletions
@@ -1,3 +1,10 @@ +2006-08-09 + + * libsylph/codeconv.[ch]: conv_check_file_encoding(): check the + encoding of text file if it is locale encoding or UTF-8,.. + * src/compose.c: compose_insert_file(): check whole file to see if it + is UTF-8. + 2006-08-07 * configure.in: use onig-config. diff --git a/libsylph/codeconv.c b/libsylph/codeconv.c index d7c726f0..981ca7f2 100644 --- a/libsylph/codeconv.c +++ b/libsylph/codeconv.c @@ -2467,6 +2467,56 @@ gint conv_copy_dir(const gchar *src, const gchar *dest, const gchar *encoding) return 0; } +CharSet conv_check_file_encoding(const gchar *file) +{ + FILE *fp; + gchar buf[BUFFSIZE]; + CharSet enc; + const gchar *enc_str; + gboolean is_locale = TRUE, is_utf8 = TRUE; + + g_return_val_if_fail(file != NULL, C_AUTO); + + enc = conv_get_locale_charset(); + enc_str = conv_get_locale_charset_str(); + if (enc == C_UTF_8) + is_locale = FALSE; + + if ((fp = g_fopen(file, "rb")) == NULL) { + FILE_OP_ERROR(file, "fopen"); + return C_AUTO; + } + + while (fgets(buf, sizeof(buf), fp) != NULL) { + gchar *str; + gint error = 0; + + if (is_locale) { + str = conv_codeset_strdup_full(buf, enc_str, + CS_INTERNAL, &error); + if (!str || error != 0) + is_locale = FALSE; + g_free(str); + } + + if (is_utf8 && g_utf8_validate(buf, -1, NULL) == FALSE) { + is_utf8 = FALSE; + } + + if (!is_locale && !is_utf8) + break; + } + + fclose(fp); + + if (is_locale) + return enc; + else if (is_utf8) + return C_UTF_8; + else + return C_AUTO; +} + gchar *conv_filename_from_utf8(const gchar *utf8_file) { gchar *fs_file; diff --git a/libsylph/codeconv.h b/libsylph/codeconv.h index 700e83b0..4e79389f 100644 --- a/libsylph/codeconv.h +++ b/libsylph/codeconv.h @@ -241,6 +241,8 @@ gint conv_copy_dir (const gchar *src, const gchar *dest, const gchar *src_encoding); +CharSet conv_check_file_encoding (const gchar *file); + gchar *conv_filename_from_utf8 (const gchar *utf8_file); gchar *conv_filename_to_utf8 (const gchar *fs_file); diff --git a/makewin32.sh b/makewin32.sh index 047b73a1..154a1f21 100755 --- a/makewin32.sh +++ b/makewin32.sh @@ -1,6 +1,6 @@ #!/bin/sh -./configure --with-localedir=share/locale --disable-ipv6 \ +./configure --with-localedir=share/locale --enable-oniguruma --disable-ipv6 \ 'CC=gcc -mtune=pentium3' CFLAGS=-O3 \ && make \ && make install-strip prefix=$HOME/dist diff --git a/src/compose.c b/src/compose.c index ee237223..c16df187 100644 --- a/src/compose.c +++ b/src/compose.c @@ -1875,9 +1875,12 @@ static void compose_insert_file(Compose *compose, const gchar *file, gint len; FILE *fp; gboolean prev_autowrap; + CharSet enc; g_return_if_fail(file != NULL); + enc = conv_check_file_encoding(file); + if ((fp = g_fopen(file, "rb")) == NULL) { FILE_OP_ERROR(file, "fopen"); return; @@ -1896,15 +1899,19 @@ static void compose_insert_file(Compose *compose, const gchar *file, gchar *str; gint error = 0; - str = conv_codeset_strdup_full(buf, cur_encoding, CS_INTERNAL, - &error); - if (!str || error != 0) { - if (g_utf8_validate(buf, -1, NULL) == TRUE) { - g_free(str); - str = g_strdup(buf); + if (enc == C_UTF_8) { + str = g_strdup(buf); + } else { + str = conv_codeset_strdup_full(buf, cur_encoding, + CS_INTERNAL, &error); + if (!str || error != 0) { + if (g_utf8_validate(buf, -1, NULL) == TRUE) { + g_free(str); + str = g_strdup(buf); + } } + if (!str) continue; } - if (!str) continue; /* strip <CR> if DOS/Windows file, replace <CR> with <LF> if Macintosh file. */ |