Back to index

glibc  2.9
tst-table-to.c
Go to the documentation of this file.
00001 /* Copyright (C) 2000-2002 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Bruno Haible <haible@clisp.cons.org>, 2000.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 /* Create a table from Unicode to CHARSET.
00021    This is a good test for CHARSET's iconv() module, in particular the
00022    TO_LOOP BODY macro.  */
00023 
00024 #include <stddef.h>
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 #include <string.h>
00028 #include <iconv.h>
00029 #include <errno.h>
00030 
00031 int
00032 main (int argc, char *argv[])
00033 {
00034   const char *charset;
00035   iconv_t cd;
00036   int bmp_only;
00037 
00038   if (argc != 2)
00039     {
00040       fprintf (stderr, "Usage: tst-table-to charset\n");
00041       return 1;
00042     }
00043   charset = argv[1];
00044 
00045   cd = iconv_open (charset, "UTF-8");
00046   if (cd == (iconv_t)(-1))
00047     {
00048       perror ("iconv_open");
00049       return 1;
00050     }
00051 
00052   /* When testing UTF-8 or GB18030, stop at 0x10000, otherwise the output
00053      file gets too big.  */
00054   bmp_only = (strcmp (charset, "UTF-8") == 0
00055              || strcmp (charset, "GB18030") == 0);
00056 
00057   {
00058     unsigned int i;
00059     unsigned char buf[10];
00060 
00061     for (i = 0; i < (bmp_only ? 0x10000 : 0x30000); i++)
00062       {
00063        unsigned char in[6];
00064        unsigned int incount =
00065          (i < 0x80 ? (in[0] = i, 1)
00066           : i < 0x800 ? (in[0] = 0xc0 | (i >> 6),
00067                        in[1] = 0x80 | (i & 0x3f), 2)
00068           : i < 0x10000 ? (in[0] = 0xe0 | (i >> 12),
00069                          in[1] = 0x80 | ((i >> 6) & 0x3f),
00070                          in[2] = 0x80 | (i & 0x3f), 3)
00071           : /* i < 0x200000 */ (in[0] = 0xf0 | (i >> 18),
00072                              in[1] = 0x80 | ((i >> 12) & 0x3f),
00073                              in[2] = 0x80 | ((i >> 6) & 0x3f),
00074                              in[3] = 0x80 | (i & 0x3f), 4));
00075        const char *inbuf = (const char *) in;
00076        size_t inbytesleft = incount;
00077        char *outbuf = (char *) buf;
00078        size_t outbytesleft = sizeof (buf);
00079        size_t result;
00080        size_t result2 = 0;
00081 
00082        iconv (cd, NULL, NULL, NULL, NULL);
00083        result = iconv (cd,
00084                      (char **) &inbuf, &inbytesleft,
00085                      &outbuf, &outbytesleft);
00086        if (result != (size_t)(-1))
00087          result2 = iconv (cd, NULL, NULL, &outbuf, &outbytesleft);
00088 
00089        if (result == (size_t)(-1) || result2 == (size_t)(-1))
00090          {
00091            if (errno != EILSEQ)
00092              {
00093               int saved_errno = errno;
00094               fprintf (stderr, "0x%02X: iconv error: ", i);
00095               errno = saved_errno;
00096               perror ("");
00097               return 1;
00098              }
00099          }
00100        else if (result == 0) /* ignore conversions with transliteration */
00101          {
00102            unsigned int j, jmax;
00103            if (inbytesleft != 0 || outbytesleft == sizeof (buf))
00104              {
00105               fprintf (stderr, "0x%02X: inbytes = %ld, outbytes = %ld\n", i,
00106                       (long) (incount - inbytesleft),
00107                       (long) (sizeof (buf) - outbytesleft));
00108               return 1;
00109              }
00110            jmax = sizeof (buf) - outbytesleft;
00111            printf ("0x");
00112            for (j = 0; j < jmax; j++)
00113              printf ("%02X", buf[j]);
00114            printf ("\t0x%04X\n", i);
00115          }
00116       }
00117   }
00118 
00119   if (iconv_close (cd) < 0)
00120     {
00121       perror ("iconv_close");
00122       return 1;
00123     }
00124 
00125   if (ferror (stdin) || fflush (stdout) || ferror (stdout))
00126     {
00127       fprintf (stderr, "I/O error\n");
00128       return 1;
00129     }
00130 
00131   return 0;
00132 }