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