Blame localedata/dump-ctype.c

Packit 6c4009
/* Dump the character classes and character maps of a locale to a bunch
Packit 6c4009
   of individual files which can be processed with diff, sed etc.
Packit 6c4009
   Copyright (C) 2000-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Bruno Haible <haible@clisp.cons.org>, 2000.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
/* Usage example:
Packit 6c4009
     $ dump-ctype de_DE.UTF-8
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <wctype.h>
Packit 6c4009
#include <locale.h>
Packit 6c4009
#include <sys/stat.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
Packit 6c4009
static const char *program_name = "dump-ctype";
Packit 6c4009
static const char *locale;
Packit 6c4009
Packit 6c4009
static const char *class_names[] =
Packit 6c4009
  {
Packit 6c4009
    "alnum", "alpha", "blank", "cntrl", "digit", "graph", "lower",
Packit 6c4009
    "print", "punct", "space", "upper", "xdigit"
Packit 6c4009
  };
Packit 6c4009
Packit 6c4009
static const char *map_names[] =
Packit 6c4009
  {
Packit 6c4009
    "tolower", "toupper", "totitle"
Packit 6c4009
  };
Packit 6c4009
Packit 6c4009
static void dump_class (const char *class_name)
Packit 6c4009
{
Packit 6c4009
  wctype_t class;
Packit 6c4009
  FILE *f;
Packit 6c4009
  unsigned int ch;
Packit 6c4009
Packit 6c4009
  class = wctype (class_name);
Packit 6c4009
  if (class == (wctype_t) 0)
Packit 6c4009
    {
Packit 6c4009
      fprintf (stderr, "%s %s: noexistent class %s\n", program_name,
Packit 6c4009
	       locale, class_name);
Packit 6c4009
      return;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  f = fopen (class_name, "w");
Packit 6c4009
  if (f == NULL)
Packit 6c4009
    {
Packit 6c4009
      fprintf (stderr, "%s %s: cannot open file %s/%s\n", program_name,
Packit 6c4009
	       locale, locale, class_name);
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  for (ch = 0; ch < 0x10000; ch++)
Packit 6c4009
    if (iswctype (ch, class))
Packit 6c4009
      fprintf (f, "0x%04X\n", ch);
Packit 6c4009
Packit 6c4009
  if (ferror (f) || fclose (f))
Packit 6c4009
    {
Packit 6c4009
      fprintf (stderr, "%s %s: I/O error on file %s/%s\n", program_name,
Packit 6c4009
	       locale, locale, class_name);
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void dump_map (const char *map_name)
Packit 6c4009
{
Packit 6c4009
  wctrans_t map;
Packit 6c4009
  FILE *f;
Packit 6c4009
  unsigned int ch;
Packit 6c4009
Packit 6c4009
  map = wctrans (map_name);
Packit 6c4009
  if (map == (wctrans_t) 0)
Packit 6c4009
    {
Packit 6c4009
      fprintf (stderr, "%s %s: noexistent map %s\n", program_name,
Packit 6c4009
	       locale, map_name);
Packit 6c4009
      return;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  f = fopen (map_name, "w");
Packit 6c4009
  if (f == NULL)
Packit 6c4009
    {
Packit 6c4009
      fprintf (stderr, "%s %s: cannot open file %s/%s\n", program_name,
Packit 6c4009
	       locale, locale, map_name);
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  for (ch = 0; ch < 0x10000; ch++)
Packit 6c4009
    if (towctrans (ch, map) != ch)
Packit 6c4009
      fprintf (f, "0x%04X\t0x%04X\n", ch, towctrans (ch, map));
Packit 6c4009
Packit 6c4009
  if (ferror (f) || fclose (f))
Packit 6c4009
    {
Packit 6c4009
      fprintf (stderr, "%s %s: I/O error on file %s/%s\n", program_name,
Packit 6c4009
	       locale, locale, map_name);
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
main (int argc, char *argv[])
Packit 6c4009
{
Packit 6c4009
  size_t i;
Packit 6c4009
Packit 6c4009
  if (argc != 2)
Packit 6c4009
    {
Packit 6c4009
      fprintf (stderr, "Usage: dump-ctype locale\n");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
  locale = argv[1];
Packit 6c4009
Packit 6c4009
  if (setlocale (LC_ALL, locale) == NULL)
Packit 6c4009
    {
Packit 6c4009
      fprintf (stderr, "%s: setlocale cannot switch to locale %s\n",
Packit 6c4009
	       program_name, locale);
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (mkdir (locale, 0777) < 0)
Packit 6c4009
    {
Packit 6c4009
      char buf[100];
Packit 6c4009
      int save_errno = errno;
Packit 6c4009
Packit 6c4009
      sprintf (buf, "%s: cannot create directory %s", program_name, locale);
Packit 6c4009
      errno = save_errno;
Packit 6c4009
      perror (buf);
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (chdir (locale) < 0)
Packit 6c4009
    {
Packit 6c4009
      char buf[100];
Packit 6c4009
      int save_errno = errno;
Packit 6c4009
Packit 6c4009
      sprintf (buf, "%s: cannot chdir to %s", program_name, locale);
Packit 6c4009
      errno = save_errno;
Packit 6c4009
      perror (buf);
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  for (i = 0; i < sizeof (class_names) / sizeof (class_names[0]); i++)
Packit 6c4009
    dump_class (class_names[i]);
Packit 6c4009
Packit 6c4009
  for (i = 0; i < sizeof (map_names) / sizeof (map_names[0]); i++)
Packit 6c4009
    dump_map (map_names[i]);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}