aboutsummaryrefslogtreecommitdiff
path: root/libsylph/codeconv.c
diff options
context:
space:
mode:
authorhiro <hiro@ee746299-78ed-0310-b773-934348b2243d>2006-08-09 02:49:40 +0000
committerhiro <hiro@ee746299-78ed-0310-b773-934348b2243d>2006-08-09 02:49:40 +0000
commit7a15ea11b11c309884e208849fcd6597a4c12b5a (patch)
treededb62ea28c82654e05a4aa7901c1bebad1da9da /libsylph/codeconv.c
parentb536a7965390e03e8635c21d6c2d8d3d7efa8f04 (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
Diffstat (limited to 'libsylph/codeconv.c')
-rw-r--r--libsylph/codeconv.c50
1 files changed, 50 insertions, 0 deletions
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;