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