Back to index

glibc  2.9
dump-ctype.c
Go to the documentation of this file.
00001 /* Dump the character classes and character maps of a locale to a bunch
00002    of individual files which can be processed with diff, sed etc.
00003    Copyright (C) 2000 Free Software Foundation, Inc.
00004    This file is part of the GNU C Library.
00005    Contributed by Bruno Haible <haible@clisp.cons.org>, 2000.
00006 
00007    The GNU C Library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Lesser General Public
00009    License as published by the Free Software Foundation; either
00010    version 2.1 of the License, or (at your option) any later version.
00011 
00012    The GNU C Library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Lesser General Public License for more details.
00016 
00017    You should have received a copy of the GNU Lesser General Public
00018    License along with the GNU C Library; if not, write to the Free
00019    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00020    02111-1307 USA.  */
00021 
00022 /* Usage example:
00023      $ dump-ctype de_DE.UTF-8
00024  */
00025 
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <wctype.h>
00029 #include <locale.h>
00030 #include <sys/stat.h>
00031 #include <unistd.h>
00032 #include <errno.h>
00033 
00034 static const char *program_name = "dump-ctype";
00035 static const char *locale;
00036 
00037 static const char *class_names[] =
00038   {
00039     "alnum", "alpha", "blank", "cntrl", "digit", "graph", "lower",
00040     "print", "punct", "space", "upper", "xdigit"
00041   };
00042 
00043 static const char *map_names[] =
00044   {
00045     "tolower", "toupper", "totitle"
00046   };
00047 
00048 static void dump_class (const char *class_name)
00049 {
00050   wctype_t class;
00051   FILE *f;
00052   unsigned int ch;
00053 
00054   class = wctype (class_name);
00055   if (class == (wctype_t) 0)
00056     {
00057       fprintf (stderr, "%s %s: noexistent class %s\n", program_name,
00058               locale, class_name);
00059       return;
00060     }
00061 
00062   f = fopen (class_name, "w");
00063   if (f == NULL)
00064     {
00065       fprintf (stderr, "%s %s: cannot open file %s/%s\n", program_name,
00066               locale, locale, class_name);
00067       exit (1);
00068     }
00069 
00070   for (ch = 0; ch < 0x10000; ch++)
00071     if (iswctype (ch, class))
00072       fprintf (f, "0x%04X\n", ch);
00073 
00074   if (ferror (f) || fclose (f))
00075     {
00076       fprintf (stderr, "%s %s: I/O error on file %s/%s\n", program_name,
00077               locale, locale, class_name);
00078       exit (1);
00079     }
00080 }
00081 
00082 static void dump_map (const char *map_name)
00083 {
00084   wctrans_t map;
00085   FILE *f;
00086   unsigned int ch;
00087 
00088   map = wctrans (map_name);
00089   if (map == (wctrans_t) 0)
00090     {
00091       fprintf (stderr, "%s %s: noexistent map %s\n", program_name,
00092               locale, map_name);
00093       return;
00094     }
00095 
00096   f = fopen (map_name, "w");
00097   if (f == NULL)
00098     {
00099       fprintf (stderr, "%s %s: cannot open file %s/%s\n", program_name,
00100               locale, locale, map_name);
00101       exit (1);
00102     }
00103 
00104   for (ch = 0; ch < 0x10000; ch++)
00105     if (towctrans (ch, map) != ch)
00106       fprintf (f, "0x%04X\t0x%04X\n", ch, towctrans (ch, map));
00107 
00108   if (ferror (f) || fclose (f))
00109     {
00110       fprintf (stderr, "%s %s: I/O error on file %s/%s\n", program_name,
00111               locale, locale, map_name);
00112       exit (1);
00113     }
00114 }
00115 
00116 int
00117 main (int argc, char *argv[])
00118 {
00119   size_t i;
00120 
00121   if (argc != 2)
00122     {
00123       fprintf (stderr, "Usage: dump-ctype locale\n");
00124       exit (1);
00125     }
00126   locale = argv[1];
00127 
00128   if (setlocale (LC_ALL, locale) == NULL)
00129     {
00130       fprintf (stderr, "%s: setlocale cannot switch to locale %s\n",
00131               program_name, locale);
00132       exit (1);
00133     }
00134 
00135   if (mkdir (locale, 0777) < 0)
00136     {
00137       char buf[100];
00138       int save_errno = errno;
00139 
00140       sprintf (buf, "%s: cannot create directory %s", program_name, locale);
00141       errno = save_errno;
00142       perror (buf);
00143       exit (1);
00144     }
00145 
00146   if (chdir (locale) < 0)
00147     {
00148       char buf[100];
00149       int save_errno = errno;
00150 
00151       sprintf (buf, "%s: cannot chdir to %s", program_name, locale);
00152       errno = save_errno;
00153       perror (buf);
00154       exit (1);
00155     }
00156 
00157   for (i = 0; i < sizeof (class_names) / sizeof (class_names[0]); i++)
00158     dump_class (class_names[i]);
00159 
00160   for (i = 0; i < sizeof (map_names) / sizeof (map_names[0]); i++)
00161     dump_map (map_names[i]);
00162 
00163   return 0;
00164 }