]>
Dogcows Code - chaz/tar/blob - lib/unicodeio.c
1 /* Unicode character output to streams with locale dependent encoding.
3 Copyright (C) 2000, 2001 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 /* Written by Bruno Haible <haible@clisp.cons.org>. */
41 /* Define EILSEQ and ENOTSUP as portably as possible. Some
42 nonstandard systems, like SunOS 4, don't have EILSEQ. Others, like
43 BSD/OS 4.1, define it in <wchar.h>. Callers that use EILSEQ and/or
44 ENOTSUP and that want to be portable to these nonstandard systems
45 should mimic the following includes and defines. */
47 /* BSD/OS 4.1 wchar.h defines EILSEQ, but it requires FILE (defined in
48 <stdio.h>, included above) and struct tm (defined in <time.h>) to
50 #if HAVE_WCHAR_H && ! defined EILSEQ
55 /* Do not define EILSEQ to be EINVAL, since callers may want to
56 distinguish EINVAL and EILSEQ. */
58 # define EILSEQ ENOENT
61 # define ENOTSUP EINVAL
68 #if HAVE_LANGINFO_CODESET && ! USE_INCLUDED_LIBINTL
69 # include <langinfo.h>
72 #include "unicodeio.h"
74 /* When we pass a Unicode character to iconv(), we must pass it in a
75 suitable encoding. The standardized Unicode encodings are
76 UTF-8, UCS-2, UCS-4, UTF-16, UTF-16BE, UTF-16LE, UTF-7.
77 UCS-2 supports only characters up to \U0000FFFF.
78 UTF-16 and variants support only characters up to \U0010FFFF.
79 UTF-7 is way too complex and not supported by glibc-2.1.
80 UCS-4 specification leaves doubts about endianness and byte order
81 mark. glibc currently interprets it as big endian without byte order
82 mark, but this is not backed by an RFC.
83 So we use UTF-8. It supports characters up to \U7FFFFFFF and is
84 unambiguously defined. */
86 /* Stores the UTF-8 representation of the Unicode character wc in r[0..5].
87 Returns the number of bytes stored, or -1 if wc is out of range. */
89 utf8_wctomb (unsigned char *r
, unsigned int wc
)
97 else if (wc
< 0x10000)
99 else if (wc
< 0x200000)
101 else if (wc
< 0x4000000)
103 else if (wc
<= 0x7fffffff)
110 /* Note: code falls through cases! */
111 case 6: r
[5] = 0x80 | (wc
& 0x3f); wc
= wc
>> 6; wc
|= 0x4000000;
112 case 5: r
[4] = 0x80 | (wc
& 0x3f); wc
= wc
>> 6; wc
|= 0x200000;
113 case 4: r
[3] = 0x80 | (wc
& 0x3f); wc
= wc
>> 6; wc
|= 0x10000;
114 case 3: r
[2] = 0x80 | (wc
& 0x3f); wc
= wc
>> 6; wc
|= 0x800;
115 case 2: r
[1] = 0x80 | (wc
& 0x3f); wc
= wc
>> 6; wc
|= 0xc0;
122 /* Luckily, the encoding's name is platform independent. */
123 #define UTF8_NAME "UTF-8"
125 /* Converts the Unicode character CODE to its multibyte representation
126 in the current locale and calls SUCCESS on the resulting byte
127 sequence. If an error occurs, invoke FAILURE instead,
128 passing it CODE with errno set appropriately.
129 Assumes that the locale doesn't change between two calls.
130 Return whatever the SUCCESS or FAILURE returns. */
132 unicode_to_mb (unsigned int code
,
133 int (*success
) PARAMS((const char *buf
, size_t buflen
,
134 void *callback_arg
)),
135 int (*failure
) PARAMS((unsigned int code
,
136 void *callback_arg
)),
139 static int initialized
;
142 static iconv_t utf8_to_local
;
152 #if USE_INCLUDED_LIBINTL
153 extern const char *locale_charset
PARAMS ((void));
154 charset
= locale_charset ();
156 # if HAVE_LANGINFO_CODESET
157 charset
= nl_langinfo (CODESET
);
163 is_utf8
= !strcmp (charset
, UTF8_NAME
);
167 utf8_to_local
= iconv_open (charset
, UTF8_NAME
);
168 if (utf8_to_local
== (iconv_t
)(-1))
170 /* For an unknown encoding, assume ASCII. */
171 utf8_to_local
= iconv_open ("ASCII", UTF8_NAME
);
172 if (utf8_to_local
== (iconv_t
)(-1))
173 return failure (code
, callback_arg
);
180 /* Convert the character to UTF-8. */
181 count
= utf8_wctomb ((unsigned char *) inbuf
, code
);
185 return failure (code
, callback_arg
);
190 return success (inbuf
, count
, callback_arg
);
205 outbytesleft
= sizeof (outbuf
);
207 /* Convert the character from UTF-8 to the locale's charset. */
208 res
= iconv (utf8_to_local
,
209 (ICONV_CONST
char **)&inptr
, &inbytesleft
,
210 &outptr
, &outbytesleft
);
211 if (inbytesleft
> 0 || res
== (size_t)(-1)
212 /* Irix iconv() inserts a NUL byte if it cannot convert. */
213 # if !defined _LIBICONV_VERSION && (defined sgi || defined __sgi)
214 || (res
> 0 && code
!= 0 && outptr
- outbuf
== 1 && *outbuf
== '\0')
218 if (res
!= (size_t)(-1))
220 return failure (code
, callback_arg
);
223 /* Avoid glibc-2.1 bug and Solaris 2.7 bug. */
224 # if defined _LIBICONV_VERSION \
225 || !((__GLIBC__ - 0 == 2 && __GLIBC_MINOR__ - 0 <= 1) || defined __sun)
227 /* Get back to the initial shift state. */
228 res
= iconv (utf8_to_local
, NULL
, NULL
, &outptr
, &outbytesleft
);
229 if (res
== (size_t)(-1))
230 return failure (code
, callback_arg
);
233 return success (outbuf
, outptr
- outbuf
, callback_arg
);
236 return failure (code
, callback_arg
);
241 /* Simple success callback that outputs the converted string.
242 The STREAM is passed as callback_arg. */
244 print_unicode_success (const char *buf
, size_t buflen
, void *callback_arg
)
246 FILE *stream
= (FILE *) callback_arg
;
248 return fwrite (buf
, 1, buflen
, stream
) == 0 ? -1 : 0;
251 /* Simple failure callback that prints an ASCII representation, using
252 the same notation as C99 strings. */
254 print_unicode_failure (unsigned int code
, void *callback_arg
)
257 FILE *stream
= callback_arg
;
259 fprintf (stream
, code
< 0x10000 ? "\\u%04X" : "\\U%08X", code
);
264 /* Outputs the Unicode character CODE to the output stream STREAM.
265 Returns zero if successful, -1 (setting errno) otherwise.
266 Assumes that the locale doesn't change between two calls. */
268 print_unicode_char (FILE *stream
, unsigned int code
)
270 return unicode_to_mb (code
, print_unicode_success
, print_unicode_failure
,
This page took 0.044801 seconds and 5 git commands to generate.