14 struct codepage_desc
*codepages
;
16 uchar conv_displ
[256];
17 uchar conv_input
[256];
19 int load_codepages_list(void)
26 extern int display_codepage
;
27 char * default_codepage
= NULL
;
29 fname
= concat_dir_and_file (mc_home
, CHARSETS_INDEX
);
30 if ( !( f
= fopen( fname
, "r" ) ) ) {
31 fprintf (stderr
, _("Warning: file %s not found\n"), fname
);
37 for ( n_codepages
=0; fgets( buf
, sizeof (buf
), f
); )
38 if ( buf
[0] != '\n' && buf
[0] != '\0' && buf
[0] != '#' )
42 codepages
= g_new0 ( struct codepage_desc
, n_codepages
+ 1 );
44 for( n_codepages
= 0; fgets( buf
, sizeof buf
, f
); ) {
45 /* split string into id and cpname */
47 int buflen
= strlen( buf
);
49 if ( *p
== '\n' || *p
== '\0' || *p
== '#')
52 if ( buflen
> 0 && buf
[ buflen
- 1 ] == '\n' )
53 buf
[ buflen
- 1 ] = '\0';
54 while ( *p
!= '\t' && *p
!= ' ' && *p
!= '\0' )
61 while ( *p
== '\t' || *p
== ' ' )
66 if (strcmp (buf
, "default") == 0) {
67 default_codepage
= g_strdup (p
);
71 codepages
[n_codepages
].id
= g_strdup( buf
);
72 codepages
[n_codepages
].name
= g_strdup( p
);
76 if (default_codepage
) {
77 display_codepage
= get_codepage_index (default_codepage
);
78 g_free (default_codepage
);
87 void free_codepages_list (void)
89 if (n_codepages
> 0) {
91 for (i
= 0; i
< n_codepages
; i
++) {
92 g_free (codepages
[i
].id
);
93 g_free (codepages
[i
].name
);
101 #define OTHER_8BIT "Other_8_bit"
103 char *get_codepage_id( int n
)
105 return (n
< 0) ? OTHER_8BIT
: codepages
[ n
].id
;
108 int get_codepage_index( const char *id
)
111 if (strcmp( id
, OTHER_8BIT
) == 0)
113 for ( i
=0; codepages
[ i
].id
; ++i
)
114 if (strcmp( id
, codepages
[ i
].id
) == 0)
119 static char translate_character( iconv_t cd
, char c
)
121 char outbuf
[4], *obuf
;
122 size_t ibuflen
, obuflen
, count
;
124 ICONV_CONST
char *ibuf
= &c
;
126 ibuflen
= 1; obuflen
= 4;
128 count
= iconv(cd
, &ibuf
, &ibuflen
, &obuf
, &obuflen
);
129 if (count
>= 0 && ibuflen
== 0)
138 * FIXME: This assumes that ASCII is always the first encoding
143 char* init_translation_table( int cpsource
, int cpdisplay
)
147 char *cpsour
, *cpdisp
;
149 /* Fill inpit <-> display tables */
151 if (cpsource
< 0 || cpdisplay
< 0 || cpsource
== cpdisplay
) {
152 for (i
=0; i
<=255; ++i
) {
159 for (i
=0; i
<=127; ++i
) {
164 cpsour
= codepages
[ cpsource
].id
;
165 cpdisp
= codepages
[ cpdisplay
].id
;
167 /* display <- inpit table */
169 cd
= iconv_open( cpdisp
, cpsour
);
170 if (cd
== (iconv_t
) -1) {
171 sprintf( errbuf
, _("Cannot translate from %s to %s"), cpsour
, cpdisp
);
175 for (i
=128; i
<=255; ++i
)
176 conv_displ
[i
] = translate_character( cd
, i
);
180 /* inpit <- display table */
182 cd
= iconv_open( cpsour
, cpdisp
);
183 if (cd
== (iconv_t
) -1) {
184 sprintf( errbuf
, _("Cannot translate from %s to %s"), cpdisp
, cpsour
);
188 for (i
=128; i
<=255; ++i
) {
190 ch
= translate_character( cd
, i
);
191 conv_input
[i
] = (ch
== UNKNCHAR
) ? i
: ch
;
199 void convert_to_display( char *str
)
201 while ((*str
= conv_displ
[ (uchar
) *str
]))
205 void convert_from_input( char *str
)
207 while ((*str
= conv_input
[ (uchar
) *str
]))
210 #endif /* HAVE_CHARSET */