Set license using %license
[platform/upstream/fribidi.git] / gen.tab / gen-mirroring-tab.c
1 /* FriBidi
2  * gen-mirroring-tab.c - generate mirroring.tab.i
3  *
4  * $Id: gen-mirroring-tab.c,v 1.14 2006-01-31 03:23:12 behdad Exp $
5  * $Author: behdad $
6  * $Date: 2006-01-31 03:23:12 $
7  * $Revision: 1.14 $
8  * $Source: /home/behdad/src/fdo/fribidi/togit/git/../fribidi/fribidi2/gen.tab/gen-mirroring-tab.c,v $
9  *
10  * Author:
11  *   Behdad Esfahbod, 2001, 2002, 2004
12  *
13  * Copyright (C) 2004 Sharif FarsiWeb, Inc
14  * Copyright (C) 2001,2002,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., 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-mirroring-tab"
60 #define outputname "mirroring.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 die4 (
85   const char *fmt,
86   unsigned long l,
87   unsigned long p,
88   unsigned long q
89 )
90 {
91   fprintf (stderr, appname ": ");
92   fprintf (stderr, fmt, l, p, q);
93   fprintf (stderr, "\n");
94   exit (1);
95 }
96
97 #define table_name "Mir"
98 #define macro_name "FRIBIDI_GET_MIRRORING"
99
100 static signed int table[FRIBIDI_UNICODE_CHARS];
101 static char buf[4000];
102 static signed long max_dist;
103
104 static void
105 init (
106   void
107 )
108 {
109   max_dist = 0;
110 }
111
112 static void
113 clear_tab (
114   void
115 )
116 {
117   register FriBidiChar c;
118
119   for (c = 0; c < FRIBIDI_UNICODE_CHARS; c++)
120     table[c] = 0;
121 }
122
123 static void
124 init_tab_mirroring_txt (
125   void
126 )
127 {
128   clear_tab ();
129 }
130
131 static void
132 read_bidi_mirroring_txt (
133   FILE *f
134 )
135 {
136   unsigned long l;
137
138   init_tab_mirroring_txt ();
139
140   l = 0;
141   while (fgets (buf, sizeof buf, f))
142     {
143       unsigned long i, j;
144       signed long dist;
145       int k;
146       const char *s = buf;
147
148       l++;
149
150       while (*s == ' ')
151         s++;
152
153       if (s[0] == '#' || s[0] == '\0' || s[0] == '\n')
154         continue;
155
156       k = sscanf (s, "%lx; %lx", &i, &j);
157       if (k != 2 || i >= FRIBIDI_UNICODE_CHARS || j >= FRIBIDI_UNICODE_CHARS)
158         die4 ("invalid pair in input at line %ld: %04lX, %04lX", l, i, j);
159       dist = ((signed long) j - (signed long) i);
160       table[i] = dist;
161       if (dist > max_dist)
162         max_dist = dist;
163       else if (-dist > max_dist)
164         max_dist = -dist;
165     }
166 }
167
168 static void
169 read_data (
170   const char *data_file_type,
171   const char *data_file_name
172 )
173 {
174   FILE *f;
175
176   fprintf (stderr, "Reading `%s'\n", data_file_name);
177   if (!(f = fopen (data_file_name, "rt")))
178     die2 ("error: cannot open `%s' for reading", data_file_name);
179
180   if (!strcmp (data_file_type, "BidiMirroring.txt"))
181     read_bidi_mirroring_txt (f);
182   else
183     die2 ("error: unknown data-file-type %s", data_file_type);
184
185   fclose (f);
186 }
187
188 static void
189 gen_mirroring_tab (
190   int max_depth,
191   const char *data_file_type
192 )
193 {
194   int key_bytes;
195   const char *key_type;
196
197   fprintf (stderr,
198            "Generating `" outputname "', it may take up to a few minutes\n");
199   printf ("/* " outputname "\n * generated by " appname " (" FRIBIDI_NAME " "
200           FRIBIDI_VERSION ")\n" " * from the file %s of Unicode version "
201           FRIBIDI_UNICODE_VERSION ". */\n\n", data_file_type);
202
203   printf ("#define PACKTAB_UINT8 fribidi_uint8\n"
204           "#define PACKTAB_UINT16 fribidi_uint16\n"
205           "#define PACKTAB_UINT32 fribidi_uint32\n\n");
206
207   key_bytes = max_dist <= 0x7f ? 1 : max_dist < 0x7fff ? 2 : 4;
208   key_type = key_bytes == 1 ? "fribidi_int8" : key_bytes == 2 ?
209     "fribidi_int16" : "fribidi_int32";
210
211   if (!pack_table
212       (table, FRIBIDI_UNICODE_CHARS, key_bytes, 0, max_depth, 1, NULL,
213        key_type, table_name, macro_name "_DELTA", stdout))
214     die ("error: insufficient memory, decrease max_depth");
215
216   printf ("#undef PACKTAB_UINT8\n"
217           "#undef PACKTAB_UINT16\n" "#undef PACKTAB_UINT32\n\n");
218
219   printf ("#define " macro_name "(x) ((x) + " macro_name "_DELTA(x))\n\n");
220
221   printf ("/* End of generated " outputname " */\n");
222 }
223
224 int
225 main (
226   int argc,
227   const char **argv
228 )
229 {
230   const char *data_file_type = "BidiMirroring.txt";
231
232   if (argc < 3)
233     die2 ("usage:\n  " appname " max-depth /path/to/%s [junk...]",
234           data_file_type);
235
236   {
237     int max_depth = atoi (argv[1]);
238     const char *data_file_name = argv[2];
239
240     if (max_depth < 2)
241       die ("invalid depth");
242
243     init ();
244     read_data (data_file_type, data_file_name);
245     gen_mirroring_tab (max_depth, data_file_type);
246   }
247
248   return 0;
249 }