Debian Codegraph @

src/compose/dump.c

raw
1 /*
2 * Copyright © 2021 Ran Benita <[email protected]>
3 * Copyright © 2023 Pierre Le Marre <[email protected]>
4 * SPDX-License-Identifier: MIT
5 */
6
7 #include "config.h"
8
9 #include "src/darray.h"
10 #include <stdio.h>
11 #include <stdbool.h>
12 #include <string.h>
13
14 #include "xkbcommon/xkbcommon-compose.h"
15 #include "parser.h"
16 #include "escape.h"
17 #include "dump.h"
18 #include "src/keysym.h"
19
20 bool
21 print_compose_table_entry(FILE *file, struct xkb_compose_table_entry *entry)
22 {
23 size_t nsyms;
24 const xkb_keysym_t *syms = xkb_compose_table_entry_sequence(entry, &nsyms);
25 char buf[XKB_KEYSYM_NAME_MAX_SIZE];
26 for (size_t i = 0; i < nsyms; i++) {
27 xkb_keysym_get_name(syms[i], buf, sizeof(buf));
28 fprintf(file, "<%s>", buf);
29 if (i + 1 < nsyms) {
30 fprintf(file, " ");
31 }
32 }
33 fprintf(file, " : ");
34 const char *utf8 = xkb_compose_table_entry_utf8(entry);
35 if (*utf8 != '\0') {
36 char *escaped = escape_utf8_string_literal(utf8);
37 if (!escaped) {
38 fprintf(stderr, "ERROR: Cannot escape the string: allocation error\n");
39 return false;
40 } else {
41 fprintf(file, " \"%s\"", escaped);
42 free(escaped);
43 }
44 }
45 const xkb_keysym_t keysym = xkb_compose_table_entry_keysym(entry);
46 if (keysym != XKB_KEY_NoSymbol) {
47 xkb_keysym_get_name(keysym, buf, sizeof(buf));
48 fprintf(file, " %s", buf);
49 }
50 fprintf(file, "\n");
51 return true;
52 }
53
54 bool
55 xkb_compose_table_dump(FILE *file, struct xkb_compose_table *table)
56 {
57 struct xkb_compose_table_entry *entry;
58 struct xkb_compose_table_iterator *iter = xkb_compose_table_iterator_new(table);
59
60 if (!iter)
61 return false;
62
63 bool ok = true;
64 while ((entry = xkb_compose_table_iterator_next(iter))) {
65 if (!print_compose_table_entry(file, entry)) {
66 ok = false;
67 break;
68 }
69 }
70
71 xkb_compose_table_iterator_free(iter);
72 return ok;
73 }