1 /* gen-mirroring-tab.c - generate gmirroringtable.h for glib
2 * copied from FriBidi.
3 *
4 * $Id$
5 * $Author$
6 * $Date$
7 * $Revision$
8 * $Source$
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; if not, see <http://www.gnu.org/licenses/>.
28 *
29 * For licensing issues, contact <license@farsiweb.info>.
30 */
31
32 #include <glib.h>
33
34 #include <stdlib.h>
35 #include <stdio.h>
36
37 #include "packtab.h"
38
39 #define appname "gen-mirroring-tab"
40 #define outputname "gmirroringtable.h"
41
42 static void
43 die (
44 const char *msg
45 )
46 {
47 fprintf (stderr, appname ": %s\n", msg);
48 exit (1);
49 }
50
51 static void
52 die2 (
53 const char *fmt,
54 const char *p
55 )
56 {
57 fprintf (stderr, appname ": ");
58 fprintf (stderr, fmt, p);
59 fprintf (stderr, "\n");
60 exit (1);
61 }
62
63 static void
64 die4 (
65 const char *fmt,
66 unsigned long l,
67 unsigned long p,
68 unsigned long q
69 )
70 {
71 fprintf (stderr, appname ": ");
72 fprintf (stderr, fmt, l, p, q);
73 fprintf (stderr, "\n");
74 exit (1);
75 }
76
77 #define table_name "Mir"
78 #define macro_name "GLIB_GET_MIRRORING"
79
80 #define UNICODE_CHARS 0x110000
81
82 static signed int table[UNICODE_CHARS];
83 static char buf[4000];
84 static signed long max_dist;
85
86 static void
87 init (
88 void
89 )
90 {
91 max_dist = 0;
92 }
93
94 static void
95 clear_tab (
96 void
97 )
98 {
99 register gunichar c;
100
101 for (c = 0; c < UNICODE_CHARS; c++)
102 table[c] = 0;
103 }
104
105 static void
106 init_tab_mirroring_txt (
107 void
108 )
109 {
110 clear_tab ();
111 }
112
113 static void
114 read_bidi_mirroring_txt (
115 FILE *f
116 )
117 {
118 unsigned long l;
119
120 init_tab_mirroring_txt ();
121
122 l = 0;
123 while (fgets (buf, sizeof buf, f))
124 {
125 unsigned long i, j;
126 signed long dist;
127 int k;
128 const char *s = buf;
129
130 l++;
131
132 while (*s == ' ')
133 s++;
134
135 if (s[0] == '#' || s[0] == '\0' || s[0] == '\n')
136 continue;
137
138 k = sscanf (s, "%lx; %lx", &i, &j);
139 if (k != 2 || i >= UNICODE_CHARS || j >= UNICODE_CHARS)
140 die4 ("invalid pair in input at line %lu: %04lX, %04lX", l, i, j);
141 dist = ((signed long) j - (signed long) i);
142 table[i] = dist;
143 if (dist > max_dist)
144 max_dist = dist;
145 else if (-dist > max_dist)
146 max_dist = -dist;
147 }
148 }
149
150 static void
151 read_data (
152 const char *data_file_type,
153 const char *data_file_name
154 )
155 {
156 FILE *f;
157
158 fprintf (stderr, "Reading '%s'\n", data_file_name);
159 if (!(f = fopen (data_file_name, "rt")))
160 die2 ("error: cannot open '%s' for reading", data_file_name);
161
162 if (!strcmp (data_file_type, "BidiMirroring.txt"))
163 read_bidi_mirroring_txt (f);
164 else
165 die2 ("error: unknown data-file-type %s", data_file_type);
166
167 fclose (f);
168 }
169
170 static void
171 gen_mirroring_tab (
172 int max_depth,
173 const char *data_file_type
174 )
175 {
176 int key_bytes;
177 const char *key_type;
178
179 fprintf (stderr,
180 "Generating '" outputname "', it may take up to a few minutes\n");
181 printf ("/* " outputname "\n * generated by " appname " "
182 "\n" " * from the file %s of */\n\n", data_file_type);
183
184 printf ("#define PACKTAB_UINT8 guint8\n"
185 "#define PACKTAB_UINT16 guint16\n"
186 "#define PACKTAB_UINT32 guint32\n\n");
187
188 key_bytes = max_dist <= 0x7f ? 1 : max_dist < 0x7fff ? 2 : 4;
189 key_type = key_bytes == 1 ? "gint8" : key_bytes == 2 ?
190 "gint16" : "gint32";
191
192 if (!pack_table
193 (table, UNICODE_CHARS, key_bytes, 0, max_depth, 1, NULL,
194 key_type, table_name, macro_name "_DELTA", stdout))
195 die ("error: insufficient memory, decrease max_depth");
196
197 printf ("#undef PACKTAB_UINT8\n"
198 "#undef PACKTAB_UINT16\n" "#undef PACKTAB_UINT32\n\n");
199
200 printf ("#define " macro_name "(x) ((x) + " macro_name "_DELTA(x))\n\n");
201
202 printf ("/* End of generated " outputname " */\n");
203 }
204
205 int
206 main (
207 int argc,
208 const char **argv
209 )
210 {
211 const char *data_file_type = "BidiMirroring.txt";
212
213 if (argc < 3)
214 die2 ("usage:\n " appname " max-lookups /path/to/%s [junk...]",
215 data_file_type);
216
217 {
218 int max_depth = atoi (argv[1]);
219 const char *data_file_name = argv[2];
220
221 if (max_depth < 2)
222 die ("invalid depth");
223
224 init ();
225 read_data (data_file_type, data_file_name);
226 gen_mirroring_tab (max_depth, data_file_type);
227 }
228
229 return 0;
230 }