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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
35 struct codepage_desc
*codepages
;
37 unsigned char conv_displ
[256];
38 unsigned char conv_input
[256];
41 load_codepages_list (void)
48 extern int display_codepage
;
49 char *default_codepage
= NULL
;
51 fname
= concat_dir_and_file (mc_home
, CHARSETS_INDEX
);
52 if (!(f
= fopen (fname
, "r"))) {
53 fprintf (stderr
, _("Warning: file %s not found\n"), fname
);
59 for (n_codepages
= 0; fgets (buf
, sizeof (buf
), f
);)
60 if (buf
[0] != '\n' && buf
[0] != '\0' && buf
[0] != '#')
64 codepages
= g_new0 (struct codepage_desc
, n_codepages
+ 1);
66 for (n_codepages
= 0; fgets (buf
, sizeof buf
, f
);) {
67 /* split string into id and cpname */
69 int buflen
= strlen (buf
);
71 if (*p
== '\n' || *p
== '\0' || *p
== '#')
74 if (buflen
> 0 && buf
[buflen
- 1] == '\n')
75 buf
[buflen
- 1] = '\0';
76 while (*p
!= '\t' && *p
!= ' ' && *p
!= '\0')
83 while (*p
== '\t' || *p
== ' ')
88 if (strcmp (buf
, "default") == 0) {
89 default_codepage
= g_strdup (p
);
93 codepages
[n_codepages
].id
= g_strdup (buf
);
94 codepages
[n_codepages
].name
= g_strdup (p
);
98 if (default_codepage
) {
99 display_codepage
= get_codepage_index (default_codepage
);
100 g_free (default_codepage
);
103 result
= n_codepages
;
110 free_codepages_list (void)
112 if (n_codepages
> 0) {
114 for (i
= 0; i
< n_codepages
; i
++) {
115 g_free (codepages
[i
].id
);
116 g_free (codepages
[i
].name
);
124 #define OTHER_8BIT "Other_8_bit"
127 get_codepage_id (int n
)
129 return (n
< 0) ? OTHER_8BIT
: codepages
[n
].id
;
133 get_codepage_index (const char *id
)
136 if (strcmp (id
, OTHER_8BIT
) == 0)
138 for (i
= 0; codepages
[i
].id
; ++i
)
139 if (strcmp (id
, codepages
[i
].id
) == 0)
145 translate_character (iconv_t cd
, char c
)
147 char outbuf
[4], *obuf
;
148 size_t ibuflen
, obuflen
, count
;
150 ICONV_CONST
char *ibuf
= &c
;
155 count
= iconv (cd
, &ibuf
, &ibuflen
, &obuf
, &obuflen
);
156 if (count
!= ((size_t) -1) && ibuflen
== 0)
165 * FIXME: This assumes that ASCII is always the first encoding
171 init_translation_table (int cpsource
, int cpdisplay
)
175 const char *cpsour
, *cpdisp
;
177 /* Fill inpit <-> display tables */
179 if (cpsource
< 0 || cpdisplay
< 0 || cpsource
== cpdisplay
) {
180 for (i
= 0; i
<= 255; ++i
) {
187 for (i
= 0; i
<= 127; ++i
) {
192 cpsour
= codepages
[cpsource
].id
;
193 cpdisp
= codepages
[cpdisplay
].id
;
195 /* display <- inpit table */
197 cd
= iconv_open (cpdisp
, cpsour
);
198 if (cd
== (iconv_t
) - 1) {
199 g_snprintf (errbuf
, sizeof (errbuf
),
200 _("Cannot translate from %s to %s"), cpsour
, cpdisp
);
204 for (i
= 128; i
<= 255; ++i
)
205 conv_displ
[i
] = translate_character (cd
, i
);
209 /* inpit <- display table */
211 cd
= iconv_open (cpsour
, cpdisp
);
212 if (cd
== (iconv_t
) - 1) {
213 g_snprintf (errbuf
, sizeof (errbuf
),
214 _("Cannot translate from %s to %s"), cpdisp
, cpsour
);
218 for (i
= 128; i
<= 255; ++i
) {
220 ch
= translate_character (cd
, i
);
221 conv_input
[i
] = (ch
== UNKNCHAR
) ? i
: ch
;
230 convert_to_display (char *str
)
236 *str
= conv_displ
[(unsigned char) *str
];
242 convert_from_input (char *str
)
248 *str
= conv_input
[(unsigned char) *str
];
252 #endif /* HAVE_CHARSET */