1
1

Ticket #1529 (remove search converter overhead)

* add: void tty_print_anychar (int c)
    * remove superfluous search converter in convert_from_utf_to_current_c,
      convert_from_8bit_to_utf_c add converter to WEdit struct
    * fix: init defaulf codesets
    * fix: segfault on replace if LANG=C

Signed-off-by: Ilia Maslakov <il.smind@gmail.com>
Этот коммит содержится в:
Ilia Maslakov 2009-08-13 05:59:31 +00:00
родитель 0ff82e59c1
Коммит 4afee14f94
10 изменённых файлов: 107 добавлений и 71 удалений

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

@ -132,6 +132,7 @@ struct WEdit {
/* user map stuff */ /* user map stuff */
const edit_key_map_type *user_map; const edit_key_map_type *user_map;
const edit_key_map_type *ext_map; const edit_key_map_type *ext_map;
GIConv converter;
int extmod; int extmod;

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

@ -285,19 +285,9 @@ edit_load_file_fast (WEdit *edit, const char *filename)
{ {
long buf, buf2; long buf, buf2;
int file = -1; int file = -1;
#ifdef HAVE_CHARSET
const char *cp_id;
#endif
edit->curs2 = edit->last_byte; edit->curs2 = edit->last_byte;
buf2 = edit->curs2 >> S_EDIT_BUF_SIZE; buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
edit->utf8 = 0; edit->utf8 = 0;
#ifdef HAVE_CHARSET
cp_id = get_codepage_id (source_codepage);
if (cp_id != NULL)
edit->utf8 = str_isutf8 (cp_id);
#endif
if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1) { if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1) {
GString *errmsg = g_string_new(NULL); GString *errmsg = g_string_new(NULL);
g_string_sprintf(errmsg, _(" Cannot open %s for reading "), filename); g_string_sprintf(errmsg, _(" Cannot open %s for reading "), filename);
@ -723,6 +713,26 @@ edit_init (WEdit *edit, int lines, int columns, const char *filename,
g_free (edit); g_free (edit);
return 0; return 0;
} }
edit->utf8 = 0;
edit->converter = str_cnv_from_term;
#ifdef HAVE_CHARSET
const char *cp_id = NULL;
cp_id = get_codepage_id (source_codepage >= 0 ?
source_codepage : display_codepage);
if (cp_id != NULL) {
GIConv conv;
conv = str_crt_conv_from (cp_id);
if (conv != INVALID_CONV) {
if (edit->converter != str_cnv_from_term)
str_close_conv (edit->converter);
edit->converter = conv;
}
}
if (cp_id != NULL)
edit->utf8 = str_isutf8 (cp_id);
#endif
edit->loading_done = 1; edit->loading_done = 1;
edit->modified = 0; edit->modified = 0;
edit->locked = 0; edit->locked = 0;
@ -1092,7 +1102,9 @@ edit_insert (WEdit *edit, int c)
void void
edit_insert_over (WEdit * edit) edit_insert_over (WEdit * edit)
{ {
for (int i = 0; i < edit->over_col; i++ ) { int i;
for ( i = 0; i < edit->over_col; i++ ) {
edit_insert (edit, ' '); edit_insert (edit, ' ');
} }
edit->over_col = 0; edit->over_col = 0;

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

@ -2393,12 +2393,22 @@ void
edit_select_codepage_cmd (WEdit *edit) edit_select_codepage_cmd (WEdit *edit)
{ {
#ifdef HAVE_CHARSET #ifdef HAVE_CHARSET
const char *cp_id = NULL;
if (do_select_codepage ()) { if (do_select_codepage ()) {
const char *cp_id;
cp_id = get_codepage_id (source_codepage >= 0 ? cp_id = get_codepage_id (source_codepage >= 0 ?
source_codepage : display_codepage); source_codepage : display_codepage);
if (cp_id != NULL) {
GIConv conv;
conv = str_crt_conv_from (cp_id);
if (conv != INVALID_CONV) {
if (edit->converter != str_cnv_from_term)
str_close_conv (edit->converter);
edit->converter = conv;
}
}
if (cp_id != NULL) if (cp_id != NULL)
edit->utf8 = str_isutf8 (cp_id); edit->utf8 = str_isutf8 (cp_id);
} }

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

@ -332,18 +332,7 @@ print_to_widget (WEdit *edit, long row, int start_col, int start_col_real,
tty_lowlevel_setcolor (color); tty_lowlevel_setcolor (color);
} }
} }
if ( textchar > 255 ) { tty_print_anychar (textchar);
int res = g_unichar_to_utf8 (textchar, (char *)str);
if ( res == 0 ) {
str[0] = '.';
str[1] = '\0';
} else {
str[res] = '\0';
}
tty_print_string ((char *) str);
} else {
tty_print_char (textchar);
}
p++; p++;
} }
} }
@ -497,11 +486,11 @@ edit_draw_this_line (WEdit *edit, long b, long row, long start_col,
#ifdef HAVE_CHARSET #ifdef HAVE_CHARSET
if ( utf8_display ) { if ( utf8_display ) {
if ( !edit->utf8 ) { if ( !edit->utf8 ) {
c = convert_from_8bit_to_utf_c ((unsigned char) c); c = convert_from_8bit_to_utf_c ((unsigned char) c, edit->converter);
} }
} else { } else {
if ( edit->utf8 ) { if ( edit->utf8 ) {
c = convert_from_utf_to_current_c (c); c = convert_from_utf_to_current_c (c, edit->converter);
} else { } else {
#endif #endif
c = convert_to_display_c (c); c = convert_to_display_c (c);

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

@ -351,14 +351,12 @@ convert_from_utf_to_current (const char *str)
} }
unsigned char unsigned char
convert_from_utf_to_current_c (const int input_char) convert_from_utf_to_current_c (const int input_char, GIConv conv)
{ {
unsigned char str[6 + 1]; unsigned char str[6 + 1];
unsigned char buf_ch[6 + 1]; unsigned char buf_ch[6 + 1];
unsigned char ch = '.'; unsigned char ch = '.';
const char *cp_from;
GIConv conv;
int res = 0; int res = 0;
res = g_unichar_to_utf8 (input_char, (char *)str); res = g_unichar_to_utf8 (input_char, (char *)str);
@ -367,59 +365,44 @@ convert_from_utf_to_current_c (const int input_char)
} }
str[res] = '\0'; str[res] = '\0';
cp_from = get_codepage_id ( source_codepage ); switch (str_translate_char (conv, (char *)str, -1, (char *)buf_ch, sizeof(buf_ch))) {
conv = str_crt_conv_from ( cp_from ); case ESTR_SUCCESS:
ch = buf_ch[0];
if (conv != INVALID_CONV) { break;
switch (str_translate_char (conv, (char *)str, -1, (char *)buf_ch, sizeof(buf_ch))) { case ESTR_PROBLEM:
case ESTR_SUCCESS: case ESTR_FAILURE:
ch = buf_ch[0]; ch = '.';
break; break;
case ESTR_PROBLEM:
case ESTR_FAILURE:
ch = '.';
break;
}
str_close_conv (conv);
} }
return ch; return ch;
} }
int int
convert_from_8bit_to_utf_c (const char input_char) convert_from_8bit_to_utf_c (const char input_char, GIConv conv)
{ {
unsigned char str[2]; unsigned char str[2];
unsigned char buf_ch[6 + 1]; unsigned char buf_ch[6 + 1];
int ch = '.'; int ch = '.';
int res = 0; int res = 0;
GIConv conv;
const char *cp_from;
str[0] = (unsigned char) input_char; str[0] = (unsigned char) input_char;
str[1] = '\0'; str[1] = '\0';
cp_from = get_codepage_id ( source_codepage ); switch (str_translate_char (conv, (char *)str, -1, (char *)buf_ch, sizeof(buf_ch))) {
conv = str_crt_conv_from (cp_from); case ESTR_SUCCESS:
res = g_utf8_get_char_validated ((char *)buf_ch, -1);
if (conv != INVALID_CONV) { if ( res < 0 ) {
switch (str_translate_char (conv, (char *)str, -1, (char *)buf_ch, sizeof(buf_ch))) { ch = buf_ch[0];
case ESTR_SUCCESS: } else {
res = g_utf8_get_char_validated ((char *)buf_ch, -1); ch = res;
if ( res < 0 ) {
ch = buf_ch[0];
} else {
ch = res;
}
break;
case ESTR_PROBLEM:
case ESTR_FAILURE:
ch = '.';
break;
} }
str_close_conv (conv); break;
case ESTR_PROBLEM:
case ESTR_FAILURE:
ch = '.';
break;
} }
return ch; return ch;
} }
int int

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

@ -44,16 +44,16 @@ unsigned char convert_from_utf_to_current (const char *str);
* param input_char, gunichar * param input_char, gunichar
* return char in needle codepage (by global int source_codepage) * return char in needle codepage (by global int source_codepage)
*/ */
unsigned char convert_from_utf_to_current_c (const int input_char); unsigned char convert_from_utf_to_current_c (const int input_char, GIConv conv);
/* /*
* Converter from selected codepage 8-bit * Converter from selected codepage 8-bit
* param char input_char * param char input_char, GIConv converter
* return int utf char * return int utf char
*/ */
int convert_from_8bit_to_utf_c (const char input_char); int convert_from_8bit_to_utf_c (const char input_char, GIConv conv);
/* /*
* Converter from display codepage 8-bit to utf-8 * Converter from display codepage 8-bit to utf-8
* param char input_char * param char input_char, GIConv converter
* return int utf char * return int utf char
*/ */
int convert_from_8bit_to_utf_c2 (const char input_char); int convert_from_8bit_to_utf_c2 (const char input_char);

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

@ -1313,7 +1313,6 @@ static void check_codeset()
_system_codepage = str_detect_termencoding(); _system_codepage = str_detect_termencoding();
_source_codepage = get_codepage_id (source_codepage); _source_codepage = get_codepage_id (source_codepage);
_display_codepage = get_codepage_id (display_codepage); _display_codepage = get_codepage_id (display_codepage);
if ( (strcmp (_system_codepage, _display_codepage)) || if ( (strcmp (_system_codepage, _display_codepage)) ||
(strcmp (_system_codepage, _source_codepage)) ) { (strcmp (_system_codepage, _source_codepage)) ) {
if (quick_dialog (&ecs) == B_ENTER){ if (quick_dialog (&ecs) == B_ENTER){
@ -1327,6 +1326,8 @@ static void check_codeset()
source_codepage = display_codepage; source_codepage = display_codepage;
cp_source = cp_display; cp_source = cp_display;
profile_changed = 1; profile_changed = 1;
} else {
utf8_display = str_isutf8 (_system_codepage);
} }
} else { } else {
if ( skip_check_codeset ) { if ( skip_check_codeset ) {

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

@ -6,6 +6,7 @@
Written by: Written by:
Andrew Borodin <aborodin@vmail.ru>, 2009. Andrew Borodin <aborodin@vmail.ru>, 2009.
Ilia Maslakov <il.smind@gmail.com>, 2009.
This file is part of the Midnight Commander. This file is part of the Midnight Commander.
@ -284,6 +285,25 @@ tty_print_char (int c)
addch (c); addch (c);
} }
void
tty_print_anychar (int c)
{
unsigned char str[6 + 1];
if ( c > 255 ) {
int res = g_unichar_to_utf8 (c, (char *)str);
if ( res == 0 ) {
str[0] = '.';
str[1] = '\0';
} else {
str[res] = '\0';
}
addstr (str_term_form (s));
} else {
addch (c);
}
}
void void
tty_print_alt_char (int c) tty_print_alt_char (int c)
{ {

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

@ -471,6 +471,25 @@ tty_print_alt_char (int c)
SLsmg_draw_object (SLsmg_get_row(), SLsmg_get_column(), c); SLsmg_draw_object (SLsmg_get_row(), SLsmg_get_column(), c);
} }
void
tty_print_anychar (int c)
{
unsigned char str[6 + 1];
if ( c > 255 ) {
int res = g_unichar_to_utf8 (c, (char *)str);
if ( res == 0 ) {
str[0] = '.';
str[1] = '\0';
} else {
str[res] = '\0';
}
SLsmg_write_string ((char *) str_term_form (str));
} else {
SLsmg_write_char ((SLwchar_Type) ((unsigned int) c));
}
}
void void
tty_print_string (const char *s) tty_print_string (const char *s)
{ {

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

@ -65,6 +65,7 @@ extern void tty_set_alt_charset (gboolean alt_charset);
extern void tty_display_8bit (gboolean what); extern void tty_display_8bit (gboolean what);
extern void tty_print_char(int c); extern void tty_print_char(int c);
extern void tty_print_alt_char(int c); extern void tty_print_alt_char(int c);
extern void tty_print_anychar(int c);
extern void tty_print_string(const char *s); extern void tty_print_string(const char *s);
extern void tty_printf(const char *s, ...); extern void tty_printf(const char *s, ...);