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.
33 struct codepage_desc
*codepages
;
35 unsigned char conv_displ
[256];
36 unsigned char conv_input
[256];
39 load_codepages_list (void)
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
);
57 for (n_codepages
= 0; fgets (buf
, sizeof (buf
), f
);)
58 if (buf
[0] != '\n' && buf
[0] != '\0' && buf
[0] != '#')
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 */
67 int buflen
= strlen (buf
);
69 if (*p
== '\n' || *p
== '\0' || *p
== '#')
72 if (buflen
> 0 && buf
[buflen
- 1] == '\n')
73 buf
[buflen
- 1] = '\0';
74 while (*p
!= '\t' && *p
!= ' ' && *p
!= '\0')
81 while (*p
== '\t' || *p
== ' ')
86 if (strcmp (buf
, "default") == 0) {
87 default_codepage
= g_strdup (p
);
91 codepages
[n_codepages
].id
= g_strdup (buf
);
92 codepages
[n_codepages
].name
= g_strdup (p
);
96 if (default_codepage
) {
97 display_codepage
= get_codepage_index (default_codepage
);
98 g_free (default_codepage
);
101 result
= n_codepages
;
108 free_codepages_list (void)
110 if (n_codepages
> 0) {
112 for (i
= 0; i
< n_codepages
; i
++) {
113 g_free (codepages
[i
].id
);
114 g_free (codepages
[i
].name
);
122 #define OTHER_8BIT "Other_8_bit"
125 get_codepage_id (int n
)
127 return (n
< 0) ? OTHER_8BIT
: codepages
[n
].id
;
131 get_codepage_index (const char *id
)
134 if (strcmp (id
, OTHER_8BIT
) == 0)
136 for (i
= 0; codepages
[i
].id
; ++i
)
137 if (strcmp (id
, codepages
[i
].id
) == 0)
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
;
153 count
= iconv (cd
, &ibuf
, &ibuflen
, &obuf
, &obuflen
);
154 if (count
>= 0 && ibuflen
== 0)
163 * FIXME: This assumes that ASCII is always the first encoding
169 init_translation_table (int cpsource
, int cpdisplay
)
173 char *cpsour
, *cpdisp
;
175 /* Fill inpit <-> display tables */
177 if (cpsource
< 0 || cpdisplay
< 0 || cpsource
== cpdisplay
) {
178 for (i
= 0; i
<= 255; ++i
) {
185 for (i
= 0; i
<= 127; ++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
);
202 for (i
= 128; i
<= 255; ++i
)
203 conv_displ
[i
] = translate_character (cd
, i
);
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
);
216 for (i
= 128; i
<= 255; ++i
) {
218 ch
= translate_character (cd
, i
);
219 conv_input
[i
] = (ch
== UNKNCHAR
) ? i
: ch
;
228 convert_to_display (char *str
)
234 *str
= conv_displ
[(unsigned char) *str
];
240 convert_from_input (char *str
)
246 *str
= conv_input
[(unsigned char) *str
];
250 #endif /* HAVE_CHARSET */