2 /* This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 /* ---------------------------- included header files ---------------------- */
25 #include "safemalloc.h"
27 /* ---------------------------- local definitions -------------------------- */
29 /* ---------------------------- local macros ------------------------------- */
31 /* ---------------------------- imports ------------------------------------ */
33 /* ---------------------------- included code files ------------------------ */
35 /* ---------------------------- local types -------------------------------- */
37 /* ---------------------------- forward declarations ----------------------- */
39 /* ---------------------------- local variables ---------------------------- */
41 /* ---------------------------- exported variables (globals) --------------- */
43 /* ---------------------------- local functions ---------------------------- */
45 /* ---------------------------- interface functions ------------------------ */
47 /* Turns a string context of context or modifier values into an array of
48 * true/false values (bits). */
49 int charmap_string_to_mask(
50 int *ret
, const char *string
, charmap_t
*table
, char *errstring
)
52 int len
= strlen(string
);
57 for (i
= 0; i
< len
; ++i
)
63 c
= tolower(string
[i
]);
64 for (j
= 0, found_match
= 0; table
[j
].key
!= 0; j
++)
66 if (table
[j
].key
== c
)
68 *ret
|= table
[j
].value
;
75 fputs("charmap_string_to_mask: ", stderr
);
76 if (errstring
!= NULL
)
78 fputs(errstring
, stderr
);
90 /* Reverse function of above. Returns zero if no matching mask is found in the
92 char charmap_mask_to_char(int mask
, charmap_t
*table
)
96 for (c
= 0; table
->key
!= 0; table
++)
98 if (mask
== table
->value
)
108 /* Used from "PrintInfo Bindings". */
109 char *charmap_table_to_string(int mask
, charmap_t
*table
)
117 allmods
= safemalloc(sizeof(table
->value
) * 8 + 1);
119 for (; table
->key
!=0; table
++)
121 c
[0] = toupper(table
->key
);
122 if (mask
== table
->value
)
128 else if (modmask
& table
->value
)
130 /* incremental match */
132 modmask
&= ~table
->value
;