[kdbus] KDBUS_ITEM_PAYLOAD_OFF items are (once again) relative to msg header
[platform/upstream/glib.git] / glib / glib-mirroring-tab / gen-mirroring-tab.c
1 /* gen-mirroring-tab.c - generate gmirroringtable.h for glib
2  * copied from FriBidi.
3  *
4  * $Id$
5  * $Author$
6  * $Date$
7  * $Revision$
8  * $Source$
9  *
10  * Author:
11  *   Behdad Esfahbod, 2001, 2002, 2004
12  *
13  * Copyright (C) 2004 Sharif FarsiWeb, Inc
14  * Copyright (C) 2001,2002,2004 Behdad Esfahbod
15  * 
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.
20  * 
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.
25  * 
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
30  * 
31  * For licensing issues, contact <license@farsiweb.info>.
32  */
33
34 #include <glib.h>
35
36 #include <stdlib.h>
37 #include <stdio.h>
38
39 #include "packtab.h"
40
41 #define appname "gen-mirroring-tab"
42 #define outputname "gmirroringtable.h"
43
44 static void
45 die (
46   const char *msg
47 )
48 {
49   fprintf (stderr, appname ": %s\n", msg);
50   exit (1);
51 }
52
53 static void
54 die2 (
55   const char *fmt,
56   const char *p
57 )
58 {
59   fprintf (stderr, appname ": ");
60   fprintf (stderr, fmt, p);
61   fprintf (stderr, "\n");
62   exit (1);
63 }
64
65 static void
66 die4 (
67   const char *fmt,
68   unsigned long l,
69   unsigned long p,
70   unsigned long q
71 )
72 {
73   fprintf (stderr, appname ": ");
74   fprintf (stderr, fmt, l, p, q);
75   fprintf (stderr, "\n");
76   exit (1);
77 }
78
79 #define table_name "Mir"
80 #define macro_name "GLIB_GET_MIRRORING"
81
82 #define UNICODE_CHARS 0x110000
83
84 static signed int table[UNICODE_CHARS];
85 static char buf[4000];
86 static signed long max_dist;
87
88 static void
89 init (
90   void
91 )
92 {
93   max_dist = 0;
94 }
95
96 static void
97 clear_tab (
98   void
99 )
100 {
101   register gunichar c;
102
103   for (c = 0; c < UNICODE_CHARS; c++)
104     table[c] = 0;
105 }
106
107 static void
108 init_tab_mirroring_txt (
109   void
110 )
111 {
112   clear_tab ();
113 }
114
115 static void
116 read_bidi_mirroring_txt (
117   FILE *f
118 )
119 {
120   unsigned long l;
121
122   init_tab_mirroring_txt ();
123
124   l = 0;
125   while (fgets (buf, sizeof buf, f))
126     {
127       unsigned long i, j;
128       signed long dist;
129       int k;
130       const char *s = buf;
131
132       l++;
133
134       while (*s == ' ')
135         s++;
136
137       if (s[0] == '#' || s[0] == '\0' || s[0] == '\n')
138         continue;
139
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;
149     }
150 }
151
152 static void
153 read_data (
154   const char *data_file_type,
155   const char *data_file_name
156 )
157 {
158   FILE *f;
159
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);
163
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);
168
169   fclose (f);
170 }
171
172 static void
173 gen_mirroring_tab (
174   int max_depth,
175   const char *data_file_type
176 )
177 {
178   int key_bytes;
179   const char *key_type;
180
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);
185
186   printf ("#define PACKTAB_UINT8 guint8\n"
187           "#define PACKTAB_UINT16 guint16\n"
188           "#define PACKTAB_UINT32 guint32\n\n");
189
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";
193
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");
198
199   printf ("#undef PACKTAB_UINT8\n"
200           "#undef PACKTAB_UINT16\n" "#undef PACKTAB_UINT32\n\n");
201
202   printf ("#define " macro_name "(x) ((x) + " macro_name "_DELTA(x))\n\n");
203
204   printf ("/* End of generated " outputname " */\n");
205 }
206
207 int
208 main (
209   int argc,
210   const char **argv
211 )
212 {
213   const char *data_file_type = "BidiMirroring.txt";
214
215   if (argc < 3)
216     die2 ("usage:\n  " appname " max-lookups /path/to/%s [junk...]",
217           data_file_type);
218
219   {
220     int max_depth = atoi (argv[1]);
221     const char *data_file_name = argv[2];
222
223     if (max_depth < 2)
224       die ("invalid depth");
225
226     init ();
227     read_data (data_file_type, data_file_name);
228     gen_mirroring_tab (max_depth, data_file_type);
229   }
230
231   return 0;
232 }