Debian Codegraph @

src/ucd-tools/tests/printcdata.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2012-2017 Reece H. Dunn
3 *
4 * This file is part of ucd-tools.
5 *
6 * ucd-tools is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ucd-tools is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with ucd-tools. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "config.h"
21 #include "ucd/ucd.h"
22
23 #include <locale.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <wchar.h>
27 #include <wctype.h>
28
29 #ifndef HAVE_ISWBLANK
30 static int iswblank(wint_t c)
31 {
32 return iswspace(c) && !(c >= 0x0A && c <= 0x0D);
33 }
34 #endif
35
36 static void fput_utf8c(FILE *out, codepoint_t c)
37 {
38 if (c < 0x80)
39 fputc((uint8_t)c, out);
40 else if (c < 0x800)
41 {
42 fputc(0xC0 | (c >> 6), out);
43 fputc(0x80 + (c & 0x3F), out);
44 }
45 else if (c < 0x10000)
46 {
47 fputc(0xE0 | (c >> 12), out);
48 fputc(0x80 + ((c >> 6) & 0x3F), out);
49 fputc(0x80 + (c & 0x3F), out);
50 }
51 else if (c < 0x200000)
52 {
53 fputc(0xF0 | (c >> 18), out);
54 fputc(0x80 + ((c >> 12) & 0x3F), out);
55 fputc(0x80 + ((c >> 6) & 0x3F), out);
56 fputc(0x80 + (c & 0x3F), out);
57 }
58 }
59
60 static int fget_utf8c(FILE *in, codepoint_t *c)
61 {
62 int ch = EOF;
63 if ((ch = fgetc(in)) == EOF) return 0;
64 if ((uint8_t)ch < 0x80)
65 *c = (uint8_t)ch;
66 else switch ((uint8_t)ch & 0xF0)
67 {
68 default:
69 *c = (uint8_t)ch & 0x1F;
70 if ((ch = fgetc(in)) == EOF) return 0;
71 *c = (*c << 6) + ((uint8_t)ch & 0x3F);
72 break;
73 case 0xE0:
74 *c = (uint8_t)ch & 0x0F;
75 if ((ch = fgetc(in)) == EOF) return 0;
76 *c = (*c << 6) + ((uint8_t)ch & 0x3F);
77 if ((ch = fgetc(in)) == EOF) return 0;
78 *c = (*c << 6) + ((uint8_t)ch & 0x3F);
79 break;
80 case 0xF0:
81 *c = (uint8_t)ch & 0x07;
82 if ((ch = fgetc(in)) == EOF) return 0;
83 *c = (*c << 6) + ((uint8_t)ch & 0x3F);
84 if ((ch = fgetc(in)) == EOF) return 0;
85 *c = (*c << 6) + ((uint8_t)ch & 0x3F);
86 if ((ch = fgetc(in)) == EOF) return 0;
87 *c = (*c << 6) + ((uint8_t)ch & 0x3F);
88 break;
89 }
90 return 1;
91 }
92
93 static void uprintf_codepoint(FILE *out, codepoint_t c, char mode)
94 {
95 switch (mode)
96 {
97 case 'c': /* character */
98 switch (c)
99 {
100 case '\t': fputs("\\t", out); break;
101 case '\r': fputs("\\r", out); break;
102 case '\n': fputs("\\n", out); break;
103 default: fput_utf8c(out, c); break;
104 }
105 break;
106 case 'h': /* hexadecimal (lower) */
107 fprintf(out, "%06x", c);
108 break;
109 case 'H': /* hexadecimal (upper) */
110 fprintf(out, "%06X", c);
111 break;
112 }
113 }
114
115 static void uprintf_is(FILE *out, codepoint_t c, char mode)
116 {
117 switch (mode)
118 {
119 case 'A': /* alpha-numeric */
120 fputc(iswalnum(c) ? '1' : '0', out);
121 break;
122 case 'a': /* alpha */
123 fputc(iswalpha(c) ? '1' : '0', out);
124 break;
125 case 'b': /* blank */
126 fputc(iswblank(c) ? '1' : '0', out);
127 break;
128 case 'c': /* control */
129 fputc(iswcntrl(c) ? '1' : '0', out);
130 break;
131 case 'd': /* numeric */
132 fputc(iswdigit(c) ? '1' : '0', out);
133 break;
134 case 'g': /* glyph */
135 fputc(iswgraph(c) ? '1' : '0', out);
136 break;
137 case 'l': /* lower case */
138 fputc(iswlower(c) ? '1' : '0', out);
139 break;
140 case 'P': /* printable */
141 fputc(iswprint(c) ? '1' : '0', out);
142 break;
143 case 'p': /* punctuation */
144 fputc(iswpunct(c) ? '1' : '0', out);
145 break;
146 case 's': /* whitespace */
147 fputc(iswspace(c) ? '1' : '0', out);
148 break;
149 case 'u': /* upper case */
150 fputc(iswupper(c) ? '1' : '0', out);
151 break;
152 case 'x': /* xdigit */
153 fputc(iswxdigit(c) ? '1' : '0', out);
154 break;
155 }
156 }
157
158 static void uprintf(FILE *out, codepoint_t c, const char *format)
159 {
160 while (*format) switch (*format)
161 {
162 case '%':
163 switch (*++format)
164 {
165 case 'c': /* category */
166 fputs(ucd_get_category_string(ucd_lookup_category(c)), out);
167 break;
168 case 'C': /* category group */
169 fputs(ucd_get_category_group_string(ucd_lookup_category_group(c)), out);
170 break;
171 case 'p': /* codepoint */
172 uprintf_codepoint(out, c, *++format);
173 break;
174 case 'P': /* properties */
175 fprintf(out, "%016llx", ucd_properties(c, ucd_lookup_category(c)));
176 break;
177 case 'i': /* is* */
178 uprintf_is(out, c, *++format);
179 break;
180 case 'L': /* lowercase */
181 uprintf_codepoint(out, ucd_tolower(c), *++format);
182 break;
183 case 's': /* script */
184 fputs(ucd_get_script_string(ucd_lookup_script(c)), out);
185 break;
186 case 'T': /* titlecase */
187 uprintf_codepoint(out, ucd_totitle(c), *++format);
188 break;
189 case 'U': /* uppercase */
190 uprintf_codepoint(out, ucd_toupper(c), *++format);
191 break;
192 }
193 ++format;
194 break;
195 case '\\':
196 switch (*++format) {
197 case 0:
198 break;
199 case 't':
200 fputc('\t', out);
201 ++format;
202 break;
203 case 'r':
204 fputc('\r', out);
205 ++format;
206 break;
207 case 'n':
208 fputc('\n', out);
209 ++format;
210 break;
211 default:
212 fputc(*format, out);
213 ++format;
214 break;
215 }
216 break;
217 default:
218 fputc(*format, out);
219 ++format;
220 break;
221 }
222 }
223
224 static void print_file(FILE *in, const char *format)
225 {
226 codepoint_t c = 0;
227 while (fget_utf8c(in, &c))
228 uprintf(stdout, c, format ? format : "%pc\t%pH\t%s\t%c\t%Uc\t%Lc\t%Tc\t%is\n");
229 }
230
231 int main(int argc, char **argv)
232 {
233 FILE *in = NULL;
234 const char *format = NULL;
235 int argn;
236 for (argn = 1; argn != argc; ++argn)
237 {
238 const char *arg = argv[argn];
239 if (!strcmp(arg, "--stdin") || !strcmp(arg, "-"))
240 in = stdin;
241 else if (!strncmp(arg, "--format=", 9))
242 format = arg + 9;
243 else if (!strncmp(arg, "--locale=", 9))
244 setlocale(LC_CTYPE, arg + 9);
245 else if (in == NULL)
246 {
247 in = fopen(arg, "r");
248 if (!in)
249 fprintf(stdout, "cannot open `%s`\n", argv[1]);
250 }
251 }
252
253 if (in == stdin)
254 print_file(stdin, format);
255 else if (in != NULL)
256 {
257 print_file(in, format);
258 fclose(in);
259 }
260 else
261 {
262 codepoint_t c;
263 for (c = 0; c <= 0x10FFFF; ++c)
264 uprintf(stdout, c, format ? format :
265 "%pH %s %C %c %UH %LH %TH %id %ix %ic %is %ib %ip %iP %ig %iA %ia %iu %il %P\n");
266 }
267 return 0;
268 }