Debian Codegraph @

src/testdump.c

raw symbols SCIP
1 /* Conversion of files between different charsets and surfaces.
2 Copyright © 1996-2019 Free Software Foundation, Inc.
3 Contributed by François Pinard <[email protected]>, 1997.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public License
7 as published by the Free Software Foundation; either version 3 of the
8 License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be
11 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the Recode Library; see the file `COPYING.LIB'.
17 If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 #include "config.h"
21 #include "common.h"
22 #include "decsteps.h"
23 #include "hash.h"
24
25 /*------------------------.
26 | Produce test patterns. |
27 `------------------------*/
28
29 /* One should _remove_ pseudo-surfaces to _produce_ test patterns. This
30 strange-looking decision comes from the fact that test patterns are usually
31 requested from the `before' position in the request. */
32
33 static bool
34 test7_data (RECODE_SUBTASK subtask)
35 {
36 unsigned counter;
37 int value;
38
39 for (counter = 0; counter < 1 << 7; counter++)
40 recode_put_byte (counter, subtask);
41
42 /* Copy the rest verbatim. */
43 while (value = recode_get_byte (subtask), value != EOF)
44 recode_put_byte (value, subtask);
45
46 SUBTASK_RETURN (subtask);
47 }
48
49 static bool
50 test8_data (RECODE_SUBTASK subtask)
51 {
52 unsigned counter;
53 int value;
54
55 for (counter = 0; counter < 1 << 8; counter++)
56 recode_put_byte (counter, subtask);
57
58 /* Copy the rest verbatim. */
59 while (value = recode_get_byte (subtask), value != EOF)
60 recode_put_byte (value, subtask);
61
62 SUBTASK_RETURN (subtask);
63 }
64
65 static bool
66 test15_data (RECODE_SUBTASK subtask)
67 {
68 unsigned counter;
69 unsigned value;
70
71 recode_put_ucs2 (BYTE_ORDER_MARK, subtask);
72
73 /* Before surrogate area. */
74 for (counter = 0; counter < 0xDC00; counter++)
75 recode_put_ucs2 (counter, subtask);
76 /* After surrogate area. */
77 for (counter = 0xE000; counter < 1 << 16; counter++)
78 switch (counter)
79 {
80 case BYTE_ORDER_MARK:
81 case REPLACEMENT_CHARACTER:
82 case BYTE_ORDER_MARK_SWAPPED:
83 case NOT_A_CHARACTER:
84 break;
85
86 default:
87 recode_put_ucs2 (counter, subtask);
88 }
89
90 /* Copy the rest verbatim. */
91 while (value = recode_get_byte (subtask), value != (unsigned)EOF)
92 recode_put_byte (value, subtask);
93
94 SUBTASK_RETURN (subtask);
95 }
96
97 static bool
98 test16_data (RECODE_SUBTASK subtask)
99 {
100 unsigned counter;
101 unsigned value;
102
103 for (counter = 0; counter < 1 << 16; counter++)
104 recode_put_ucs2 (counter, subtask);
105
106 /* Copy the rest verbatim. */
107 while (value = recode_get_byte (subtask), value != (unsigned)EOF)
108 recode_put_byte (value, subtask);
109
110 SUBTASK_RETURN (subtask);
111 }
112
113 /*-----------------------------------------------.
114 | Produce frequency count for UCS-2 characters. |
115 `-----------------------------------------------*/
116
117 struct ucs2_to_count
118 {
119 recode_ucs2 code; /* UCS-2 value */
120 unsigned count; /* corresponding count */
121 };
122
123 static size_t
124 ucs2_to_count_hash (const void *void_data, size_t table_size)
125 {
126 const struct ucs2_to_count *data = (const struct ucs2_to_count *) void_data;
127
128 return data->code % table_size;
129 }
130
131 static bool
132 ucs2_to_count_compare (const void *void_first, const void *void_second)
133 {
134 const struct ucs2_to_count *first
135 = (const struct ucs2_to_count *) void_first;
136 const struct ucs2_to_count *second
137 = (const struct ucs2_to_count *) void_second;
138
139 return first->code == second->code;
140 }
141
142 static int
143 compare_item (const void *void_first, const void *void_second)
144 {
145 struct ucs2_to_count *const *first
146 = (struct ucs2_to_count *const *) void_first;
147 struct ucs2_to_count *const *second
148 = (struct ucs2_to_count *const *) void_second;
149
150 return (*first)->code - (*second)->code;
151 }
152
153 static void
154 put_string (const char *string, RECODE_SUBTASK subtask)
155 {
156 const char *cursor;
157
158 for (cursor = string; *cursor; cursor++)
159 recode_put_byte (*cursor, subtask);
160 }
161
162 static bool
163 produce_count (RECODE_SUBTASK subtask)
164 {
165 RECODE_OUTER outer = subtask->task->request->outer;
166 Hash_table *table; /* hash table for UCS-2 characters */
167 size_t size; /* number of different characters */
168 struct ucs2_to_count **array; /* array into hash table items */
169
170 table = hash_initialize (0, NULL,
171 ucs2_to_count_hash, ucs2_to_count_compare, free);
172 if (!table)
173 return false;
174
175 /* Count characters. */
176
177 {
178 unsigned character; /* current character being counted */
179
180 while (recode_get_ucs2 (&character, subtask))
181 {
182 struct ucs2_to_count lookup;
183 struct ucs2_to_count *entry;
184
185 lookup.code = character;
186 entry = (struct ucs2_to_count *) hash_lookup (table, &lookup);
187 if (entry)
188 entry->count++;
189 else
190 {
191 if (!ALLOC (entry, 1, struct ucs2_to_count))
192 {
193 hash_free (table);
194 return false;
195 }
196 entry->code = character;
197 entry->count = 1;
198 if (!hash_insert (table, entry))
199 {
200 hash_free (table);
201 free (entry);
202 return false;
203 }
204 }
205 }
206 }
207
208 /* Sort results. */
209
210 size = hash_get_n_entries (table);
211
212 if (!ALLOC (array, size, struct ucs2_to_count *))
213 {
214 hash_free (table);
215 return false;
216 }
217 hash_get_entries (table, (void **) array, size);
218
219 qsort (array, size, sizeof (struct ucs2_to_count *), compare_item);
220
221 /* Produce the report. */
222
223 /* FIXME: Produce it column-wise. (See transp.c). */
224
225 {
226 const unsigned non_count_width = 12;
227 struct ucs2_to_count **cursor;
228 char *buffer;
229 unsigned count_width;
230 unsigned maximum_count = 0;
231 unsigned column = 0;
232 unsigned delayed = 0;
233
234 for (cursor = array; cursor < array + size; cursor++)
235 if ((*cursor)->count > maximum_count)
236 maximum_count = (*cursor)->count;
237 if (asprintf (&buffer, "%u", maximum_count) == -1)
238 return false;
239 count_width = strlen (buffer);
240 free (buffer);
241
242 for (cursor = array; cursor < array + size; cursor++)
243 {
244 unsigned character = (*cursor)->code;
245 const char *mnemonic = recode_ucs2_to_rfc1345 (character);
246
247 if (column + count_width + non_count_width > 80)
248 {
249 recode_put_byte ('\n', subtask);
250 delayed = 0;
251 column = 0;
252 }
253 else
254 while (delayed)
255 {
256 recode_put_byte (' ', subtask);
257 delayed--;
258 }
259
260 if (asprintf (&buffer, "%*u %.4X", (int)count_width, (*cursor)->count, character) == -1)
261 return false;
262 put_string (buffer, subtask);
263 free (buffer);
264 if (mnemonic)
265 {
266 recode_put_byte (' ', subtask);
267 put_string (mnemonic, subtask);
268 delayed = 6 - 1 - strlen (mnemonic);
269 }
270 else
271 delayed = 6;
272
273 column += count_width + non_count_width;
274 }
275
276 if (column)
277 recode_put_byte ('\n', subtask);
278 }
279
280 /* Clean-up. */
281
282 free (array);
283 hash_free (table);
284
285 SUBTASK_RETURN (subtask);
286 }
287
288 /*---------------------------.
289 | Fully dump an UCS-2 file. |
290 `---------------------------*/
291
292 static bool
293 produce_full_dump (RECODE_SUBTASK subtask)
294 {
295 unsigned character; /* character to dump */
296
297 /* Dump all characters. */
298
299 if (recode_get_ucs2 (&character, subtask))
300 {
301 bool french = recode_should_prefer_french();
302 const char *charname; /* charname for code */
303 char buffer[50];
304
305 put_string (_("UCS2 Mne Description\n\n"), subtask);
306
307 while (1)
308 {
309 const char *mnemonic = recode_ucs2_to_rfc1345 (character);
310
311 sprintf (buffer, "%.4X", character);
312 put_string (buffer, subtask);
313 if (mnemonic)
314 {
315 sprintf (buffer, " %-3s", mnemonic);
316 put_string (buffer, subtask);
317 }
318 else
319 put_string (" ", subtask);
320
321 if (french)
322 {
323 charname = recode_ucs2_to_french_charname (character);
324 if (!charname)
325 charname = recode_ucs2_to_charname (character);
326 }
327 else
328 {
329 charname = recode_ucs2_to_charname (character);
330 if (!charname)
331 charname = recode_ucs2_to_french_charname (character);
332 }
333
334 if (charname)
335 {
336 put_string (" ", subtask);
337 put_string (charname, subtask);
338 }
339 recode_put_byte ('\n', subtask);
340
341 if (!recode_get_ucs2 (&character, subtask))
342 break;
343 }
344 }
345
346 SUBTASK_RETURN (subtask);
347 }
348
349 /*-----------------------------------------.
350 | Declare charsets, surfaces and aliases. |
351 `-----------------------------------------*/
352
353 bool
354 module_testdump (RECODE_OUTER outer)
355 {
356 /* Test surfaces. */
357
358 if (!recode_declare_single (outer, "test7", "data",
359 outer->quality_variable_to_byte,
360 NULL, test7_data))
361 return false;
362 if (!recode_declare_single (outer, "test8", "data",
363 outer->quality_variable_to_byte,
364 NULL, test8_data))
365 return false;
366 if (!recode_declare_single (outer, "test15", "data",
367 outer->quality_variable_to_ucs2,
368 NULL, test15_data))
369 return false;
370 if (!recode_declare_single (outer, "test16", "data",
371 outer->quality_variable_to_ucs2,
372 NULL, test16_data))
373 return false;
374
375 /* Analysis charsets. */
376
377 if (!recode_declare_single (outer, "ISO-10646-UCS-2", "count-characters",
378 outer->quality_ucs2_to_variable,
379 NULL, produce_count))
380 return false;
381 if (!recode_declare_single (outer, "ISO-10646-UCS-2", "dump-with-names",
382 outer->quality_ucs2_to_variable,
383 NULL, produce_full_dump))
384 return false;
385
386 return true;
387 }
388
389 void
390 delmodule_testdump (_GL_UNUSED RECODE_OUTER outer)
391 {
392 }