]> git.ipfire.org Git - thirdparty/glibc.git/blob - iconvdata/tst-table-to.c
Update.
[thirdparty/glibc.git] / iconvdata / tst-table-to.c
1 /* Copyright (C) 2000 Free Software Foundation, Inc.
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
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
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
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
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>
27 #include <iconv.h>
28 #include <errno.h>
29
30 int
31 main (int argc, char *argv[])
32 {
33 const char *charset;
34 iconv_t cd;
35
36 if (argc != 2)
37 {
38 fprintf (stderr, "Usage: tst-table-to charset\n");
39 exit (1);
40 }
41 charset = argv[1];
42
43 cd = iconv_open (charset, "UCS-2");
44 if (cd == (iconv_t)(-1))
45 {
46 perror ("iconv_open");
47 exit (1);
48 }
49
50 {
51 unsigned int i;
52 unsigned char buf[10];
53
54 for (i = 0; i < 0x10000; i++)
55 {
56 unsigned short in = i;
57 const char *inbuf = (const char *) &in;
58 size_t inbytesleft = sizeof (unsigned short);
59 char *outbuf = (char *) buf;
60 size_t outbytesleft = sizeof (buf);
61 size_t result = iconv (cd,
62 (char **) &inbuf, &inbytesleft,
63 &outbuf, &outbytesleft);
64 if (result == (size_t)(-1))
65 {
66 if (errno != EILSEQ)
67 {
68 int saved_errno = errno;
69 fprintf (stderr, "0x%02X: iconv error: ", i);
70 errno = saved_errno;
71 perror ("");
72 exit (1);
73 }
74 }
75 else if (result == 0) /* ignore conversions with transliteration */
76 {
77 unsigned int j, jmax;
78 if (inbytesleft != 0 || outbytesleft == sizeof (buf))
79 {
80 fprintf (stderr, "0x%02X: inbytes = %ld, outbytes = %ld\n", i,
81 (long) (sizeof (unsigned short) - inbytesleft),
82 (long) (sizeof (buf) - outbytesleft));
83 exit (1);
84 }
85 jmax = sizeof (buf) - outbytesleft;
86 printf ("0x");
87 for (j = 0; j < jmax; j++)
88 printf ("%02X", buf[j]);
89 printf ("\t0x%04X\n", i);
90 }
91 }
92 }
93
94 if (iconv_close (cd) < 0)
95 {
96 perror ("iconv_close");
97 exit (1);
98 }
99
100 if (ferror (stdin) || ferror (stdout))
101 {
102 fprintf (stderr, "I/O error\n");
103 exit (1);
104 }
105
106 exit (0);
107 }