Just a little correction at the it.po file.
[midnight-commander.git] / src / charsets.c
blobc0c96d99113e18b07d6fc5c2329a4e2ff1a696ff
1 /* Text conversion from one charset to another.
3 Copyright (C) 2001 Walery Studennikov <despair@sama.ru>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #include <config.h>
22 #ifdef HAVE_CHARSET
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <iconv.h>
28 #include "global.h"
29 #include "charsets.h"
31 int n_codepages = 0;
33 struct codepage_desc *codepages;
35 unsigned char conv_displ[256];
36 unsigned char conv_input[256];
38 int
39 load_codepages_list (void)
41 int result = -1;
42 FILE *f;
43 char *fname;
44 char buf[256];
45 extern char *mc_home;
46 extern int display_codepage;
47 char *default_codepage = NULL;
49 fname = concat_dir_and_file (mc_home, CHARSETS_INDEX);
50 if (!(f = fopen (fname, "r"))) {
51 fprintf (stderr, _("Warning: file %s not found\n"), fname);
52 g_free (fname);
53 return -1;
55 g_free (fname);
57 for (n_codepages = 0; fgets (buf, sizeof (buf), f);)
58 if (buf[0] != '\n' && buf[0] != '\0' && buf[0] != '#')
59 ++n_codepages;
60 rewind (f);
62 codepages = g_new0 (struct codepage_desc, n_codepages + 1);
64 for (n_codepages = 0; fgets (buf, sizeof buf, f);) {
65 /* split string into id and cpname */
66 char *p = buf;
67 int buflen = strlen (buf);
69 if (*p == '\n' || *p == '\0' || *p == '#')
70 continue;
72 if (buflen > 0 && buf[buflen - 1] == '\n')
73 buf[buflen - 1] = '\0';
74 while (*p != '\t' && *p != ' ' && *p != '\0')
75 ++p;
76 if (*p == '\0')
77 goto fail;
79 *p++ = 0;
81 while (*p == '\t' || *p == ' ')
82 ++p;
83 if (*p == '\0')
84 goto fail;
86 if (strcmp (buf, "default") == 0) {
87 default_codepage = g_strdup (p);
88 continue;
91 codepages[n_codepages].id = g_strdup (buf);
92 codepages[n_codepages].name = g_strdup (p);
93 ++n_codepages;
96 if (default_codepage) {
97 display_codepage = get_codepage_index (default_codepage);
98 g_free (default_codepage);
101 result = n_codepages;
102 fail:
103 fclose (f);
104 return result;
107 void
108 free_codepages_list (void)
110 if (n_codepages > 0) {
111 int i;
112 for (i = 0; i < n_codepages; i++) {
113 g_free (codepages[i].id);
114 g_free (codepages[i].name);
116 n_codepages = 0;
117 g_free (codepages);
118 codepages = 0;
122 #define OTHER_8BIT "Other_8_bit"
124 char *
125 get_codepage_id (int n)
127 return (n < 0) ? OTHER_8BIT : codepages[n].id;
131 get_codepage_index (const char *id)
133 int i;
134 if (strcmp (id, OTHER_8BIT) == 0)
135 return -1;
136 for (i = 0; codepages[i].id; ++i)
137 if (strcmp (id, codepages[i].id) == 0)
138 return i;
139 return -1;
142 static char
143 translate_character (iconv_t cd, char c)
145 char outbuf[4], *obuf;
146 size_t ibuflen, obuflen, count;
148 ICONV_CONST char *ibuf = &c;
149 obuf = outbuf;
150 ibuflen = 1;
151 obuflen = 4;
153 count = iconv (cd, &ibuf, &ibuflen, &obuf, &obuflen);
154 if (count >= 0 && ibuflen == 0)
155 return outbuf[0];
157 return UNKNCHAR;
160 char errbuf[255];
163 * FIXME: This assumes that ASCII is always the first encoding
164 * in mc.charsets
166 #define CP_ASCII 0
168 char *
169 init_translation_table (int cpsource, int cpdisplay)
171 int i;
172 iconv_t cd;
173 char *cpsour, *cpdisp;
175 /* Fill inpit <-> display tables */
177 if (cpsource < 0 || cpdisplay < 0 || cpsource == cpdisplay) {
178 for (i = 0; i <= 255; ++i) {
179 conv_displ[i] = i;
180 conv_input[i] = i;
182 return NULL;
185 for (i = 0; i <= 127; ++i) {
186 conv_displ[i] = i;
187 conv_input[i] = i;
190 cpsour = codepages[cpsource].id;
191 cpdisp = codepages[cpdisplay].id;
193 /* display <- inpit table */
195 cd = iconv_open (cpdisp, cpsour);
196 if (cd == (iconv_t) - 1) {
197 g_snprintf (errbuf, sizeof (errbuf),
198 _("Cannot translate from %s to %s"), cpsour, cpdisp);
199 return errbuf;
202 for (i = 128; i <= 255; ++i)
203 conv_displ[i] = translate_character (cd, i);
205 iconv_close (cd);
207 /* inpit <- display table */
209 cd = iconv_open (cpsour, cpdisp);
210 if (cd == (iconv_t) - 1) {
211 g_snprintf (errbuf, sizeof (errbuf),
212 _("Cannot translate from %s to %s"), cpdisp, cpsour);
213 return errbuf;
216 for (i = 128; i <= 255; ++i) {
217 unsigned char ch;
218 ch = translate_character (cd, i);
219 conv_input[i] = (ch == UNKNCHAR) ? i : ch;
222 iconv_close (cd);
224 return NULL;
227 void
228 convert_to_display (char *str)
230 if (!str)
231 return;
233 while (*str) {
234 *str = conv_displ[(unsigned char) *str];
235 str++;
239 void
240 convert_from_input (char *str)
242 if (!str)
243 return;
245 while (*str) {
246 *str = conv_input[(unsigned char) *str];
247 str++;
250 #endif /* HAVE_CHARSET */