Debian Codegraph @

src/ucd-tools/tests/printucddata_cpp.cpp

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 void fput_utf8c(FILE *out, ucd::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 bool fget_utf8c(FILE *in, ucd::codepoint_t &c)
50 {
51 int ch = EOF;
52 if ((ch = fgetc(in)) == EOF) return false;
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 false;
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 false;
65 c = (c << 6) + (uint8_t(ch) & 0x3F);
66 if ((ch = fgetc(in)) == EOF) return false;
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 false;
72 c = (c << 6) + (uint8_t(ch) & 0x3F);
73 if ((ch = fgetc(in)) == EOF) return false;
74 c = (c << 6) + (uint8_t(ch) & 0x3F);
75 if ((ch = fgetc(in)) == EOF) return false;
76 c = (c << 6) + (uint8_t(ch) & 0x3F);
77 break;
78 }
79 return true;
80 }
81
82 void uprintf_codepoint(FILE *out, ucd::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 void uprintf_is(FILE *out, ucd::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 void uprintf(FILE *out, ucd::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 void print_file(FILE *in, const char *format)
214 {
215 ucd::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 for (int argn = 1; argn != argc; ++argn)
225 {
226 const char *arg = argv[argn];
227 if (!strcmp(arg, "--stdin") || !strcmp(arg, "-"))
228 in = stdin;
229 else if (!strncmp(arg, "--format=", 9))
230 format = arg + 9;
231 else if (in == NULL)
232 {
233 in = fopen(arg, "r");
234 if (!in)
235 fprintf(stdout, "cannot open `%s`\n", argv[1]);
236 }
237 }
238
239 if (in == stdin)
240 print_file(stdin, format);
241 else if (in != NULL)
242 {
243 print_file(in, format);
244 fclose(in);
245 }
246 else
247 {
248 for (ucd::codepoint_t c = 0; c <= 0x10FFFF; ++c)
249 uprintf(stdout, c, format ? format :
250 "%pH %s %C %c %UH %LH %TH %id %ix %ic %is %ib %ip %iP %ig %iA %ia %iu %il %P\n");
251 }
252 return 0;
253 }