dump-ctype.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* Dump the character classes and character maps of a locale to a bunch
  2. of individual files which can be processed with diff, sed etc.
  3. Copyright (C) 2000 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. Contributed by Bruno Haible <haible@clisp.cons.org>, 2000.
  6. The GNU C Library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. version 2.1 of the License, or (at your option) any later version.
  10. The GNU C Library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with the GNU C Library; if not, write to the Free
  16. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  17. 02111-1307 USA. */
  18. /* Usage example:
  19. $ dump-ctype de_DE.UTF-8
  20. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <wctype.h>
  24. #include <locale.h>
  25. #include <sys/stat.h>
  26. #include <unistd.h>
  27. #include <errno.h>
  28. static const char *program_name = "dump-ctype";
  29. static const char *locale;
  30. static const char *class_names[] =
  31. {
  32. "alnum", "alpha", "blank", "cntrl", "digit", "graph", "lower",
  33. "print", "punct", "space", "upper", "xdigit"
  34. };
  35. static const char *map_names[] =
  36. {
  37. "tolower", "toupper", "totitle"
  38. };
  39. static void dump_class (const char *class_name)
  40. {
  41. wctype_t class;
  42. FILE *f;
  43. unsigned int ch;
  44. class = wctype (class_name);
  45. if (class == (wctype_t) 0)
  46. {
  47. fprintf (stderr, "%s %s: noexistent class %s\n", program_name,
  48. locale, class_name);
  49. return;
  50. }
  51. f = fopen (class_name, "w");
  52. if (f == NULL)
  53. {
  54. fprintf (stderr, "%s %s: cannot open file %s/%s\n", program_name,
  55. locale, locale, class_name);
  56. exit (1);
  57. }
  58. for (ch = 0; ch < 0x10000; ch++)
  59. if (iswctype (ch, class))
  60. fprintf (f, "0x%04X\n", ch);
  61. if (ferror (f) || fclose (f))
  62. {
  63. fprintf (stderr, "%s %s: I/O error on file %s/%s\n", program_name,
  64. locale, locale, class_name);
  65. exit (1);
  66. }
  67. }
  68. static void dump_map (const char *map_name)
  69. {
  70. wctrans_t map;
  71. FILE *f;
  72. unsigned int ch;
  73. map = wctrans (map_name);
  74. if (map == (wctrans_t) 0)
  75. {
  76. fprintf (stderr, "%s %s: noexistent map %s\n", program_name,
  77. locale, map_name);
  78. return;
  79. }
  80. f = fopen (map_name, "w");
  81. if (f == NULL)
  82. {
  83. fprintf (stderr, "%s %s: cannot open file %s/%s\n", program_name,
  84. locale, locale, map_name);
  85. exit (1);
  86. }
  87. for (ch = 0; ch < 0x10000; ch++)
  88. if (towctrans (ch, map) != ch)
  89. fprintf (f, "0x%04X\t0x%04X\n", ch, towctrans (ch, map));
  90. if (ferror (f) || fclose (f))
  91. {
  92. fprintf (stderr, "%s %s: I/O error on file %s/%s\n", program_name,
  93. locale, locale, map_name);
  94. exit (1);
  95. }
  96. }
  97. int
  98. main (int argc, char *argv[])
  99. {
  100. size_t i;
  101. if (argc != 2)
  102. {
  103. fprintf (stderr, "Usage: dump-ctype locale\n");
  104. exit (1);
  105. }
  106. locale = argv[1];
  107. if (setlocale (LC_ALL, locale) == NULL)
  108. {
  109. fprintf (stderr, "%s: setlocale cannot switch to locale %s\n",
  110. program_name, locale);
  111. exit (1);
  112. }
  113. if (mkdir (locale, 0777) < 0)
  114. {
  115. char buf[100];
  116. int save_errno = errno;
  117. sprintf (buf, "%s: cannot create directory %s", program_name, locale);
  118. errno = save_errno;
  119. perror (buf);
  120. exit (1);
  121. }
  122. if (chdir (locale) < 0)
  123. {
  124. char buf[100];
  125. int save_errno = errno;
  126. sprintf (buf, "%s: cannot chdir to %s", program_name, locale);
  127. errno = save_errno;
  128. perror (buf);
  129. exit (1);
  130. }
  131. for (i = 0; i < sizeof (class_names) / sizeof (class_names[0]); i++)
  132. dump_class (class_names[i]);
  133. for (i = 0; i < sizeof (map_names) / sizeof (map_names[0]); i++)
  134. dump_map (map_names[i]);
  135. return 0;
  136. }