b3994c57cb1c96c464a9d47021c33bee83325263
[platform/upstream/fribidi.git] / gen.tab / gen-joining-type-tab.c
1 /* FriBidi
2  * gen-joining-type-tab.c - generate joining-type.tab.i
3  *
4  * $Id: gen-joining-type-tab.c,v 1.7 2010-12-07 19:44:26 behdad Exp $
5  * $Author: behdad $
6  * $Date: 2010-12-07 19:44:26 $
7  * $Revision: 1.7 $
8  * $Source: /home/behdad/src/fdo/fribidi/togit/git/../fribidi/fribidi2/gen.tab/gen-joining-type-tab.c,v $
9  *
10  * Author:
11  *   Behdad Esfahbod, 2004
12  *
13  * Copyright (C) 2004 Sharif FarsiWeb, Inc
14  * Copyright (C) 2004 Behdad Esfahbod
15  * 
16  * This library is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU Lesser General Public
18  * License as published by the Free Software Foundation; either
19  * version 2.1 of the License, or (at your option) any later version.
20  * 
21  * This library is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24  * Lesser General Public License for more details.
25  * 
26  * You should have received a copy of the GNU Lesser General Public License
27  * along with this library, in a file named COPYING; if not, write to the
28  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
29  * Boston, MA 02111-1307, USA
30  * 
31  * For licensing issues, contact <license@farsiweb.info>.
32  */
33
34 #include <common.h>
35
36 #include <fribidi-unicode.h>
37
38 #include <stdio.h>
39 #if STDC_HEADERS+0
40 # include <stdlib.h>
41 # include <stddef.h>
42 #else
43 # if HAVE_STDLIB_H
44 #  include <stdlib.h>
45 # endif
46 #endif
47 #if HAVE_STRING_H+0
48 # if !STDC_HEADERS && HAVE_MEMORY_H
49 #  include <memory.h>
50 # endif
51 # include <string.h>
52 #endif
53 #if HAVE_STRINGS_H+0
54 # include <strings.h>
55 #endif
56
57 #include "packtab.h"
58
59 #define appname "gen-joining-type-tab"
60 #define outputname "joining-type.tab.i"
61
62 static void
63 die (
64   const char *msg
65 )
66 {
67   fprintf (stderr, appname ": %s\n", msg);
68   exit (1);
69 }
70
71 static void
72 die2 (
73   const char *fmt,
74   const char *p
75 )
76 {
77   fprintf (stderr, appname ": ");
78   fprintf (stderr, fmt, p);
79   fprintf (stderr, "\n");
80   exit (1);
81 }
82
83 static void
84 die3 (
85   const char *fmt,
86   const char *p,
87   const char *q
88 )
89 {
90   fprintf (stderr, appname ": ");
91   fprintf (stderr, fmt, p, q);
92   fprintf (stderr, "\n");
93   exit (1);
94 }
95
96 static void
97 die3l (
98   const char *fmt,
99   unsigned long l,
100   const char *p
101 )
102 {
103   fprintf (stderr, appname ": ");
104   fprintf (stderr, fmt, l, p);
105   fprintf (stderr, "\n");
106   exit (1);
107 }
108
109 enum FriBidiJoiningLinearEnumOffsetOne
110 {
111 # define _FRIBIDI_ADD_TYPE(TYPE,SYMBOL) TYPE,
112 # include <fribidi-joining-types-list.h>
113 # undef _FRIBIDI_ADD_TYPE
114   NUM_TYPES
115 };
116
117 struct
118 {
119   const char *name;
120   int key;
121 }
122 type_names[] =
123 {
124 # define _FRIBIDI_ADD_TYPE(TYPE,SYMBOL) {STRINGIZE(TYPE), TYPE},
125 # include <fribidi-joining-types-list.h>
126 # undef _FRIBIDI_ADD_TYPE
127 };
128
129 #define type_names_count (sizeof (type_names) / sizeof (type_names[0]))
130
131 static const char *names[type_names_count];
132
133 static char
134 get_type (
135   const char *s
136 )
137 {
138   unsigned int i;
139
140   for (i = 0; i < type_names_count; i++)
141     if (!strcmp (s, type_names[i].name))
142       return type_names[i].key;
143   die2 ("joining type name `%s' not found", s);
144   return -1;
145 }
146
147 static const char *ignored_bidi_types[] = {
148   "BN",
149   "LRE",
150   "RLE",
151   "LRO",
152   "RLO",
153   "PDF",
154   NULL
155 };
156
157 static const char *transparent_general_categories[] = {
158   "Mn",
159   "Mn",
160   "Cf",
161   NULL
162 };
163
164 static const char *
165 type_is (
166   const char *s,
167   const char *type_list[]
168 )
169 {
170   for (; type_list[0]; type_list++)
171     if (!strcmp (s, type_list[0]))
172       return type_list[0];
173   return NULL;
174 }
175
176 #define table_name "Joi"
177 #define macro_name "FRIBIDI_GET_JOINING_TYPE"
178
179 static signed int table[FRIBIDI_UNICODE_CHARS];
180 static char buf[4000];
181 static char tp[sizeof (buf)], tp_gen[sizeof (buf)], tp_bidi[sizeof (buf)];
182
183 static void
184 clear_tab (
185   void
186 )
187 {
188   register FriBidiChar c;
189
190   for (c = 0; c < FRIBIDI_UNICODE_CHARS; c++)
191     table[c] = U;
192 }
193
194 static void
195 init (
196   void
197 )
198 {
199   register int i;
200
201   for (i = 0; i < type_names_count; i++)
202     names[i] = 0;
203   for (i = type_names_count - 1; i >= 0; i--)
204     names[type_names[i].key] = type_names[i].name;
205
206   clear_tab ();
207 }
208
209 static void
210 read_unicode_data_txt (
211   FILE *f
212 )
213 {
214   unsigned long c, l;
215
216   l = 0;
217   while (fgets (buf, sizeof buf, f))
218     {
219       int i;
220       const char *s = buf;
221
222       l++;
223
224       while (*s == ' ')
225         s++;
226
227       if (*s == '#' || *s == '\0' || *s == '\n')
228         continue;
229
230       i = sscanf (s, "%lx;%*[^;];%[^; ];%*[^;];%[^; ]", &c, tp_gen, tp_bidi);
231       if (i != 3 || c >= FRIBIDI_UNICODE_CHARS)
232         die3l ("UnicodeData.txt: invalid input at line %ld: %s", l, s);
233
234       if (type_is (tp_bidi, ignored_bidi_types))
235         table[c] = G;
236       if (type_is (tp_gen, transparent_general_categories))
237         table[c] = T;
238     }
239 }
240
241 static void
242 read_arabic_shaping_txt (
243   FILE *f
244 )
245 {
246   unsigned long c, c2, l;
247
248   l = 0;
249   while (fgets (buf, sizeof buf, f))
250     {
251       int i;
252       register char typ;
253       const char *s = buf;
254
255       l++;
256
257       while (*s == ' ')
258         s++;
259
260       if (*s == '#' || *s == '\0' || *s == '\n')
261         continue;
262
263       i = sscanf (s, "%lx ; %*[^;]; %[^; ]", &c, tp);
264       if (i == 2)
265         c2 = c;
266       else
267         i = sscanf (s, "%lx..%lx ; %*[^;]; %[^; ]", &c, &c2, tp) - 1;
268
269       if (i != 2 || c > c2 || c2 >= FRIBIDI_UNICODE_CHARS)
270         die3l ("ArabicShaping.txt: invalid input at line %ld: %s", l, s);
271
272       typ = get_type (tp);
273       for (; c <= c2; c++)
274         table[c] = typ;
275     }
276 }
277
278 static void
279 read_data (
280   const char *data_file_type[],
281   const char *data_file_name[]
282 )
283 {
284   FILE *f;
285
286   for (; data_file_name[0] && data_file_type[0];
287        data_file_name++, data_file_type++)
288     {
289       fprintf (stderr, "Reading `%s'\n", data_file_name[0]);
290       if (!(f = fopen (data_file_name[0], "rt")))
291         die2 ("error: cannot open `%s' for reading", data_file_name[0]);
292
293       if (!strcmp (data_file_type[0], "UnicodeData.txt"))
294         read_unicode_data_txt (f);
295       else if (!strcmp (data_file_type[0], "ArabicShaping.txt"))
296         read_arabic_shaping_txt (f);
297       else
298         die2 ("error: unknown data-file type %s", data_file_type[0]);
299
300       fclose (f);
301     }
302
303 }
304
305 static void
306 gen_joining_type_tab (
307   int max_depth,
308   const char *data_file_type[]
309 )
310 {
311   fprintf (stderr,
312            "Generating `" outputname "', it may take up to a few minutes\n");
313   printf ("/* " outputname "\n * generated by " appname " (" FRIBIDI_NAME " "
314           FRIBIDI_VERSION ")\n" " * from the files %s, %s of Unicode version "
315           FRIBIDI_UNICODE_VERSION ". */\n\n", data_file_type[0],
316           data_file_type[1]);
317
318   printf ("#define PACKTAB_UINT8 fribidi_uint8\n"
319           "#define PACKTAB_UINT16 fribidi_uint16\n"
320           "#define PACKTAB_UINT32 fribidi_uint32\n\n");
321
322   if (!pack_table
323       (table, FRIBIDI_UNICODE_CHARS, 1, U, max_depth, 1, 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[] =
340     { "UnicodeData.txt", "ArabicShaping.txt", NULL };
341
342   if (argc < 4)
343     die3 ("usage:\n  " appname " max-depth /path/to/%s /path/to/%s [junk...]",
344           data_file_type[0], data_file_type[1]);
345
346   {
347     int max_depth = atoi (argv[1]);
348     const char *data_file_name[] = { NULL, NULL, NULL };
349     data_file_name[0] = argv[2];
350     data_file_name[1] = argv[3];
351
352     if (max_depth < 2)
353       die ("invalid depth");
354
355     init ();
356     read_data (data_file_type, data_file_name);
357     gen_joining_type_tab (max_depth, data_file_type);
358   }
359
360   return 0;
361 }