ISO-8859-16 has changed.
[libiconv.git] / tests / table-to.c
blobe6fee352af7e8ee57e5852045397b2eed065ea6a
1 /* Copyright (C) 2000-2001 Free Software Foundation, Inc.
2 This file is part of the GNU LIBICONV Library.
4 The GNU LIBICONV Library is free software; you can redistribute it
5 and/or modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 The GNU LIBICONV Library is distributed in the hope that it will be
10 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU LIBICONV Library; see the file COPYING.LIB.
16 If not, write to the Free Software Foundation, Inc., 59 Temple Place -
17 Suite 330, Boston, MA 02111-1307, USA. */
19 /* Create a table from Unicode to CHARSET. */
21 #include "config.h"
23 #include <stddef.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <iconv.h>
28 #include <errno.h>
30 int main (int argc, char* argv[])
32 const char* charset;
33 iconv_t cd;
34 int bmp_only;
36 if (argc != 2) {
37 fprintf(stderr,"Usage: table-to charset\n");
38 exit(1);
40 charset = argv[1];
42 cd = iconv_open(charset,"UCS-4-INTERNAL");
43 if (cd == (iconv_t)(-1)) {
44 perror("iconv_open");
45 exit(1);
48 /* When testing UTF-8 or GB18030, stop at 0x10000, otherwise the output
49 file gets too big. */
50 bmp_only = (strcmp(charset,"UTF-8") == 0 || strcmp(charset,"GB18030") == 0);
53 unsigned int i;
54 unsigned char buf[10];
55 for (i = 0; i < (bmp_only ? 0x10000 : 0x30000); i++) {
56 unsigned int in = i;
57 const char* inbuf = (const char*) &in;
58 size_t inbytesleft = sizeof(unsigned int);
59 char* outbuf = (char*)buf;
60 size_t outbytesleft = sizeof(buf);
61 size_t result = iconv(cd,(ICONV_CONST char**)&inbuf,&inbytesleft,&outbuf,&outbytesleft);
62 if (result == (size_t)(-1)) {
63 if (errno != EILSEQ) {
64 int saved_errno = errno;
65 fprintf(stderr,"0x%02X: iconv error: ",i);
66 errno = saved_errno;
67 perror("");
68 exit(1);
70 } else if (result == 0) /* ignore conversions with transliteration */ {
71 unsigned int j, jmax;
72 if (inbytesleft != 0 || outbytesleft == sizeof(buf)) {
73 fprintf(stderr,"0x%02X: inbytes = %ld, outbytes = %ld\n",i,(long)(sizeof(unsigned int)-inbytesleft),(long)(sizeof(buf)-outbytesleft));
74 exit(1);
76 jmax = sizeof(buf) - outbytesleft;
77 printf("0x");
78 for (j = 0; j < jmax; j++)
79 printf("%02X",buf[j]);
80 printf("\t0x%04X\n",i);
85 if (iconv_close(cd) < 0) {
86 perror("iconv_close");
87 exit(1);
90 if (ferror(stdin) || ferror(stdout)) {
91 fprintf(stderr,"I/O error\n");
92 exit(1);
95 exit(0);