Remove redundant header inclusions
[glib.git] / glib / glib-mirroring-tab / gen-mirroring-tab.c
blob6b163765759f7fef9dd4aa94171e6edcce5c05d8
1 /* gen-mirroring-tab.c - generate gmirroringtable.h for glib
2 * copied from FriBidi.
4 * $Id$
5 * $Author$
6 * $Date$
7 * $Revision$
8 * $Source$
10 * Author:
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>.
34 #include <glib.h>
36 #include <stdlib.h>
37 #include <stdio.h>
39 #include "packtab.h"
41 #define appname "gen-mirroring-tab"
42 #define outputname "gmirroringtable.h"
44 static void
45 die (
46 const char *msg
49 fprintf (stderr, appname ": %s\n", msg);
50 exit (1);
53 static void
54 die2 (
55 const char *fmt,
56 const char *p
59 fprintf (stderr, appname ": ");
60 fprintf (stderr, fmt, p);
61 fprintf (stderr, "\n");
62 exit (1);
65 static void
66 die4 (
67 const char *fmt,
68 unsigned long l,
69 unsigned long p,
70 unsigned long q
73 fprintf (stderr, appname ": ");
74 fprintf (stderr, fmt, l, p, q);
75 fprintf (stderr, "\n");
76 exit (1);
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;
88 static void
89 init (
90 void
93 max_dist = 0;
96 static void
97 clear_tab (
98 void
101 register gunichar c;
103 for (c = 0; c < UNICODE_CHARS; c++)
104 table[c] = 0;
107 static void
108 init_tab_mirroring_txt (
109 void
112 clear_tab ();
115 static void
116 read_bidi_mirroring_txt (
117 FILE *f
120 unsigned long l;
122 init_tab_mirroring_txt ();
124 l = 0;
125 while (fgets (buf, sizeof buf, f))
127 unsigned long i, j;
128 signed long dist;
129 int k;
130 const char *s = buf;
132 l++;
134 while (*s == ' ')
135 s++;
137 if (s[0] == '#' || s[0] == '\0' || s[0] == '\n')
138 continue;
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);
144 table[i] = dist;
145 if (dist > max_dist)
146 max_dist = dist;
147 else if (-dist > max_dist)
148 max_dist = -dist;
152 static void
153 read_data (
154 const char *data_file_type,
155 const char *data_file_name
158 FILE *f;
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);
166 else
167 die2 ("error: unknown data-file-type %s", data_file_type);
169 fclose (f);
172 static void
173 gen_mirroring_tab (
174 int max_depth,
175 const char *data_file_type
178 int key_bytes;
179 const char *key_type;
181 fprintf (stderr,
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 ?
192 "gint16" : "gint32";
194 if (!pack_table
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");
208 main (
209 int argc,
210 const char **argv
213 const char *data_file_type = "BidiMirroring.txt";
215 if (argc < 3)
216 die2 ("usage:\n " appname " max-lookups /path/to/%s [junk...]",
217 data_file_type);
220 int max_depth = atoi (argv[1]);
221 const char *data_file_name = argv[2];
223 if (max_depth < 2)
224 die ("invalid depth");
226 init ();
227 read_data (data_file_type, data_file_name);
228 gen_mirroring_tab (max_depth, data_file_type);
231 return 0;