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