Merge branch 'upstream' into tizen
[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 STDC_HEADERS
34 # include <stdlib.h>
35 # include <stddef.h>
36 #else
37 # if HAVE_STDLIB_H
38 #  include <stdlib.h>
39 # endif
40 #endif
41 #ifdef HAVE_STRING_H
42 # if !STDC_HEADERS && HAVE_MEMORY_H
43 #  include <memory.h>
44 # endif
45 # include <string.h>
46 #endif
47 #ifdef HAVE_STRINGS_H
48 # include <strings.h>
49 #endif
50
51 #include "packtab.h"
52
53 #define appname "gen-bidi-type-tab"
54 #define outputname "bidi-type.tab.i"
55
56 static void
57 die (
58   const char *msg
59 )
60 {
61   fprintf (stderr, appname ": %s\n", msg);
62   exit (1);
63 }
64
65 static void
66 die2 (
67   const char *fmt,
68   const char *p
69 )
70 {
71   fprintf (stderr, appname ": ");
72   fprintf (stderr, fmt, p);
73   fprintf (stderr, "\n");
74   exit (1);
75 }
76
77 static void
78 die3 (
79   const char *fmt,
80   unsigned long l,
81   const char *p
82 )
83 {
84   fprintf (stderr, appname ": ");
85   fprintf (stderr, fmt, l, p);
86   fprintf (stderr, "\n");
87   exit (1);
88 }
89
90 enum FriBidiCharTypeLinearEnumOffsetOne
91 {
92 # define _FRIBIDI_ADD_TYPE(TYPE,SYMBOL) TYPE,
93 # define _FRIBIDI_ADD_ALIAS(TYPE1,TYPE2) TYPE1 = TYPE2,
94 # include <fribidi-bidi-types-list.h>
95 # undef _FRIBIDI_ADD_ALIAS
96 # undef _FRIBIDI_ADD_TYPE
97   NUM_TYPES
98 };
99
100 struct
101 {
102   const char *name;
103   int key;
104 }
105 type_names[] =
106 {
107 # define _FRIBIDI_ADD_TYPE(TYPE,SYMBOL) {STRINGIZE(TYPE), TYPE},
108 # define _FRIBIDI_ADD_ALIAS(TYPE1,TYPE2) _FRIBIDI_ADD_TYPE(TYPE1, SYMBOL)
109 # include <fribidi-bidi-types-list.h>
110 # undef _FRIBIDI_ADD_ALIAS
111 # undef _FRIBIDI_ADD_TYPE
112 };
113
114 #define type_names_count (sizeof (type_names) / sizeof (type_names[0]))
115
116 static const char *names[type_names_count];
117
118 static char
119 get_type (
120   const char *s
121 )
122 {
123   unsigned int i;
124
125   for (i = 0; i < type_names_count; i++)
126     if (!strcmp (s, type_names[i].name))
127       return type_names[i].key;
128   die2 ("bidi type name `%s' not found", s);
129   return 0;
130 }
131
132 #define table_name "Bid"
133 #define macro_name "FRIBIDI_GET_BIDI_TYPE"
134
135 static signed int table[FRIBIDI_UNICODE_CHARS];
136 static char buf[4000];
137 static char tp[sizeof (buf)];
138
139 static void
140 init (
141   void
142 )
143 {
144   register int i;
145
146   for (i = 0; i < type_names_count; i++)
147     names[i] = 0;
148   for (i = type_names_count - 1; i >= 0; i--)
149     names[type_names[i].key] = type_names[i].name;
150 }
151
152 static void
153 clear_tab (
154   void
155 )
156 {
157   register FriBidiChar c;
158
159   for (c = 0; c < FRIBIDI_UNICODE_CHARS; c++)
160     table[c] = LTR;
161 }
162
163 static void
164 init_tab_unicode_data_txt (
165   void
166 )
167 {
168   register FriBidiChar c;
169
170   clear_tab ();
171
172   for (c = 0x0590; c < 0x0600; c++)
173     table[c] = RTL;
174   for (c = 0x07C0; c < 0x0900; c++)
175     table[c] = RTL;
176   for (c = 0xFB1D; c < 0xFB50; c++)
177     table[c] = RTL;
178
179   for (c = 0x0600; c < 0x07C0; c++)
180     table[c] = AL;
181   for (c = 0xFB50; c < 0xFDD0; c++)
182     table[c] = AL;
183   for (c = 0xFDF0; c < 0xFE00; c++)
184     table[c] = AL;
185   for (c = 0xFE70; c < 0xFF00; c++)
186     table[c] = AL;
187
188   for (c = 0x2060; c < 0x2070; c++)
189     table[c] = BN;
190   for (c = 0xFDD0; c < 0xFDF0; c++)
191     table[c] = BN;
192   for (c = 0xFFF0; c < 0xFFF9; c++)
193     table[c] = BN;
194   for (c = 0xFFFF; c < FRIBIDI_UNICODE_CHARS; c += 0x10000)
195     table[c - 1] = table[c] = BN;
196
197   if (FRIBIDI_UNICODE_CHARS > 0x10000)
198     {
199       for (c = 0x10800; c < 0x11000; c++)
200         table[c] = RTL;
201
202       for (c = 0xE0000; c < 0xE1000; c++)
203         table[c] = BN;
204     }
205 }
206
207 static void
208 init_tab_derived_bidi_class_txt (
209   void
210 )
211 {
212   clear_tab ();
213 }
214
215 static void
216 read_unicode_data_txt (
217   FILE *f
218 )
219 {
220   unsigned long c, l;
221
222   init_tab_unicode_data_txt ();
223
224   l = 0;
225   while (fgets (buf, sizeof buf, f))
226     {
227       int i;
228       const char *s = buf;
229
230       l++;
231
232       while (*s == ' ')
233         s++;
234
235       if (s[0] == '#' || s[0] == '\0' || s[0] == '\n')
236         continue;
237
238       i = sscanf (s, "%lx;%*[^;];%*[^;];%*[^;];%[^; ]", &c, tp);
239       if (i != 2 || c >= FRIBIDI_UNICODE_CHARS)
240         die3 ("invalid input at line %ld: %s", l, s);
241
242       /* FIXME: We don't handle First..Last parts of UnicodeData.txt,
243        * but it works, since all those are LTR. */
244       table[c] = get_type (tp);
245     }
246 }
247
248 static void
249 read_derived_bidi_class_txt (
250   FILE *f
251 )
252 {
253   unsigned long c, c2, l;
254
255   init_tab_derived_bidi_class_txt ();
256
257   l = 0;
258   while (fgets (buf, sizeof buf, f))
259     {
260       int i;
261       register char typ;
262       const char *s = buf;
263
264       l++;
265
266       while (*s == ' ')
267         s++;
268
269       if (s[0] == '#' || s[0] == '\0' || s[0] == '\n')
270         continue;
271
272       i = sscanf (s, "%lx ; %[^; ]", &c, tp);
273       if (i == 2)
274         c2 = c;
275       else
276         i = sscanf (s, "%lx..%lx ; %[^; ]", &c, &c2, tp) - 1;
277
278       if (i != 2 || c > c2 || c2 >= FRIBIDI_UNICODE_CHARS)
279         die3 ("invalid input at line %ld: %s", l, s);
280
281       typ = get_type (tp);
282       for (; c <= c2; c++)
283         table[c] = typ;
284     }
285 }
286
287 static void
288 read_data (
289   const char *data_file_type,
290   const char *data_file_name
291 )
292 {
293   FILE *f;
294
295   if (!(f = fopen (data_file_name, "rt")))
296     die2 ("error: cannot open `%s' for reading", data_file_name);
297
298   if (!strcmp (data_file_type, "UnicodeData.txt"))
299     read_unicode_data_txt (f);
300   else if (!strcmp (data_file_type, "DerivedBidiClass.txt"))
301     read_derived_bidi_class_txt (f);
302   else
303     die2 ("error: unknown data-file type %s", data_file_type);
304
305   fclose (f);
306 }
307
308 static void
309 gen_bidi_type_tab (
310   int max_depth,
311   const char *data_file_type
312 )
313 {
314   printf ("/* " outputname "\n * generated by " appname " (" FRIBIDI_NAME " "
315           FRIBIDI_VERSION ")\n" " * from the file %s of Unicode version "
316           FRIBIDI_UNICODE_VERSION ". */\n\n", data_file_type);
317
318   printf ("#define PACKTAB_UINT8 uint8_t\n"
319           "#define PACKTAB_UINT16 uint16_t\n"
320           "#define PACKTAB_UINT32 uint32_t\n\n");
321
322   if (!pack_table
323       (table, FRIBIDI_UNICODE_CHARS, 1, LTR, max_depth, 3, names,
324        "unsigned char", table_name, macro_name, stdout))
325     die ("error: insufficient memory, decrease max_depth");
326
327   printf ("#undef PACKTAB_UINT8\n"
328           "#undef PACKTAB_UINT16\n" "#undef PACKTAB_UINT32\n\n");
329
330   printf ("/* End of generated " outputname " */\n");
331 }
332
333 int
334 main (
335   int argc,
336   const char **argv
337 )
338 {
339   const char *data_file_type = "UnicodeData.txt";
340
341   if (argc < 3)
342     die2 ("usage:\n  " appname " max-depth /path/to/%s [junk...]",
343           data_file_type);
344
345   {
346     int max_depth = atoi (argv[1]);
347     const char *data_file_name = argv[2];
348
349     if (max_depth < 2)
350       die ("invalid depth");
351
352     init ();
353     read_data (data_file_type, data_file_name);
354     gen_bidi_type_tab (max_depth, data_file_type);
355   }
356
357   return 0;
358 }