Imported Upstream version 1.0.10
[platform/upstream/fribidi.git] / gen.tab / gen-bidi-type-tab.c
1 /* FriBidi
2  * gen-bidi-type-tab.c - generate bidi-type.tab.i
3  *
4  * Author:
5  *   Behdad Esfahbod, 2001, 2002, 2004
6  *
7  * Copyright (C) 2004 Sharif FarsiWeb, Inc
8  * Copyright (C) 2001,2002,2004 Behdad Esfahbod
9  * 
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  * 
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  * 
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this library, in a file named COPYING; if not, write to the
22  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23  * Boston, MA 02110-1301, USA
24  * 
25  * For licensing issues, contact <fribidi.license@gmail.com>.
26  */
27
28 #include <common.h>
29
30 #include <fribidi-unicode.h>
31
32 #include <stdio.h>
33 #ifdef HAVE_CONFIG_H
34 # include <config.h>
35 #endif
36
37 #ifdef STDC_HEADERS
38 # include <stdlib.h>
39 # include <stddef.h>
40 #else
41 # if HAVE_STDLIB_H
42 #  include <stdlib.h>
43 # endif
44 #endif
45 #ifdef HAVE_STRING_H
46 # if !STDC_HEADERS && HAVE_MEMORY_H
47 #  include <memory.h>
48 # endif
49 # include <string.h>
50 #endif
51 #ifdef HAVE_STRINGS_H
52 # include <strings.h>
53 #endif
54
55 #include "packtab.h"
56
57 #define appname "gen-bidi-type-tab"
58 #define outputname "bidi-type.tab.i"
59
60 static void
61 die (
62   const char *msg
63 )
64 {
65   fprintf (stderr, appname ": %s\n", msg);
66   exit (1);
67 }
68
69 static void
70 die2 (
71   const char *fmt,
72   const char *p
73 )
74 {
75   fprintf (stderr, appname ": ");
76   fprintf (stderr, fmt, p);
77   fprintf (stderr, "\n");
78   exit (1);
79 }
80
81 static void
82 die3 (
83   const char *fmt,
84   unsigned long l,
85   const char *p
86 )
87 {
88   fprintf (stderr, appname ": ");
89   fprintf (stderr, fmt, l, p);
90   fprintf (stderr, "\n");
91   exit (1);
92 }
93
94 enum FriBidiCharTypeLinearEnumOffsetOne
95 {
96 # define _FRIBIDI_ADD_TYPE(TYPE,SYMBOL) TYPE,
97 # define _FRIBIDI_ADD_ALIAS(TYPE1,TYPE2) TYPE1 = TYPE2,
98 # include <fribidi-bidi-types-list.h>
99 # undef _FRIBIDI_ADD_ALIAS
100 # undef _FRIBIDI_ADD_TYPE
101   NUM_TYPES
102 };
103
104 struct
105 {
106   const char *name;
107   int key;
108 }
109 type_names[] =
110 {
111 # define _FRIBIDI_ADD_TYPE(TYPE,SYMBOL) {STRINGIZE(TYPE), TYPE},
112 # define _FRIBIDI_ADD_ALIAS(TYPE1,TYPE2) _FRIBIDI_ADD_TYPE(TYPE1, SYMBOL)
113 # include <fribidi-bidi-types-list.h>
114 # undef _FRIBIDI_ADD_ALIAS
115 # undef _FRIBIDI_ADD_TYPE
116 };
117
118 #define type_names_count (sizeof (type_names) / sizeof (type_names[0]))
119
120 static const char *names[type_names_count];
121
122 static char
123 get_type (
124   const char *s
125 )
126 {
127   unsigned int i;
128
129   for (i = 0; i < type_names_count; i++)
130     if (!strcmp (s, type_names[i].name))
131       return type_names[i].key;
132   die2 ("bidi type name `%s' not found", s);
133   return 0;
134 }
135
136 #define table_name "Bid"
137 #define macro_name "FRIBIDI_GET_BIDI_TYPE"
138
139 static signed int table[FRIBIDI_UNICODE_CHARS];
140 static char buf[4000];
141 static char tp[sizeof (buf)];
142
143 static void
144 init (
145   void
146 )
147 {
148   register int i;
149
150   for (i = 0; i < type_names_count; i++)
151     names[i] = 0;
152   for (i = type_names_count - 1; i >= 0; i--)
153     names[type_names[i].key] = type_names[i].name;
154 }
155
156 static void
157 clear_tab (
158   void
159 )
160 {
161   register FriBidiChar c;
162
163   for (c = 0; c < FRIBIDI_UNICODE_CHARS; c++)
164     table[c] = LTR;
165 }
166
167 static void
168 init_tab_unicode_data_txt (
169   void
170 )
171 {
172   register FriBidiChar c;
173
174   clear_tab ();
175
176   for (c = 0x0590; c < 0x0600; c++)
177     table[c] = RTL;
178   for (c = 0x07C0; c < 0x0900; c++)
179     table[c] = RTL;
180   for (c = 0xFB1D; c < 0xFB50; c++)
181     table[c] = RTL;
182
183   for (c = 0x0600; c < 0x07C0; c++)
184     table[c] = AL;
185   for (c = 0xFB50; c < 0xFDD0; c++)
186     table[c] = AL;
187   for (c = 0xFDF0; c < 0xFE00; c++)
188     table[c] = AL;
189   for (c = 0xFE70; c < 0xFF00; c++)
190     table[c] = AL;
191
192   for (c = 0x2060; c < 0x2070; c++)
193     table[c] = BN;
194   for (c = 0xFDD0; c < 0xFDF0; c++)
195     table[c] = BN;
196   for (c = 0xFFF0; c < 0xFFF9; c++)
197     table[c] = BN;
198   for (c = 0xFFFF; c < FRIBIDI_UNICODE_CHARS; c += 0x10000)
199     table[c - 1] = table[c] = BN;
200
201   if (FRIBIDI_UNICODE_CHARS > 0x10000)
202     {
203       for (c = 0x10800; c < 0x11000; c++)
204         table[c] = RTL;
205
206       for (c = 0xE0000; c < 0xE1000; c++)
207         table[c] = BN;
208     }
209 }
210
211 static void
212 init_tab_derived_bidi_class_txt (
213   void
214 )
215 {
216   clear_tab ();
217 }
218
219 static void
220 read_unicode_data_txt (
221   FILE *f
222 )
223 {
224   unsigned long c, l;
225
226   init_tab_unicode_data_txt ();
227
228   l = 0;
229   while (fgets (buf, sizeof buf, f))
230     {
231       int i;
232       const char *s = buf;
233
234       l++;
235
236       while (*s == ' ')
237         s++;
238
239       if (s[0] == '#' || s[0] == '\0' || s[0] == '\n')
240         continue;
241
242       i = sscanf (s, "%lx;%*[^;];%*[^;];%*[^;];%[^; ]", &c, tp);
243       if (i != 2 || c >= FRIBIDI_UNICODE_CHARS)
244         die3 ("invalid input at line %ld: %s", l, s);
245
246       /* FIXME: We don't handle First..Last parts of UnicodeData.txt,
247        * but it works, since all those are LTR. */
248       table[c] = get_type (tp);
249     }
250 }
251
252 static void
253 read_derived_bidi_class_txt (
254   FILE *f
255 )
256 {
257   unsigned long c, c2, l;
258
259   init_tab_derived_bidi_class_txt ();
260
261   l = 0;
262   while (fgets (buf, sizeof buf, f))
263     {
264       int i;
265       register char typ;
266       const char *s = buf;
267
268       l++;
269
270       while (*s == ' ')
271         s++;
272
273       if (s[0] == '#' || s[0] == '\0' || s[0] == '\n')
274         continue;
275
276       i = sscanf (s, "%lx ; %[^; ]", &c, tp);
277       if (i == 2)
278         c2 = c;
279       else
280         i = sscanf (s, "%lx..%lx ; %[^; ]", &c, &c2, tp) - 1;
281
282       if (i != 2 || c > c2 || c2 >= FRIBIDI_UNICODE_CHARS)
283         die3 ("invalid input at line %ld: %s", l, s);
284
285       typ = get_type (tp);
286       for (; c <= c2; c++)
287         table[c] = typ;
288     }
289 }
290
291 static void
292 read_data (
293   const char *data_file_type,
294   const char *data_file_name
295 )
296 {
297   FILE *f;
298
299   if (!(f = fopen (data_file_name, "rt")))
300     die2 ("error: cannot open `%s' for reading", data_file_name);
301
302   if (!strcmp (data_file_type, "UnicodeData.txt"))
303     read_unicode_data_txt (f);
304   else if (!strcmp (data_file_type, "DerivedBidiClass.txt"))
305     read_derived_bidi_class_txt (f);
306   else
307     die2 ("error: unknown data-file type %s", data_file_type);
308
309   fclose (f);
310 }
311
312 static void
313 gen_bidi_type_tab (
314   int max_depth,
315   const char *data_file_type
316 )
317 {
318   printf ("/* " outputname "\n * generated by " appname " (" FRIBIDI_NAME " "
319           FRIBIDI_VERSION ")\n" " * from the file %s of Unicode version "
320           FRIBIDI_UNICODE_VERSION ". */\n\n", data_file_type);
321
322   printf ("#define PACKTAB_UINT8 uint8_t\n"
323           "#define PACKTAB_UINT16 uint16_t\n"
324           "#define PACKTAB_UINT32 uint32_t\n\n");
325
326   if (!pack_table
327       (table, FRIBIDI_UNICODE_CHARS, 1, LTR, max_depth, 3, names,
328        "unsigned char", table_name, macro_name, stdout))
329     die ("error: insufficient memory, decrease max_depth");
330
331   printf ("#undef PACKTAB_UINT8\n"
332           "#undef PACKTAB_UINT16\n" "#undef PACKTAB_UINT32\n\n");
333
334   printf ("/* End of generated " outputname " */\n");
335 }
336
337 int
338 main (
339   int argc,
340   const char **argv
341 )
342 {
343   const char *data_file_type = "UnicodeData.txt";
344
345   if (argc < 3)
346     die2 ("usage:\n  " appname " max-depth /path/to/%s [junk...]",
347           data_file_type);
348
349   {
350     int max_depth = atoi (argv[1]);
351     const char *data_file_name = argv[2];
352
353     if (max_depth < 2)
354       die ("invalid depth");
355
356     init ();
357     read_data (data_file_type, data_file_name);
358     gen_bidi_type_tab (max_depth, data_file_type);
359   }
360
361   return 0;
362 }