Annotation of win32/gnome/gmime-x.x.x/gmime-iconv-utils.c, revision 1.1

1.1     ! paf         1: /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
        !             2: /*
        !             3:  *  Authors: Jeffrey Stedfast <fejj@ximian.com>
        !             4:  *
        !             5:  *  Copyright 2002 Ximian, Inc. (www.ximian.com)
        !             6:  *
        !             7:  *  This program is free software; you can redistribute it and/or modify
        !             8:  *  it under the terms of the GNU General Public License as published by
        !             9:  *  the Free Software Foundation; either version 2 of the License, or
        !            10:  *  (at your option) any later version.
        !            11:  *
        !            12:  *  This program is distributed in the hope that it will be useful,
        !            13:  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
        !            14:  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        !            15:  *  GNU General Public License for more details.
        !            16:  *
        !            17:  *  You should have received a copy of the GNU General Public License
        !            18:  *  along with this program; if not, write to the Free Software
        !            19:  *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
        !            20:  *
        !            21:  */
        !            22: 
        !            23: #ifdef HAVE_CONFIG_H
        !            24: #include <config.h>
        !            25: #endif
        !            26: 
        !            27: #include <glib.h>
        !            28: #include <stdio.h>
        !            29: #include <string.h>
        !            30: #include <errno.h>
        !            31: #include <iconv.h>
        !            32: 
        !            33: #include "gmime-iconv-utils.h"
        !            34: #include "gmime-charset.h"
        !            35: 
        !            36: 
        !            37: static gboolean initialized = FALSE;
        !            38: 
        !            39: static iconv_t utf8_to_locale;
        !            40: static iconv_t locale_to_utf8;
        !            41: 
        !            42: 
        !            43: static void
        !            44: iconv_utils_init (void)
        !            45: {
        !            46:        const char *utf8, *locale;
        !            47:        
        !            48:        g_mime_charset_init ();
        !            49:        
        !            50:        utf8 = g_mime_charset_name ("utf-8");
        !            51:        locale = g_mime_charset_name (g_mime_charset_locale_name ());
        !            52:        
        !            53:        utf8_to_locale = iconv_open (locale, utf8);
        !            54:        locale_to_utf8 = iconv_open (utf8, locale);
        !            55:        
        !            56:        initialized = TRUE;
        !            57: }
        !            58: 
        !            59: 
        !            60: /**
        !            61:  * g_mime_iconv_strndup:
        !            62:  * @cd: conversion descriptor
        !            63:  * @string: string in source charset
        !            64:  * @n: number of bytes to convert
        !            65:  *
        !            66:  * Allocates a new string buffer containing the first @n bytes of
        !            67:  * @string converted to the destination charset as described by the
        !            68:  * conversion descriptor @cd.
        !            69:  *
        !            70:  * Returns a new string buffer containing the first @n bytes of
        !            71:  * @string converted to the destination charset as described by the
        !            72:  * conversion descriptor @cd.
        !            73:  **/
        !            74: char *
        !            75: g_mime_iconv_strndup (iconv_t cd, const char *string, size_t n)
        !            76: {
        !            77:        size_t inleft, outleft, converted = 0;
        !            78:        char *out, *outbuf;
        !            79:        const char *inbuf;
        !            80:        size_t outlen;
        !            81:        
        !            82:        if (cd == (iconv_t) -1)
        !            83:                return g_strndup (string, n);
        !            84:        
        !            85:        outlen = n * 2 + 16;
        !            86:        out = g_malloc (outlen + 1);
        !            87:        
        !            88:        inbuf = string;
        !            89:        inleft = n;
        !            90:        
        !            91:        do {
        !            92:                outbuf = out + converted;
        !            93:                outleft = outlen - converted;
        !            94:                
        !            95:                converted = iconv (cd, (char **) &inbuf, &inleft, &outbuf, &outleft);
        !            96:                if (converted == (size_t) -1) {
        !            97:                        if (errno != E2BIG && errno != EINVAL)
        !            98:                                goto fail;
        !            99:                }
        !           100:                
        !           101:                /*
        !           102:                 * E2BIG   There is not sufficient room at *outbuf.
        !           103:                 *
        !           104:                 * We just need to grow our outbuffer and try again.
        !           105:                 */
        !           106:                
        !           107:                converted = outlen - outleft;
        !           108:                if (errno == E2BIG) {
        !           109:                        outlen += inleft * 2 + 16;
        !           110:                        out = g_realloc (out, outlen + 1);
        !           111:                        outbuf = out + converted;
        !           112:                }
        !           113:                
        !           114:        } while (errno == E2BIG && inleft > 0);
        !           115:        
        !           116:        /*
        !           117:         * EINVAL  An  incomplete  multibyte sequence has been encoun­
        !           118:         *         tered in the input.
        !           119:         *
        !           120:         * We'll just have to ignore it...
        !           121:         */
        !           122:        
        !           123:        /* flush the iconv conversion */
        !           124:        iconv (cd, NULL, NULL, &outbuf, &outleft);
        !           125:        *outbuf = '\0';
        !           126:        
        !           127:        /* reset the cd */
        !           128:        iconv (cd, NULL, NULL, NULL, NULL);
        !           129:        
        !           130:        return out;
        !           131:        
        !           132:  fail:
        !           133:        
        !           134:        g_warning ("g_mime_iconv_strndup: %s", g_strerror (errno));
        !           135:        
        !           136:        g_free (out);
        !           137:        
        !           138:        /* reset the cd */
        !           139:        iconv (cd, NULL, NULL, NULL, NULL);
        !           140:        
        !           141:        return NULL;
        !           142: }
        !           143: 
        !           144: 
        !           145: /**
        !           146:  * g_mime_iconv_strdup:
        !           147:  * @cd: conversion descriptor
        !           148:  * @string: string in source charset
        !           149:  *
        !           150:  * Allocates a new string buffer containing @string converted to
        !           151:  * the destination charset described in @cd.
        !           152:  *
        !           153:  * Returns a new string buffer containing the original string
        !           154:  * converted to the new charset.
        !           155:  **/
        !           156: char *
        !           157: g_mime_iconv_strdup (iconv_t cd, const char *string)
        !           158: {
        !           159:        return g_mime_iconv_strndup (cd, string, strlen (string));
        !           160: }
        !           161: 
        !           162: 
        !           163: /**
        !           164:  * g_mime_iconv_locale_to_utf8:
        !           165:  * @string: string in locale charset
        !           166:  *
        !           167:  * Allocates a new string buffer containing @string in UTF-8.
        !           168:  *
        !           169:  * Returns a new string buffer containing @string converted to UTF-8.
        !           170:  **/
        !           171: char *
        !           172: g_mime_iconv_locale_to_utf8 (const char *string)
        !           173: {
        !           174:        if (!initialized)
        !           175:                iconv_utils_init ();
        !           176:        
        !           177:        return g_mime_iconv_strdup (locale_to_utf8, string);
        !           178: }
        !           179: 
        !           180: 
        !           181: /**
        !           182:  * g_mime_iconv_locale_to_utf8_length:
        !           183:  * @string: string in locale charset
        !           184:  * @n: number of bytes to convert
        !           185:  *
        !           186:  * Allocates a new string buffer containing the first @n bytes of
        !           187:  * @string converted to UTF-8.
        !           188:  *
        !           189:  * Returns a new string buffer containing the first @n bytes of
        !           190:  * @string converted to UTF-8.
        !           191:  **/
        !           192: char *
        !           193: g_mime_iconv_locale_to_utf8_length (const char *string, size_t n)
        !           194: {
        !           195:        if (!initialized)
        !           196:                iconv_utils_init ();
        !           197:        
        !           198:        return g_mime_iconv_strndup (locale_to_utf8, string, n);
        !           199: }
        !           200: 
        !           201: 
        !           202: /**
        !           203:  * g_mime_iconv_utf8_to_locale:
        !           204:  * @string: string in UTF-8 charset
        !           205:  *
        !           206:  * Allocates a new string buffer containing @string converted to the
        !           207:  * user's locale charset.
        !           208:  *
        !           209:  * Returns a new string buffer containing @string converted to the
        !           210:  * user's locale charset.
        !           211:  **/
        !           212: char *
        !           213: g_mime_iconv_utf8_to_locale (const char *string)
        !           214: {
        !           215:        if (!initialized)
        !           216:                iconv_utils_init ();
        !           217:        
        !           218:        return g_mime_iconv_strdup (utf8_to_locale, string);
        !           219: }
        !           220: 
        !           221: 
        !           222: /**
        !           223:  * g_mime_iconv_utf8_to_locale_length:
        !           224:  * @string: string in UTF-8 charset
        !           225:  * @n: number of bytes to convert
        !           226:  *
        !           227:  * Allocates a new string buffer containing the first @n bytes of
        !           228:  * @string converted to the user's locale charset.
        !           229:  *
        !           230:  * Returns a new string buffer containing the first @n bytes of
        !           231:  * @string converted to the user's locale charset.
        !           232:  **/
        !           233: char *
        !           234: g_mime_iconv_utf8_to_locale_length (const char *string, size_t n)
        !           235: {
        !           236:        if (!initialized)
        !           237:                iconv_utils_init ();
        !           238:        
        !           239:        return g_mime_iconv_strndup (utf8_to_locale, string, n);
        !           240: }

E-mail: