1 /* gen-mirroring-tab.c - generate gmirroringtable.h for glib
11 * Behdad Esfahbod, 2001, 2002, 2004
13 * Copyright (C) 2004 Sharif FarsiWeb, Inc
14 * Copyright (C) 2001,2002,2004 Behdad Esfahbod
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.
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.
26 * You should have received a copy of the GNU Lesser General Public License
27 * along with this library, in a file named COPYING; if not, write to the
28 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
29 * Boston, MA 02111-1307, USA
31 * For licensing issues, contact <license@farsiweb.info>.
41 #define appname "gen-mirroring-tab"
42 #define outputname "gmirroringtable.h"
49 fprintf (stderr, appname ": %s\n", msg);
59 fprintf (stderr, appname ": ");
60 fprintf (stderr, fmt, p);
61 fprintf (stderr, "\n");
73 fprintf (stderr, appname ": ");
74 fprintf (stderr, fmt, l, p, q);
75 fprintf (stderr, "\n");
79 #define table_name "Mir"
80 #define macro_name "GLIB_GET_MIRRORING"
82 #define UNICODE_CHARS 0x110000
84 static signed int table[UNICODE_CHARS];
85 static char buf[4000];
86 static signed long max_dist;
103 for (c = 0; c < UNICODE_CHARS; c++)
108 init_tab_mirroring_txt (
116 read_bidi_mirroring_txt (
122 init_tab_mirroring_txt ();
125 while (fgets (buf, sizeof buf, f))
137 if (s[0] == '#' || s[0] == '\0' || s[0] == '\n')
140 k = sscanf (s, "%lx; %lx", &i, &j);
141 if (k != 2 || i >= UNICODE_CHARS || j >= UNICODE_CHARS)
142 die4 ("invalid pair in input at line %ld: %04lX, %04lX", l, i, j);
143 dist = ((signed long) j - (signed long) i);
147 else if (-dist > max_dist)
154 const char *data_file_type,
155 const char *data_file_name
160 fprintf (stderr, "Reading '%s'\n", data_file_name);
161 if (!(f = fopen (data_file_name, "rt")))
162 die2 ("error: cannot open '%s' for reading", data_file_name);
164 if (!strcmp (data_file_type, "BidiMirroring.txt"))
165 read_bidi_mirroring_txt (f);
167 die2 ("error: unknown data-file-type %s", data_file_type);
175 const char *data_file_type
179 const char *key_type;
182 "Generating '" outputname "', it may take up to a few minutes\n");
183 printf ("/* " outputname "\n * generated by " appname " "
184 "\n" " * from the file %s of */\n\n", data_file_type);
186 printf ("#define PACKTAB_UINT8 guint8\n"
187 "#define PACKTAB_UINT16 guint16\n"
188 "#define PACKTAB_UINT32 guint32\n\n");
190 key_bytes = max_dist <= 0x7f ? 1 : max_dist < 0x7fff ? 2 : 4;
191 key_type = key_bytes == 1 ? "gint8" : key_bytes == 2 ?
195 (table, UNICODE_CHARS, key_bytes, 0, max_depth, 1, NULL,
196 key_type, table_name, macro_name "_DELTA", stdout))
197 die ("error: insufficient memory, decrease max_depth");
199 printf ("#undef PACKTAB_UINT8\n"
200 "#undef PACKTAB_UINT16\n" "#undef PACKTAB_UINT32\n\n");
202 printf ("#define " macro_name "(x) ((x) + " macro_name "_DELTA(x))\n\n");
204 printf ("/* End of generated " outputname " */\n");
213 const char *data_file_type = "BidiMirroring.txt";
216 die2 ("usage:\n " appname " max-lookups /path/to/%s [junk...]",
220 int max_depth = atoi (argv[1]);
221 const char *data_file_name = argv[2];
224 die ("invalid depth");
227 read_data (data_file_type, data_file_name);
228 gen_mirroring_tab (max_depth, data_file_type);