Merge branch 'test-ip_mreq_source-android-only' into 'master'
[glib.git] / glib / glib-mirroring-tab / gen-mirroring-tab.c
blob19efbecf36ea4a3ee5c353cc89185752cdc55911
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; if not, see <http://www.gnu.org/licenses/>.
29 * For licensing issues, contact <license@farsiweb.info>.
32 #include <glib.h>
34 #include <stdlib.h>
35 #include <stdio.h>
37 #include "packtab.h"
39 #define appname "gen-mirroring-tab"
40 #define outputname "gmirroringtable.h"
42 static void
43 die (
44 const char *msg
47 fprintf (stderr, appname ": %s\n", msg);
48 exit (1);
51 static void
52 die2 (
53 const char *fmt,
54 const char *p
57 fprintf (stderr, appname ": ");
58 fprintf (stderr, fmt, p);
59 fprintf (stderr, "\n");
60 exit (1);
63 static void
64 die4 (
65 const char *fmt,
66 unsigned long l,
67 unsigned long p,
68 unsigned long q
71 fprintf (stderr, appname ": ");
72 fprintf (stderr, fmt, l, p, q);
73 fprintf (stderr, "\n");
74 exit (1);
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;
86 static void
87 init (
88 void
91 max_dist = 0;
94 static void
95 clear_tab (
96 void
99 register gunichar c;
101 for (c = 0; c < UNICODE_CHARS; c++)
102 table[c] = 0;
105 static void
106 init_tab_mirroring_txt (
107 void
110 clear_tab ();
113 static void
114 read_bidi_mirroring_txt (
115 FILE *f
118 unsigned long l;
120 init_tab_mirroring_txt ();
122 l = 0;
123 while (fgets (buf, sizeof buf, f))
125 unsigned long i, j;
126 signed long dist;
127 int k;
128 const char *s = buf;
130 l++;
132 while (*s == ' ')
133 s++;
135 if (s[0] == '#' || s[0] == '\0' || s[0] == '\n')
136 continue;
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);
142 table[i] = dist;
143 if (dist > max_dist)
144 max_dist = dist;
145 else if (-dist > max_dist)
146 max_dist = -dist;
150 static void
151 read_data (
152 const char *data_file_type,
153 const char *data_file_name
156 FILE *f;
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);
164 else
165 die2 ("error: unknown data-file-type %s", data_file_type);
167 fclose (f);
170 static void
171 gen_mirroring_tab (
172 int max_depth,
173 const char *data_file_type
176 int key_bytes;
177 const char *key_type;
179 fprintf (stderr,
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 ?
190 "gint16" : "gint32";
192 if (!pack_table
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");
206 main (
207 int argc,
208 const char **argv
211 const char *data_file_type = "BidiMirroring.txt";
213 if (argc < 3)
214 die2 ("usage:\n " appname " max-lookups /path/to/%s [junk...]",
215 data_file_type);
218 int max_depth = atoi (argv[1]);
219 const char *data_file_name = argv[2];
221 if (max_depth < 2)
222 die ("invalid depth");
224 init ();
225 read_data (data_file_type, data_file_name);
226 gen_mirroring_tab (max_depth, data_file_type);
229 return 0;