dump-ctype.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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, see
  16. <http://www.gnu.org/licenses/>. */
  17. /* Usage example:
  18. $ dump-ctype de_DE.UTF-8
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <wctype.h>
  23. #include <locale.h>
  24. #include <sys/stat.h>
  25. #include <unistd.h>
  26. #include <errno.h>
  27. static const char *program_name = "dump-ctype";
  28. static const char *locale;
  29. static const char *class_names[] =
  30. {
  31. "alnum", "alpha", "blank", "cntrl", "digit", "graph", "lower",
  32. "print", "punct", "space", "upper", "xdigit"
  33. };
  34. static const char *map_names[] =
  35. {
  36. "tolower", "toupper", "totitle"
  37. };
  38. static void dump_class (const char *class_name)
  39. {
  40. wctype_t class;
  41. FILE *f;
  42. unsigned int ch;
  43. class = wctype (class_name);
  44. if (class == (wctype_t) 0)
  45. {
  46. fprintf (stderr, "%s %s: noexistent class %s\n", program_name,
  47. locale, class_name);
  48. return;
  49. }
  50. f = fopen (class_name, "w");
  51. if (f == NULL)
  52. {
  53. fprintf (stderr, "%s %s: cannot open file %s/%s\n", program_name,
  54. locale, locale, class_name);
  55. exit (1);
  56. }
  57. for (ch = 0; ch < 0x10000; ch++)
  58. if (iswctype (ch, class))
  59. fprintf (f, "0x%04X\n", ch);
  60. if (ferror (f) || fclose (f))
  61. {
  62. fprintf (stderr, "%s %s: I/O error on file %s/%s\n", program_name,
  63. locale, locale, class_name);
  64. exit (1);
  65. }
  66. }
  67. static void dump_map (const char *map_name)
  68. {
  69. wctrans_t map;
  70. FILE *f;
  71. unsigned int ch;
  72. map = wctrans (map_name);
  73. if (map == (wctrans_t) 0)
  74. {
  75. fprintf (stderr, "%s %s: noexistent map %s\n", program_name,
  76. locale, map_name);
  77. return;
  78. }
  79. f = fopen (map_name, "w");
  80. if (f == NULL)
  81. {
  82. fprintf (stderr, "%s %s: cannot open file %s/%s\n", program_name,
  83. locale, locale, map_name);
  84. exit (1);
  85. }
  86. for (ch = 0; ch < 0x10000; ch++)
  87. if (towctrans (ch, map) != ch)
  88. fprintf (f, "0x%04X\t0x%04X\n", ch, towctrans (ch, map));
  89. if (ferror (f) || fclose (f))
  90. {
  91. fprintf (stderr, "%s %s: I/O error on file %s/%s\n", program_name,
  92. locale, locale, map_name);
  93. exit (1);
  94. }
  95. }
  96. int
  97. main (int argc, char *argv[])
  98. {
  99. size_t i;
  100. if (argc != 2)
  101. {
  102. fprintf (stderr, "Usage: dump-ctype locale\n");
  103. exit (1);
  104. }
  105. locale = argv[1];
  106. if (setlocale (LC_ALL, locale) == NULL)
  107. {
  108. fprintf (stderr, "%s: setlocale cannot switch to locale %s\n",
  109. program_name, locale);
  110. exit (1);
  111. }
  112. if (mkdir (locale, 0777) < 0)
  113. {
  114. char buf[100];
  115. int save_errno = errno;
  116. sprintf (buf, "%s: cannot create directory %s", program_name, locale);
  117. errno = save_errno;
  118. perror (buf);
  119. exit (1);
  120. }
  121. if (chdir (locale) < 0)
  122. {
  123. char buf[100];
  124. int save_errno = errno;
  125. sprintf (buf, "%s: cannot chdir to %s", program_name, locale);
  126. errno = save_errno;
  127. perror (buf);
  128. exit (1);
  129. }
  130. for (i = 0; i < sizeof (class_names) / sizeof (class_names[0]); i++)
  131. dump_class (class_names[i]);
  132. for (i = 0; i < sizeof (map_names) / sizeof (map_names[0]); i++)
  133. dump_map (map_names[i]);
  134. return 0;
  135. }