2005-03-04 [paul] 1.0.1cvs21
[claws.git] / src / codeconv.c
index d5517a5031e4fc83864e5676c9a37862cd71da52..d177f743c3d65aae811451ff1d7b8607a658b808 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2004 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2005 Hiroyuki Yamamoto
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -246,7 +246,7 @@ void conv_euctojis(gchar *outbuf, gint outlen, const gchar *inbuf)
        JISState state = JIS_ASCII;
 
        while (*in != '\0') {
-               if (isascii(*in)) {
+               if (IS_ASCII(*in)) {
                        K_OUT();
                        *out++ = *in++;
                } else if (iseuckanji(*in)) {
@@ -258,41 +258,43 @@ void conv_euctojis(gchar *outbuf, gint outlen, const gchar *inbuf)
                                K_OUT();
                                *out++ = SUBST_CHAR;
                                in++;
-                               if (*in != '\0' && !isascii(*in)) {
+                               if (*in != '\0' && !IS_ASCII(*in)) {
                                        *out++ = SUBST_CHAR;
                                        in++;
                                }
                        }
                } else if (iseuchwkana1(*in)) {
                        if (iseuchwkana2(*(in + 1))) {
-#ifdef CONV_ALLOW_HWKANA
-                               HW_IN();
-                               in++;
-                               *out++ = *in++ & 0x7f;
-#else
-                               guchar jis_ch[2];
-                               gint len;
-
-                               if (iseuchwkana1(*(in + 2)) &&
-                                   iseuchwkana2(*(in + 3)))
-                                       len = conv_jis_hantozen
-                                               (jis_ch, *(in + 1), *(in + 3));
-                               else
-                                       len = conv_jis_hantozen
-                                               (jis_ch, *(in + 1), '\0');
-                               if (len == 0)
-                                       in += 2;
-                               else {
-                                       K_IN();
-                                       in += len * 2;
-                                       *out++ = jis_ch[0];
-                                       *out++ = jis_ch[1];
+                               if (prefs_common.allow_jisx0201_kana) {
+                                       HW_IN();
+                                       in++;
+                                       *out++ = *in++ & 0x7f;
+                               } else {
+                                       guchar jis_ch[2];
+                                       gint len;
+
+                                       if (iseuchwkana1(*(in + 2)) &&
+                                           iseuchwkana2(*(in + 3)))
+                                               len = conv_jis_hantozen
+                                                       (jis_ch,
+                                                        *(in + 1), *(in + 3));
+                                       else
+                                               len = conv_jis_hantozen
+                                                       (jis_ch,
+                                                        *(in + 1), '\0');
+                                       if (len == 0)
+                                               in += 2;
+                                       else {
+                                               K_IN();
+                                               in += len * 2;
+                                               *out++ = jis_ch[0];
+                                               *out++ = jis_ch[1];
+                                       }
                                }
-#endif
                        } else {
                                K_OUT();
                                in++;
-                               if (*in != '\0' && !isascii(*in)) {
+                               if (*in != '\0' && !IS_ASCII(*in)) {
                                        *out++ = SUBST_CHAR;
                                        in++;
                                }
@@ -305,10 +307,10 @@ void conv_euctojis(gchar *outbuf, gint outlen, const gchar *inbuf)
                                *out++ = *in++ & 0x7f;
                        } else {
                                K_OUT();
-                               if (*in != '\0' && !isascii(*in)) {
+                               if (*in != '\0' && !IS_ASCII(*in)) {
                                        *out++ = SUBST_CHAR;
                                        in++;
-                                       if (*in != '\0' && !isascii(*in)) {
+                                       if (*in != '\0' && !IS_ASCII(*in)) {
                                                *out++ = SUBST_CHAR;
                                                in++;
                                        }
@@ -331,7 +333,7 @@ void conv_sjistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
        guchar *out = outbuf;
 
        while (*in != '\0') {
-               if (isascii(*in)) {
+               if (IS_ASCII(*in)) {
                        *out++ = *in++;
                } else if (issjiskanji1(*in)) {
                        if (issjiskanji2(*(in + 1))) {
@@ -354,7 +356,7 @@ void conv_sjistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
                        } else {
                                *out++ = SUBST_CHAR;
                                in++;
-                               if (*in != '\0' && !isascii(*in)) {
+                               if (*in != '\0' && !IS_ASCII(*in)) {
                                        *out++ = SUBST_CHAR;
                                        in++;
                                }
@@ -485,7 +487,7 @@ void conv_unreadable_eucjp(gchar *str)
        register guchar *p = str;
 
        while (*p != '\0') {
-               if (isascii(*p)) {
+               if (IS_ASCII(*p)) {
                        /* convert CR+LF -> LF */
                        if (*p == '\r' && *(p + 1) == '\n')
                                memmove(p, p + 1, strlen(p));
@@ -499,7 +501,7 @@ void conv_unreadable_eucjp(gchar *str)
                                /* substitute unprintable code */
                                *p++ = SUBST_CHAR;
                                if (*p != '\0') {
-                                       if (isascii(*p))
+                                       if (IS_ASCII(*p))
                                                p++;
                                        else
                                                *p++ = SUBST_CHAR;
@@ -531,7 +533,7 @@ void conv_unreadable_8bit(gchar *str)
                /* convert CR+LF -> LF */
                if (*p == '\r' && *(p + 1) == '\n')
                        memmove(p, p + 1, strlen(p));
-               else if (!isascii(*p)) *p = SUBST_CHAR;
+               else if (!IS_ASCII(*p)) *p = SUBST_CHAR;
                p++;
        }
 }
@@ -654,7 +656,7 @@ CharSet conv_guess_ja_encoding(const gchar *str)
                        if (guessed == C_US_ASCII)
                                return C_ISO_2022_JP;
                        p += 2;
-               } else if (isascii(*p)) {
+               } else if (IS_ASCII(*p)) {
                        p++;
                } else if (iseuckanji(*p) && iseuckanji(*(p + 1))) {
                        if (*p >= 0xfd && *p <= 0xfe)
@@ -831,6 +833,7 @@ CodeConvFunc conv_get_code_conv_func(const gchar *src_charset_str,
        switch (src_charset) {
        case C_ISO_2022_JP:
        case C_ISO_2022_JP_2:
+       case C_ISO_2022_JP_3:
                if (dest_charset == C_AUTO &&
                    conv_get_current_charset() == C_EUC_JP)
                        code_conv = conv_jistodisp;
@@ -871,8 +874,9 @@ CodeConvFunc conv_get_code_conv_func(const gchar *src_charset_str,
                if (dest_charset == C_AUTO &&
                    conv_get_current_charset() == C_EUC_JP)
                        code_conv = conv_euctodisp;
-               else if (dest_charset == C_ISO_2022_JP ||
-                        dest_charset == C_ISO_2022_JP_2)
+               else if (dest_charset == C_ISO_2022_JP   ||
+                        dest_charset == C_ISO_2022_JP_2 ||
+                        dest_charset == C_ISO_2022_JP_3)
                        code_conv = conv_euctojis;
                break;
        default:
@@ -903,11 +907,11 @@ gchar *conv_iconv_strdup(const gchar *inbuf,
                dest_code = conv_get_current_charset_str();
 
        /* don't convert if current codeset is US-ASCII */
-       if (!strcasecmp(dest_code, CS_US_ASCII))
+       if (!g_strcasecmp(dest_code, CS_US_ASCII))
                return g_strdup(inbuf);
 
        /* don't convert if src and dest codeset are identical */
-       if (!strcasecmp(src_code, dest_code))
+       if (!g_strcasecmp(src_code, dest_code))
                return g_strdup(inbuf);
 
        cd = iconv_open(dest_code, src_code);
@@ -1021,6 +1025,7 @@ static const struct {
        {C_KOI8_U,              CS_KOI8_U},
        {C_ISO_2022_JP,         CS_ISO_2022_JP},
        {C_ISO_2022_JP_2,       CS_ISO_2022_JP_2},
+       {C_ISO_2022_JP_3,       CS_ISO_2022_JP_3},
        {C_EUC_JP,              CS_EUC_JP},
        {C_EUC_JP,              CS_EUCJP},
        {C_SHIFT_JIS,           CS_SHIFT_JIS},
@@ -1070,6 +1075,7 @@ static const struct {
        {"ru_RU"        , C_ISO_8859_5  , C_KOI8_R},
        {"tg_TJ"        , C_KOI8_T      , C_KOI8_T},
        {"ru_UA"        , C_KOI8_U      , C_KOI8_U},
+       {"uk_UA.CP1251" , C_WINDOWS_1251, C_KOI8_U},
        {"uk_UA"        , C_KOI8_U      , C_KOI8_U},
 
        {"be_BY"        , C_WINDOWS_1251, C_WINDOWS_1251},
@@ -1309,14 +1315,14 @@ CharSet conv_get_current_charset(void)
 
                /* "ja_JP.EUC" matches with "ja_JP.eucJP", "ja_JP.EUC" and
                   "ja_JP". "ja_JP" matches with "ja_JP.xxxx" and "ja" */
-               if (!strncasecmp(cur_locale, locale_table[i].locale,
+               if (!g_strncasecmp(cur_locale, locale_table[i].locale,
                                 strlen(locale_table[i].locale))) {
                        cur_charset = locale_table[i].charset;
                        return cur_charset;
                } else if ((p = strchr(locale_table[i].locale, '_')) &&
                         !strchr(p + 1, '.')) {
                        if (strlen(cur_locale) == 2 &&
-                           !strncasecmp(cur_locale, locale_table[i].locale, 2)) {
+                           !g_strncasecmp(cur_locale, locale_table[i].locale, 2)) {
                                cur_charset = locale_table[i].charset;
                                return cur_charset;
                        }
@@ -1361,14 +1367,14 @@ CharSet conv_get_outgoing_charset(void)
        for (i = 0; i < sizeof(locale_table) / sizeof(locale_table[0]); i++) {
                const gchar *p;
 
-               if (!strncasecmp(cur_locale, locale_table[i].locale,
+               if (!g_strncasecmp(cur_locale, locale_table[i].locale,
                                 strlen(locale_table[i].locale))) {
                        out_charset = locale_table[i].out_charset;
                        break;
                } else if ((p = strchr(locale_table[i].locale, '_')) &&
                         !strchr(p + 1, '.')) {
                        if (strlen(cur_locale) == 2 &&
-                           !strncasecmp(cur_locale, locale_table[i].locale, 2)) {
+                           !g_strncasecmp(cur_locale, locale_table[i].locale, 2)) {
                                out_charset = locale_table[i].out_charset;
                                break;
                        }
@@ -1415,6 +1421,7 @@ gboolean conv_is_multibyte_encoding(CharSet encoding)
        case C_EUC_CN:
        case C_ISO_2022_JP:
        case C_ISO_2022_JP_2:
+       case C_ISO_2022_JP_3:
        case C_ISO_2022_KR:
        case C_ISO_2022_CN:
        case C_SHIFT_JIS:
@@ -1433,9 +1440,12 @@ const gchar *conv_get_current_locale(void)
        gchar *cur_locale;
 
        cur_locale = g_getenv("LC_ALL");
-       if (!cur_locale) cur_locale = g_getenv("LC_CTYPE");
-       if (!cur_locale) cur_locale = g_getenv("LANG");
-       if (!cur_locale) cur_locale = setlocale(LC_CTYPE, NULL);
+       if (!cur_locale || !strlen(cur_locale)) 
+               cur_locale = g_getenv("LC_CTYPE");
+       if (!cur_locale || !strlen(cur_locale)) 
+               cur_locale = g_getenv("LANG");
+       if (!cur_locale || !strlen(cur_locale)) 
+               cur_locale = setlocale(LC_CTYPE, NULL);
 
        debug_print("current locale: %s\n",
                    cur_locale ? cur_locale : "(none)");
@@ -1443,27 +1453,6 @@ const gchar *conv_get_current_locale(void)
        return cur_locale;
 }
 
-void conv_unmime_header_overwrite(gchar *str)
-{
-       gchar *buf;
-       gint buflen;
-       CharSet cur_charset;
-
-       cur_charset = conv_get_current_charset();
-
-       if (cur_charset == C_EUC_JP) {
-               buflen = strlen(str) * 2 + 1;
-               Xalloca(buf, buflen, return);
-               conv_anytodisp(buf, buflen, str);
-               unmime_header(str, buf);
-       } else {
-               buflen = strlen(str) + 1;
-               Xalloca(buf, buflen, return);
-               unmime_header(buf, str);
-               strncpy2(str, buf, buflen);
-       }
-}
-
 void conv_unmime_header(gchar *outbuf, gint outlen, const gchar *str,
                        const gchar *charset)
 {
@@ -1488,8 +1477,6 @@ void conv_unmime_header(gchar *outbuf, gint outlen, const gchar *str,
 #define MIMESEP_BEGIN          "=?"
 #define MIMESEP_END            "?="
 
-#define B64LEN(len)    ((len) / 3 * 4 + ((len) % 3 ? 4 : 0))
-
 #define LBREAK_IF_REQUIRED(cond, is_plain_text)                                \
 {                                                                      \
        if (len - (destp - (guchar *)dest) < MAX_LINELEN + 2) {         \