ltconfig is removed.
[libiconv.git] / tests / table-from.c
blobabd6b3c0c0176bc8f8134c0fd022f557f08206d7
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 CHARSET to Unicode. */
21 #include "config.h"
23 #include <stddef.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <iconv.h>
27 #include <errno.h>
29 /* If nonzero, ignore conversions outside Unicode plane 0. */
30 static int bmp_only;
32 static const char* hexbuf (unsigned char buf[], unsigned int buflen)
34 static char msg[50];
35 switch (buflen) {
36 case 1: sprintf(msg,"0x%02X",buf[0]); break;
37 case 2: sprintf(msg,"0x%02X%02X",buf[0],buf[1]); break;
38 case 3: sprintf(msg,"0x%02X%02X%02X",buf[0],buf[1],buf[2]); break;
39 case 4: sprintf(msg,"0x%02X%02X%02X%02X",buf[0],buf[1],buf[2],buf[3]); break;
40 default: abort();
42 return msg;
45 static int try (iconv_t cd, unsigned char buf[], unsigned int buflen, unsigned int* out)
47 const char* inbuf = (const char*) buf;
48 size_t inbytesleft = buflen;
49 char* outbuf = (char*) out;
50 size_t outbytesleft = 3*sizeof(unsigned int);
51 size_t result;
52 iconv(cd,NULL,NULL,NULL,NULL);
53 result = iconv(cd,(ICONV_CONST char**)&inbuf,&inbytesleft,&outbuf,&outbytesleft);
54 if (result != (size_t)(-1))
55 result = iconv(cd,NULL,NULL,&outbuf,&outbytesleft);
56 if (result == (size_t)(-1)) {
57 if (errno == EILSEQ) {
58 return -1;
59 } else if (errno == EINVAL) {
60 return 0;
61 } else {
62 int saved_errno = errno;
63 fprintf(stderr,"%s: iconv error: ",hexbuf(buf,buflen));
64 errno = saved_errno;
65 perror("");
66 exit(1);
68 } else if (result > 0) /* ignore conversions with transliteration */ {
69 return -1;
70 } else {
71 if (inbytesleft != 0) {
72 fprintf(stderr,"%s: inbytes = %ld, outbytes = %ld\n",hexbuf(buf,buflen),(long)(buflen-inbytesleft),(long)(3*sizeof(unsigned int)-outbytesleft));
73 exit(1);
75 return (3*sizeof(unsigned int)-outbytesleft)/sizeof(unsigned int);
79 /* Returns the out[] buffer as a Unicode value, formatted as 0x%04X. */
80 static const char* ucs4_decode (const unsigned int* out, unsigned int outlen)
82 static char hexbuf[21];
83 char* p = hexbuf;
84 while (outlen > 0) {
85 if (p > hexbuf)
86 *p++ = ' ';
87 sprintf (p, "0x%04X", out[0]);
88 out += 1; outlen -= 1;
89 if (bmp_only && strlen(p) > 6)
90 return NULL;
91 p += strlen(p);
93 return hexbuf;
96 int main (int argc, char* argv[])
98 const char* charset;
99 iconv_t cd;
100 int search_depth;
102 if (argc != 2) {
103 fprintf(stderr,"Usage: table-to charset\n");
104 exit(1);
106 charset = argv[1];
108 cd = iconv_open("UCS-4-INTERNAL",charset);
109 if (cd == (iconv_t)(-1)) {
110 perror("iconv_open");
111 exit(1);
114 /* When testing UTF-8 or GB18030, stop at 0x10000, otherwise the output
115 file gets too big. */
116 bmp_only = (strcmp(charset,"UTF-8") == 0 || strcmp(charset,"GB18030") == 0);
117 search_depth = (strcmp(charset,"UTF-8") == 0 ? 3 : 4);
120 unsigned int out[3];
121 unsigned char buf[4];
122 unsigned int i0, i1, i2, i3;
123 int result;
124 for (i0 = 0; i0 < 0x100; i0++) {
125 buf[0] = i0;
126 result = try(cd,buf,1,out);
127 if (result < 0) {
128 } else if (result > 0) {
129 const char* unicode = ucs4_decode(out,result);
130 if (unicode != NULL)
131 printf("0x%02X\t%s\n",i0,unicode);
132 } else {
133 for (i1 = 0; i1 < 0x100; i1++) {
134 buf[1] = i1;
135 result = try(cd,buf,2,out);
136 if (result < 0) {
137 } else if (result > 0) {
138 const char* unicode = ucs4_decode(out,result);
139 if (unicode != NULL)
140 printf("0x%02X%02X\t%s\n",i0,i1,unicode);
141 } else {
142 for (i2 = 0; i2 < 0x100; i2++) {
143 buf[2] = i2;
144 result = try(cd,buf,3,out);
145 if (result < 0) {
146 } else if (result > 0) {
147 const char* unicode = ucs4_decode(out,result);
148 if (unicode != NULL)
149 printf("0x%02X%02X%02X\t%s\n",i0,i1,i2,unicode);
150 } else if (search_depth > 3) {
151 for (i3 = 0; i3 < 0x100; i3++) {
152 buf[3] = i3;
153 result = try(cd,buf,4,out);
154 if (result < 0) {
155 } else if (result > 0) {
156 const char* unicode = ucs4_decode(out,result);
157 if (unicode != NULL)
158 printf("0x%02X%02X%02X%02X\t%s\n",i0,i1,i2,i3,unicode);
159 } else {
160 fprintf(stderr,"%s: incomplete byte sequence\n",hexbuf(buf,4));
161 exit(1);
172 if (iconv_close(cd) < 0) {
173 perror("iconv_close");
174 exit(1);
177 if (ferror(stdin) || ferror(stdout)) {
178 fprintf(stderr,"I/O error\n");
179 exit(1);
182 exit(0);