2 * gen-mirroring-tab.c - generate mirroring.tab.i
5 * Behdad Esfahbod, 2001, 2002, 2004
7 * Copyright (C) 2004 Sharif FarsiWeb, Inc
8 * Copyright (C) 2001,2002,2004 Behdad Esfahbod
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.
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.
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
25 * For licensing issues, contact <fribidi.license@gmail.com>.
30 #include <fribidi-unicode.h>
42 # if !STDC_HEADERS && HAVE_MEMORY_H
53 #define appname "gen-mirroring-tab"
54 #define outputname "mirroring.tab.i"
61 fprintf (stderr, appname ": %s\n", msg);
71 fprintf (stderr, appname ": ");
72 fprintf (stderr, fmt, p);
73 fprintf (stderr, "\n");
85 fprintf (stderr, appname ": ");
86 fprintf (stderr, fmt, l, p, q);
87 fprintf (stderr, "\n");
91 #define table_name "Mir"
92 #define macro_name "FRIBIDI_GET_MIRRORING"
94 static signed int table[FRIBIDI_UNICODE_CHARS];
95 static char buf[4000];
96 static signed long max_dist;
111 register FriBidiChar c;
113 for (c = 0; c < FRIBIDI_UNICODE_CHARS; c++)
118 init_tab_mirroring_txt (
126 read_bidi_mirroring_txt (
132 init_tab_mirroring_txt ();
135 while (fgets (buf, sizeof buf, f))
147 if (s[0] == '#' || s[0] == '\0' || s[0] == '\n')
150 k = sscanf (s, "%lx; %lx", &i, &j);
151 if (k != 2 || i >= FRIBIDI_UNICODE_CHARS || j >= FRIBIDI_UNICODE_CHARS)
152 die4 ("invalid pair in input at line %ld: %04lX, %04lX", l, i, j);
153 dist = ((signed long) j - (signed long) i);
157 else if (-dist > max_dist)
164 const char *data_file_type,
165 const char *data_file_name
170 if (!(f = fopen (data_file_name, "rt")))
171 die2 ("error: cannot open `%s' for reading", data_file_name);
173 if (!strcmp (data_file_type, "BidiMirroring.txt"))
174 read_bidi_mirroring_txt (f);
176 die2 ("error: unknown data-file-type %s", data_file_type);
184 const char *data_file_type
188 const char *key_type;
190 printf ("/* " outputname "\n * generated by " appname " (" FRIBIDI_NAME " "
191 FRIBIDI_VERSION ")\n" " * from the file %s of Unicode version "
192 FRIBIDI_UNICODE_VERSION ". */\n\n", data_file_type);
194 printf ("#define PACKTAB_UINT8 uint8_t\n"
195 "#define PACKTAB_UINT16 uint16_t\n"
196 "#define PACKTAB_UINT32 uint32_t\n\n");
198 key_bytes = max_dist <= 0x7f ? 1 : max_dist < 0x7fff ? 2 : 4;
199 key_type = key_bytes == 1 ? "int8_t" : key_bytes == 2 ?
200 "int16_t" : "int32_t";
203 (table, FRIBIDI_UNICODE_CHARS, key_bytes, 0, max_depth, 1, NULL,
204 key_type, table_name, macro_name "_DELTA", stdout))
205 die ("error: insufficient memory, decrease max_depth");
207 printf ("#undef PACKTAB_UINT8\n"
208 "#undef PACKTAB_UINT16\n" "#undef PACKTAB_UINT32\n\n");
210 printf ("#define " macro_name "(x) ((x) + " macro_name "_DELTA(x))\n\n");
212 printf ("/* End of generated " outputname " */\n");
221 const char *data_file_type = "BidiMirroring.txt";
224 die2 ("usage:\n " appname " max-depth /path/to/%s [junk...]",
228 int max_depth = atoi (argv[1]);
229 const char *data_file_name = argv[2];
232 die ("invalid depth");
235 read_data (data_file_type, data_file_name);
236 gen_mirroring_tab (max_depth, data_file_type);