summaryrefslogtreecommitdiff
path: root/src/fe-common/core/translation.c
blob: 2713cc7392dba4b30ef48bf5f42df42dd4849a65 (plain)
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
/*
 translation.c : irssi

    Copyright (C) 1999-2000 Timo Sirainen

    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, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "module.h"
#include "signals.h"
#include "line-split.h"
#include "misc.h"
#include "settings.h"

unsigned char translation_in[256], translation_out[256];

void translation_reset(void)
{
	int n;

	for (n = 0; n < 256; n++)
		translation_in[n] = (unsigned char) n;
	for (n = 0; n < 256; n++)
		translation_out[n] = (unsigned char) n;
}

void translate_output(char *text)
{
	while (*text != '\0') {
		*text = (char) translation_out[(int) (unsigned char) *text];
		text++;
	}
}

#define gethex(a) \
	(isdigit(a) ? ((a)-'0') : (toupper(a)-'A'+10))

void translation_parse_line(const char *str, int *pos)
{
	const char *ptr;
	int value;

	for (ptr = str; *ptr != '\0'; ptr++) {
		if (ptr[0] != '0' || ptr[1] != 'x')
			break;
		ptr += 2;

		value = (gethex(ptr[0]) << 4) + gethex(ptr[1]);
		if (*pos < 256)
			translation_in[*pos] = (unsigned char) value;
		else
			translation_out[*pos-256] = (unsigned char) value;
		(*pos)++;

		ptr += 2;
		if (*ptr != ',') break;
	}
}

int translation_read(const char *file)
{
	char tmpbuf[1024], *str, *path;
	LINEBUF_REC *buffer;
	int f, pos, ret, recvlen;

	g_return_val_if_fail(file != NULL, FALSE);

	path = convert_home(file);
	f = open(file, O_RDONLY);
	g_free(path);

	if (f == -1) return FALSE;

	pos = 0; buffer = NULL;
	while (pos < 512) {
		recvlen = read(f, tmpbuf, sizeof(tmpbuf));

		ret = line_split(tmpbuf, recvlen, &str, &buffer);
		if (ret <= 0) break;

                translation_parse_line(str, &pos);
	}
	line_split_free(buffer);

	close(f);
	if (pos != 512)
		translation_reset();
	return pos == 512;
}

static void read_settings(void)
{
	translation_read(settings_get_str("translation"));
}

void translation_init(void)
{
	translation_reset();

	settings_add_str("misc", "translation", "");
	signal_add("setup changed", (SIGNAL_FUNC) read_settings);

	read_settings();
}

void translation_deinit(void)
{
	read_settings();
	signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
}