Hash :
652b03cc
Author :
Date :
2024-09-01T08:32:21
compose: Add Compose table dump internal API - Move `print_compose_table_entry` to own file and add a `file` argument to select output. - Add `xkb_compose_table_dump` to dump a Compose table. This change is needed in order to share the feature “dump a Compose table” between tests and tools.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
/*
* Copyright © 2021 Ran Benita <ran@unusedvar.com>
* Copyright © 2023 Pierre Le Marre <dev@wismill.eu>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "config.h"
#include "src/darray.h"
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "xkbcommon/xkbcommon-compose.h"
#include "parser.h"
#include "escape.h"
#include "dump.h"
#include "src/keysym.h"
#include "src/utils.h"
bool
print_compose_table_entry(FILE *file, struct xkb_compose_table_entry *entry)
{
size_t nsyms;
const xkb_keysym_t *syms = xkb_compose_table_entry_sequence(entry, &nsyms);
char buf[XKB_KEYSYM_NAME_MAX_SIZE];
for (size_t i = 0; i < nsyms; i++) {
xkb_keysym_get_name(syms[i], buf, sizeof(buf));
fprintf(file, "<%s>", buf);
if (i + 1 < nsyms) {
fprintf(file, " ");
}
}
fprintf(file, " : ");
const char *utf8 = xkb_compose_table_entry_utf8(entry);
if (*utf8 != '\0') {
char *escaped = escape_utf8_string_literal(utf8);
if (!escaped) {
fprintf(stderr, "ERROR: Cannot escape the string: allocation error\n");
return false;
} else {
fprintf(file, " \"%s\"", escaped);
free(escaped);
}
}
const xkb_keysym_t keysym = xkb_compose_table_entry_keysym(entry);
if (keysym != XKB_KEY_NoSymbol) {
xkb_keysym_get_name(keysym, buf, sizeof(buf));
fprintf(file, " %s", buf);
}
fprintf(file, "\n");
return true;
}
bool
xkb_compose_table_dump(FILE *file, struct xkb_compose_table *table)
{
struct xkb_compose_table_entry *entry;
struct xkb_compose_table_iterator *iter = xkb_compose_table_iterator_new(table);
if (!iter)
return false;
bool ok = true;
while ((entry = xkb_compose_table_iterator_next(iter))) {
if (!print_compose_table_entry(file, entry)) {
ok = false;
break;
}
}
xkb_compose_table_iterator_free(iter);
return ok;
}