Add initial draft of fvwm-convert-2.6
[fvwm.git] / libs / charmap.c
blob4df90e3e93270c2b0d18f632f14fbb4e36ac8d32
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 "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);
53 int error = 0;
54 int i;
56 *ret = 0;
57 for (i = 0; i < len; ++i)
59 int found_match;
60 int j;
61 char c;
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;
69 found_match = 1;
70 break;
73 if (!found_match)
75 fputs("charmap_string_to_mask: ", stderr);
76 if (errstring != NULL)
78 fputs(errstring, stderr);
80 fputc(' ', stderr);
81 fputc(c, stderr);
82 fputc('\n', stderr);
83 error = 1;
87 return error;
90 /* Reverse function of above. Returns zero if no matching mask is found in the
91 * table. */
92 char charmap_mask_to_char(int mask, charmap_t *table)
94 char c;
96 for (c = 0; table->key != 0; table++)
98 if (mask == table->value)
100 c = table->key;
101 break;
105 return c;
108 /* Used from "PrintInfo Bindings". */
109 char *charmap_table_to_string(int mask, charmap_t *table)
111 char *allmods;
112 int modmask;
113 char c[2];
115 c[1] = 0;
116 modmask = mask;
117 allmods = safemalloc(sizeof(table->value) * 8 + 1);
118 *allmods = 0;
119 for (; table->key !=0; table++)
121 c[0] = toupper(table->key);
122 if (mask == table->value)
124 /* exact match */
125 strcpy(allmods, c);
126 break;
128 else if (modmask & table->value)
130 /* incremental match */
131 strcat(allmods, c);
132 modmask &= ~table->value;
136 return allmods;