# HG changeset patch # User Timo Sirainen # Date 1037195279 -7200 # Node ID 5470c0cb13a7c52877e8de2af64a769b95c59116 # Parent debb8468514e0c03ee53caa831e3a72d01807524 We can support UTF-8 charset too without any translations. diff -r debb8468514e -r 5470c0cb13a7 src/lib-charset/Makefile.am --- a/src/lib-charset/Makefile.am Wed Nov 13 13:08:18 2002 +0200 +++ b/src/lib-charset/Makefile.am Wed Nov 13 15:47:59 2002 +0200 @@ -4,8 +4,8 @@ -I$(top_srcdir)/src/lib libcharset_a_SOURCES = \ - charset-ascii.c \ - charset-iconv.c + charset-iconv.c \ + charset-utf8.c noinst_HEADERS = \ charset-utf8.h diff -r debb8468514e -r 5470c0cb13a7 src/lib-charset/charset-ascii.c --- a/src/lib-charset/charset-ascii.c Wed Nov 13 13:08:18 2002 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,71 +0,0 @@ -/* Copyright (C) 2002 Timo Sirainen */ - -#include "lib.h" -#include "charset-utf8.h" - -#ifndef HAVE_ICONV_H - -#include - -struct _CharsetTranslation { - int dummy; -}; - -static CharsetTranslation ascii_translation; - -CharsetTranslation *charset_to_utf8_begin(const char *charset, - int *unknown_charset) -{ - if (unknown_charset != NULL) - *unknown_charset = FALSE; - - if (strcasecmp(charset, "us-ascii") != 0 && - strcasecmp(charset, "ascii") != 0) { - /* no support for non-ascii charsets */ - if (unknown_charset != NULL) - *unknown_charset = TRUE; - return NULL; - } - - return &ascii_translation; -} - -void charset_to_utf8_end(CharsetTranslation *t __attr_unused__) -{ -} - -void charset_to_utf8_reset(CharsetTranslation *t __attr_unused__) -{ -} - -int charset_to_ucase_utf8(CharsetTranslation *t __attr_unused__, - const unsigned char **inbuf, size_t *insize, - unsigned char *outbuf, size_t *outsize) -{ - size_t max_size, i; - - max_size = I_MIN(*insize, *outsize); - for (i = 0; i < max_size; i++) - outbuf[i] = i_toupper((*inbuf)[i]); - - *insize = 0; - *outsize = max_size; - - return TRUE; -} - -const char * -charset_to_ucase_utf8_string(const char *charset, int *unknown_charset, - const unsigned char *buf, - size_t *size __attr_unused__) -{ - if (charset == NULL || strcasecmp(charset, "us-ascii") == 0 || - strcasecmp(charset, "ascii") == 0) - return str_ucase(t_strdup_noconst(buf)); - - if (unknown_charset != NULL) - *unknown_charset = TRUE; - return NULL; -} - -#endif diff -r debb8468514e -r 5470c0cb13a7 src/lib-charset/charset-iconv.c --- a/src/lib-charset/charset-iconv.c Wed Nov 13 13:08:18 2002 +0200 +++ b/src/lib-charset/charset-iconv.c Wed Nov 13 15:47:59 2002 +0200 @@ -10,6 +10,7 @@ struct _CharsetTranslation { iconv_t cd; + int ascii; }; CharsetTranslation *charset_to_utf8_begin(const char *charset, @@ -17,16 +18,22 @@ { CharsetTranslation *t; iconv_t cd; + int ascii; if (unknown_charset != NULL) *unknown_charset = FALSE; if (strcasecmp(charset, "us-ascii") == 0 || strcasecmp(charset, "ascii") == 0) { - /* no need to do any actual translation */ + cd = NULL; + ascii = TRUE; + } else if (strcasecmp(charset, "UTF-8") == 0 || + strcasecmp(charset, "UTF8") == 0) { cd = NULL; + ascii = FALSE; } else { - cd = iconv_open("UTF8", charset); + ascii = FALSE; + cd = iconv_open("UTF-8", charset); if (cd == (iconv_t)-1) { if (unknown_charset != NULL) *unknown_charset = TRUE; @@ -36,6 +43,7 @@ t = i_new(CharsetTranslation, 1); t->cd = cd; + t->ascii = ascii; return t; } @@ -60,10 +68,10 @@ size_t outleft, max_size, i; if (t->cd == NULL) { - /* ascii - just copy it to outbuf uppercased */ + /* no translation needed - just copy it to outbuf uppercased */ max_size = I_MIN(*insize, *outsize); for (i = 0; i < max_size; i++) - outbuf[i] = i_toupper((*inbuf)[i]); + outbuf[i] = i_toupper((*inbuf)[i]); /* FIXME: utf8 */ *insize = 0; *outsize = max_size; return TRUE; @@ -86,7 +94,7 @@ max_size = *outsize; for (i = 0; i < max_size; i++) - outbuf[i] = i_toupper(outbuf[i]); + outbuf[i] = i_toupper(outbuf[i]); /* FIXME: utf8 */ return TRUE; } @@ -103,7 +111,7 @@ strcasecmp(charset, "ascii") == 0) return str_ucase(t_strdup_noconst(buf)); - cd = iconv_open("UTF8", charset); + cd = iconv_open("UTF-8", charset); if (cd == (iconv_t)-1) { if (unknown_charset != NULL) *unknown_charset = TRUE; @@ -139,8 +147,7 @@ *outpos++ = '\0'; t_buffer_alloc(*size + 1); - /* FIXME: this works only for ASCII */ - str_ucase(outbuf); + str_ucase(outbuf); /* FIXME: utf8 */ iconv_close(cd); return outbuf; diff -r debb8468514e -r 5470c0cb13a7 src/lib-charset/charset-utf8.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lib-charset/charset-utf8.c Wed Nov 13 15:47:59 2002 +0200 @@ -0,0 +1,76 @@ +/* Copyright (C) 2002 Timo Sirainen */ + +#include "lib.h" +#include "charset-utf8.h" + +#ifndef HAVE_ICONV_H + +#include + +struct _CharsetTranslation { + int dummy; +}; + +static CharsetTranslation ascii_translation, utf8_translation; + +CharsetTranslation *charset_to_utf8_begin(const char *charset, + int *unknown_charset) +{ + if (unknown_charset != NULL) + *unknown_charset = FALSE; + + if (strcasecmp(charset, "us-ascii") == 0 || + strcasecmp(charset, "ascii") == 0) + return &ascii_translation; + + if (strcasecmp(charset, "UTF-8") == 0 || + strcasecmp(charset, "UTF8") == 0) + return &utf8_translation; + + /* no support for charsets that need translation */ + if (unknown_charset != NULL) + *unknown_charset = TRUE; + return NULL; +} + +void charset_to_utf8_end(CharsetTranslation *t __attr_unused__) +{ +} + +void charset_to_utf8_reset(CharsetTranslation *t __attr_unused__) +{ +} + +int charset_to_ucase_utf8(CharsetTranslation *t __attr_unused__, + const unsigned char **inbuf, size_t *insize, + unsigned char *outbuf, size_t *outsize) +{ + size_t max_size, i; + + max_size = I_MIN(*insize, *outsize); + for (i = 0; i < max_size; i++) + outbuf[i] = i_toupper((*inbuf)[i]); /* FIXME: utf8 */ + + *insize = 0; + *outsize = max_size; + + return TRUE; +} + +const char * +charset_to_ucase_utf8_string(const char *charset, int *unknown_charset, + const unsigned char *buf, + size_t *size __attr_unused__) +{ + if (charset == NULL || strcasecmp(charset, "us-ascii") == 0 || + strcasecmp(charset, "ascii") == 0 || + strcasecmp(charset, "UTF-8") == 0 || + strcasecmp(charset, "UTF8") == 0) + return str_ucase(t_strdup_noconst(buf)); /* FIXME: utf8 */ + + if (unknown_charset != NULL) + *unknown_charset = TRUE; + return NULL; +} + +#endif