1
1

* charset.h [!HAVE_CHARSET]: Provide dummy macros for

convert_to_display and convert_from_input.  Eliminate uchar
definition, use unsigned char, adjust dependencies.
* charsets.c (convert_to_display): Tolerate NULL argument.
(convert_from_input): Likewise.
Этот коммит содержится в:
Pavel Roskin 2002-10-30 23:14:26 +00:00
родитель a305ec68f9
Коммит 517243efa0
3 изменённых файлов: 98 добавлений и 74 удалений

Просмотреть файл

@ -1,5 +1,11 @@
2002-10-30 Pavel Roskin <proski@gnu.org>
* charset.h [!HAVE_CHARSET]: Provide dummy macros for
convert_to_display and convert_from_input. Eliminate uchar
definition, use unsigned char, adjust dependencies.
* charsets.c (convert_to_display): Tolerate NULL argument.
(convert_from_input): Likewise.
* ext.c (exec_extension): Don't create self-destructing scripts
for the viewer - remove those scripts manually after calling
view(). This fixes the problem with double F8 in the viewer on

Просмотреть файл

@ -32,54 +32,55 @@ int n_codepages = 0;
struct codepage_desc *codepages;
uchar conv_displ[256];
uchar conv_input[256];
unsigned char conv_displ[256];
unsigned char conv_input[256];
int load_codepages_list(void)
int
load_codepages_list (void)
{
int result = -1;
FILE *f;
char *fname;
char buf[256];
extern char* mc_home;
extern char *mc_home;
extern int display_codepage;
char * default_codepage = NULL;
char *default_codepage = NULL;
fname = concat_dir_and_file (mc_home, CHARSETS_INDEX);
if ( !( f = fopen( fname, "r" ) ) ) {
if (!(f = fopen (fname, "r"))) {
fprintf (stderr, _("Warning: file %s not found\n"), fname);
g_free (fname);
return -1;
}
g_free (fname);
for ( n_codepages=0; fgets( buf, sizeof (buf), f ); )
if ( buf[0] != '\n' && buf[0] != '\0' && buf [0] != '#' )
for (n_codepages = 0; fgets (buf, sizeof (buf), f);)
if (buf[0] != '\n' && buf[0] != '\0' && buf[0] != '#')
++n_codepages;
rewind( f );
rewind (f);
codepages = g_new0 ( struct codepage_desc, n_codepages + 1 );
codepages = g_new0 (struct codepage_desc, n_codepages + 1);
for( n_codepages = 0; fgets( buf, sizeof buf, f ); ) {
for (n_codepages = 0; fgets (buf, sizeof buf, f);) {
/* split string into id and cpname */
char *p = buf;
int buflen = strlen( buf );
int buflen = strlen (buf);
if ( *p == '\n' || *p == '\0' || *p == '#')
if (*p == '\n' || *p == '\0' || *p == '#')
continue;
if ( buflen > 0 && buf[ buflen - 1 ] == '\n' )
buf[ buflen - 1 ] = '\0';
while ( *p != '\t' && *p != ' ' && *p != '\0' )
if (buflen > 0 && buf[buflen - 1] == '\n')
buf[buflen - 1] = '\0';
while (*p != '\t' && *p != ' ' && *p != '\0')
++p;
if ( *p == '\0' )
if (*p == '\0')
goto fail;
*p++ = 0;
while ( *p == '\t' || *p == ' ' )
while (*p == '\t' || *p == ' ')
++p;
if ( *p == '\0' )
if (*p == '\0')
goto fail;
if (strcmp (buf, "default") == 0) {
@ -87,8 +88,8 @@ int load_codepages_list(void)
continue;
}
codepages[n_codepages].id = g_strdup( buf );
codepages[n_codepages].name = g_strdup( p );
codepages[n_codepages].id = g_strdup (buf);
codepages[n_codepages].name = g_strdup (p);
++n_codepages;
}
@ -98,12 +99,13 @@ int load_codepages_list(void)
}
result = n_codepages;
fail:
fclose( f );
fail:
fclose (f);
return result;
}
void free_codepages_list (void)
void
free_codepages_list (void)
{
if (n_codepages > 0) {
int i;
@ -119,32 +121,36 @@ void free_codepages_list (void)
#define OTHER_8BIT "Other_8_bit"
char *get_codepage_id( int n )
char *
get_codepage_id (int n)
{
return (n < 0) ? OTHER_8BIT : codepages[ n ].id;
return (n < 0) ? OTHER_8BIT : codepages[n].id;
}
int get_codepage_index( const char *id )
int
get_codepage_index (const char *id)
{
int i;
if (strcmp( id, OTHER_8BIT ) == 0)
if (strcmp (id, OTHER_8BIT) == 0)
return -1;
for ( i=0; codepages[ i ].id; ++i )
if (strcmp( id, codepages[ i ].id ) == 0)
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, char c )
static char
translate_character (iconv_t cd, char c)
{
char outbuf[4], *obuf;
size_t ibuflen, obuflen, count;
ICONV_CONST char *ibuf = &c;
ICONV_CONST char *ibuf = &c;
obuf = outbuf;
ibuflen = 1; obuflen = 4;
ibuflen = 1;
obuflen = 4;
count = iconv(cd, &ibuf, &ibuflen, &obuf, &obuflen);
count = iconv (cd, &ibuf, &ibuflen, &obuf, &obuflen);
if (count >= 0 && ibuflen == 0)
return outbuf[0];
@ -159,7 +165,8 @@ char errbuf[255];
*/
#define CP_ASCII 0
char* init_translation_table( int cpsource, int cpdisplay )
char *
init_translation_table (int cpsource, int cpdisplay)
{
int i;
iconv_t cd;
@ -168,64 +175,72 @@ char* init_translation_table( int cpsource, int cpdisplay )
/* Fill inpit <-> display tables */
if (cpsource < 0 || cpdisplay < 0 || cpsource == cpdisplay) {
for (i=0; i<=255; ++i) {
for (i = 0; i <= 255; ++i) {
conv_displ[i] = i;
conv_input[i] = i;
}
return NULL;
}
for (i=0; i<=127; ++i) {
for (i = 0; i <= 127; ++i) {
conv_displ[i] = i;
conv_input[i] = i;
}
cpsour = codepages[ cpsource ].id;
cpdisp = codepages[ cpdisplay ].id;
cpsour = codepages[cpsource].id;
cpdisp = codepages[cpdisplay].id;
/* display <- inpit table */
cd = iconv_open( cpdisp, cpsour );
if (cd == (iconv_t) -1) {
g_snprintf( errbuf, sizeof (errbuf),
_("Cannot translate from %s to %s"), cpsour, cpdisp );
cd = iconv_open (cpdisp, cpsour);
if (cd == (iconv_t) - 1) {
g_snprintf (errbuf, sizeof (errbuf),
_("Cannot translate from %s to %s"), cpsour, cpdisp);
return errbuf;
}
for (i=128; i<=255; ++i)
conv_displ[i] = translate_character( cd, i );
for (i = 128; i <= 255; ++i)
conv_displ[i] = translate_character (cd, i);
iconv_close( cd );
iconv_close (cd);
/* inpit <- display table */
cd = iconv_open( cpsour, cpdisp );
if (cd == (iconv_t) -1) {
g_snprintf( errbuf, sizeof (errbuf),
_("Cannot translate from %s to %s"), cpdisp, cpsour );
cd = iconv_open (cpsour, cpdisp);
if (cd == (iconv_t) - 1) {
g_snprintf (errbuf, sizeof (errbuf),
_("Cannot translate from %s to %s"), cpdisp, cpsour);
return errbuf;
}
for (i=128; i<=255; ++i) {
uchar ch;
ch = translate_character( cd, i );
for (i = 128; i <= 255; ++i) {
unsigned char ch;
ch = translate_character (cd, i);
conv_input[i] = (ch == UNKNCHAR) ? i : ch;
}
iconv_close( cd );
iconv_close (cd);
return NULL;
}
void convert_to_display( char *str )
void
convert_to_display (char *str)
{
while ((*str = conv_displ[ (uchar) *str ]))
if (!str)
return;
while ((*str = conv_displ[(unsigned char) *str]))
str++;
}
void convert_from_input( char *str )
void
convert_from_input (char *str)
{
while ((*str = conv_input[ (uchar) *str ]))
if (!str)
return;
while ((*str = conv_input[(unsigned char) *str]))
str++;
}
#endif /* HAVE_CHARSET */
#endif /* HAVE_CHARSET */

Просмотреть файл

@ -1,18 +1,17 @@
#ifdef HAVE_CHARSET
#ifndef __CHARSETS_H__
#define __CHARSETS_H__
#ifdef HAVE_CHARSET
#define UNKNCHAR '\001'
#define CHARSETS_INDEX "mc.charsets"
typedef unsigned char uchar;
extern int n_codepages;
extern uchar conv_displ[256];
extern uchar conv_input[256];
extern uchar printable[256];
extern unsigned char conv_displ[256];
extern unsigned char conv_input[256];
extern unsigned char printable[256];
struct codepage_desc {
char *id;
@ -21,14 +20,18 @@ struct codepage_desc {
extern struct codepage_desc *codepages;
char *get_codepage_id( int n );
int get_codepage_index( const char *id );
int load_codepages_list(void);
void free_codepages_list(void);
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 );
char *get_codepage_id (int n);
int get_codepage_index (const char *id);
int load_codepages_list (void);
void free_codepages_list (void);
char *init_translation_table (int cpsource, int cpdisplay);
void convert_to_display (char *str);
void convert_from_input (char *str);
void convert_string (unsigned char *str);
#else /* !HAVE_CHARSET */
#define convert_to_display(x) do {} while (0)
#define convert_from_input(x) do {} while (0)
#endif /* HAVE_CHARSET */
#endif /* __CHARSETS_H__ */
#endif /* HAVE_CHARSET */