1 /* FriBidi
2 * gen-brackets-type-tab.c - generate brackets.tab.i
3 *
4 * Author:
5 * Behdad Esfahbod, 2001, 2002, 2004
6 * Dov Grobgeld 2017
7 *
8 * Copyright (C) 2004 Sharif FarsiWeb, Inc
9 * Copyright (C) 2001,2002,2004 Behdad Esfahbod
10 * Copyright (C) 2017 Dov Grobgeld
11 *
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
16 *
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this library, in a file named COPYING; if not, write to the
24 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 * Boston, MA 02110-1301, USA
26 *
27 * For licensing issues, contact <fribidi.license@gmail.com>.
28 */
29
30 #include <common.h>
31
32 #include <fribidi-unicode.h>
33
34 #include <stdio.h>
35 #ifdef HAVE_CONFIG_H
36 # include <config.h>
37 #endif
38
39 #ifdef STDC_HEADERS
40 # include <stdlib.h>
41 # include <stddef.h>
42 #else
43 # if HAVE_STDLIB_H
44 # include <stdlib.h>
45 # endif
46 #endif
47 #ifdef HAVE_STRING_H
48 # if !STDC_HEADERS && HAVE_MEMORY_H
49 # include <memory.h>
50 # endif
51 # include <string.h>
52 #endif
53 #ifdef HAVE_STRINGS_H
54 # include <strings.h>
55 #endif
56
57 #include "packtab.h"
58
59 #define appname "gen-brackets-type-tab"
60 #define outputname "brackets-type.tab.i"
61
62 static void
63 die (
64 const char *msg
65 )
66 {
67 fprintf (stderr, appname ": %s\n", msg);
68 exit (1);
69 }
70
71 static void
72 die2 (
73 const char *fmt,
74 const char *p
75 )
76 {
77 fprintf (stderr, appname ": ");
78 fprintf (stderr, fmt, p);
79 fprintf (stderr, "\n");
80 exit (1);
81 }
82
83 static void
84 die4 (
85 const char *fmt,
86 unsigned long l,
87 unsigned long p,
88 unsigned long q
89 )
90 {
91 fprintf (stderr, appname ": ");
92 fprintf (stderr, fmt, l, p, q);
93 fprintf (stderr, "\n");
94 exit (1);
95 }
96
97 #define table_name "Brt"
98 #define macro_name "FRIBIDI_GET_BRACKET_TYPE"
99
100 static signed int table[FRIBIDI_UNICODE_CHARS];
101 static char buf[4000];
102 static signed long max_dist;
103
104 static void
105 init (
106 void
107 )
108 {
109 max_dist = 0;
110 }
111
112 static void
113 clear_tab (
114 void
115 )
116 {
117 register FriBidiChar c;
118
119 for (c = 0; c < FRIBIDI_UNICODE_CHARS; c++)
120 table[c] = 0;
121 }
122
123 static void
124 init_tab_brackets_type_txt (
125 void
126 )
127 {
128 clear_tab ();
129 }
130
131 static void
132 read_bidi_brackets_type_txt (
133 FILE *f
134 )
135 {
136 unsigned long l;
137
138 init_tab_brackets_type_txt ();
139
140 l = 0;
141 while (fgets (buf, sizeof buf, f))
142 {
143 unsigned long i, j;
144 int k;
145 const char *s = buf;
146 char open_close;
147
148 l++;
149
150 while (*s == ' ')
151 s++;
152
153 if (s[0] == '#' || s[0] == '\0' || s[0] == '\n')
154 continue;
155
156 k = sscanf (s, "%lx; %lx; %c", &i, &j, &open_close);
157 if (k != 3 || i >= FRIBIDI_UNICODE_CHARS || j >= FRIBIDI_UNICODE_CHARS)
158 die4 ("invalid pair in input at line %ld: %04lX, %04lX", l, i, j);
159 table[i] = 1 + (0x2 * (open_close=='o'));
160 }
161 }
162
163 static void
164 read_data (
165 const char *data_file_type,
166 const char *data_file_name
167 )
168 {
169 FILE *f;
170
171 if (!(f = fopen (data_file_name, "rt")))
172 die2 ("error: cannot open `%s' for reading", data_file_name);
173
174 if (!strcmp (data_file_type, "BidiBrackets.txt"))
175 read_bidi_brackets_type_txt (f);
176 else
177 die2 ("error: unknown data-file-type %s", data_file_type);
178
179 fclose (f);
180 }
181
182 static void
183 gen_brackets_tab (
184 int max_depth,
185 const char *data_file_type
186 )
187 {
188 int key_bytes;
189 const char *key_type;
190
191 printf ("/* " outputname "\n * generated by " appname " (" FRIBIDI_NAME " "
192 FRIBIDI_VERSION ")\n" " * from the file %s of Unicode version "
193 FRIBIDI_UNICODE_VERSION ". */\n\n", data_file_type);
194
195 printf ("#define PACKTAB_UINT8 uint8_t\n"
196 "#define PACKTAB_UINT16 uint16_t\n"
197 "#define PACKTAB_UINT32 uint32_t\n\n");
198
199 key_bytes = 1;
200 key_type = key_bytes == 1 ? "int8_t" : key_bytes == 2 ?
201 "int16_t" : "int32_t";
202
203 if (!pack_table
204 (table, FRIBIDI_UNICODE_CHARS, key_bytes, 0, max_depth, 1, NULL,
205 key_type, table_name, macro_name, stdout))
206 die ("error: insufficient memory, decrease max_depth");
207
208 printf ("#undef PACKTAB_UINT8\n"
209 "#undef PACKTAB_UINT16\n" "#undef PACKTAB_UINT32\n\n");
210
211 printf ("/* End of generated " outputname " */\n");
212 }
213
214 int
215 main (
216 int argc,
217 const char **argv
218 )
219 {
220 const char *data_file_type = "BidiBrackets.txt";
221
222 if (argc < 3)
223 die2 ("usage:\n " appname " max-depth /path/to/%s [junk...]",
224 data_file_type);
225
226 {
227 int max_depth = atoi (argv[1]);
228 const char *data_file_name = argv[2];
229
230 if (max_depth < 2)
231 die ("invalid depth");
232
233 init ();
234 read_data (data_file_type, data_file_name);
235 gen_brackets_tab (max_depth, data_file_type);
236 }
237
238 return 0;
239 }