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>
46 # if !STDC_HEADERS && HAVE_MEMORY_H
57 #define appname "gen-mirroring-tab"
58 #define outputname "mirroring.tab.i"
65 fprintf (stderr, appname ": %s\n", msg);
75 fprintf (stderr, appname ": ");
76 fprintf (stderr, fmt, p);
77 fprintf (stderr, "\n");
89 fprintf (stderr, appname ": ");
90 fprintf (stderr, fmt, l, p, q);
91 fprintf (stderr, "\n");
95 #define table_name "Mir"
96 #define macro_name "FRIBIDI_GET_MIRRORING"
98 static signed int table[FRIBIDI_UNICODE_CHARS];
99 static char buf[4000];
100 static signed long max_dist;
115 register FriBidiChar c;
117 for (c = 0; c < FRIBIDI_UNICODE_CHARS; c++)
122 init_tab_mirroring_txt (
130 read_bidi_mirroring_txt (
136 init_tab_mirroring_txt ();
139 while (fgets (buf, sizeof buf, f))
151 if (s[0] == '#' || s[0] == '\0' || s[0] == '\n')
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);
161 else if (-dist > max_dist)
168 const char *data_file_type,
169 const char *data_file_name
174 if (!(f = fopen (data_file_name, "rt")))
175 die2 ("error: cannot open `%s' for reading", data_file_name);
177 if (!strcmp (data_file_type, "BidiMirroring.txt"))
178 read_bidi_mirroring_txt (f);
180 die2 ("error: unknown data-file-type %s", data_file_type);
188 const char *data_file_type
192 const char *key_type;
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);
198 printf ("#define PACKTAB_UINT8 uint8_t\n"
199 "#define PACKTAB_UINT16 uint16_t\n"
200 "#define PACKTAB_UINT32 uint32_t\n\n");
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";
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");
211 printf ("#undef PACKTAB_UINT8\n"
212 "#undef PACKTAB_UINT16\n" "#undef PACKTAB_UINT32\n\n");
214 printf ("#define " macro_name "(x) ((x) + " macro_name "_DELTA(x))\n\n");
216 printf ("/* End of generated " outputname " */\n");
225 const char *data_file_type = "BidiMirroring.txt";
228 die2 ("usage:\n " appname " max-depth /path/to/%s [junk...]",
232 int max_depth = atoi (argv[1]);
233 const char *data_file_name = argv[2];
236 die ("invalid depth");
239 read_data (data_file_type, data_file_name);
240 gen_mirroring_tab (max_depth, data_file_type);