NEWS/Changelog for previous commit.
[fvwm.git] / libs / charmap.c
blob788721de44774c0c921d591d0ef3327a06e043cc
1 /* -*-c-*- */
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 ---------------------- */
19 #include "config.h"
20 #include <stdio.h>
21 #include <ctype.h>
22 #include <string.h>
24 #include "charmap.h"
25 #include "wcontext.h"
26 #include "safemalloc.h"
28 /* ---------------------------- local definitions -------------------------- */
30 /* ---------------------------- local macros ------------------------------- */
32 /* ---------------------------- imports ------------------------------------ */
34 /* ---------------------------- included code files ------------------------ */
36 /* ---------------------------- local types -------------------------------- */
38 /* ---------------------------- forward declarations ----------------------- */
40 /* ---------------------------- local variables ---------------------------- */
42 /* ---------------------------- exported variables (globals) --------------- */
44 /* ---------------------------- local functions ---------------------------- */
46 /* ---------------------------- interface functions ------------------------ */
48 /* Turns a string context of context or modifier values into an array of
49 * true/false values (bits). */
50 int charmap_string_to_mask(
51 int *ret, const char *string, charmap_t *table, char *errstring)
53 int len = strlen(string);
54 int error = 0;
55 int i;
57 *ret = 0;
58 for (i = 0; i < len; ++i)
60 int found_match;
61 int j;
62 char c;
64 c = tolower(string[i]);
65 for (j = 0, found_match = 0; table[j].key != 0; j++)
67 if (table[j].key == c)
69 *ret |= table[j].value;
70 found_match = 1;
71 break;
74 if (!found_match)
76 fputs("charmap_string_to_mask: ", stderr);
77 if (errstring != NULL)
79 fputs(errstring, stderr);
81 fputc(' ', stderr);
82 fputc(c, stderr);
83 fputc('\n', stderr);
84 error = 1;
88 return error;
91 /* Reverse function of above. Returns zero if no matching mask is found in the
92 * table. */
93 char charmap_mask_to_char(int mask, charmap_t *table)
95 char c;
97 for (c = 0; table->key != 0; table++)
99 if (mask == table->value)
101 c = table->key;
102 break;
106 return c;
109 /* Used from "PrintInfo Bindings". */
110 char *charmap_table_to_string(int mask, charmap_t *table)
112 char *allmods;
113 int modmask;
114 char c[2];
116 c[1] = 0;
117 modmask = mask;
118 allmods = safemalloc(sizeof(table->value) * 8 + 1);
119 *allmods = 0;
120 for (; table->key !=0; table++)
122 c[0] = toupper(table->key);
124 /* Don't explicitly match "A" for any context as doing so
125 * means we never see the individual bindings. Incremental
126 * matching here for AnyContext is disasterous.*/
127 if ((modmask & table->value) &&
128 (table->value != C_ALL))
130 /* incremental match */
131 strcat(allmods, c);
132 modmask &= ~table->value;
134 else if (mask == table->value)
136 /* exact match */
137 strcpy(allmods, c);
138 break;
142 return allmods;