* charsets.c: New file for charset conversion support.
From Walery Studennikov. * charsets.h: Likewise. * selcodepage.c: Likewise. * selcodepage.h: Likewise.
Этот коммит содержится в:
родитель
64b09e307a
Коммит
e72a150ac9
@ -1,5 +1,11 @@
|
||||
2001-05-30 Pavel Roskin <proski@gnu.org>
|
||||
|
||||
* charsets.c: New file for charset conversion support.
|
||||
From Walery Studennikov.
|
||||
* charsets.h: Likewise.
|
||||
* selcodepage.c: Likewise.
|
||||
* selcodepage.h: Likewise.
|
||||
|
||||
* layout.c (init_curses) [!HAVE_SLANG]: Set ESCDELAY to 0 if
|
||||
possible to prevent ncurses from waiting after escape.
|
||||
|
||||
|
184
src/charsets.c
Обычный файл
184
src/charsets.c
Обычный файл
@ -0,0 +1,184 @@
|
||||
#ifdef HAVE_CHARSET
|
||||
#include <config.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <iconv.h>
|
||||
|
||||
#include "charsets.h"
|
||||
#include "i18n.h"
|
||||
|
||||
int n_codepages = 0;
|
||||
|
||||
struct codepage_desc *codepages;
|
||||
|
||||
uchar conv_displ[256];
|
||||
uchar conv_input[256];
|
||||
uchar printable[256];
|
||||
|
||||
static char *xstrncpy( char *dest, const char *src, int n )
|
||||
{
|
||||
strncpy( dest, src, n );
|
||||
dest[n] = '\0';
|
||||
return dest;
|
||||
}
|
||||
|
||||
int load_codepages_list()
|
||||
{
|
||||
int result = -1;
|
||||
FILE *f;
|
||||
char buf[256];
|
||||
extern char* mc_home;
|
||||
|
||||
strcpy ( buf, mc_home );
|
||||
strcat ( buf, "/mc.charsets" );
|
||||
if ( !( f = fopen( buf, "r" ) ) )
|
||||
return -1;
|
||||
|
||||
for ( n_codepages=0; fgets( buf, sizeof buf, f ); )
|
||||
if ( buf[0] != '\n' && buf[0] != '\0' )
|
||||
++n_codepages;
|
||||
rewind( f );
|
||||
|
||||
codepages = calloc( n_codepages + 1, sizeof(struct codepage_desc) );
|
||||
|
||||
for( n_codepages = 0; fgets( buf, sizeof buf, f ); ) {
|
||||
/* split string into id and cpname */
|
||||
char *p = buf;
|
||||
int buflen = strlen( buf );
|
||||
if ( *p == '\n' || *p == '\0' )
|
||||
continue;
|
||||
|
||||
if ( buflen > 0 && buf[ buflen - 1 ] == '\n' )
|
||||
buf[ buflen - 1 ] = '\0';
|
||||
while ( *p != '\t' && *p != ' ' && *p != '\0' )
|
||||
++p;
|
||||
if ( *p == '\0' )
|
||||
goto fail;
|
||||
|
||||
codepages[n_codepages].id = malloc( p - buf + 1 );
|
||||
xstrncpy( codepages[n_codepages].id, buf, p - buf );
|
||||
|
||||
while ( *p == '\t' || *p == ' ' )
|
||||
++p;
|
||||
if ( *p == '\0' )
|
||||
goto fail;
|
||||
|
||||
codepages[n_codepages].name = strdup( p );
|
||||
++n_codepages;
|
||||
}
|
||||
|
||||
result = n_codepages;
|
||||
fail:
|
||||
fclose( f );
|
||||
return result;
|
||||
}
|
||||
|
||||
#define OTHER_8BIT "Other_8_bit"
|
||||
|
||||
char *get_codepage_id( int n )
|
||||
{
|
||||
return (n < 0) ? OTHER_8BIT : codepages[ n ].id;
|
||||
}
|
||||
|
||||
int get_codepage_index( const char *id )
|
||||
{
|
||||
int i;
|
||||
if (strcmp( id, OTHER_8BIT ) == 0)
|
||||
return -1;
|
||||
for ( i=0; codepages[ i ].id; ++i )
|
||||
if (strcmp( id, codepages[ i ].id ) == 0)
|
||||
return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static char translate_character( iconv_t cd, const char c )
|
||||
{
|
||||
char outbuf[4], *obuf;
|
||||
size_t ibuflen, obuflen, count;
|
||||
|
||||
const char *ibuf = &c;
|
||||
obuf = outbuf;
|
||||
ibuflen = 1; obuflen = 4;
|
||||
|
||||
count = iconv(cd, &ibuf, &ibuflen, &obuf, &obuflen);
|
||||
if (count >= 0 && ibuflen == 0)
|
||||
return outbuf[0];
|
||||
|
||||
return UNKNCHAR;
|
||||
}
|
||||
|
||||
char errbuf[255];
|
||||
|
||||
char* init_translation_table( int cpsource, int cpdisplay )
|
||||
{
|
||||
int i;
|
||||
iconv_t cd;
|
||||
uchar ch;
|
||||
char *cpsour, *cpdisp;
|
||||
|
||||
/* Fill inpit <-> display tables */
|
||||
|
||||
if (cpsource < 0 || cpdisplay < 0 || cpsource == cpdisplay) {
|
||||
for (i=0; i<=255; ++i) {
|
||||
conv_displ[i] = i;
|
||||
conv_input[i] = i;
|
||||
printable[i] = (i > 31 && i != 127);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (i=0; i<=127; ++i) {
|
||||
conv_displ[i] = i;
|
||||
conv_input[i] = i;
|
||||
printable[i] = (i > 31 && i != 127);
|
||||
}
|
||||
|
||||
cpsour = codepages[ cpsource ].id;
|
||||
cpdisp = codepages[ cpdisplay ].id;
|
||||
|
||||
/* display <- inpit table */
|
||||
|
||||
cd = iconv_open( cpdisp, cpsour );
|
||||
if (cd == (iconv_t) -1) {
|
||||
sprintf( errbuf, _("Can't translate from %s to %s"), cpsour, cpdisp );
|
||||
return errbuf;
|
||||
}
|
||||
|
||||
for (i=128; i<=255; ++i)
|
||||
conv_displ[i] = translate_character( cd, i );
|
||||
|
||||
iconv_close( cd );
|
||||
|
||||
/* inpit <- display table */
|
||||
|
||||
cd = iconv_open( cpsour, cpdisp );
|
||||
if (cd == (iconv_t) -1) {
|
||||
sprintf( errbuf, _("Can't translate from %s to %s"), cpdisp, cpsour );
|
||||
return errbuf;
|
||||
}
|
||||
|
||||
for (i=128; i<=255; ++i) {
|
||||
ch = translate_character( cd, i );
|
||||
conv_input[i] = (ch == UNKNCHAR) ? i : ch;
|
||||
}
|
||||
|
||||
iconv_close( cd );
|
||||
|
||||
/* ch = (strcmp( cpdisp, "ASCII" ) == 0) ? 0 : 1; */
|
||||
for (i=128; i<=255; ++i)
|
||||
printable[i] = 1;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void convert_to_display( char *str )
|
||||
{
|
||||
while ( (*str++ = conv_displ[ (uchar) *str ]) ) ;
|
||||
}
|
||||
|
||||
void convert_from_input( char *str )
|
||||
{
|
||||
while ( (*str++ = conv_input[ (uchar) *str ]) ) ;
|
||||
}
|
||||
#endif /* HAVE_CHARSET */
|
33
src/charsets.h
Обычный файл
33
src/charsets.h
Обычный файл
@ -0,0 +1,33 @@
|
||||
#ifdef HAVE_CHARSET
|
||||
#ifndef __CHARSETS_H__
|
||||
#define __CHARSETS_H__
|
||||
|
||||
#define UNKNCHAR '\001'
|
||||
|
||||
#define CHARSETS_INDEX LIBDIR "mc.charsets"
|
||||
|
||||
typedef unsigned char uchar;
|
||||
|
||||
extern int n_codepages;
|
||||
|
||||
extern uchar conv_displ[256];
|
||||
extern uchar conv_input[256];
|
||||
extern uchar printable[256];
|
||||
|
||||
struct codepage_desc {
|
||||
char *id;
|
||||
char *name;
|
||||
};
|
||||
|
||||
extern struct codepage_desc *codepages;
|
||||
|
||||
char *get_codepage_id( int n );
|
||||
int get_codepage_index( const char *id );
|
||||
int load_codepages_list();
|
||||
char* init_translation_table( int cpsource, int cpdisplay );
|
||||
void convert_to_display( char *str );
|
||||
void convert_from_input( char *str );
|
||||
void convert_string( uchar *str );
|
||||
|
||||
#endif /* __CHARSETS_H__ */
|
||||
#endif /* HAVE_CHARSET */
|
61
src/selcodepage.c
Обычный файл
61
src/selcodepage.c
Обычный файл
@ -0,0 +1,61 @@
|
||||
#ifdef HAVE_CHARSET
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "dlg.h"
|
||||
#include "widget.h"
|
||||
#include "wtools.h"
|
||||
#include "charsets.h"
|
||||
#include "i18n.h"
|
||||
|
||||
#define ENTRY_LEN 35
|
||||
|
||||
/* Numbers of (file I/O) and (input/display) codepages. -1 if not selected */
|
||||
int source_codepage = -1;
|
||||
int display_codepage = -1;
|
||||
|
||||
unsigned char get_hotkey( int n )
|
||||
{
|
||||
return (n <= 9) ? '0' + n : 'a' + n - 10;
|
||||
}
|
||||
|
||||
int select_charset( int current_charset, int seldisplay )
|
||||
{
|
||||
int i, menu_lines = n_codepages + 1;
|
||||
|
||||
/* Create listbox */
|
||||
Listbox* listbox =
|
||||
create_listbox_window ( ENTRY_LEN + 2, menu_lines,
|
||||
_(" Choose input codepage "),
|
||||
"[Codepages Translation]");
|
||||
|
||||
if (!seldisplay)
|
||||
LISTBOX_APPEND_TEXT( listbox, '-', _("- < No translation >"), NULL );
|
||||
|
||||
/* insert all the items found */
|
||||
for (i = 0; i < n_codepages; i++) {
|
||||
struct codepage_desc cpdesc = codepages[i];
|
||||
char buffer[255];
|
||||
sprintf( buffer, "%c %s", get_hotkey(i), cpdesc.name );
|
||||
LISTBOX_APPEND_TEXT( listbox, get_hotkey(i), buffer, NULL );
|
||||
}
|
||||
if (seldisplay) {
|
||||
char buffer[255];
|
||||
sprintf( buffer, "%c %s", get_hotkey(n_codepages), _("Other 8 bit"));
|
||||
LISTBOX_APPEND_TEXT( listbox, get_hotkey(n_codepages), buffer, NULL );
|
||||
}
|
||||
|
||||
/* Select the default entry */
|
||||
i = (seldisplay)
|
||||
?
|
||||
( (current_charset < 0) ? n_codepages : current_charset )
|
||||
:
|
||||
( current_charset + 1 );
|
||||
|
||||
listbox_select_by_number( listbox->list, i );
|
||||
|
||||
i = run_listbox( listbox );
|
||||
|
||||
return (seldisplay) ? ( (i >= n_codepages) ? -1 : i )
|
||||
: ( i - 1 );
|
||||
}
|
||||
#endif /* HAVE_CHARSET */
|
8
src/selcodepage.h
Обычный файл
8
src/selcodepage.h
Обычный файл
@ -0,0 +1,8 @@
|
||||
#ifdef HAVE_CHARSET
|
||||
#ifndef __SELCODEPAGE_H__
|
||||
#define __SELCODEPAGE_H__
|
||||
|
||||
int select_charset( int current_charset, int seldisplay );
|
||||
|
||||
#endif /* __SELCODEPAGE_H__ */
|
||||
#endif /* HAVE_CHARSET */
|
Загрузка…
Ссылка в новой задаче
Block a user