Update.
[glibc.git] / iconvdata / tst-table-to.c
blobc763c845c9cdd0198fb25a484b6ffe4555c4c7e2
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.
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.
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.
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. */
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. */
24 #include <stddef.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <iconv.h>
28 #include <errno.h>
30 int
31 main (int argc, char *argv[])
33 const char *charset;
34 iconv_t cd;
36 if (argc != 2)
38 fprintf (stderr, "Usage: tst-table-to charset\n");
39 return 1;
41 charset = argv[1];
43 cd = iconv_open (charset, "UCS-2");
44 if (cd == (iconv_t)(-1))
46 perror ("iconv_open");
47 return 1;
51 unsigned int i;
52 unsigned char buf[10];
54 for (i = 0; i < 0x10000; i++)
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))
66 if (errno != EILSEQ)
68 int saved_errno = errno;
69 fprintf (stderr, "0x%02X: iconv error: ", i);
70 errno = saved_errno;
71 perror ("");
72 return 1;
75 else if (result == 0) /* ignore conversions with transliteration */
77 unsigned int j, jmax;
78 if (inbytesleft != 0 || outbytesleft == sizeof (buf))
80 fprintf (stderr, "0x%02X: inbytes = %ld, outbytes = %ld\n", i,
81 (long) (sizeof (unsigned short) - inbytesleft),
82 (long) (sizeof (buf) - outbytesleft));
83 return 1;
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);
94 if (iconv_close (cd) < 0)
96 perror ("iconv_close");
97 return 1;
100 if (ferror (stdin) || fflush (stdout) || ferror (stdout))
102 fprintf (stderr, "I/O error\n");
103 return 1;
106 return 0;