14 struct codepage_desc
*codepages
;
16 uchar conv_displ
[256];
17 uchar conv_input
[256];
20 int load_codepages_list(void)
27 extern int display_codepage
;
28 char * default_codepage
= NULL
;
30 fname
= concat_dir_and_file (mc_home
, CHARSETS_INDEX
);
31 if ( !( f
= fopen( fname
, "r" ) ) ) {
32 fprintf (stderr
, _("Warning: file %s not found\n"), fname
);
38 for ( n_codepages
=0; fgets( buf
, sizeof (buf
), f
); )
39 if ( buf
[0] != '\n' && buf
[0] != '\0' && buf
[0] != '#' )
43 codepages
= calloc( n_codepages
+ 1, sizeof(struct codepage_desc
) );
45 for( n_codepages
= 0; fgets( buf
, sizeof buf
, f
); ) {
46 /* split string into id and cpname */
48 int buflen
= strlen( buf
);
50 if ( *p
== '\n' || *p
== '\0' || *p
== '#')
53 if ( buflen
> 0 && buf
[ buflen
- 1 ] == '\n' )
54 buf
[ buflen
- 1 ] = '\0';
55 while ( *p
!= '\t' && *p
!= ' ' && *p
!= '\0' )
62 while ( *p
== '\t' || *p
== ' ' )
67 if (strcmp (buf
, "default") == 0) {
68 default_codepage
= strdup (p
);
72 codepages
[n_codepages
].id
= strdup( buf
);
73 codepages
[n_codepages
].name
= strdup( p
);
77 if (default_codepage
) {
78 display_codepage
= get_codepage_index (default_codepage
);
79 free (default_codepage
);
88 #define OTHER_8BIT "Other_8_bit"
90 char *get_codepage_id( int n
)
92 return (n
< 0) ? OTHER_8BIT
: codepages
[ n
].id
;
95 int get_codepage_index( const char *id
)
98 if (strcmp( id
, OTHER_8BIT
) == 0)
100 for ( i
=0; codepages
[ i
].id
; ++i
)
101 if (strcmp( id
, codepages
[ i
].id
) == 0)
106 static char translate_character( iconv_t cd
, char c
)
108 char outbuf
[4], *obuf
;
109 size_t ibuflen
, obuflen
, count
;
113 ibuflen
= 1; obuflen
= 4;
115 count
= iconv(cd
, &ibuf
, &ibuflen
, &obuf
, &obuflen
);
116 if (count
>= 0 && ibuflen
== 0)
125 * FIXME: This assumes that ASCII is always the first encoding
130 char* init_printable_table( int cpdisplay
)
134 /* Fill printable characters table */
135 for (i
=0; i
<=127; ++i
)
136 printable
[i
] = (i
> 31 && i
!= 127);
138 for (i
=128; i
<=255; ++i
) {
145 char* init_translation_table( int cpsource
, int cpdisplay
)
149 char *cpsour
, *cpdisp
;
151 /* Fill inpit <-> display tables */
153 if (cpsource
< 0 || cpdisplay
< 0 || cpsource
== cpdisplay
) {
154 for (i
=0; i
<=255; ++i
) {
161 for (i
=0; i
<=127; ++i
) {
166 cpsour
= codepages
[ cpsource
].id
;
167 cpdisp
= codepages
[ cpdisplay
].id
;
169 /* display <- inpit table */
171 cd
= iconv_open( cpdisp
, cpsour
);
172 if (cd
== (iconv_t
) -1) {
173 sprintf( errbuf
, _("Cannot translate from %s to %s"), cpsour
, cpdisp
);
177 for (i
=128; i
<=255; ++i
)
178 conv_displ
[i
] = translate_character( cd
, i
);
182 /* inpit <- display table */
184 cd
= iconv_open( cpsour
, cpdisp
);
185 if (cd
== (iconv_t
) -1) {
186 sprintf( errbuf
, _("Cannot translate from %s to %s"), cpdisp
, cpsour
);
190 for (i
=128; i
<=255; ++i
) {
192 ch
= translate_character( cd
, i
);
193 conv_input
[i
] = (ch
== UNKNCHAR
) ? i
: ch
;
201 void convert_to_display( char *str
)
203 while ((*str
= conv_displ
[ (uchar
) *str
]))
207 void convert_from_input( char *str
)
209 while ((*str
= conv_input
[ (uchar
) *str
]))
212 #endif /* HAVE_CHARSET */