]>
Commit | Line | Data |
---|---|---|
93a568aa | 1 | /* Copyright (C) 2000-2002 Free Software Foundation, Inc. |
b79f74cd UD |
2 | This file is part of the GNU C Library. |
3 | Contributed by Bruno Haible <haible@clisp.cons.org>, 2000. | |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
b79f74cd UD |
9 | |
10 | The GNU C Library 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 GNU | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
b79f74cd | 14 | |
41bdb6e2 AJ |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, write to the Free | |
17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | |
18 | 02111-1307 USA. */ | |
b79f74cd UD |
19 | |
20 | /* Create a table from Unicode to CHARSET. | |
21 | This is a good test for CHARSET's iconv() module, in particular the | |
22 | TO_LOOP BODY macro. */ | |
23 | ||
24 | #include <stddef.h> | |
25 | #include <stdio.h> | |
26 | #include <stdlib.h> | |
a29a3e1a | 27 | #include <string.h> |
b79f74cd UD |
28 | #include <iconv.h> |
29 | #include <errno.h> | |
30 | ||
31 | int | |
32 | main (int argc, char *argv[]) | |
33 | { | |
34 | const char *charset; | |
35 | iconv_t cd; | |
601d2942 | 36 | int bmp_only; |
b79f74cd UD |
37 | |
38 | if (argc != 2) | |
39 | { | |
40 | fprintf (stderr, "Usage: tst-table-to charset\n"); | |
4e6bc1f6 | 41 | return 1; |
b79f74cd UD |
42 | } |
43 | charset = argv[1]; | |
44 | ||
601d2942 | 45 | cd = iconv_open (charset, "UTF-8"); |
b79f74cd UD |
46 | if (cd == (iconv_t)(-1)) |
47 | { | |
48 | perror ("iconv_open"); | |
4e6bc1f6 | 49 | return 1; |
b79f74cd UD |
50 | } |
51 | ||
601d2942 UD |
52 | /* When testing UTF-8 or GB18030, stop at 0x10000, otherwise the output |
53 | file gets too big. */ | |
54 | bmp_only = (strcmp (charset, "UTF-8") == 0 | |
55 | || strcmp (charset, "GB18030") == 0); | |
56 | ||
b79f74cd UD |
57 | { |
58 | unsigned int i; | |
59 | unsigned char buf[10]; | |
60 | ||
601d2942 | 61 | for (i = 0; i < (bmp_only ? 0x10000 : 0x30000); i++) |
b79f74cd | 62 | { |
601d2942 UD |
63 | unsigned char in[6]; |
64 | unsigned int incount = | |
65 | (i < 0x80 ? (in[0] = i, 1) | |
66 | : i < 0x800 ? (in[0] = 0xc0 | (i >> 6), | |
67 | in[1] = 0x80 | (i & 0x3f), 2) | |
68 | : i < 0x10000 ? (in[0] = 0xe0 | (i >> 12), | |
69 | in[1] = 0x80 | ((i >> 6) & 0x3f), | |
70 | in[2] = 0x80 | (i & 0x3f), 3) | |
71 | : /* i < 0x200000 */ (in[0] = 0xf0 | (i >> 18), | |
72 | in[1] = 0x80 | ((i >> 12) & 0x3f), | |
73 | in[2] = 0x80 | ((i >> 6) & 0x3f), | |
74 | in[3] = 0x80 | (i & 0x3f), 4)); | |
75 | const char *inbuf = (const char *) in; | |
76 | size_t inbytesleft = incount; | |
b79f74cd UD |
77 | char *outbuf = (char *) buf; |
78 | size_t outbytesleft = sizeof (buf); | |
93a568aa | 79 | size_t result; |
faaa6f62 | 80 | size_t result2 = 0; |
93a568aa UD |
81 | |
82 | iconv (cd, NULL, NULL, NULL, NULL); | |
83 | result = iconv (cd, | |
84 | (char **) &inbuf, &inbytesleft, | |
85 | &outbuf, &outbytesleft); | |
86 | if (result != (size_t)(-1)) | |
faaa6f62 | 87 | result2 = iconv (cd, NULL, NULL, &outbuf, &outbytesleft); |
93a568aa | 88 | |
faaa6f62 | 89 | if (result == (size_t)(-1) || result2 == (size_t)(-1)) |
b79f74cd UD |
90 | { |
91 | if (errno != EILSEQ) | |
92 | { | |
93 | int saved_errno = errno; | |
94 | fprintf (stderr, "0x%02X: iconv error: ", i); | |
95 | errno = saved_errno; | |
96 | perror (""); | |
4e6bc1f6 | 97 | return 1; |
b79f74cd UD |
98 | } |
99 | } | |
100 | else if (result == 0) /* ignore conversions with transliteration */ | |
101 | { | |
102 | unsigned int j, jmax; | |
103 | if (inbytesleft != 0 || outbytesleft == sizeof (buf)) | |
104 | { | |
105 | fprintf (stderr, "0x%02X: inbytes = %ld, outbytes = %ld\n", i, | |
601d2942 | 106 | (long) (incount - inbytesleft), |
b79f74cd | 107 | (long) (sizeof (buf) - outbytesleft)); |
4e6bc1f6 | 108 | return 1; |
b79f74cd UD |
109 | } |
110 | jmax = sizeof (buf) - outbytesleft; | |
111 | printf ("0x"); | |
112 | for (j = 0; j < jmax; j++) | |
113 | printf ("%02X", buf[j]); | |
114 | printf ("\t0x%04X\n", i); | |
115 | } | |
116 | } | |
117 | } | |
118 | ||
119 | if (iconv_close (cd) < 0) | |
120 | { | |
121 | perror ("iconv_close"); | |
4e6bc1f6 | 122 | return 1; |
b79f74cd UD |
123 | } |
124 | ||
755104ed | 125 | if (ferror (stdin) || fflush (stdout) || ferror (stdout)) |
b79f74cd UD |
126 | { |
127 | fprintf (stderr, "I/O error\n"); | |
4e6bc1f6 | 128 | return 1; |
b79f74cd UD |
129 | } |
130 | ||
4e6bc1f6 | 131 | return 0; |
b79f74cd | 132 | } |