File: charmap.c

package info (click to toggle)
fvwm 1:2.6.8-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 15,804 kB
  • sloc: ansic: 145,770; xml: 17,093; perl: 7,302; sh: 4,921; makefile: 1,094; yacc: 688; lex: 187; sed: 11
file content (142 lines) | stat: -rw-r--r-- 3,494 bytes parent folder | download
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* -*-c-*- */
/* This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see: <http://www.gnu.org/licenses/>
 */

/* ---------------------------- included header files ---------------------- */

#include "config.h"
#include <stdio.h>
#include <ctype.h>
#include <string.h>

#include "charmap.h"
#include "wcontext.h"
#include "safemalloc.h"

/* ---------------------------- local definitions -------------------------- */

/* ---------------------------- local macros ------------------------------- */

/* ---------------------------- imports ------------------------------------ */

/* ---------------------------- included code files ------------------------ */

/* ---------------------------- local types -------------------------------- */

/* ---------------------------- forward declarations ----------------------- */

/* ---------------------------- local variables ---------------------------- */

/* ---------------------------- exported variables (globals) --------------- */

/* ---------------------------- local functions ---------------------------- */

/* ---------------------------- interface functions ------------------------ */

/* Turns a  string context of context or modifier values into an array of
 * true/false values (bits). */
int charmap_string_to_mask(
	int *ret, const char *string, charmap_t *table, char *errstring)
{
	int len = strlen(string);
	int error = 0;
	int i;

	*ret = 0;
	for (i = 0; i < len; ++i)
	{
		int found_match;
		int j;
		char c;

		c = tolower(string[i]);
		for (j = 0, found_match = 0; table[j].key != 0; j++)
		{
			if (table[j].key == c)
			{
				*ret |= table[j].value;
				found_match = 1;
				break;
			}
		}
		if (!found_match)
		{
			fputs("charmap_string_to_mask: ", stderr);
			if (errstring != NULL)
			{
				fputs(errstring, stderr);
			}
			fputc(' ', stderr);
			fputc(c, stderr);
			fputc('\n', stderr);
			error = 1;
		}
	}

	return error;
}

/* Reverse function of above.  Returns zero if no matching mask is found in the
 * table. */
char charmap_mask_to_char(int mask, charmap_t *table)
{
	char c;

	for (c = 0; table->key != 0; table++)
	{
		if (mask == table->value)
		{
			c = table->key;
			break;
		}
	}

	return c;
}

/* Used from "PrintInfo Bindings". */
char *charmap_table_to_string(int mask, charmap_t *table)
{
	char *allmods;
	int modmask;
	char c[2];

	c[1] = 0;
	modmask = mask;
	allmods = safemalloc(sizeof(table->value) * 8 + 1);
	*allmods = 0;
	for (; table->key !=0; table++)
	{
		c[0] = toupper(table->key);

		/* Don't explicitly match "A" for any context as doing so
		 * means we never see the individual bindings.  Incremental
		 * matching here for AnyContext is disasterous.*/
		if ((modmask & table->value) &&
			(table->value != C_ALL))
		{
			/* incremental match */
			strcat(allmods, c);
			modmask &= ~table->value;
		}
		else if (mask == table->value)
		{
			/* exact match */
			strcpy(allmods, c);
			break;
		}
	}

	return allmods;
}