Imported Upstream version 1.0.10
[platform/upstream/fribidi.git] / gen.tab / gen-arabic-shaping-tab.c
1 /* FriBidi
2  * gen-arabic-shaping-tab.c - generate arabic-shaping.tab.i
3  *
4  * Author:
5  *   Behdad Esfahbod, 2004, 2005
6  *
7  * Copyright (C) 2004 Sharif FarsiWeb, Inc
8  * Copyright (C) 2004, 2005 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-arabic-shaping-tab"
58 #define outputname "arabic-shaping.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   const char *p,
85   const char *q
86 )
87 {
88   fprintf (stderr, appname ": ");
89   fprintf (stderr, fmt, p, q);
90   fprintf (stderr, "\n");
91   exit (1);
92 }
93
94 static void
95 die3l (
96   const char *fmt,
97   unsigned long l,
98   const char *p
99 )
100 {
101   fprintf (stderr, appname ": ");
102   fprintf (stderr, fmt, l, p);
103   fprintf (stderr, "\n");
104   exit (1);
105 }
106
107 static const char *arabic_shaping_tags[] = {
108   "isolated",
109   "final",
110   "initial",
111   "medial",
112   NULL
113 };
114
115 static int
116 shape_is (
117   const char *s,
118   const char *type_list[]
119 )
120 {
121   const char **p = type_list;
122   for (; *p; p++)
123     if (!strcmp (s, p[0]))
124       return  p - type_list;
125   return -1;
126 }
127
128 #define table_name "ArShap"
129 #define macro_name "FRIBIDI_GET_ARABIC_SHAPE_PRES"
130
131 #define START_CHAR 0x600
132 #define END_CHAR 0x700
133
134 static FriBidiChar table[FRIBIDI_UNICODE_CHARS][4];
135 static char buf[4000];
136 static char tag[sizeof (buf)], buf2[sizeof (buf)];
137 static FriBidiChar minshaped, maxshaped;
138
139 static void
140 clear_tab (
141   void
142 )
143 {
144   register FriBidiChar c;
145   register int shape;
146
147   for (c = 0; c < FRIBIDI_UNICODE_CHARS; c++)
148     for (shape = 0; shape < 4; shape++)
149       table[c][shape] = c;
150 }
151
152 static void
153 init (
154   void
155 )
156 {
157   minshaped = FRIBIDI_UNICODE_CHARS;
158   maxshaped = 0;
159
160   clear_tab ();
161 }
162
163 static void
164 read_unicode_data_txt (
165   FILE *f
166 )
167 {
168   unsigned long c, unshaped, l;
169
170   l = 0;
171   while (fgets (buf, sizeof buf, f))
172     {
173       int i, shape;
174       const char *s = buf;
175
176       l++;
177
178       while (*s == ' ')
179         s++;
180
181       if (*s == '#' || *s == '\0' || *s == '\n')
182         continue;
183
184       i = sscanf (s, "%lx;%*[^;];%*[^;];%*[^;];%*[^;];<%[^;> ]> %lx %[^; ]", &c, tag, &unshaped, buf2);
185
186       if (i != 3)
187         continue;
188
189       if (i != 3 || c >= FRIBIDI_UNICODE_CHARS || unshaped >= FRIBIDI_UNICODE_CHARS)
190         die3l ("UnicodeData.txt: invalid input at line %ld: %s", l, s);
191
192       shape = shape_is (tag, arabic_shaping_tags);
193       if (shape >= 0)
194         {
195           table[unshaped][shape] = c;
196           if (unshaped < minshaped)
197             minshaped = unshaped;
198           if (unshaped > maxshaped)
199             maxshaped = unshaped;
200         }
201     }
202 }
203
204 static void
205 read_data (
206   const char *data_file_type[],
207   const char *data_file_name[]
208 )
209 {
210   FILE *f;
211
212   for (; data_file_name[0] && data_file_type[0];
213        data_file_name++, data_file_type++)
214     {
215       if (!(f = fopen (data_file_name[0], "rt")))
216         die2 ("error: cannot open `%s' for reading", data_file_name[0]);
217
218       if (!strcmp (data_file_type[0], "UnicodeData.txt"))
219         read_unicode_data_txt (f);
220       else
221         die2 ("error: unknown data-file type %s", data_file_type[0]);
222
223       fclose (f);
224     }
225
226 }
227
228 static void
229 gen_arabic_shaping_tab (
230   int max_depth /* currently unused */,
231   const char *data_file_type[]
232 )
233 {
234   register FriBidiChar c;
235   register int shape;
236  
237   if (maxshaped < minshaped)
238     die ("error: no shaping pair found, something wrong with reading input");
239
240   printf ("/* " outputname "\n * generated by " appname " (" FRIBIDI_NAME " "
241           FRIBIDI_VERSION ")\n" " * from the files %s, %s of Unicode version "
242           FRIBIDI_UNICODE_VERSION ". */\n\n", data_file_type[0],
243           data_file_type[1]);
244
245   printf ("/*\n"
246            "  use %s(key,shape) to access your table\n\n"
247            "  required memory: %ld\n"
248            " */\n\n",
249            macro_name, (long)(maxshaped - minshaped + 1) * 4 * sizeof (FriBidiChar));
250
251   printf ("\n" "/* *IND" "ENT-OFF* */\n\n");
252
253   printf ("static const FriBidiChar %s[%d][%d] = {\n", table_name, maxshaped - minshaped + 1, 4);
254   for (c = minshaped; c <= maxshaped; c++)
255     {
256       printf ("  {");
257       for (shape = 0; shape < 4; shape++)
258         printf ("0x%04lx,", (unsigned long)table[c][shape]);
259       printf ("},\n");
260     }
261       
262
263   printf ("};\n\n");
264
265   printf ("/* *IND" "ENT-ON* */\n\n");
266
267   printf ("#ifndef FRIBIDI_ACCESS_SHAPE_TABLE\n"
268           "# define FRIBIDI_ACCESS_SHAPE_TABLE(table,min,max,x,shape) \\\n"
269           "     (((x)<(min)||(x)>(max))?(x):(table)[(x)-(min)][(shape)])\n"
270           "#endif\n\n");
271   printf ("#define %s(x,shape) "
272           "FRIBIDI_ACCESS_SHAPE_TABLE(%s, 0x%04lx, 0x%04lx, (x), (shape))\n\n",
273           macro_name, table_name, (unsigned long)minshaped, (unsigned long)maxshaped);
274
275   printf ("/* End of generated " outputname " */\n");
276 }
277
278 int
279 main (
280   int argc,
281   const char **argv
282 )
283 {
284   const char *data_file_type[] =
285     { "UnicodeData.txt", NULL };
286
287   if (argc < 3)
288     die3 ("usage:\n  " appname " max-depth /path/to/%s /path/to/%s [junk...]",
289           data_file_type[0], data_file_type[1]);
290
291   {
292     int max_depth = atoi (argv[1]);
293     const char *data_file_name[] = { NULL, NULL };
294     data_file_name[0] = argv[2];
295
296     if (max_depth < 2)
297       die ("invalid depth");
298
299     init ();
300     read_data (data_file_type, data_file_name);
301     gen_arabic_shaping_tab (max_depth, data_file_type);
302   }
303
304   return 0;
305 }