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; if not, see <http://www.gnu.org/licenses/>.
29 * For licensing issues, contact <license@farsiweb.info>.
39 #define appname "gen-mirroring-tab"
40 #define outputname "gmirroringtable.h"
47 fprintf (stderr, appname ": %s\n", msg);
57 fprintf (stderr, appname ": ");
58 fprintf (stderr, fmt, p);
59 fprintf (stderr, "\n");
71 fprintf (stderr, appname ": ");
72 fprintf (stderr, fmt, l, p, q);
73 fprintf (stderr, "\n");
77 #define table_name "Mir"
78 #define macro_name "GLIB_GET_MIRRORING"
80 #define UNICODE_CHARS 0x110000
82 static signed int table[UNICODE_CHARS];
83 static char buf[4000];
84 static signed long max_dist;
101 for (c = 0; c < UNICODE_CHARS; c++)
106 init_tab_mirroring_txt (
114 read_bidi_mirroring_txt (
120 init_tab_mirroring_txt ();
123 while (fgets (buf, sizeof buf, f))
135 if (s[0] == '#' || s[0] == '\0' || s[0] == '\n')
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);
145 else if (-dist > max_dist)
152 const char *data_file_type,
153 const char *data_file_name
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);
162 if (!strcmp (data_file_type, "BidiMirroring.txt"))
163 read_bidi_mirroring_txt (f);
165 die2 ("error: unknown data-file-type %s", data_file_type);
173 const char *data_file_type
177 const char *key_type;
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);
184 printf ("#define PACKTAB_UINT8 guint8\n"
185 "#define PACKTAB_UINT16 guint16\n"
186 "#define PACKTAB_UINT32 guint32\n\n");
188 key_bytes = max_dist <= 0x7f ? 1 : max_dist < 0x7fff ? 2 : 4;
189 key_type = key_bytes == 1 ? "gint8" : key_bytes == 2 ?
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");
197 printf ("#undef PACKTAB_UINT8\n"
198 "#undef PACKTAB_UINT16\n" "#undef PACKTAB_UINT32\n\n");
200 printf ("#define " macro_name "(x) ((x) + " macro_name "_DELTA(x))\n\n");
202 printf ("/* End of generated " outputname " */\n");
211 const char *data_file_type = "BidiMirroring.txt";
214 die2 ("usage:\n " appname " max-lookups /path/to/%s [junk...]",
218 int max_depth = atoi (argv[1]);
219 const char *data_file_name = argv[2];
222 die ("invalid depth");
225 read_data (data_file_type, data_file_name);
226 gen_mirroring_tab (max_depth, data_file_type);