2 * gen-bidi-type-tab.c - generate bidi-type.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-bidi-type-tab"
54 #define outputname "bidi-type.tab.i"
61 fprintf (stderr, appname ": %s\n", msg);
71 fprintf (stderr, appname ": ");
72 fprintf (stderr, fmt, p);
73 fprintf (stderr, "\n");
84 fprintf (stderr, appname ": ");
85 fprintf (stderr, fmt, l, p);
86 fprintf (stderr, "\n");
90 enum FriBidiCharTypeLinearEnumOffsetOne
92 # define _FRIBIDI_ADD_TYPE(TYPE,SYMBOL) TYPE,
93 # define _FRIBIDI_ADD_ALIAS(TYPE1,TYPE2) TYPE1 = TYPE2,
94 # include <fribidi-bidi-types-list.h>
95 # undef _FRIBIDI_ADD_ALIAS
96 # undef _FRIBIDI_ADD_TYPE
107 # define _FRIBIDI_ADD_TYPE(TYPE,SYMBOL) {STRINGIZE(TYPE), TYPE},
108 # define _FRIBIDI_ADD_ALIAS(TYPE1,TYPE2) _FRIBIDI_ADD_TYPE(TYPE1, SYMBOL)
109 # include <fribidi-bidi-types-list.h>
110 # undef _FRIBIDI_ADD_ALIAS
111 # undef _FRIBIDI_ADD_TYPE
114 #define type_names_count (sizeof (type_names) / sizeof (type_names[0]))
116 static const char *names[type_names_count];
125 for (i = 0; i < type_names_count; i++)
126 if (!strcmp (s, type_names[i].name))
127 return type_names[i].key;
128 die2 ("bidi type name `%s' not found", s);
132 #define table_name "Bid"
133 #define macro_name "FRIBIDI_GET_BIDI_TYPE"
135 static signed int table[FRIBIDI_UNICODE_CHARS];
136 static char buf[4000];
137 static char tp[sizeof (buf)];
146 for (i = 0; i < type_names_count; i++)
148 for (i = type_names_count - 1; i >= 0; i--)
149 names[type_names[i].key] = type_names[i].name;
157 register FriBidiChar c;
159 for (c = 0; c < FRIBIDI_UNICODE_CHARS; c++)
164 init_tab_unicode_data_txt (
168 register FriBidiChar c;
172 for (c = 0x0590; c < 0x0600; c++)
174 for (c = 0x07C0; c < 0x0900; c++)
176 for (c = 0xFB1D; c < 0xFB50; c++)
179 for (c = 0x0600; c < 0x07C0; c++)
181 for (c = 0xFB50; c < 0xFDD0; c++)
183 for (c = 0xFDF0; c < 0xFE00; c++)
185 for (c = 0xFE70; c < 0xFF00; c++)
188 for (c = 0x2060; c < 0x2070; c++)
190 for (c = 0xFDD0; c < 0xFDF0; c++)
192 for (c = 0xFFF0; c < 0xFFF9; c++)
194 for (c = 0xFFFF; c < FRIBIDI_UNICODE_CHARS; c += 0x10000)
195 table[c - 1] = table[c] = BN;
197 if (FRIBIDI_UNICODE_CHARS > 0x10000)
199 for (c = 0x10800; c < 0x11000; c++)
202 for (c = 0xE0000; c < 0xE1000; c++)
208 init_tab_derived_bidi_class_txt (
216 read_unicode_data_txt (
222 init_tab_unicode_data_txt ();
225 while (fgets (buf, sizeof buf, f))
235 if (s[0] == '#' || s[0] == '\0' || s[0] == '\n')
238 i = sscanf (s, "%lx;%*[^;];%*[^;];%*[^;];%[^; ]", &c, tp);
239 if (i != 2 || c >= FRIBIDI_UNICODE_CHARS)
240 die3 ("invalid input at line %ld: %s", l, s);
242 /* FIXME: We don't handle First..Last parts of UnicodeData.txt,
243 * but it works, since all those are LTR. */
244 table[c] = get_type (tp);
249 read_derived_bidi_class_txt (
253 unsigned long c, c2, l;
255 init_tab_derived_bidi_class_txt ();
258 while (fgets (buf, sizeof buf, f))
269 if (s[0] == '#' || s[0] == '\0' || s[0] == '\n')
272 i = sscanf (s, "%lx ; %[^; ]", &c, tp);
276 i = sscanf (s, "%lx..%lx ; %[^; ]", &c, &c2, tp) - 1;
278 if (i != 2 || c > c2 || c2 >= FRIBIDI_UNICODE_CHARS)
279 die3 ("invalid input at line %ld: %s", l, s);
289 const char *data_file_type,
290 const char *data_file_name
295 if (!(f = fopen (data_file_name, "rt")))
296 die2 ("error: cannot open `%s' for reading", data_file_name);
298 if (!strcmp (data_file_type, "UnicodeData.txt"))
299 read_unicode_data_txt (f);
300 else if (!strcmp (data_file_type, "DerivedBidiClass.txt"))
301 read_derived_bidi_class_txt (f);
303 die2 ("error: unknown data-file type %s", data_file_type);
311 const char *data_file_type
314 printf ("/* " outputname "\n * generated by " appname " (" FRIBIDI_NAME " "
315 FRIBIDI_VERSION ")\n" " * from the file %s of Unicode version "
316 FRIBIDI_UNICODE_VERSION ". */\n\n", data_file_type);
318 printf ("#define PACKTAB_UINT8 uint8_t\n"
319 "#define PACKTAB_UINT16 uint16_t\n"
320 "#define PACKTAB_UINT32 uint32_t\n\n");
323 (table, FRIBIDI_UNICODE_CHARS, 1, LTR, max_depth, 3, names,
324 "unsigned char", table_name, macro_name, stdout))
325 die ("error: insufficient memory, decrease max_depth");
327 printf ("#undef PACKTAB_UINT8\n"
328 "#undef PACKTAB_UINT16\n" "#undef PACKTAB_UINT32\n\n");
330 printf ("/* End of generated " outputname " */\n");
339 const char *data_file_type = "UnicodeData.txt";
342 die2 ("usage:\n " appname " max-depth /path/to/%s [junk...]",
346 int max_depth = atoi (argv[1]);
347 const char *data_file_name = argv[2];
350 die ("invalid depth");
353 read_data (data_file_type, data_file_name);
354 gen_bidi_type_tab (max_depth, data_file_type);