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-2022 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5
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
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
19
20/* Usage example:
21 $ dump-ctype de_DE.UTF-8
22 */
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <wctype.h>
27#include <locale.h>
28#include <sys/stat.h>
29#include <unistd.h>
30#include <errno.h>
31
32static const char *program_name = "dump-ctype";
33static const char *locale;
34
35static const char *class_names[] =
36 {
37 "alnum", "alpha", "blank", "cntrl", "digit", "graph", "lower",
38 "print", "punct", "space", "upper", "xdigit"
39 };
40
41static const char *map_names[] =
42 {
43 "tolower", "toupper", "totitle"
44 };
45
46static void dump_class (const char *class_name)
47{
48 wctype_t class;
49 FILE *f;
50 unsigned int ch;
51
52 class = wctype (property: class_name);
53 if (class == (wctype_t) 0)
54 {
55 fprintf (stderr, "%s %s: noexistent class %s\n", program_name,
56 locale, class_name);
57 return;
58 }
59
60 f = fopen (class_name, "w");
61 if (f == NULL)
62 {
63 fprintf (stderr, "%s %s: cannot open file %s/%s\n", program_name,
64 locale, locale, class_name);
65 exit (1);
66 }
67
68 for (ch = 0; ch < 0x10000; ch++)
69 if (iswctype (wc: ch, desc: class))
70 fprintf (f, "0x%04X\n", ch);
71
72 if (ferror (stream: f) || fclose (f))
73 {
74 fprintf (stderr, "%s %s: I/O error on file %s/%s\n", program_name,
75 locale, locale, class_name);
76 exit (1);
77 }
78}
79
80static void dump_map (const char *map_name)
81{
82 wctrans_t map;
83 FILE *f;
84 unsigned int ch;
85
86 map = wctrans (property: map_name);
87 if (map == (wctrans_t) 0)
88 {
89 fprintf (stderr, "%s %s: noexistent map %s\n", program_name,
90 locale, map_name);
91 return;
92 }
93
94 f = fopen (map_name, "w");
95 if (f == NULL)
96 {
97 fprintf (stderr, "%s %s: cannot open file %s/%s\n", program_name,
98 locale, locale, map_name);
99 exit (1);
100 }
101
102 for (ch = 0; ch < 0x10000; ch++)
103 if (towctrans (wc: ch, desc: map) != ch)
104 fprintf (f, "0x%04X\t0x%04X\n", ch, towctrans (wc: ch, desc: map));
105
106 if (ferror (stream: f) || fclose (f))
107 {
108 fprintf (stderr, "%s %s: I/O error on file %s/%s\n", program_name,
109 locale, locale, map_name);
110 exit (1);
111 }
112}
113
114int
115main (int argc, char *argv[])
116{
117 size_t i;
118
119 if (argc != 2)
120 {
121 fprintf (stderr, "Usage: dump-ctype locale\n");
122 exit (1);
123 }
124 locale = argv[1];
125
126 if (setlocale (LC_ALL, locale) == NULL)
127 {
128 fprintf (stderr, "%s: setlocale cannot switch to locale %s\n",
129 program_name, locale);
130 exit (1);
131 }
132
133 if (mkdir (path: locale, mode: 0777) < 0)
134 {
135 char buf[100];
136 int save_errno = errno;
137
138 sprintf (buf, "%s: cannot create directory %s", program_name, locale);
139 errno = save_errno;
140 perror (buf);
141 exit (1);
142 }
143
144 if (chdir (path: locale) < 0)
145 {
146 char buf[100];
147 int save_errno = errno;
148
149 sprintf (buf, "%s: cannot chdir to %s", program_name, locale);
150 errno = save_errno;
151 perror (buf);
152 exit (1);
153 }
154
155 for (i = 0; i < sizeof (class_names) / sizeof (class_names[0]); i++)
156 dump_class (class_name: class_names[i]);
157
158 for (i = 0; i < sizeof (map_names) / sizeof (map_names[0]); i++)
159 dump_map (map_name: map_names[i]);
160
161 return 0;
162}
163

source code of glibc/localedata/dump-ctype.c