Debian Codegraph @

src/ucd-tools/tests/printucddata.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 "ucd/ucd.h"
21
22 #include <string.h>
23 #include <stdio.h>
24
25 static void fput_utf8c(FILE *out, codepoint_t c)
26 {
27 if (c < 0x80)
28 fputc((uint8_t)c, out);
29 else if (c < 0x800)
30 {
31 fputc(0xC0 | (c >> 6), out);
32 fputc(0x80 + (c & 0x3F), out);
33 }
34 else if (c < 0x10000)
35 {
36 fputc(0xE0 | (c >> 12), out);
37 fputc(0x80 + ((c >> 6) & 0x3F), out);
38 fputc(0x80 + (c & 0x3F), out);
39 }
40 else if (c < 0x200000)
41 {
42 fputc(0xF0 | (c >> 18), out);
43 fputc(0x80 + ((c >> 12) & 0x3F), out);
44 fputc(0x80 + ((c >> 6) & 0x3F), out);
45 fputc(0x80 + (c & 0x3F), out);
46 }
47 }
48
49 static int fget_utf8c(FILE *in, codepoint_t *c)
50 {
51 int ch = EOF;
52 if ((ch = fgetc(in)) == EOF) return 0;
53 if ((uint8_t)ch < 0x80)
54 *c = (uint8_t)ch;
55 else switch ((uint8_t)ch & 0xF0)
56 {
57 default:
58 *c = (uint8_t)ch & 0x1F;
59 if ((ch = fgetc(in)) == EOF) return 0;
60 *c = (*c << 6) + ((uint8_t)ch & 0x3F);
61 break;
62 case 0xE0:
63 *c = (uint8_t)ch & 0x0F;
64 if ((ch = fgetc(in)) == EOF) return 0;
65 *c = (*c << 6) + ((uint8_t)ch & 0x3F);
66 if ((ch = fgetc(in)) == EOF) return 0;
67 *c = (*c << 6) + ((uint8_t)ch & 0x3F);
68 break;
69 case 0xF0:
70 *c = (uint8_t)ch & 0x07;
71 if ((ch = fgetc(in)) == EOF) return 0;
72 *c = (*c << 6) + ((uint8_t)ch & 0x3F);
73 if ((ch = fgetc(in)) == EOF) return 0;
74 *c = (*c << 6) + ((uint8_t)ch & 0x3F);
75 if ((ch = fgetc(in)) == EOF) return 0;
76 *c = (*c << 6) + ((uint8_t)ch & 0x3F);
77 break;
78 }
79 return 1;
80 }
81
82 static void uprintf_codepoint(FILE *out, codepoint_t c, char mode)
83 {
84 switch (mode)
85 {
86 case 'c': /* character */
87 switch (c)
88 {
89 case '\t': fputs("\\t", out); break;
90 case '\r': fputs("\\r", out); break;
91 case '\n': fputs("\\n", out); break;
92 default: fput_utf8c(out, c); break;
93 }
94 break;
95 case 'h': /* hexadecimal (lower) */
96 fprintf(out, "%06x", c);
97 break;
98 case 'H': /* hexadecimal (upper) */
99 fprintf(out, "%06X", c);
100 break;
101 }
102 }
103
104 static void uprintf_is(FILE *out, codepoint_t c, char mode)
105 {
106 switch (mode)
107 {
108 case 'A': /* alpha-numeric */
109 fputc(ucd_isalnum(c) ? '1' : '0', out);
110 break;
111 case 'a': /* alpha */
112 fputc(ucd_isalpha(c) ? '1' : '0', out);
113 break;
114 case 'b': /* blank */
115 fputc(ucd_isblank(c) ? '1' : '0', out);
116 break;
117 case 'c': /* control */
118 fputc(ucd_iscntrl(c) ? '1' : '0', out);
119 break;
120 case 'd': /* numeric */
121 fputc(ucd_isdigit(c) ? '1' : '0', out);
122 break;
123 case 'g': /* glyph */
124 fputc(ucd_isgraph(c) ? '1' : '0', out);
125 break;
126 case 'l': /* lower case */
127 fputc(ucd_islower(c) ? '1' : '0', out);
128 break;
129 case 'P': /* printable */
130 fputc(ucd_isprint(c) ? '1' : '0', out);
131 break;
132 case 'p': /* punctuation */
133 fputc(ucd_ispunct(c) ? '1' : '0', out);
134 break;
135 case 's': /* whitespace */
136 fputc(ucd_isspace(c) ? '1' : '0', out);
137 break;
138 case 'u': /* upper case */
139 fputc(ucd_isupper(c) ? '1' : '0', out);
140 break;
141 case 'x': /* xdigit */
142 fputc(ucd_isxdigit(c) ? '1' : '0', out);
143 break;
144 }
145 }
146
147 static void uprintf(FILE *out, codepoint_t c, const char *format)
148 {
149 while (*format) switch (*format)
150 {
151 case '%':
152 switch (*++format)
153 {
154 case 'c': /* category */
155 fputs(ucd_get_category_string(ucd_lookup_category(c)), out);
156 break;
157 case 'C': /* category group */
158 fputs(ucd_get_category_group_string(ucd_lookup_category_group(c)), out);
159 break;
160 case 'p': /* codepoint */
161 uprintf_codepoint(out, c, *++format);
162 break;
163 case 'P': /* properties */
164 fprintf(out, "%016llx", ucd_properties(c, ucd_lookup_category(c)));
165 break;
166 case 'i': /* is* */
167 uprintf_is(out, c, *++format);
168 break;
169 case 'L': /* lowercase */
170 uprintf_codepoint(out, ucd_tolower(c), *++format);
171 break;
172 case 's': /* script */
173 fputs(ucd_get_script_string(ucd_lookup_script(c)), out);
174 break;
175 case 'T': /* titlecase */
176 uprintf_codepoint(out, ucd_totitle(c), *++format);
177 break;
178 case 'U': /* uppercase */
179 uprintf_codepoint(out, ucd_toupper(c), *++format);
180 break;
181 }
182 ++format;
183 break;
184 case '\\':
185 switch (*++format) {
186 case 0:
187 break;
188 case 't':
189 fputc('\t', out);
190 ++format;
191 break;
192 case 'r':
193 fputc('\r', out);
194 ++format;
195 break;
196 case 'n':
197 fputc('\n', out);
198 ++format;
199 break;
200 default:
201 fputc(*format, out);
202 ++format;
203 break;
204 }
205 break;
206 default:
207 fputc(*format, out);
208 ++format;
209 break;
210 }
211 }
212
213 static void print_file(FILE *in, const char *format)
214 {
215 codepoint_t c = 0;
216 while (fget_utf8c(in, &c))
217 uprintf(stdout, c, format ? format : "%pc\t%pH\t%s\t%c\t%Uc\t%Lc\t%Tc\t%is\n");
218 }
219
220 int main(int argc, char **argv)
221 {
222 FILE *in = NULL;
223 const char *format = NULL;
224 int argn;
225 for (argn = 1; argn != argc; ++argn)
226 {
227 const char *arg = argv[argn];
228 if (!strcmp(arg, "--stdin") || !strcmp(arg, "-"))
229 in = stdin;
230 else if (!strncmp(arg, "--format=", 9))
231 format = arg + 9;
232 else if (in == NULL)
233 {
234 in = fopen(arg, "r");
235 if (!in)
236 fprintf(stdout, "cannot open `%s`\n", argv[1]);
237 }
238 }
239
240 if (in == stdin)
241 print_file(stdin, format);
242 else if (in != NULL)
243 {
244 print_file(in, format);
245 fclose(in);
246 }
247 else
248 {
249 codepoint_t c;
250 for (c = 0; c <= 0x10FFFF; ++c)
251 uprintf(stdout, c, format ? format :
252 "%pH %s %C %c %UH %LH %TH %id %ix %ic %is %ib %ip %iP %ig %iA %ia %iu %il %P\n");
253 }
254 return 0;
255 }