addition of silc.css
[runtime.git] / apps / irssi / src / fe-common / core / translation.c
1 /*
2  translation.c : irssi
3
4     Copyright (C) 1999-2000 Timo Sirainen
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "module.h"
22 #include "signals.h"
23 #include "line-split.h"
24 #include "misc.h"
25 #include "settings.h"
26
27 unsigned char translation_in[256], translation_out[256];
28
29 void translation_reset(void)
30 {
31         int n;
32
33         for (n = 0; n < 256; n++)
34                 translation_in[n] = (unsigned char) n;
35         for (n = 0; n < 256; n++)
36                 translation_out[n] = (unsigned char) n;
37 }
38
39 void translate_output(char *text)
40 {
41         while (*text != '\0') {
42                 *text = (char) translation_out[(int) (unsigned char) *text];
43                 text++;
44         }
45 }
46
47 #define gethex(a) \
48         (isdigit(a) ? ((a)-'0') : (toupper(a)-'A'+10))
49
50 void translation_parse_line(const char *str, int *pos)
51 {
52         const char *ptr;
53         int value;
54
55         for (ptr = str; *ptr != '\0'; ptr++) {
56                 if (ptr[0] != '0' || ptr[1] != 'x')
57                         break;
58                 ptr += 2;
59
60                 value = (gethex(ptr[0]) << 4) + gethex(ptr[1]);
61                 if (*pos < 256)
62                         translation_in[*pos] = (unsigned char) value;
63                 else
64                         translation_out[*pos-256] = (unsigned char) value;
65                 (*pos)++;
66
67                 ptr += 2;
68                 if (*ptr != ',') break;
69         }
70 }
71
72 int translation_read(const char *file)
73 {
74         char tmpbuf[1024], *str, *path;
75         LINEBUF_REC *buffer;
76         int f, pos, ret, recvlen;
77
78         g_return_val_if_fail(file != NULL, FALSE);
79
80         path = convert_home(file);
81         f = open(file, O_RDONLY);
82         g_free(path);
83
84         if (f == -1) return FALSE;
85
86         pos = 0; buffer = NULL;
87         while (pos < 512) {
88                 recvlen = read(f, tmpbuf, sizeof(tmpbuf));
89
90                 ret = line_split(tmpbuf, recvlen, &str, &buffer);
91                 if (ret <= 0) break;
92
93                 translation_parse_line(str, &pos);
94         }
95         line_split_free(buffer);
96
97         close(f);
98         if (pos != 512)
99                 translation_reset();
100         return pos == 512;
101 }
102
103 static void read_settings(void)
104 {
105         translation_read(settings_get_str("translation"));
106 }
107
108 void translation_init(void)
109 {
110         translation_reset();
111
112         settings_add_str("misc", "translation", "");
113         signal_add("setup changed", (SIGNAL_FUNC) read_settings);
114
115         read_settings();
116 }
117
118 void translation_deinit(void)
119 {
120         read_settings();
121         signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
122 }