| 1 | /*
|
|---|
| 2 | * SPDX-FileCopyrightText: 2022 Jiri Svoboda
|
|---|
| 3 | *
|
|---|
| 4 | * SPDX-License-Identifier: BSD-3-Clause
|
|---|
| 5 | */
|
|---|
| 6 |
|
|---|
| 7 | /** @addtogroup libui
|
|---|
| 8 | * @{
|
|---|
| 9 | */
|
|---|
| 10 | /**
|
|---|
| 11 | * @file Accelerator processing
|
|---|
| 12 | */
|
|---|
| 13 |
|
|---|
| 14 | #include <ctype.h>
|
|---|
| 15 | #include <errno.h>
|
|---|
| 16 | #include <stdlib.h>
|
|---|
| 17 | #include <str.h>
|
|---|
| 18 | #include <ui/accel.h>
|
|---|
| 19 |
|
|---|
| 20 | /** Process text with accelerator markup.
|
|---|
| 21 | *
|
|---|
| 22 | * Parse text with tilde markup into a list of strings. @a *rbuf is set
|
|---|
| 23 | * to point to a newly allocated buffer containing consecutive null-terminated
|
|---|
| 24 | * strings.
|
|---|
| 25 | *
|
|---|
| 26 | * Each part between two '~' becomes one string. '~~' is translated into
|
|---|
| 27 | * a literal '~' character. @a *endptr is set to point to the first character
|
|---|
| 28 | * beyond the end of the list.
|
|---|
| 29 | *
|
|---|
| 30 | * @param str String with tilde markup
|
|---|
| 31 | * @param rbuf Place to store pointer to newly allocated buffer.
|
|---|
| 32 | * @param endptr Place to store end pointer (just after last character)
|
|---|
| 33 | * @return EOK on success or an error code
|
|---|
| 34 | */
|
|---|
| 35 | errno_t ui_accel_process(const char *str, char **rbuf, char **endptr)
|
|---|
| 36 | {
|
|---|
| 37 | const char *sp;
|
|---|
| 38 | char *dp;
|
|---|
| 39 | char *buf;
|
|---|
| 40 |
|
|---|
| 41 | buf = malloc(str_size(str) + 1);
|
|---|
| 42 | if (buf == NULL)
|
|---|
| 43 | return ENOMEM;
|
|---|
| 44 |
|
|---|
| 45 | /* Break down string into list of (non)highlighted parts */
|
|---|
| 46 | sp = str;
|
|---|
| 47 | dp = buf;
|
|---|
| 48 | while (*sp != '\0') {
|
|---|
| 49 | if (*sp == '~') {
|
|---|
| 50 | if (sp[1] == '~') {
|
|---|
| 51 | sp += 2;
|
|---|
| 52 | *dp++ = '~';
|
|---|
| 53 | } else {
|
|---|
| 54 | ++sp;
|
|---|
| 55 | *dp++ = '\0';
|
|---|
| 56 | }
|
|---|
| 57 | } else {
|
|---|
| 58 | *dp++ = *sp++;
|
|---|
| 59 | }
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | *dp++ = '\0';
|
|---|
| 63 | *rbuf = buf;
|
|---|
| 64 | *endptr = dp;
|
|---|
| 65 |
|
|---|
| 66 | return EOK;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | /** Get accelerator character from marked-up string.
|
|---|
| 70 | *
|
|---|
| 71 | * @param str String with '~' accelerator markup
|
|---|
| 72 | *
|
|---|
| 73 | * @return Accelerator character (lowercase) or the null character if
|
|---|
| 74 | * the menu has no accelerator.
|
|---|
| 75 | */
|
|---|
| 76 | char32_t ui_accel_get(const char *str)
|
|---|
| 77 | {
|
|---|
| 78 | const char *sp = str;
|
|---|
| 79 | size_t off;
|
|---|
| 80 |
|
|---|
| 81 | while (*sp != '\0') {
|
|---|
| 82 | if (*sp == '~') {
|
|---|
| 83 | if (sp[1] == '~') {
|
|---|
| 84 | ++sp;
|
|---|
| 85 | } else {
|
|---|
| 86 | /* Found the accelerator */
|
|---|
| 87 | ++sp;
|
|---|
| 88 | break;
|
|---|
| 89 | }
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | ++sp;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | if (*sp == '\0') {
|
|---|
| 96 | /* No accelerator */
|
|---|
| 97 | return '\0';
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | /* Decode accelerator character */
|
|---|
| 101 | off = 0;
|
|---|
| 102 |
|
|---|
| 103 | /*
|
|---|
| 104 | * NOTE: tolower is unlike to actually convert to lowercase
|
|---|
| 105 | * in case of non-ASCII characters
|
|---|
| 106 | */
|
|---|
| 107 | return tolower(str_decode(sp, &off, STR_NO_LIMIT));
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | /** @}
|
|---|
| 111 | */
|
|---|