From 2ac6b553c666dd34a92b36efafaa6b9f84cd4430 Mon Sep 17 00:00:00 2001 From: Slava Zanko Date: Tue, 14 Apr 2009 13:29:01 +0300 Subject: [PATCH 01/12] Rework strutils for usage GString instread of self-made buffers. --- src/cmd.c | 15 +- src/help.c | 24 +-- src/hotlist.c | 92 +++++----- src/info.c | 34 ++-- src/strutil.c | 415 +++++++++++++++++---------------------------- src/strutil.h | 77 ++------- src/strutil8bit.c | 74 ++------ src/strutilascii.c | 14 +- src/strutilutf8.c | 293 ++++++++++++++++++-------------- src/view.c | 46 ++--- vfs/vfs.c | 48 +++--- 11 files changed, 484 insertions(+), 648 deletions(-) diff --git a/src/cmd.c b/src/cmd.c index 866a270aa..7f716aed9 100644 --- a/src/cmd.c +++ b/src/cmd.c @@ -1068,8 +1068,8 @@ get_random_hint (int force) int start; static int last_sec; static struct timeval tv; - str_conv_t conv; - struct str_buffer *buffer; + GIConv conv; + GString *buffer; /* Do not change hints more often than one minute */ gettimeofday (&tv, NULL); @@ -1095,20 +1095,19 @@ get_random_hint (int force) eol = strchr (&data[start], '\n'); if (eol) *eol = 0; - + /* hint files are stored in utf-8 */ /* try convert hint file from utf-8 to terminal encoding */ conv = str_crt_conv_from ("UTF-8"); if (conv != INVALID_CONV) { - buffer = str_get_buffer (); + buffer = g_string_new (""); if (str_convert (conv, &data[start], buffer) != ESTR_FAILURE) { - result = g_strdup (buffer->data); + result = g_strdup (buffer->str); } - - str_release_buffer (buffer); + g_string_free (buffer, TRUE); str_close_conv (conv); } - + g_free (data); return result; } diff --git a/src/help.c b/src/help.c index 9007824b0..a5bc256d8 100644 --- a/src/help.c +++ b/src/help.c @@ -69,12 +69,6 @@ #define STRING_LINK_END "\03" #define STRING_NODE_END "\04" -/* every help file is supposed to be in utf-8 and is translated to terminal - * encoding */ -/* buffer for translation */ -static struct str_buffer *translated_data = NULL; -/* point into translated_data->data or in NULL, - * if help file could not be converted */ static char *data; static int help_lines; /* Lines in help viewer */ static int history_ptr; /* For the history queue */ @@ -775,26 +769,26 @@ interactive_display_finish (void) clear_link_areas (); } -/* translate help file into terminal encoding - * translated_data is initialized */ +/* translate help file into terminal encoding */ static void translate_file (char *filedata) { - str_conv_t conv; - - if (translated_data == NULL) translated_data = str_get_buffer (); - - str_reset_buffer (translated_data); + GIConv conv; + GString *translated_data; + + translated_data = g_string_new (""); + conv = str_crt_conv_from ("UTF-8"); - + if (conv != INVALID_CONV) { if (str_convert (conv, filedata, translated_data) != ESTR_FAILURE) { - data = translated_data->data; + data = translated_data->str; } else { data = NULL; } str_close_conv (conv); } + g_string_free (translated_data, TRUE); } void diff --git a/src/hotlist.c b/src/hotlist.c index 8fd77ce9a..4c81ebb93 100644 --- a/src/hotlist.c +++ b/src/hotlist.c @@ -218,20 +218,18 @@ do { \ static void fill_listbox (void) { struct hotlist *current = current_group->head; - struct str_buffer *buff; - - buff = str_get_buffer (); + GString *buff = g_string_new (""); while (current){ switch (current->type) { case HL_TYPE_GROUP: { - str_insert_string ("->", buff); - str_insert_string (current->label, buff); + g_string_append(buff,"->"); + g_string_append(buff,current->label); if (hotlist_state.moving) - listbox_add_item (l_movelist, 0, 0, buff->data, current); + listbox_add_item (l_movelist, 0, 0, buff->str, current); else - listbox_add_item (l_hotlist, 0, 0, buff->data, current); + listbox_add_item (l_hotlist, 0, 0, buff->str, current); } break; case HL_TYPE_DOTDOT: @@ -246,8 +244,7 @@ static void fill_listbox (void) } current = current->next; } - - str_release_buffer (buff); + g_string_free (buff, TRUE); } static void @@ -1203,7 +1200,7 @@ load_group (struct hotlist *grp) #define TKN_EOF 126 #define TKN_UNKNOWN 127 -static struct str_buffer *tkn_buf = NULL; +static GString *tkn_buf = NULL; static char *hotlist_file_name; static FILE *hotlist_file; @@ -1221,41 +1218,47 @@ static int hot_skip_blanks (void) static int hot_next_token (void) { - int c; + int c, ret=0; size_t l; - if (tkn_buf == NULL) tkn_buf = str_get_buffer (); - str_reset_buffer (tkn_buf); + + if (tkn_buf == NULL) tkn_buf = g_string_new (""); + g_string_set_size(tkn_buf,0); again: c = hot_skip_blanks (); switch (c) { case EOF: - return TKN_EOF; + ret = TKN_EOF; break; case '\n': - return TKN_EOL; + ret = TKN_EOL; break; case '#': while ((c = getc (hotlist_file)) != EOF && c != '\n') { - str_insert_char (c, tkn_buf); - } - return TKN_COMMENT; + g_string_append_c (tkn_buf, c); + } + ret = TKN_COMMENT; break; case '"': while ((c = getc (hotlist_file)) != EOF && c != '"') { if (c == '\\') - if ((c = getc (hotlist_file)) == EOF) + if ((c = getc (hotlist_file)) == EOF){ + g_string_free (tkn_buf, TRUE); return TKN_EOF; - str_insert_char (c == '\n' ? ' ' : c, tkn_buf); + } + g_string_append_c (tkn_buf, c == '\n' ? ' ' : c); } if (c == EOF) - return TKN_EOF; - return TKN_STRING; + ret = TKN_EOF; + else + ret = TKN_STRING; break; case '\\': - if ((c = getc (hotlist_file)) == EOF) + if ((c = getc (hotlist_file)) == EOF){ + g_string_free (tkn_buf, TRUE); return TKN_EOF; + } if (c == '\n') goto again; @@ -1263,24 +1266,25 @@ again: default: do { - str_insert_char (g_ascii_toupper (c), tkn_buf); + g_string_append_c (tkn_buf, g_ascii_toupper (c)); } while ((c = fgetc (hotlist_file)) != EOF && (g_ascii_isalnum (c) || !isascii (c))); if (c != EOF) ungetc (c, hotlist_file); - l = tkn_buf->size - tkn_buf->remain; - if (strncmp (tkn_buf->data, "GROUP", l) == 0) - return TKN_GROUP; - else if (strncmp (tkn_buf->data, "ENTRY", l) == 0) - return TKN_ENTRY; - else if (strncmp (tkn_buf->data, "ENDGROUP", l) == 0) - return TKN_ENDGROUP; - else if (strncmp (tkn_buf->data, "URL", l) == 0) - return TKN_URL; + l = tkn_buf->len; + if (strncmp (tkn_buf->str, "GROUP", l) == 0) + ret = TKN_GROUP; + else if (strncmp (tkn_buf->str, "ENTRY", l) == 0) + ret = TKN_ENTRY; + else if (strncmp (tkn_buf->str, "ENDGROUP", l) == 0) + ret = TKN_ENDGROUP; + else if (strncmp (tkn_buf->str, "URL", l) == 0) + ret = TKN_URL; else - return TKN_UNKNOWN; + ret = TKN_UNKNOWN; break; } + return ret; } #define SKIP_TO_EOL { \ @@ -1310,22 +1314,22 @@ hot_load_group (struct hotlist * grp) switch (tkn) { case TKN_GROUP: CHECK_TOKEN(TKN_STRING); - new_grp = add2hotlist (g_strdup (tkn_buf->data), 0, HL_TYPE_GROUP, 0); + new_grp = add2hotlist (g_strdup (tkn_buf->str), 0, HL_TYPE_GROUP, 0); SKIP_TO_EOL; hot_load_group (new_grp); current_group = grp; break; case TKN_ENTRY: CHECK_TOKEN(TKN_STRING); - label = g_strdup (tkn_buf->data); + label = g_strdup (tkn_buf->str); CHECK_TOKEN(TKN_URL); CHECK_TOKEN(TKN_STRING); - url = g_strdup (tkn_buf->data); + url = g_strdup (tkn_buf->str); add2hotlist (label, url, HL_TYPE_ENTRY, 0); SKIP_TO_EOL; break; case TKN_COMMENT: - label = g_strdup (tkn_buf->data); + label = g_strdup (tkn_buf->str); add2hotlist (label, 0, HL_TYPE_COMMENT, 0); break; case TKN_EOF: @@ -1358,22 +1362,22 @@ hot_load_file (struct hotlist * grp) switch (tkn) { case TKN_GROUP: CHECK_TOKEN(TKN_STRING); - new_grp = add2hotlist (g_strdup (tkn_buf->data), 0, HL_TYPE_GROUP, 0); + new_grp = add2hotlist (g_strdup (tkn_buf->str), 0, HL_TYPE_GROUP, 0); SKIP_TO_EOL; hot_load_group (new_grp); current_group = grp; break; case TKN_ENTRY: CHECK_TOKEN(TKN_STRING); - label = g_strdup (tkn_buf->data); + label = g_strdup (tkn_buf->str); CHECK_TOKEN(TKN_URL); CHECK_TOKEN(TKN_STRING); - url = g_strdup (tkn_buf->data); + url = g_strdup (tkn_buf->str); add2hotlist (label, url, HL_TYPE_ENTRY, 0); SKIP_TO_EOL; break; case TKN_COMMENT: - label = g_strdup (tkn_buf->data); + label = g_strdup (tkn_buf->str); add2hotlist (label, 0, HL_TYPE_COMMENT, 0); break; case TKN_EOL: @@ -1570,7 +1574,7 @@ void done_hotlist (void) g_free (hotlist); hotlist = 0; } - + hotlist_state.loaded = 0; g_free (hotlist_file_name); @@ -1579,7 +1583,7 @@ void done_hotlist (void) current_group = 0; if (tkn_buf){ - str_release_buffer (tkn_buf); + g_string_free (tkn_buf, TRUE); tkn_buf = NULL; } } diff --git a/src/info.c b/src/info.c index e32063ef9..53951fc33 100644 --- a/src/info.c +++ b/src/info.c @@ -63,8 +63,7 @@ info_show_info (struct WInfo *info) { static int i18n_adjust=0; static const char *file_label; - struct str_buffer *buff; - + GString *buff; struct stat st; if (!is_idle ()) @@ -82,23 +81,23 @@ info_show_info (struct WInfo *info) if (!info->ready) return; - + my_statfs (&myfs_stats, current_panel->cwd); st = current_panel->dir.list [current_panel->selected].st; /* Print only lines which fit */ - + if(!i18n_adjust) { /* This printf pattern string is used as a reference for size */ file_label=_("File: %s"); i18n_adjust = str_term_width1(file_label) + 2; } - - buff = str_get_buffer (); - + + buff = g_string_new (""); + switch (info->widget.lines-2){ /* Note: all cases are fall-throughs */ - + default: case 16: @@ -135,24 +134,21 @@ info_show_info (struct WInfo *info) widget_move (&info->widget, 13, 3); str_printf (buff, _("Device: %s"), str_trunc (myfs_stats.device, info->widget.cols - i18n_adjust)); - addstr (str_term_form (buff->data)); - str_reset_buffer (buff); + addstr (str_term_form (buff->str)); + g_string_set_size(buff,0); case 12: widget_move (&info->widget, 12, 3); str_printf (buff, _("Filesystem: %s"), str_trunc (myfs_stats.mpoint, info->widget.cols - i18n_adjust)); - addstr (str_term_form (buff->data)); - str_reset_buffer (buff); + addstr (str_term_form (buff->str)); case 11: widget_move (&info->widget, 11, 3); str_printf (buff, _("Accessed: %s"), file_date (st.st_atime)); - addstr (str_term_form (buff->data)); - str_reset_buffer (buff); + addstr (str_term_form (buff->str)); case 10: widget_move (&info->widget, 10, 3); str_printf (buff, _("Modified: %s"), file_date (st.st_mtime)); - addstr (str_term_form (buff->data)); - str_reset_buffer (buff); + addstr (str_term_form (buff->str)); case 9: widget_move (&info->widget, 9, 3); /* TRANSLATORS: "Status changed", like in the stat(2) man page */ @@ -204,8 +200,7 @@ info_show_info (struct WInfo *info) str_printf (buff, file_label, str_trunc (current_panel->dir.list [current_panel->selected].fname, info->widget.cols - i18n_adjust)); - addstr (str_term_form (buff->data)); - str_reset_buffer (buff); + addstr (str_term_form (buff->str)); } else addstr (_("File: None")); @@ -214,8 +209,7 @@ info_show_info (struct WInfo *info) case 0: ; } /* switch */ - - str_release_buffer (buff); + g_string_free (buff, TRUE); } static void info_hook (void *data) diff --git a/src/strutil.c b/src/strutil.c index 58b9e7fc5..7dc9b6bff 100644 --- a/src/strutil.c +++ b/src/strutil.c @@ -39,7 +39,8 @@ static const char *str_utf8_encodings[] = { "utf-8", "utf8", - NULL}; + NULL +}; // standard 8bit encodings, no wide or multibytes characters static const char *str_8bit_encodings[] = { @@ -63,8 +64,6 @@ static const char *str_8bit_encodings[] = { static char *codeset; // function for encoding specific operations static struct str_class used_class; -// linked list of string buffers -static struct str_buffer *buffer_list = NULL; iconv_t str_cnv_to_term; iconv_t str_cnv_from_term; @@ -77,277 +76,197 @@ str_test_not_convert (const char *enc) return g_ascii_strcasecmp (enc, codeset) == 0; } -str_conv_t +GIConv str_crt_conv_to (const char *to_enc) { - return (!str_test_not_convert (to_enc)) ? iconv_open (to_enc, codeset) : - str_cnv_not_convert; + return (!str_test_not_convert (to_enc)) + ? g_iconv_open (to_enc, codeset) : str_cnv_not_convert; } -str_conv_t +GIConv str_crt_conv_from (const char *from_enc) { - return (!str_test_not_convert (from_enc)) ? iconv_open (codeset, from_enc) : - str_cnv_not_convert; + return (!str_test_not_convert (from_enc)) + ? g_iconv_open (codeset, from_enc) : str_cnv_not_convert; } void -str_close_conv (str_conv_t conv) +str_close_conv (GIConv conv) { if (conv != str_cnv_not_convert) - iconv_close (conv); -} - -struct str_buffer * -str_get_buffer () -{ - struct str_buffer *result; - - result = buffer_list; - - while (result != NULL) { - if (!result->used) { - str_reset_buffer (result); - result->used = 1; - return result; - } - result = result->next; - } - - result = g_new (struct str_buffer, 1); - result->size = BUF_TINY; - result->data = g_new0 (char, result->size); - result->data[0] = '\0'; - result->actual = result->data; - result->remain = result->size; - - result->next = buffer_list; - buffer_list = result; - - result->used = 1; - - return result; -} - - -void -str_release_buffer (struct str_buffer *buffer) -{ - buffer->used = 0; -} - -void -str_incrase_buffer (struct str_buffer *buffer) -{ - size_t offset; - - offset = buffer->actual - buffer->data; - buffer->remain+= buffer->size; - buffer->size*= 2; - buffer->data = g_renew (char, buffer->data, buffer->size); - buffer->actual = buffer->data + offset; -} - -void -str_reset_buffer (struct str_buffer *buffer) -{ - buffer->data[0] = '\0'; - buffer->actual = buffer->data; - buffer->remain = buffer->size; + g_iconv_close (conv); } static int -_str_convert (str_conv_t coder, char *string, struct str_buffer *buffer) +_str_convert (GIConv coder, char *string, int size, GString * buffer) { int state; - - size_t left; - size_t nconv; + gchar *tmp_buff; + gssize left; + gsize bytes_read, bytes_written; + GError *error = NULL; errno = 0; - if (used_class.is_valid_string (string)) { - state = 0; + if (used_class.is_valid_string (string)) + { + state = 0; + if (size < 0) + { + size = strlen (string); + } + else + { + left = strlen (string); + if (left < size) + size = left; + } + left = size; - left = strlen (string); + if (coder == (GIConv) (-1)) + return ESTR_FAILURE; - if (coder == (iconv_t) (-1)) return ESTR_FAILURE; + g_iconv (coder, NULL, NULL, NULL, NULL); - iconv(coder, NULL, NULL, NULL, NULL); + while (left) + { + tmp_buff = g_convert_with_iconv ((const gchar *) string, + left, + coder, + &bytes_read, + &bytes_written, &error); - while (((int)left) > 0) { - nconv = iconv(coder, &string, &left, - &(buffer->actual), &(buffer->remain)); - if (nconv == (size_t) (-1)) { - switch (errno) { - case EINVAL: - return ESTR_FAILURE; - case EILSEQ: - string++; - left--; - if (buffer->remain <= 0) { - str_incrase_buffer (buffer); - } - buffer->actual[0] = '?'; - buffer->actual++; - buffer->remain--; - state = ESTR_PROBLEM; - break; - case E2BIG: - str_incrase_buffer (buffer); - break; - } - } - }; + if (error) + { + switch (error->code) + { + case G_CONVERT_ERROR_NO_CONVERSION: + /* Conversion between the requested character sets is not supported. */ + tmp_buff = g_strnfill (strlen (string), '?'); + g_string_append (buffer, tmp_buff); + g_free (tmp_buff); + g_error_free (error); + return ESTR_PROBLEM; + break; + case G_CONVERT_ERROR_ILLEGAL_SEQUENCE: + /* Invalid byte sequence in conversion input. */ + g_string_append (buffer, tmp_buff); + g_string_append (buffer, "?"); + g_free (tmp_buff); + if (bytes_read < left) + { + string += bytes_read + 1; + size -= (bytes_read + 1); + left -= (bytes_read + 1); + } + else + { + g_error_free (error); + return ESTR_PROBLEM; + } + state = ESTR_PROBLEM; + break; + case G_CONVERT_ERROR_PARTIAL_INPUT: + /* Partial character sequence at end of input. */ + g_error_free (error); + g_string_append (buffer, tmp_buff); + g_free (tmp_buff); + if (bytes_read < left) + { + left = left - bytes_read; + tmp_buff = g_strnfill (left, '?'); + g_string_append (buffer, tmp_buff); + g_free (tmp_buff); + } + return ESTR_PROBLEM; + break; + case G_CONVERT_ERROR_BAD_URI: /* Don't know how handle this error :( */ + case G_CONVERT_ERROR_NOT_ABSOLUTE_PATH: /* Don't know how handle this error :( */ + case G_CONVERT_ERROR_FAILED: /* Conversion failed for some reason. */ + default: + g_error_free (error); + if (tmp_buff) + g_free (tmp_buff); - return state; - } else return ESTR_FAILURE; + return ESTR_FAILURE; + } + g_error_free (error); + } + else + { + g_string_append (buffer, tmp_buff); + g_free (tmp_buff); + string += bytes_read; + left -= bytes_read; + } + } + return state; + } + else + return ESTR_FAILURE; } int -str_convert (str_conv_t coder, char *string, struct str_buffer *buffer) -{ - int result; - - result = _str_convert (coder, string, buffer); - buffer->actual[0] = '\0'; - - return result; -} - -static int -_str_vfs_convert_from (str_conv_t coder, char *string, - struct str_buffer *buffer) -{ - size_t left; - size_t nconv; - - left = strlen (string); - - if (coder == (iconv_t) (-1)) return ESTR_FAILURE; - - iconv(coder, NULL, NULL, NULL, NULL); - - do { - nconv = iconv(coder, &string, &left, - &(buffer->actual), &(buffer->remain)); - if (nconv == (size_t) (-1)) { - switch (errno) { - case EINVAL: - return ESTR_FAILURE; - case EILSEQ: - return ESTR_FAILURE; - case E2BIG: - str_incrase_buffer (buffer); - break; - } - } - } while (left > 0); - - return 0; -} - -int -str_vfs_convert_from (str_conv_t coder, char *string, struct str_buffer *buffer) +str_convert (GIConv coder, char *string, GString * buffer) { int result; - if (coder == str_cnv_not_convert) { - str_insert_string (string, buffer); - result = 0; - } else result = _str_vfs_convert_from (coder, string, buffer); - buffer->actual[0] = '\0'; + result = _str_convert (coder, string, -1, buffer); return result; } int -str_vfs_convert_to (str_conv_t coder, const char *string, - int size, struct str_buffer *buffer) +str_nconvert (GIConv coder, char *string, int size, GString * buffer) +{ + int result; + + result = _str_convert (coder, string, size, buffer); + + return result; +} + +int +str_vfs_convert_from (GIConv coder, char *string, GString * buffer) +{ + int result; + + if (coder == str_cnv_not_convert) + { + g_string_append (buffer, string); + result = 0; + } + else + result = _str_convert (coder, string, -1, buffer); + + return result; +} + +int +str_vfs_convert_to (GIConv coder, const char *string, int size, + GString * buffer) { return used_class.vfs_convert_to (coder, string, size, buffer); } void -str_insert_string (const char *string, struct str_buffer *buffer) +str_printf (GString * buffer, const char *format, ...) { - size_t s; - - s = strlen (string); - while (buffer->remain < s) str_incrase_buffer (buffer); - - memcpy (buffer->actual, string, s); - buffer->actual+= s; - buffer->remain-= s; - buffer->actual[0] = '\0'; -} - -void -str_insert_string2 (const char *string, int size, struct str_buffer *buffer) -{ - size_t s; - - s = (size >= 0) ? size : strlen (string); - while (buffer->remain < s) str_incrase_buffer (buffer); - - memcpy (buffer->actual, string, s); - buffer->actual+= s; - buffer->remain-= s; - buffer->actual[0] = '\0'; -} - -void -str_printf (struct str_buffer *buffer, const char *format, ...) -{ - int size; va_list ap; - va_start (ap, format); - size = vsnprintf (buffer->actual, buffer->remain, format, ap); - while (buffer->remain <= size) { - str_incrase_buffer (buffer); - size = vsnprintf (buffer->actual, buffer->remain, format, ap); - } - buffer->actual+= size; - buffer->remain-= size; + g_string_append_vprintf (buffer, format, ap); va_end (ap); } void -str_insert_char (char ch, struct str_buffer *buffer) -{ - if (buffer->remain <= 1) str_incrase_buffer (buffer); - - buffer->actual[0] = ch; - buffer->actual++; - buffer->remain--; - buffer->actual[0] = '\0'; -} - -void -str_insert_replace_char (struct str_buffer *buffer) +str_insert_replace_char (GString * buffer) { used_class.insert_replace_char (buffer); } -void -str_backward_buffer (struct str_buffer *buffer, int count) -{ - char *prev; - - while ((count > 0) && (buffer->actual > buffer->data)) { - prev = str_get_prev_char (buffer->actual); - buffer->remain+= buffer->actual - prev; - buffer->actual = prev; - buffer->actual[0] = '\0'; - count--; - } -} - - int -str_translate_char (str_conv_t conv, char *keys, size_t ch_size, - char *output, size_t out_size) +str_translate_char (str_conv_t conv, char *keys, size_t ch_size, + char *output, size_t out_size) { size_t left; size_t cnv; @@ -356,7 +275,7 @@ str_translate_char (str_conv_t conv, char *keys, size_t ch_size, left = (ch_size == (size_t)(-1)) ? strlen (keys) : ch_size; - cnv = iconv (conv, &keys, &left, &output, &out_size); + cnv = g_iconv (conv, &keys, &left, &output, &out_size); if (cnv == (size_t)(-1)) { if (errno == EINVAL) return ESTR_PROBLEM; else return ESTR_FAILURE; } else { @@ -382,7 +301,6 @@ str_test_encoding_class (const char *encoding, const char **table) result+= (g_ascii_strncasecmp (encoding, table[t], strlen (table[t])) == 0); } - return result; } @@ -416,18 +334,21 @@ str_init_strings (const char *termenc) : str_detect_termencoding ()); str_cnv_not_convert = iconv_open (codeset, codeset); - if (str_cnv_not_convert == INVALID_CONV) { - if (termenc != NULL) { - g_free (codeset); - codeset = g_strdup (str_detect_termencoding ()); - str_cnv_not_convert = iconv_open (codeset, codeset); - } + if (str_cnv_not_convert == INVALID_CONV) + { + if (termenc != NULL) + { + g_free (codeset); + codeset = g_strdup (str_detect_termencoding ()); + str_cnv_not_convert = iconv_open (codeset, codeset); + } - if (str_cnv_not_convert == INVALID_CONV) { - g_free (codeset); - codeset = g_strdup ("ascii"); - str_cnv_not_convert = iconv_open (codeset, codeset); - } + if (str_cnv_not_convert == INVALID_CONV) + { + g_free (codeset); + codeset = g_strdup ("ascii"); + str_cnv_not_convert = iconv_open (codeset, codeset); + } } str_cnv_to_term = str_cnv_not_convert; @@ -436,26 +357,9 @@ str_init_strings (const char *termenc) str_choose_str_functions (); } -static void -str_release_buffer_list () -{ - struct str_buffer *buffer; - struct str_buffer *next; - - buffer = buffer_list; - while (buffer != NULL) { - next = buffer->next; - g_free (buffer->data); - g_free (buffer); - buffer = next; - } -} - void str_uninit_strings () { - str_release_buffer_list (); - iconv_close (str_cnv_not_convert); } @@ -477,7 +381,7 @@ str_term_trim (const char *text, int width) return used_class.term_trim (text, width); } -void +void str_msg_term_size (const char *text, int *lines, int *columns) { return used_class.msg_term_size (text, lines, columns); @@ -493,14 +397,14 @@ char * str_get_next_char (char *text) { - used_class.cnext_char ((const char **)&text); + used_class.cnext_char ((const char **) &text); return text; } const char * str_cget_next_char (const char *text) { - used_class.cnext_char (&text); + used_class.cnext_char(&text); return text; } @@ -822,4 +726,3 @@ str_release_key (char *key, int case_sen) { used_class.release_key (key, case_sen); } - diff --git a/src/strutil.h b/src/strutil.h index 7767dfa26..fadb6b5d5 100644 --- a/src/strutil.h +++ b/src/strutil.h @@ -74,29 +74,11 @@ extern str_conv_t str_cnv_from_term; // from terminal encoding to terminal encoding extern str_conv_t str_cnv_not_convert; -/* structure for growing strings - * try to avoid set any members manually - */ -struct str_buffer { - // all buffers are stored in linked list - struct str_buffer *next; - // if is buffer in use or not - int used; - // whole string - char *data; - // size of string - size_t size; - // end of string, actual[0] is always '\0' - char *actual; - // how many (chars)bytes remain after actual - size_t remain; -}; - // all functions in str_class must be defined for every encoding struct str_class { int (*vfs_convert_to) (str_conv_t coder, const char *string, - int size, struct str_buffer *buffer); //I - void (*insert_replace_char) (struct str_buffer *buffer); + int size, GString *buffer); //I + void (*insert_replace_char) (GString *buffer); int (*is_valid_string) (const char *); //I int (*is_valid_char) (const char *, size_t); //I void (*cnext_char) (const char **); @@ -151,85 +133,52 @@ struct str_class str_ascii_init (); /* create convertor from "from_enc" to terminal encoding * if "from_enc" is not supported return INVALID_CONV */ -str_conv_t str_crt_conv_from (const char *from_enc); +GIConv str_crt_conv_from (const char *); /* create convertor from terminal encoding to "to_enc" * if "to_enc" is not supported return INVALID_CONV */ -str_conv_t str_crt_conv_to (const char *to_enc); +GIConv str_crt_conv_to (const char *); /* close convertor, do not close str_cnv_to_term, str_cnv_from_term, * str_cnv_not_convert */ -void str_close_conv (str_conv_t conv); +void str_close_conv (GIConv); /* return on of not used buffers (.used == 0) or create new * returned buffer has set .used to 1 */ -struct str_buffer *str_get_buffer (); - -/* clear buffer, in .data is empty string, .actual = .data, .remain = .size - * do not set .used - */ -void str_reset_buffer (struct str_buffer *buffer); - -/* set .used of buffer to 0, so can be returned by str_get_buffer again - * data in buffer may stay valid after function return - */ -void str_release_buffer (struct str_buffer *buffer); - -/* incrase capacity of buffer - */ -void str_incrase_buffer (struct str_buffer *buffer); /* convert string using coder, result of conversion is appended at end of buffer * return 0 if there was no problem. * otherwise return ESTR_PROBLEM or ESTR_FAILURE */ -int str_convert (str_conv_t coder, char *string, - struct str_buffer *buffer); +int str_convert (GIConv, char *, GString *); + +int str_nconvert (GIConv, char *, int, GString *); /* return only 0 or ESTR_FAILURE, because vfs must be able to convert result to * original string. (so no replace with questionmark) * if coder is str_cnv_from_term or str_cnv_not_convert, string is only copied, * so is possible to show file, that is not valid in terminal encoding */ -int str_vfs_convert_from (str_conv_t coder, char *string, - struct str_buffer *buffer); +int str_vfs_convert_from (GIConv, char *, GString *); /* if coder is str_cnv_to_term or str_cnv_not_convert, string is only copied, * does replace with questionmark * I */ -int str_vfs_convert_to (str_conv_t coder, const char *string, - int size, struct str_buffer *buffer); - -/* append string at the end of buffer - */ -void str_insert_string (const char *string, struct str_buffer *buffer); - -/* append string at the end of buffer, limit to size - */ -void -str_insert_string2 (const char *string, int size, struct str_buffer *buffer); +int str_vfs_convert_to (GIConv, const char *, int, GString *); /* printf functin for str_buffer, append result of printf at the end of buffer - */ + */ void -str_printf (struct str_buffer *buffer, const char *format, ...); - -/* append char at the end of buffer - */ -void str_insert_char (char ch, struct str_buffer *buffer); +str_printf (GString *, const char *, ...); /* add standard replacement character in terminal encoding */ -void str_insert_replace_char (struct str_buffer *buffer); +void str_insert_replace_char (GString *); -/* rewind "count" characters buffer back - */ -void str_backward_buffer (struct str_buffer *buffer, int count); - /* init strings and set terminal encoding, * if is termenc NULL, detect terminal encoding * create all str_cnv_* and set functions for terminal encoding diff --git a/src/strutil8bit.c b/src/strutil8bit.c index fd3d4b327..75df77b3a 100644 --- a/src/strutil8bit.c +++ b/src/strutil8bit.c @@ -1,6 +1,6 @@ /* 8bit strings utilities Copyright (C) 2007 Free Software Foundation, Inc. - + Written 2007 by: Rostislav Benes @@ -11,7 +11,7 @@ it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. - + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the @@ -40,9 +40,9 @@ static const char replch = '?'; static void -str_8bit_insert_replace_char (struct str_buffer *buffer) +str_8bit_insert_replace_char (GString * buffer) { - str_insert_char (replch, buffer); + g_string_append_c (buffer, replch); } static int @@ -155,58 +155,20 @@ str_8bit_length2 (const char *text, int size) return (size >= 0) ? min (strlen (text), size) : strlen (text); } -static int -_str_8bit_vfs_convert_to (str_conv_t coder, char *string, - int size, struct str_buffer *buffer) -{ - int state; - size_t left; - size_t nconv; - - errno = 0; - - state = 0; - - left = (size >= 0) ? size : strlen (string); - - if (coder == (iconv_t) (-1)) return ESTR_FAILURE; - - iconv(coder, NULL, NULL, NULL, NULL); - - while (((int)left) > 0) { - nconv = iconv(coder, &string, &left, - &(buffer->actual), &(buffer->remain)); - if (nconv == (size_t) (-1)) { - switch (errno) { - case EINVAL: - return ESTR_FAILURE; - case EILSEQ: - string++; - left--; - str_insert_char ('?', buffer); - state = ESTR_PROBLEM; - break; - case E2BIG: - str_incrase_buffer (buffer); - break; - } - } - } - return state; -} - int -str_8bit_vfs_convert_to (str_conv_t coder, const char *string, - int size, struct str_buffer *buffer) +str_8bit_vfs_convert_to (str_conv_t coder, const char *string, + int size, GString * buffer) { - int result; - - if (coder == str_cnv_not_convert) { - str_insert_string2 (string, size, buffer); - result = 0; - } else result = _str_8bit_vfs_convert_to (coder, (char*)string, size, buffer); - buffer->actual[0] = '\0'; - + int result; + + if (coder == str_cnv_not_convert) + { + g_string_append_len (buffer, string, size); + result = 0; + } + else + result = str_nconvert (coder, (char *) string, size, buffer); + return result; } @@ -484,7 +446,7 @@ static int str_8bit_column_to_pos (const char *text, size_t pos) { return (int)pos; -} +} static char * str_8bit_create_search_needle (const char *needle, int case_sen) @@ -518,7 +480,7 @@ str_8bit_search_first (const char *text, const char *search, int case_sen) g_free (fold_text); g_free (fold_search); } - + return match; } diff --git a/src/strutilascii.c b/src/strutilascii.c index 5c70b75e4..e7d144e4e 100644 --- a/src/strutilascii.c +++ b/src/strutilascii.c @@ -34,13 +34,13 @@ /* using g_ascii function from glib * on terminal are showed only ascii characters (lower then 0x80) */ - + static const char replch = '?'; static void -str_ascii_insert_replace_char (struct str_buffer *buffer) +str_ascii_insert_replace_char (GString * buffer) { - str_insert_char (replch, buffer); + g_string_append_c (buffer, replch); } static int @@ -154,10 +154,10 @@ str_ascii_length2 (const char *text, int size) } int -str_ascii_vfs_convert_to (str_conv_t coder, const char *string, - int size, struct str_buffer *buffer) +str_ascii_vfs_convert_to (GIConv coder, const char *string, + int size, GString * buffer) { - str_insert_string2 (string, size, buffer); + g_string_append_len (buffer, string, size); return 0; } @@ -468,7 +468,7 @@ static int str_ascii_column_to_pos (const char *text, size_t pos) { return (int)pos; -} +} static char * str_ascii_create_search_needle (const char *needle, int case_sen) diff --git a/src/strutilutf8.c b/src/strutilutf8.c index dca01ab92..67709815b 100644 --- a/src/strutilutf8.c +++ b/src/strutilutf8.c @@ -47,9 +47,9 @@ str_unichar_iscombiningmark (gunichar uni) { } static void -str_utf8_insert_replace_char (struct str_buffer *buffer) +str_utf8_insert_replace_char (GString * buffer) { - str_insert_string (replch, buffer); + g_string_append (buffer, replch); } static int @@ -292,77 +292,92 @@ str_utf8_length_noncomb (const char *text) } static void -str_utf8_questmark_sustb (char **string, size_t *left, struct str_buffer *buffer) +str_utf8_questmark_sustb (char **string, size_t * left, GString * buffer) { char *next = g_utf8_next_char (*string); (*left)-= next - (*string); (*string) = next; - str_insert_char ('?', buffer); + g_string_append_c (buffer, '?'); } +/* static int -_str_utf8_vfs_convert_to (str_conv_t coder, const char *string, - int size, struct str_buffer *buffer) +_str_utf8_vfs_convert_to (str_conv_t coder, const char *string, + int size, GString * buffer) { - int state = 0; + int state = 0; size_t left; size_t nconv; char *composed, *c; const char *start, *end; - + errno = 0; - + size = (size >= 0) ? size : strlen (string); - if (coder == (iconv_t) (-1)) return ESTR_FAILURE; - iconv(coder, NULL, NULL, NULL, NULL); - + if (coder == (iconv_t) (-1)) + return ESTR_FAILURE; + iconv (coder, NULL, NULL, NULL, NULL); + start = string; - while (size > 0) { - end = strchr (start, PATH_SEP); - end = (end == NULL || end >= start + size) ? start + size : end + 1; - if (g_utf8_validate (start, end - start, NULL)) { - c = composed = g_utf8_normalize (start, end - start, G_NORMALIZE_DEFAULT_COMPOSE); - left = strlen (composed); - while (((int)left) > 0) { - nconv = iconv(coder, &c, &left, &(buffer->actual), &(buffer->remain)); - if (nconv == (size_t) (-1)) { - switch (errno) { - case EINVAL: - g_free (composed); - return ESTR_FAILURE; - case EILSEQ: - str_utf8_questmark_sustb (&c, &left, buffer); - state = ESTR_PROBLEM; - break; - case E2BIG: - str_incrase_buffer (buffer); - break; - } - } - } - g_free (composed); - } else { - str_insert_string2 (start, end - start, buffer); - } - size-= end - start; - start = end; + while (size > 0) + { + end = strchr (start, PATH_SEP); + end = (end == NULL || end >= start + size) ? start + size : end + 1; + if (g_utf8_validate (start, end - start, NULL)) + { + c = composed = + g_utf8_normalize (start, end - start, + G_NORMALIZE_DEFAULT_COMPOSE); + left = strlen (composed); + while (((int) left) > 0) + { + nconv = + iconv (coder, &c, &left, &(buffer->actual), + &(buffer->remain)); + if (nconv == (size_t) (-1)) + { + switch (errno) + { + case EINVAL: + g_free (composed); + return ESTR_FAILURE; + case EILSEQ: + str_utf8_questmark_sustb (&c, &left, buffer); + state = ESTR_PROBLEM; + break; + case E2BIG: + str_incrase_buffer (buffer); + break; + } + } + } + g_free (composed); + } + else + { + g_string_append_len (buffer, start, end - start); + } + size -= end - start; + start = end; } return state; } - +*/ static int -str_utf8_vfs_convert_to (str_conv_t coder, const char *string, - int size, struct str_buffer *buffer) +str_utf8_vfs_convert_to (str_conv_t coder, const char *string, + int size, GString * buffer) { - int result; - - if (coder == str_cnv_not_convert) { - str_insert_string2 (string, size, buffer); - result = 0; - } else result = _str_utf8_vfs_convert_to (coder, string, size, buffer); - buffer->actual[0] = '\0'; - -return result; + int result; + + if (coder == str_cnv_not_convert) + { + g_string_append_len (buffer, string, size); + result = 0; + } + else + result = str_nconvert (coder, (char *) string, size, buffer); + + return result; } struct term_form { @@ -423,8 +438,7 @@ str_utf8_make_make_term_form (const char *text, size_t length) actual+= strlen (replch); result.width++; } - if (length != (size_t) (-1)) length--; - } + if (length != (size_t) (-1)) length--; } actual[0] = '\0'; return &result; @@ -773,15 +787,16 @@ static int str_utf8_offset_to_pos (const char *text, size_t length) { if (str_utf8_is_valid_string (text)) - return g_utf8_offset_to_pointer (text, length) - text; - else { - int result; - struct str_buffer *buffer = str_get_buffer (); - str_insert_string (text, buffer); - str_utf8_fix_string (buffer->data); - result = g_utf8_offset_to_pointer (buffer->data, length) - buffer->data; - str_release_buffer (buffer); - return result; + return g_utf8_offset_to_pointer (text, length) - text; + else + { + int result; + GString *buffer = g_string_new (text); + + str_utf8_fix_string (buffer->str); + result = g_utf8_offset_to_pointer (buffer->str, length) - buffer->str; + g_string_free (buffer, TRUE); + return result; } } @@ -916,78 +931,88 @@ str_utf8_search_last (const char *text, const char *search, int case_sen) static char * str_utf8_normalize (const char *text) { - struct str_buffer *fixed = str_get_buffer (); + GString *fixed = g_string_new (""); char *tmp; char *result; const char *start; const char *end; start = text; - while (!g_utf8_validate (start, -1, &end) && start[0] != '\0') { - if (start != end) { - tmp = g_utf8_normalize (start, end - start, G_NORMALIZE_ALL); - str_insert_string (tmp, fixed); - g_free (tmp); - } - str_insert_char (end[0], fixed); - start = end + 1; + while (!g_utf8_validate (start, -1, &end) && start[0] != '\0') + { + if (start != end) + { + tmp = g_utf8_normalize (start, end - start, G_NORMALIZE_ALL); + g_string_append (fixed, tmp); + g_free (tmp); + } + g_string_append_c (fixed, end[0]); + start = end + 1; } - - if (start == text) { - result = g_utf8_normalize (text, -1, G_NORMALIZE_ALL); - } else { - if (start[0] != '\0' && start != end) { - tmp = g_utf8_normalize (start, end - start, G_NORMALIZE_ALL); - str_insert_string (tmp, fixed); - g_free (tmp); - } - result = g_strdup (fixed->data); + + if (start == text) + { + result = g_utf8_normalize (text, -1, G_NORMALIZE_ALL); } - - str_release_buffer (fixed); - + else + { + if (start[0] != '\0' && start != end) + { + tmp = g_utf8_normalize (start, end - start, G_NORMALIZE_ALL); + g_string_append (fixed, tmp); + g_free (tmp); + } + result = g_strdup (fixed->str); + } + g_string_free (fixed, TRUE); + return result; } - + static char * str_utf8_casefold_normalize (const char *text) { - struct str_buffer *fixed = str_get_buffer (); + GString *fixed = g_string_new (""); char *tmp, *fold; char *result; const char *start; const char *end; - + start = text; - while (!g_utf8_validate (start, -1, &end) && start[0] != '\0') { - if (start != end) { - fold = g_utf8_casefold (start, end - start); - tmp = g_utf8_normalize (fold, -1, G_NORMALIZE_ALL); - str_insert_string (tmp, fixed); - g_free (tmp); - g_free (fold); - } - str_insert_char (end[0], fixed); - start = end + 1; + while (!g_utf8_validate (start, -1, &end) && start[0] != '\0') + { + if (start != end) + { + fold = g_utf8_casefold (start, end - start); + tmp = g_utf8_normalize (fold, -1, G_NORMALIZE_ALL); + g_string_append (fixed, tmp); + g_free (tmp); + g_free (fold); + } + g_string_append_c (fixed, end[0]); + start = end + 1; } - - if (start == text) { - fold = g_utf8_casefold (text, -1); - result = g_utf8_normalize (fold, -1, G_NORMALIZE_ALL); - g_free (fold); - } else { - if (start[0] != '\0' && start != end) { - fold = g_utf8_casefold (start, end - start); - tmp = g_utf8_normalize (fold, -1, G_NORMALIZE_ALL); - str_insert_string (tmp, fixed); - g_free (tmp); - g_free (fold); - } - result = g_strdup (fixed->data); + + if (start == text) + { + fold = g_utf8_casefold (text, -1); + result = g_utf8_normalize (fold, -1, G_NORMALIZE_ALL); + g_free (fold); } - - str_release_buffer (fixed); - + else + { + if (start[0] != '\0' && start != end) + { + fold = g_utf8_casefold (start, end - start); + tmp = g_utf8_normalize (fold, -1, G_NORMALIZE_ALL); + g_string_append (fixed, tmp); + g_free (tmp); + g_free (fold); + } + result = g_strdup (fixed->str); + } + g_string_free (fixed, TRUE); + return result; } @@ -1126,36 +1151,42 @@ str_utf8_create_key_gen (const char *text, int case_sen, } else { const char *start, *end; char *fold, *key; - struct str_buffer *fixed = str_get_buffer (); - + GString *fixed = g_string_new (""); + start = text; - while (!g_utf8_validate (start, -1, &end) && start[0] != '\0') { - if (start != end) { + while (!g_utf8_validate (start, -1, &end) && start[0] != '\0') + { + if (start != end) + { fold = g_utf8_casefold (start, end - start); key = keygen (fold, -1); - str_insert_string (key, fixed); + g_string_append (fixed, key); g_free (key); g_free (fold); } - str_insert_char (end[0], fixed); + g_string_append_c (fixed, end[0]); start = end + 1; } - - if (start == text) { + + if (start == text) + { fold = g_utf8_casefold (text, -1); result = keygen (fold, -1); g_free (fold); - } else { - if (start[0] != '\0' && start != end) { + } + else + { + if (start[0] != '\0' && start != end) + { fold = g_utf8_casefold (start, end - start); key = keygen (fold, -1); - str_insert_string (key, fixed); + g_string_append (fixed, key); g_free (key); g_free (fold); } - result = g_strdup (fixed->data); + result = g_strdup (fixed->str); } - str_release_buffer (fixed); + g_string_free (fixed, TRUE); } return result; } @@ -1184,8 +1215,8 @@ str_utf8_release_key (char *key, int case_sen) g_free (key); } -struct str_class -str_utf8_init () +struct str_class +str_utf8_init () { struct str_class result; diff --git a/src/view.c b/src/view.c index 16f156d8f..3777d20e1 100644 --- a/src/view.c +++ b/src/view.c @@ -235,7 +235,7 @@ struct WView { * used for both normal adn nroff mode */ struct cache_line *first_showed_line; /* converter for translation of text */ - str_conv_t converter; + GIConv converter; }; @@ -2458,24 +2458,24 @@ view_display_text (WView * view) addch ('.'); } } else { - struct str_buffer *comb = str_get_buffer (); + GString *comb = g_string_new (""); if (str_isprint (info.cact)) { - str_insert_string (info.cact, comb); + g_string_append(comb,info.cact); } else { - str_insert_string (".", comb); + g_string_append(comb,"."); } while (str_iscombiningmark (info.cnxt)) { view_read_continue (view, &info); - str_insert_string (info.cact, comb); + g_string_append(comb,info.cact); } - addstr (str_term_form (comb->data)); - str_release_buffer (comb); + addstr (str_term_form (comb->str)); + g_string_free (comb, TRUE); } } else { while (str_iscombiningmark (info.cnxt)) { view_read_continue (view, &info); } - } + } col+= w; tty_setcolor (NORMAL_COLOR); @@ -2731,7 +2731,7 @@ icase_search_p (WView *view, char *text, char *data, int nothing, /* read one whole line into buffer, return where line start and end */ static int -view_get_line_at (WView *view, offset_type from, struct str_buffer * buffer, +view_get_line_at (WView *view, offset_type from, GString * buffer, offset_type *buff_start, offset_type *buff_end) { #define cmp(t1,t2) (strcmp((t1),(t2)) == 0) @@ -2756,7 +2756,7 @@ view_get_line_at (WView *view, offset_type from, struct str_buffer * buffer, (*buff_start) = start; (*buff_end) = end; - str_reset_buffer (buffer); + g_string_set_size(buffer,0); view_read_start (view, &info, start); while ((info.result != -1) && (info.next < end)) { @@ -2766,29 +2766,29 @@ view_get_line_at (WView *view, offset_type from, struct str_buffer * buffer, if (cmp (info.cact, "")) { if (info.actual < from) { /* '\0' before start offset, continue */ - str_reset_buffer (buffer); + g_string_set_size(buffer,0); (*buff_start) = info.next; continue; } else { /* '\0' after start offset, end */ (*buff_end) = info.next; return 1; - } - } + } + } if (view_read_test_new_line (view, &info)) continue; if (view_read_test_nroff_back (view, &info)) { - str_backward_buffer (buffer, 1); + g_string_truncate (buffer, buffer->len-1); continue; - } - - str_insert_string (info.cact, buffer); } + g_string_append(buffer,info.cact); + } + return 1; -} +} /* map search result positions to offsets in text */ void @@ -2803,7 +2803,7 @@ view_matchs_to_offsets (WView *view, offset_type start, offset_type end, (*search_end) = INVALID_OFFSET; view_read_start (view, &info, start); - + while ((info.result != -1) && (info.next < end)) { view_read_continue (view, &info); @@ -2981,7 +2981,7 @@ static void view_search (WView *view, char *text, int (*search) (WView *, char *, char *, int, size_t *, size_t *)) { - struct str_buffer *buffer; + GString *buffer; offset_type search_start; int search_status; Dlg_head *d = 0; @@ -2996,7 +2996,7 @@ view_search (WView *view, char *text, mc_refresh (); } - buffer = str_get_buffer (); + buffer = g_string_new (""); search_start = (view->direction != 1) ? view->search_start : view->search_end; @@ -3022,7 +3022,7 @@ view_search (WView *view, char *text, if (!view_get_line_at (view, search_start, buffer, &line_start, &line_end)) break; - search_status = (*search) (view, text, buffer->data, match_normal, + search_status = (*search) (view, text, buffer->str, match_normal, &match_start, &match_end); if (search_status < 0) { @@ -3058,7 +3058,7 @@ view_search (WView *view, char *text, message (D_NORMAL, _("Search"), _(" Search string not found ")); view->search_end = view->search_start; } - str_release_buffer (buffer); + g_string_free (buffer, TRUE); } /* Search buffer (its size is len) in the complete buffer diff --git a/vfs/vfs.c b/vfs/vfs.c index 325160a26..37a7f5d08 100644 --- a/vfs/vfs.c +++ b/vfs/vfs.c @@ -75,7 +75,7 @@ static GSList *vfs_openfiles; #define VFS_FIRST_HANDLE 100 static struct vfs_class *localfs_class; -static struct str_buffer *vfs_str_buffer; +static GString *vfs_str_buffer; static const char *supported_encodings[] = { "UTF8", @@ -382,8 +382,8 @@ vfs_supported_enconding (const char *encoding) { * buffer - used to store result of translation */ static int -_vfs_translate_path (const char *path, int size, - str_conv_t defcnv, struct str_buffer *buffer) +_vfs_translate_path (const char *path, int size, + str_conv_t defcnv, GString *buffer) { const char *semi; const char *ps; @@ -392,37 +392,37 @@ _vfs_translate_path (const char *path, int size, static char encoding[16]; str_conv_t coder; int ms; - + if (size == 0) return 0; size = (size > 0) ? size : strlen (path); - + /* try found #end: */ semi = g_strrstr_len (path, size, "#enc:"); if (semi != NULL) { /* first must be translated part before #enc: */ ms = semi - path; - + /* remove '/' before #enc */ ps = str_cget_prev_char (semi); if (ps[0] == PATH_SEP) ms = ps - path; - + state = _vfs_translate_path (path, ms, defcnv, buffer); - + if (state != 0) return state; /* now can be translated part after #enc: */ - + semi+= 5; slash = strchr (semi, PATH_SEP); // ignore slashes after size; if (slash - path >= size) slash = NULL; - + ms = (slash != NULL) ? slash - semi : strlen (semi); ms = min (ms, sizeof (encoding) - 1); // limit encoding size (ms) to path size (size) if (semi + ms > path + size) ms = path + size - semi; memcpy (encoding, semi, ms); encoding[ms] = '\0'; - + switch (vfs_supported_enconding (encoding)) { case 1: coder = str_crt_conv_to (encoding); @@ -430,9 +430,9 @@ _vfs_translate_path (const char *path, int size, if (slash != NULL) { state = str_vfs_convert_to (coder, slash, path + size - slash, buffer); - } else if (buffer->data[0] == '\0') { + } else if (buffer->str[0] == '\0') { /* exmaple "/#enc:utf-8" */ - str_insert_char (PATH_SEP, buffer); + g_string_append_c(buffer, PATH_SEP); } str_close_conv (coder); return state; @@ -459,12 +459,12 @@ char * vfs_translate_path (const char *path) { int state; - - str_reset_buffer (vfs_str_buffer); + + g_string_set_size(vfs_str_buffer,0); state = _vfs_translate_path (path, -1, str_cnv_from_term, vfs_str_buffer); // strict version //return (state == 0) ? vfs_str_buffer->data : NULL; - return (state != ESTR_FAILURE) ? vfs_str_buffer->data : NULL; + return (state != ESTR_FAILURE) ? vfs_str_buffer->str : NULL; } char * @@ -753,12 +753,12 @@ mc_readdir (DIR *dirp) do { entry = (*vfs->readdir) (dirinfo->info); if (entry == NULL) return NULL; - str_reset_buffer (vfs_str_buffer); - state = str_vfs_convert_from (dirinfo->converter, + g_string_set_size(vfs_str_buffer,0); + state = str_vfs_convert_from (dirinfo->converter, entry->d_name, vfs_str_buffer); } while (state != 0); memcpy (&result, entry, sizeof (struct dirent)); - g_strlcpy (result.d_name, vfs_str_buffer->data, NAME_MAX + 1); + g_strlcpy (result.d_name, vfs_str_buffer->str, NAME_MAX + 1); result.d_reclen = strlen (result.d_name); } if (entry == NULL) errno = vfs->readdir ? ferrno (vfs) : E_NOTSUPP; @@ -854,10 +854,10 @@ _vfs_get_cwd (void) if (encoding == NULL) { tmp = g_get_current_dir (); if (tmp != NULL) { /* One of the directories in the path is not readable */ - str_reset_buffer (vfs_str_buffer); + g_string_set_size(vfs_str_buffer,0); state = str_vfs_convert_from (str_cnv_from_term, tmp, vfs_str_buffer); g_free (tmp); - sys_cwd = (state == 0) ? g_strdup (vfs_str_buffer->data) : NULL; + sys_cwd = (state == 0) ? g_strdup (vfs_str_buffer->str) : NULL; if (!sys_cwd) return current_dir; @@ -1174,7 +1174,7 @@ mc_ungetlocalcopy (const char *pathname, const char *local, int has_changed) void vfs_init (void) { - vfs_str_buffer = str_get_buffer (); + vfs_str_buffer = g_string_new(""); /* localfs needs to be the first one */ init_localfs(); /* fallback value for vfs_get_class() */ @@ -1218,8 +1218,8 @@ vfs_shut (void) (*vfs->done) (vfs); g_slist_free (vfs_openfiles); - - str_release_buffer (vfs_str_buffer); + + g_string_free (vfs_str_buffer, TRUE); } /* From 8e6a6e48b69825225c0ae1d56b4bb1e794cbe2f9 Mon Sep 17 00:00:00 2001 From: Slava Zanko Date: Tue, 14 Apr 2009 15:12:36 +0300 Subject: [PATCH 02/12] edit/{editcmd.c,edit.h,edit.c}: remove catstrs() and freestrs() functions These functions fully replaced with g_strconcat () WARNING! This just stupid replace. Some parts of code need to usage concat_dir_and_file() function instread of g_strconcat () --- edit/edit.c | 3 - edit/edit.h | 1 - edit/editcmd.c | 270 +++++++++++++++++++++++----------------------- src/strutil.c | 18 ++-- src/strutil.h | 11 +- src/strutil8bit.c | 2 +- src/strutilutf8.c | 65 +---------- src/view.c | 2 +- 8 files changed, 155 insertions(+), 217 deletions(-) diff --git a/edit/edit.c b/edit/edit.c index cb8e79935..ddc5cb819 100644 --- a/edit/edit.c +++ b/edit/edit.c @@ -717,9 +717,6 @@ edit_clean (WEdit *edit) edit_purge_widget (edit); - /* Free temporary strings used in catstrs() */ - freestrs (); - return 1; } diff --git a/edit/edit.h b/edit/edit.h index 783bcd533..845b26ae0 100644 --- a/edit/edit.h +++ b/edit/edit.h @@ -186,7 +186,6 @@ int edit_insert_file (WEdit * edit, const char *filename); int edit_load_back_cmd (WEdit * edit); int edit_load_forward_cmd (WEdit * edit); void edit_block_process_cmd (WEdit * edit, const char *shell_cmd, int block); -void freestrs (void); void edit_refresh_cmd (WEdit * edit); void edit_date_cmd (WEdit * edit); void edit_goto_cmd (WEdit * edit); diff --git a/edit/editcmd.c b/edit/editcmd.c index 089cdf81b..d93a9fe34 100644 --- a/edit/editcmd.c +++ b/edit/editcmd.c @@ -118,58 +118,6 @@ MY_itoa (int i) return ++s; } -/* Temporary strings */ -static char *stacked[16]; - -/* - This joins strings end on end and allocates memory for the result. - The result is later automatically free'd and must not be free'd - by the caller. - */ -static const char * -catstrs (const char *first,...) -{ - static int i = 0; - va_list ap; - int len; - char *data; - - if (!first) - return 0; - - len = str_term_width1 (first); - va_start (ap, first); - - while ((data = va_arg (ap, char *)) != 0) - len += str_term_width1 (data); - - len++; - - i = (i + 1) % 16; - g_free (stacked[i]); - - stacked[i] = g_malloc (len); - va_end (ap); - va_start (ap, first); - strcpy (stacked[i], first); - while ((data = va_arg (ap, char *)) != 0) - strcat (stacked[i], data); - va_end (ap); - - return stacked[i]; -} - -/* Free temporary strings */ -void freestrs(void) -{ - size_t i; - - for (i = 0; i < sizeof(stacked) / sizeof(stacked[0]); i++) { - g_free (stacked[i]); - stacked[i] = NULL; - } -} - void edit_help_cmd (WEdit * edit) { interactive_display (NULL, "[Internal File Editor]"); @@ -205,8 +153,10 @@ static int edit_save_file (WEdit *edit, const char *filename) { char *p; + gchar *tmp; long filelen = 0; char *savename = 0; + gchar *real_filename; int this_save_mode, fd = -1; if (!filename) @@ -215,15 +165,15 @@ edit_save_file (WEdit *edit, const char *filename) return 0; if (*filename != PATH_SEP && edit->dir) { - savename = concat_dir_and_file (edit->dir, filename); - filename = catstrs (savename, (char *) NULL); - g_free (savename); + real_filename = concat_dir_and_file (edit->dir, filename); + } else { + real_filename = g_strdup(filename); } this_save_mode = option_save_mode; if (this_save_mode != EDIT_QUICK_SAVE) { - if (!vfs_file_is_local (filename) || - (fd = mc_open (filename, O_RDONLY | O_BINARY)) == -1) { + if (!vfs_file_is_local (real_filename) || + (fd = mc_open (real_filename, O_RDONLY | O_BINARY)) == -1) { /* * The file does not exists yet, so no safe save or * backup are necessary. @@ -239,7 +189,7 @@ edit_save_file (WEdit *edit, const char *filename) int rv; struct stat sb; - rv = mc_stat (filename, &sb); + rv = mc_stat (real_filename, &sb); if (rv == 0 && sb.st_nlink > 1) { rv = edit_query_dialog3 (_("Warning"), _(" File has hard-links. Detach before saving? "), @@ -252,6 +202,7 @@ edit_save_file (WEdit *edit, const char *filename) edit->skip_detach_prompt = 1; break; default: + g_free(real_filename); return -1; } } @@ -267,26 +218,30 @@ edit_save_file (WEdit *edit, const char *filename) _("The file has been modified in the meantime. Save anyway?"), _("&Yes"), _("&Cancel")); - if (rv != 0) + if (rv != 0){ + g_free(real_filename); return -1; + } } } if (this_save_mode != EDIT_QUICK_SAVE) { char *savedir, *saveprefix; const char *slashpos; - slashpos = strrchr (filename, PATH_SEP); + slashpos = strrchr (real_filename, PATH_SEP); if (slashpos) { - savedir = g_strdup (filename); - savedir[slashpos - filename + 1] = '\0'; + savedir = g_strdup (real_filename); + savedir[slashpos - real_filename + 1] = '\0'; } else savedir = g_strdup ("."); saveprefix = concat_dir_and_file (savedir, "cooledit"); g_free (savedir); fd = mc_mkstemps (&savename, saveprefix, NULL); g_free (saveprefix); - if (!savename) + if (!savename){ + g_free(real_filename); return 0; + } /* FIXME: * Close for now because mc_mkstemps use pure open system call * to create temporary file and it needs to be reopened by @@ -294,7 +249,7 @@ edit_save_file (WEdit *edit, const char *filename) */ close (fd); } else - savename = g_strdup (filename); + savename = g_strdup (real_filename); mc_chown (savename, edit->stat1.st_uid, edit->stat1.st_gid); mc_chmod (savename, edit->stat1.st_mode); @@ -305,7 +260,7 @@ edit_save_file (WEdit *edit, const char *filename) goto error_save; /* pipe save */ - if ((p = edit_get_write_filter (savename, filename))) { + if ((p = edit_get_write_filter (savename, real_filename))) { FILE *file; mc_close (fd); @@ -317,20 +272,22 @@ edit_save_file (WEdit *edit, const char *filename) pclose (file); #else if (pclose (file) != 0) { - edit_error_dialog (_("Error"), - catstrs (_(" Error writing to pipe: "), - p, " ", (char *) NULL)); + tmp = g_strconcat (_(" Error writing to pipe: "), + p, " ", (char *) NULL); + edit_error_dialog (_("Error"), tmp); + g_free(tmp); g_free (p); goto error_save; } #endif } else { + tmp = g_strconcat (_(" Cannot open pipe for writing: "), + p, " ", (char *) NULL); + edit_error_dialog (_("Error"), - get_sys_error (catstrs - (_ - (" Cannot open pipe for writing: "), - p, " ", (char *) NULL))); + get_sys_error (tmp)); g_free (p); + g_free(tmp); goto error_save; } g_free (p); @@ -386,21 +343,25 @@ edit_save_file (WEdit *edit, const char *filename) if (this_save_mode == EDIT_DO_BACKUP) { assert (option_backup_ext != NULL); - if (mc_rename (filename, catstrs (filename, option_backup_ext, - (char *) NULL)) == -1) + tmp = g_strconcat (real_filename, option_backup_ext,(char *) NULL); + if (mc_rename (real_filename, tmp) == -1){ + g_free(tmp); goto error_save; + } } if (this_save_mode != EDIT_QUICK_SAVE) - if (mc_rename (savename, filename) == -1) + if (mc_rename (savename, real_filename) == -1) goto error_save; g_free (savename); + g_free(real_filename); return 1; error_save: /* FIXME: Is this safe ? * if (this_save_mode != EDIT_QUICK_SAVE) * mc_unlink (savename); */ + g_free(real_filename); g_free (savename); return 0; } @@ -627,13 +588,18 @@ edit_raw_key_query (const char *heading, const char *query, int cancel) /* creates a macro file if it doesn't exist */ static FILE *edit_open_macro_file (const char *r) { - const char *filename; + gchar *filename; + FILE *fd; int file; - filename = catstrs (home_dir, PATH_SEP_STR MACRO_FILE, (char *) NULL); - if ((file = open (filename, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1) + filename = g_strconcat ( home_dir, PATH_SEP_STR MACRO_FILE, (char *) NULL); + if ((file = open (filename, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1){ + g_free(filename); return 0; + } close (file); - return fopen (filename, r); + fd = fopen (filename, r); + g_free(filename); + return fd; } #define MAX_MACROS 1024 @@ -658,6 +624,7 @@ macro_exists (int k) static int edit_delete_macro (WEdit * edit, int k) { + gchar *tmp, *tmp2; struct macro macro[MAX_MACRO_LENGTH]; FILE *f, *g; int s, i, n, j = 0; @@ -667,7 +634,9 @@ edit_delete_macro (WEdit * edit, int k) if (saved_macros_loaded) if ((j = macro_exists (k)) < 0) return 0; - g = fopen (catstrs (home_dir, PATH_SEP_STR TEMP_FILE, (char *) NULL), "w"); + tmp = g_strconcat (home_dir, PATH_SEP_STR TEMP_FILE, (char *) NULL); + g = fopen (tmp , "w"); + g_free(tmp); if (!g) { edit_error_dialog (_(" Delete macro "), get_sys_error (_(" Cannot open temp file "))); @@ -697,11 +666,18 @@ edit_delete_macro (WEdit * edit, int k) } fclose (f); fclose (g); - if (rename (catstrs (home_dir, PATH_SEP_STR TEMP_FILE, (char *) NULL), catstrs (home_dir, PATH_SEP_STR MACRO_FILE, (char *) NULL)) == -1) { + tmp = g_strconcat (home_dir, PATH_SEP_STR TEMP_FILE, (char *) NULL); + tmp2 = g_strconcat (home_dir, PATH_SEP_STR MACRO_FILE, (char *) NULL); + if (rename ( tmp, tmp2) == -1) { edit_error_dialog (_(" Delete macro "), get_sys_error (_(" Cannot overwrite macro file "))); + g_free(tmp); + g_free(tmp2); return 1; } + g_free(tmp); + g_free(tmp2); + if (saved_macros_loaded) memmove (saved_macro + j, saved_macro + j + 1, sizeof (int) * (MAX_MACROS - j - 1)); return 0; @@ -800,12 +776,15 @@ int edit_load_macro_cmd (WEdit * edit, struct macro macro[], int *n, int k) /* returns 1 on success */ int edit_save_confirm_cmd (WEdit * edit) { - const char *f; + gchar *f = NULL; if (edit_confirm_save) { - f = catstrs (_(" Confirm save file? : "), edit->filename, " ", (char *) NULL); - if (edit_query_dialog2 (_(" Save file "), f, _("&Save"), _("&Cancel"))) + f = g_strconcat (_(" Confirm save file? : "), edit->filename, " ", NULL); + if (edit_query_dialog2 (_(" Save file "), f, _("&Save"), _("&Cancel"))){ + g_free(f); return 0; + } + g_free(f); } return edit_save_cmd (edit); } @@ -2260,7 +2239,12 @@ edit_save_block (WEdit * edit, const char *filename, long start, /* copies a block to clipboard file */ static int edit_save_block_to_clip_file (WEdit * edit, long start, long finish) { - return edit_save_block (edit, catstrs (home_dir, PATH_SEP_STR CLIP_FILE, (char *) NULL), start, finish); + int ret; + gchar *tmp; + tmp = g_strconcat (home_dir, PATH_SEP_STR CLIP_FILE, (char *) NULL); + ret = edit_save_block (edit, tmp, start, finish); + g_free(tmp); + return ret; } @@ -2299,7 +2283,10 @@ int edit_cut_to_X_buf_cmd (WEdit * edit) void edit_paste_from_X_buf_cmd (WEdit * edit) { - edit_insert_file (edit, catstrs (home_dir, PATH_SEP_STR CLIP_FILE, (char *) NULL)); + gchar *tmp; + tmp = g_strconcat (home_dir, PATH_SEP_STR CLIP_FILE, (char *) NULL); + edit_insert_file (edit, tmp); + g_free(tmp); } @@ -2348,13 +2335,15 @@ int edit_save_block_cmd (WEdit *edit) { long start_mark, end_mark; - char *exp; + char *exp, *tmp; if (eval_marks (edit, &start_mark, &end_mark)) return 1; + tmp = g_strconcat (home_dir, PATH_SEP_STR CLIP_FILE, (char *) NULL); exp = input_expand_dialog (_(" Save Block "), _(" Enter file name: "), MC_HISTORY_EDIT_SAVE_BLOCK, - catstrs (home_dir, PATH_SEP_STR CLIP_FILE, (char *) NULL)); + tmp); + g_free(tmp); edit_push_action (edit, KEY_PRESS + edit->start_display); if (exp) { if (!*exp) { @@ -2382,9 +2371,14 @@ edit_save_block_cmd (WEdit *edit) int edit_insert_file_cmd (WEdit *edit) { - char *exp = input_expand_dialog (_(" Insert File "), _(" Enter file name: "), + gchar *tmp; + char *exp; + + tmp = g_strconcat (home_dir, PATH_SEP_STR CLIP_FILE, (char *) NULL); + exp = input_expand_dialog (_(" Insert File "), _(" Enter file name: "), MC_HISTORY_EDIT_INSERT_FILE, - catstrs (home_dir, PATH_SEP_STR CLIP_FILE, (char *) NULL)); + tmp); + g_free(tmp); edit_push_action (edit, KEY_PRESS + edit->start_display); if (exp) { if (!*exp) { @@ -2411,7 +2405,7 @@ edit_insert_file_cmd (WEdit *edit) int edit_sort_cmd (WEdit * edit) { static char *old = 0; - char *exp; + char *exp, *tmp; long start_mark, end_mark; int e; @@ -2419,7 +2413,9 @@ int edit_sort_cmd (WEdit * edit) edit_error_dialog (_(" Sort block "), _(" You must first highlight a block of text. ")); return 0; } - edit_save_block (edit, catstrs (home_dir, PATH_SEP_STR BLOCK_FILE, (char *) NULL), start_mark, end_mark); + tmp = g_strconcat (home_dir, PATH_SEP_STR BLOCK_FILE, (char *) NULL); + edit_save_block (edit, tmp, start_mark, end_mark); + g_free(tmp); exp = input_dialog (_(" Run Sort "), _(" Enter sort options (see manpage) separated by whitespace: "), @@ -2429,8 +2425,9 @@ int edit_sort_cmd (WEdit * edit) return 1; g_free (old); old = exp; - - e = system (catstrs (" sort ", exp, " ", home_dir, PATH_SEP_STR BLOCK_FILE, " > ", home_dir, PATH_SEP_STR TEMP_FILE, (char *) NULL)); + tmp = g_strconcat (" sort ", exp, " ", home_dir, PATH_SEP_STR BLOCK_FILE, " > ", home_dir, PATH_SEP_STR TEMP_FILE, (char *) NULL); + e = system (tmp); + g_free(tmp); if (e) { if (e == -1 || e == 127) { edit_error_dialog (_(" Sort "), @@ -2438,8 +2435,9 @@ int edit_sort_cmd (WEdit * edit) } else { char q[8]; sprintf (q, "%d ", e); - edit_error_dialog (_(" Sort "), - catstrs (_(" Sort returned non-zero: "), q, (char *) NULL)); + tmp = g_strconcat (_(" Sort returned non-zero: "), q, (char *) NULL); + edit_error_dialog (_(" Sort "), tmp); + g_free(tmp); } return -1; } @@ -2448,7 +2446,9 @@ int edit_sort_cmd (WEdit * edit) if (edit_block_delete_cmd (edit)) return 1; - edit_insert_file (edit, catstrs (home_dir, PATH_SEP_STR TEMP_FILE, (char *) NULL)); + tmp = g_strconcat (home_dir, PATH_SEP_STR TEMP_FILE, (char *) NULL); + edit_insert_file (edit, tmp); + g_free(tmp); return 0; } @@ -2459,7 +2459,7 @@ int edit_sort_cmd (WEdit * edit) int edit_ext_cmd (WEdit *edit) { - char *exp; + char *exp, *tmp; int e; exp = @@ -2470,7 +2470,9 @@ edit_ext_cmd (WEdit *edit) if (!exp) return 1; - e = system (catstrs (exp, " > ", home_dir, PATH_SEP_STR TEMP_FILE, (char *) NULL)); + tmp = g_strconcat (exp, " > ", home_dir, PATH_SEP_STR TEMP_FILE, (char *) NULL); + e = system (tmp); + g_free(tmp); g_free (exp); if (e) { @@ -2480,8 +2482,9 @@ edit_ext_cmd (WEdit *edit) } edit->force |= REDRAW_COMPLETELY; - - edit_insert_file (edit, catstrs (home_dir, PATH_SEP_STR TEMP_FILE, (char *) NULL)); + tmp = g_strconcat (home_dir, PATH_SEP_STR TEMP_FILE, (char *) NULL); + edit_insert_file (edit, tmp); + g_free(tmp); return 0; } @@ -2496,43 +2499,40 @@ edit_block_process_cmd (WEdit *edit, const char *shell_cmd, int block) FILE *script_home = NULL; FILE *script_src = NULL; FILE *block_file = NULL; - const char *o = NULL; - const char *h = NULL; - const char *b = NULL; + gchar *o, *h, *b, *tmp; char *quoted_name = NULL; - o = catstrs (mc_home, shell_cmd, (char *) NULL); /* original source script */ - h = catstrs (home_dir, PATH_SEP_STR EDIT_DIR, shell_cmd, (char *) NULL); /* home script */ - b = catstrs (home_dir, PATH_SEP_STR BLOCK_FILE, (char *) NULL); /* block file */ + o = g_strconcat (mc_home, shell_cmd, (char *) NULL); /* original source script */ + h = g_strconcat (home_dir, PATH_SEP_STR EDIT_DIR, shell_cmd, (char *) NULL); /* home script */ + b = g_strconcat (home_dir, PATH_SEP_STR BLOCK_FILE, (char *) NULL); /* block file */ if (!(script_home = fopen (h, "r"))) { if (!(script_home = fopen (h, "w"))) { - edit_error_dialog ("", get_sys_error (catstrs - (_ - ("Error creating script:"), - h, (char *) NULL))); - return; + tmp = g_strconcat (_("Error creating script:"), h, (char *) NULL); + edit_error_dialog ("", get_sys_error (tmp)); + g_free(tmp); + goto edit_block_process_cmd__EXIT; } if (!(script_src = fopen (o, "r"))) { fclose (script_home); unlink (h); - edit_error_dialog ("", get_sys_error (catstrs - (_("Error reading script:"), - o, (char *) NULL))); - return; + tmp = g_strconcat (_("Error reading script:"), o, (char *) NULL); + edit_error_dialog ("", get_sys_error (tmp)); + g_free(tmp); + goto edit_block_process_cmd__EXIT; } while (fgets (buf, sizeof (buf), script_src)) fputs (buf, script_home); if (fclose (script_home)) { - edit_error_dialog ("", get_sys_error (catstrs - (_ - ("Error closing script:"), - h, (char *) NULL))); - return; + tmp = g_strconcat (_("Error closing script:"), h, (char *) NULL); + edit_error_dialog ("", get_sys_error (tmp)); + g_free(tmp); + goto edit_block_process_cmd__EXIT; } chmod (h, 0700); - edit_error_dialog ("", get_sys_error (catstrs - (_("Script created:"), h, (char *) NULL))); + tmp = g_strconcat (_("Script created:"), h, (char *) NULL); + edit_error_dialog ("", get_sys_error (tmp)); + g_free(tmp); } open_error_pipe (); @@ -2542,7 +2542,7 @@ edit_block_process_cmd (WEdit *edit, const char *shell_cmd, int block) edit_error_dialog (_("Process block"), _ (" You must first highlight a block of text. ")); - return; + goto edit_block_process_cmd__EXIT; } edit_save_block (edit, b, start_mark, end_mark); quoted_name = name_quote (edit->filename, 0); @@ -2555,17 +2555,20 @@ edit_block_process_cmd (WEdit *edit, const char *shell_cmd, int block) * $3 - file where error messages should be put * (for compatibility with old scripts). */ - system (catstrs (" ", home_dir, PATH_SEP_STR EDIT_DIR, shell_cmd, " ", quoted_name, - " ", home_dir, PATH_SEP_STR BLOCK_FILE " /dev/null", (char *) NULL)); - + tmp = g_strconcat (" ", home_dir, PATH_SEP_STR EDIT_DIR, shell_cmd, " ", quoted_name, + " ", home_dir, PATH_SEP_STR BLOCK_FILE " /dev/null", (char *) NULL); + system (tmp); + g_free(tmp); } else { /* * No block selected, just execute the command for the file. * Arguments: * $1 - name of the edited file. */ - system (catstrs (" ", home_dir, PATH_SEP_STR EDIT_DIR, shell_cmd, " ", - quoted_name, (char *) NULL)); + tmp = g_strconcat (" ", home_dir, PATH_SEP_STR EDIT_DIR, shell_cmd, " ", + quoted_name, (char *) NULL); + system (tmp); + g_free(tmp); } g_free (quoted_name); close_error_pipe (D_NORMAL, NULL); @@ -2576,13 +2579,16 @@ edit_block_process_cmd (WEdit *edit, const char *shell_cmd, int block) /* insert result block */ if (block) { if (edit_block_delete_cmd (edit)) - return; + goto edit_block_process_cmd__EXIT; edit_insert_file (edit, b); if ((block_file = fopen (b, "w"))) fclose (block_file); - return; + goto edit_block_process_cmd__EXIT; } - +edit_block_process_cmd__EXIT: + g_free(b); + g_free(h); + g_free(o); return; } diff --git a/src/strutil.c b/src/strutil.c index 7dc9b6bff..1ae8eab67 100644 --- a/src/strutil.c +++ b/src/strutil.c @@ -65,9 +65,9 @@ static char *codeset; // function for encoding specific operations static struct str_class used_class; -iconv_t str_cnv_to_term; -iconv_t str_cnv_from_term; -iconv_t str_cnv_not_convert; +GIConv str_cnv_to_term; +GIConv str_cnv_from_term; +GIConv str_cnv_not_convert; // if enc is same encoding like on terminal static int @@ -265,13 +265,13 @@ str_insert_replace_char (GString * buffer) } int -str_translate_char (str_conv_t conv, char *keys, size_t ch_size, +str_translate_char (GIConv conv, char *keys, size_t ch_size, char *output, size_t out_size) { size_t left; size_t cnv; - iconv (conv, NULL, NULL, NULL, NULL); + g_iconv (conv, NULL, NULL, NULL, NULL); left = (ch_size == (size_t)(-1)) ? strlen (keys) : ch_size; @@ -333,21 +333,21 @@ str_init_strings (const char *termenc) ? termenc : str_detect_termencoding ()); - str_cnv_not_convert = iconv_open (codeset, codeset); + str_cnv_not_convert = g_iconv_open (codeset, codeset); if (str_cnv_not_convert == INVALID_CONV) { if (termenc != NULL) { g_free (codeset); codeset = g_strdup (str_detect_termencoding ()); - str_cnv_not_convert = iconv_open (codeset, codeset); + str_cnv_not_convert = g_iconv_open (codeset, codeset); } if (str_cnv_not_convert == INVALID_CONV) { g_free (codeset); codeset = g_strdup ("ascii"); - str_cnv_not_convert = iconv_open (codeset, codeset); + str_cnv_not_convert = g_iconv_open (codeset, codeset); } } @@ -360,7 +360,7 @@ str_init_strings (const char *termenc) void str_uninit_strings () { - iconv_close (str_cnv_not_convert); + g_iconv_close (str_cnv_not_convert); } const char * diff --git a/src/strutil.h b/src/strutil.h index fadb6b5d5..0b569fbfd 100644 --- a/src/strutil.h +++ b/src/strutil.h @@ -65,18 +65,17 @@ #define J_CENTER_LEFT_FIT 0x14 // redefinition of iconv_t, so is not needed include iconv.h in other files. -typedef iconv_t str_conv_t; #define INVALID_CONV ((iconv_t) (-1)) // standard convertors -extern str_conv_t str_cnv_to_term; -extern str_conv_t str_cnv_from_term; +extern GIConv str_cnv_to_term; +extern GIConv str_cnv_from_term; // from terminal encoding to terminal encoding -extern str_conv_t str_cnv_not_convert; +extern GIConv str_cnv_not_convert; // all functions in str_class must be defined for every encoding struct str_class { - int (*vfs_convert_to) (str_conv_t coder, const char *string, + int (*vfs_convert_to) (GIConv coder, const char *string, int size, GString *buffer); //I void (*insert_replace_char) (GString *buffer); int (*is_valid_string) (const char *); //I @@ -195,7 +194,7 @@ void str_uninit_strings (); * return 0 if conversion was successfully, ESTR_PROBLEM if ch contains only * part of characters, ESTR_FAILURE if conversion is not possible */ -int str_translate_char (str_conv_t conv, char *ch, size_t ch_size, +int str_translate_char (GIConv conv, char *ch, size_t ch_size, char *output, size_t out_size); /* test, if text is valid in terminal encoding diff --git a/src/strutil8bit.c b/src/strutil8bit.c index 75df77b3a..7a7efc58b 100644 --- a/src/strutil8bit.c +++ b/src/strutil8bit.c @@ -156,7 +156,7 @@ str_8bit_length2 (const char *text, int size) } int -str_8bit_vfs_convert_to (str_conv_t coder, const char *string, +str_8bit_vfs_convert_to (GIConv coder, const char *string, int size, GString * buffer) { int result; diff --git a/src/strutilutf8.c b/src/strutilutf8.c index 67709815b..acb6c82b0 100644 --- a/src/strutilutf8.c +++ b/src/strutilutf8.c @@ -300,71 +300,8 @@ str_utf8_questmark_sustb (char **string, size_t * left, GString * buffer) g_string_append_c (buffer, '?'); } -/* static int -_str_utf8_vfs_convert_to (str_conv_t coder, const char *string, - int size, GString * buffer) -{ - int state = 0; - size_t left; - size_t nconv; - char *composed, *c; - const char *start, *end; - - errno = 0; - - size = (size >= 0) ? size : strlen (string); - if (coder == (iconv_t) (-1)) - return ESTR_FAILURE; - iconv (coder, NULL, NULL, NULL, NULL); - - start = string; - while (size > 0) - { - end = strchr (start, PATH_SEP); - end = (end == NULL || end >= start + size) ? start + size : end + 1; - if (g_utf8_validate (start, end - start, NULL)) - { - c = composed = - g_utf8_normalize (start, end - start, - G_NORMALIZE_DEFAULT_COMPOSE); - left = strlen (composed); - while (((int) left) > 0) - { - nconv = - iconv (coder, &c, &left, &(buffer->actual), - &(buffer->remain)); - if (nconv == (size_t) (-1)) - { - switch (errno) - { - case EINVAL: - g_free (composed); - return ESTR_FAILURE; - case EILSEQ: - str_utf8_questmark_sustb (&c, &left, buffer); - state = ESTR_PROBLEM; - break; - case E2BIG: - str_incrase_buffer (buffer); - break; - } - } - } - g_free (composed); - } - else - { - g_string_append_len (buffer, start, end - start); - } - size -= end - start; - start = end; - } - return state; -} -*/ -static int -str_utf8_vfs_convert_to (str_conv_t coder, const char *string, +str_utf8_vfs_convert_to (GIConv coder, const char *string, int size, GString * buffer) { int result; diff --git a/src/view.c b/src/view.c index 3777d20e1..0ec1d0ce6 100644 --- a/src/view.c +++ b/src/view.c @@ -3653,7 +3653,7 @@ static void view_select_encoding (WView *view) { char *enc; - iconv_t conv; + GIConv conv; struct cache_line *line; enc = input_dialog ("Encoding", "Paste encoding", NULL, ""); From 4af2c5b93889db3fefb2f9463d93f2bbc5752eb1 Mon Sep 17 00:00:00 2001 From: Slava Zanko Date: Tue, 14 Apr 2009 15:56:46 +0300 Subject: [PATCH 03/12] src/strutil.c: replace call of g_string_append_vprintf() with g_strdup_vprintf and g_string_append Not all versions of glib2 have function g_string_append_vprintf --- src/strutil.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/strutil.c b/src/strutil.c index 1ae8eab67..8bce37170 100644 --- a/src/strutil.c +++ b/src/strutil.c @@ -252,9 +252,17 @@ str_vfs_convert_to (GIConv coder, const char *string, int size, void str_printf (GString * buffer, const char *format, ...) { + gchar *tmp; va_list ap; va_start (ap, format); - g_string_append_vprintf (buffer, format, ap); + /* + * more simple call: + g_string_append_vprintf (buffer, format, ap); + * but not all versions of glib2 have this function :( + */ + tmp = g_strdup_vprintf ( format, ap); + g_string_append (buffer, tmp); + g_free(tmp); va_end (ap); } From 83b2c8406fd5f4036922d4dae530a16e13958a0c Mon Sep 17 00:00:00 2001 From: Ilia Maslakov Date: Wed, 15 Apr 2009 13:56:19 +0000 Subject: [PATCH 04/12] fix move backward and backspace. --- edit/edit.c | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/edit/edit.c b/edit/edit.c index ddc5cb819..27017b967 100644 --- a/edit/edit.c +++ b/edit/edit.c @@ -139,14 +139,19 @@ char *edit_get_buf_ptr (WEdit * edit, long byte_index) { unsigned long p; - if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0) - return NULL; + if (byte_index >= (edit->curs1 + edit->curs2) ) { + byte_index -= 1; + }; + + if ( byte_index < 0 ) { + return NULL; + } if (byte_index >= edit->curs1) { - p = edit->curs1 + edit->curs2 - byte_index - 1; - return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE]); + p = edit->curs1 + edit->curs2 - 1; + return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] + (EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1)); } else { - return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE]); + return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] + (0 & M_EDIT_BUF_SIZE)); } } @@ -169,7 +174,7 @@ int edit_get_utf (WEdit * edit, long byte_index, int *char_width) if ( res < 0 ) { ch = *str; - width = 1; + width = 0; } else { ch = res; /* Calculate UTF-8 char width */ @@ -196,23 +201,33 @@ int edit_get_prev_utf (WEdit * edit, long byte_index, int *char_width) gunichar ch; gchar *next_ch = NULL; int width = 0; + gchar *prn_buf=NULL; - if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0) { + if ( byte_index > 0 ) { + byte_index--; + } + + ch = edit_get_utf (edit, byte_index, &width); + + if ( width == 1 ) { + *char_width = width; + return ch; + } + + if ( byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0 ) { *char_width = 1; - return '\n'; + return 0; } str = edit_get_byte_ptr (edit, byte_index); buf = edit_get_buf_ptr (edit, byte_index); - /* get prev utf8 char */ str = g_utf8_find_prev_char (buf, str); res = g_utf8_get_char_validated (str, -1); - if ( res < 0 ) { ch = *str; - width = 1; + width = 0; } else { ch = res; /* Calculate UTF-8 char width */ From 6ecea97973caf767527cd0e88dc3d1f0965be66f Mon Sep 17 00:00:00 2001 From: Slava Zanko Date: Wed, 15 Apr 2009 17:11:00 +0300 Subject: [PATCH 05/12] vfs/vfs.c: Replace str_conv_t to GIConv --- vfs/vfs.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/vfs/vfs.c b/vfs/vfs.c index 37a7f5d08..ff9f5f9da 100644 --- a/vfs/vfs.c +++ b/vfs/vfs.c @@ -67,7 +67,7 @@ struct vfs_openfile { struct vfs_dirinfo{ DIR *info; - str_conv_t converter; + GIConv converter; }; @@ -382,47 +382,47 @@ vfs_supported_enconding (const char *encoding) { * buffer - used to store result of translation */ static int -_vfs_translate_path (const char *path, int size, - str_conv_t defcnv, GString *buffer) +_vfs_translate_path (const char *path, int size, + GIConv defcnv, GString *buffer) { const char *semi; const char *ps; const char *slash; int state = 0; static char encoding[16]; - str_conv_t coder; + GIConv coder; int ms; - + if (size == 0) return 0; size = (size > 0) ? size : strlen (path); - + /* try found #end: */ semi = g_strrstr_len (path, size, "#enc:"); if (semi != NULL) { /* first must be translated part before #enc: */ ms = semi - path; - + /* remove '/' before #enc */ ps = str_cget_prev_char (semi); if (ps[0] == PATH_SEP) ms = ps - path; - + state = _vfs_translate_path (path, ms, defcnv, buffer); - + if (state != 0) return state; /* now can be translated part after #enc: */ - + semi+= 5; slash = strchr (semi, PATH_SEP); // ignore slashes after size; if (slash - path >= size) slash = NULL; - + ms = (slash != NULL) ? slash - semi : strlen (semi); ms = min (ms, sizeof (encoding) - 1); // limit encoding size (ms) to path size (size) if (semi + ms > path + size) ms = path + size - semi; memcpy (encoding, semi, ms); encoding[ms] = '\0'; - + switch (vfs_supported_enconding (encoding)) { case 1: coder = str_crt_conv_to (encoding); @@ -459,7 +459,7 @@ char * vfs_translate_path (const char *path) { int state; - + g_string_set_size(vfs_str_buffer,0); state = _vfs_translate_path (path, -1, str_cnv_from_term, vfs_str_buffer); // strict version @@ -1218,7 +1218,7 @@ vfs_shut (void) (*vfs->done) (vfs); g_slist_free (vfs_openfiles); - + g_string_free (vfs_str_buffer, TRUE); } From 12b1081c64942ef29405c43cab32e9bcbf6d39fc Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Wed, 15 Apr 2009 20:04:14 +0400 Subject: [PATCH 06/12] Unification of panel frame painting. myslang.h: modified definition of ACS_LTEE symbol. Added definition of ACS_RTEE symbol. screen.c (show_dir): paint left and right symbols of mini infor separator as for S-Lang library as for NCurses one. --- src/myslang.h | 3 ++- src/screen.c | 12 +++++------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/myslang.h b/src/myslang.h index a8c8b42b8..eb22bbfbd 100644 --- a/src/myslang.h +++ b/src/myslang.h @@ -18,12 +18,13 @@ enum { #define ACS_VLINE SLSMG_VLINE_CHAR #define ACS_HLINE SLSMG_HLINE_CHAR +#define ACS_LTEE SLSMG_LTEE_CHAR +#define ACS_RTEE SLSMG_RTEE_CHAR #define ACS_ULCORNER SLSMG_ULCORN_CHAR #define ACS_LLCORNER SLSMG_LLCORN_CHAR #define ACS_URCORNER SLSMG_URCORN_CHAR #define ACS_LRCORNER SLSMG_LRCORN_CHAR -#define ACS_LTEE 't' #define acs() SLsmg_set_char_set(1) #define noacs() SLsmg_set_char_set (0) #define baudrate() SLang_TT_Baud_Rate diff --git a/src/screen.c b/src/screen.c index 24ddc85c1..a02fc536a 100644 --- a/src/screen.c +++ b/src/screen.c @@ -774,15 +774,13 @@ show_dir (WPanel *panel) panel->widget.y, panel->widget.x, panel->widget.lines, panel->widget.cols); -#ifdef HAVE_SLANG if (show_mini_info) { - SLsmg_draw_object (panel->widget.y + llines (panel) + 2, - panel->widget.x, SLSMG_LTEE_CHAR); - SLsmg_draw_object (panel->widget.y + llines (panel) + 2, - panel->widget.x + panel->widget.cols - 1, - SLSMG_RTEE_CHAR); + widget_move (&panel->widget, llines (panel) + 2, 0); + tty_print_alt_char (ACS_LTEE); + widget_move (&panel->widget, llines (panel) + 2, + panel->widget.cols - 1); + tty_print_alt_char (ACS_RTEE); } -#endif /* HAVE_SLANG */ if (panel->active) attrset (REVERSE_COLOR); From 5141e88f703b16c1cfd56721d757a2f577e0ed7c Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Wed, 15 Apr 2009 20:12:47 +0400 Subject: [PATCH 07/12] Type accuracy. dialog.h: define values for cb_ret_t enum. widget.c: removed trailing spaces. Formatting. (insert_char): type accuracy for cb_ret_t. (listbox_key): likewise. --- src/dialog.h | 4 ++-- src/widget.c | 65 +++++++++++++++++++++++++++------------------------- 2 files changed, 36 insertions(+), 33 deletions(-) diff --git a/src/dialog.h b/src/dialog.h index 27464c220..b9c1e8b49 100644 --- a/src/dialog.h +++ b/src/dialog.h @@ -49,8 +49,8 @@ typedef enum { } widget_msg_t; typedef enum { - MSG_NOT_HANDLED, - MSG_HANDLED + MSG_NOT_HANDLED = 0, + MSG_HANDLED = 1 } cb_ret_t; /* Widgets are expected to answer to the following messages: diff --git a/src/widget.c b/src/widget.c index 62fcef101..ea8e394da 100644 --- a/src/widget.c +++ b/src/widget.c @@ -156,10 +156,9 @@ button_callback (Widget *w, widget_msg_t msg, int parm) if (b->text.hotkey != NULL) { if (g_ascii_tolower ((gchar)b->text.hotkey[0]) == g_ascii_tolower ((gchar)parm)) { - - button_callback (w, WIDGET_KEY, ' '); - return MSG_HANDLED; - } + button_callback (w, WIDGET_KEY, ' '); + return MSG_HANDLED; + } } return MSG_NOT_HANDLED; @@ -204,7 +203,7 @@ button_callback (Widget *w, widget_msg_t msg, int parm) widget_selectcolor (w, b->selected, FALSE); widget_move (w, 0, 0); - + switch (b->flags) { case DEFPUSH_BUTTON: addstr ("[< "); @@ -221,11 +220,11 @@ button_callback (Widget *w, widget_msg_t msg, int parm) } addstr (str_term_form (b->text.start)); - + if (b->text.hotkey != NULL) { widget_selectcolor (w, b->selected, TRUE); addstr (str_term_form (b->text.hotkey)); - widget_selectcolor (w, b->selected, FALSE); + widget_selectcolor (w, b->selected, FALSE); } if (b->text.end != NULL) { @@ -243,7 +242,6 @@ button_callback (Widget *w, widget_msg_t msg, int parm) addstr ("]"); break; } - return MSG_HANDLED; case WIDGET_DESTROY: @@ -411,7 +409,7 @@ radio_callback (Widget *w, widget_msg_t msg, int parm) widget_move (&r->widget, i, 0); addstr ((r->sel == i) ? "(*) " : "( ) "); - + addstr (str_term_form (r->texts[i].start)); if (r->texts[i].hotkey != NULL) { @@ -431,7 +429,7 @@ radio_callback (Widget *w, widget_msg_t msg, int parm) } g_free (r->texts); return MSG_HANDLED; - + default: return default_proc (msg, parm); } @@ -501,10 +499,10 @@ check_callback (Widget *w, widget_msg_t msg, int parm) if (c->text.hotkey != NULL) { if (g_ascii_tolower ((gchar)c->text.hotkey[0]) == g_ascii_tolower ((gchar)parm)) { - - check_callback (w, WIDGET_KEY, ' '); /* make action */ - return MSG_HANDLED; - } + + check_callback (w, WIDGET_KEY, ' '); /* make action */ + return MSG_HANDLED; + } } return MSG_NOT_HANDLED; @@ -535,7 +533,7 @@ check_callback (Widget *w, widget_msg_t msg, int parm) addstr (str_term_form (c->text.hotkey)); widget_selectcolor (w, msg == WIDGET_FOCUS, FALSE); } - + if (c->text.end != NULL) { addstr (str_term_form (c->text.end)); } @@ -624,7 +622,7 @@ label_callback (Widget *w, widget_msg_t msg, int parm) widget_move (&l->widget, y, 0); addstr (str_fit_to_term (p, l->widget.cols, J_LEFT)); - + if (q == NULL) break; q[0] = c; @@ -704,7 +702,7 @@ gauge_callback (Widget *w, widget_msg_t msg, int parm) if (msg == WIDGET_INIT) return MSG_HANDLED; - + /* We don't want to get the focus */ if (msg == WIDGET_FOCUS) return MSG_NOT_HANDLED; @@ -717,7 +715,7 @@ gauge_callback (Widget *w, widget_msg_t msg, int parm) else { int percentage, columns; long total = g->max, done = g->current; - + if (total <= 0 || done < 0) { done = 0; total = 100; @@ -738,6 +736,7 @@ gauge_callback (Widget *w, widget_msg_t msg, int parm) } return MSG_HANDLED; } + return default_proc (msg, parm); } @@ -1230,16 +1229,18 @@ insert_char (WInput *in, int c_code) if (c_code == -1) return MSG_NOT_HANDLED; - - if (in->charpoint >= MB_LEN_MAX) return 1; + + if (in->charpoint >= MB_LEN_MAX) + return MSG_HANDLED; in->charbuf[in->charpoint] = c_code; in->charpoint++; res = str_is_valid_char (in->charbuf, in->charpoint); if (res < 0) { - if (res != -2) in->charpoint = 0; /* broken multibyte char, skip */ - return 1; + if (res != -2) + in->charpoint = 0; /* broken multibyte char, skip */ + return MSG_HANDLED; } in->need_push = 1; @@ -1253,7 +1254,7 @@ insert_char (WInput *in, int c_code) in->current_max_size = new_length; } } - + if (strlen (in->buffer) + in->charpoint < in->current_max_size) { /* bytes from begin */ size_t ins_point = str_offset_to_pos (in->buffer, in->point); @@ -1267,7 +1268,7 @@ insert_char (WInput *in, int c_code) memcpy(in->buffer + ins_point, in->charbuf, in->charpoint); in->point++; } - + in->charpoint = 0; return MSG_HANDLED; } @@ -2090,11 +2091,11 @@ static cb_ret_t listbox_key (WListbox *l, int key) { int i; - int j = 0; + cb_ret_t j = MSG_NOT_HANDLED; if (!l->list) return MSG_NOT_HANDLED; - + switch (key){ case KEY_HOME: case KEY_A1: @@ -2125,14 +2126,16 @@ listbox_key (WListbox *l, int key) case KEY_NPAGE: case XCTRL('v'): for (i = 0; i < l->height-1; i++) - j |= listbox_fwd (l); - return (j > 0) ? MSG_HANDLED : MSG_NOT_HANDLED; + if (listbox_fwd (l) != MSG_NOT_HANDLED) + j = MSG_HANDLED; + return j; case KEY_PPAGE: case ALT('v'): for (i = 0; i < l->height-1; i++) - j |= listbox_back (l); - return (j > 0) ? MSG_HANDLED : MSG_NOT_HANDLED; + if (listbox_back (l) != MSG_NOT_HANDLED) + j = MSG_HANDLED; + return j; } return MSG_NOT_HANDLED; } @@ -2434,7 +2437,7 @@ buttonbat_get_button_width () { int result = COLS / 10; return (result >= 7) ? result : 7; -} +} static cb_ret_t From dd6e11fa9cec631527842572ee6d1bec34525d88 Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Wed, 15 Apr 2009 20:22:26 +0400 Subject: [PATCH 08/12] Unification of widgets API. widget.h: chaged paremeters order of listbox_new() and groupbox_new() functions. widget.c: likewise. src/achown.c: sync with modified API of WListbox and WGroupbox widgets. src/boxes.c: likewise. src/chown.c: likewise. src/complete.c: likewise. src/find.c: likewise. src/hotlist.c: likewise. src/layout.c: likewise. src/listmode.c: likewise. src/option.c: likewise. src/panelize.c: likewise. src/wtools.c: likewise. edit/editcmd.c: likewise. --- edit/editcmd.c | 4 ++-- src/achown.c | 2 +- src/boxes.c | 2 +- src/chown.c | 4 ++-- src/complete.c | 2 +- src/find.c | 2 +- src/hotlist.c | 6 +++--- src/layout.c | 6 +++--- src/listmode.c | 8 ++++---- src/option.c | 6 +++--- src/panelize.c | 2 +- src/widget.c | 6 +++--- src/widget.h | 4 ++-- src/wtools.c | 2 +- 14 files changed, 28 insertions(+), 28 deletions(-) diff --git a/edit/editcmd.c b/edit/editcmd.c index d93a9fe34..9cfd27a1b 100644 --- a/edit/editcmd.c +++ b/edit/editcmd.c @@ -2840,7 +2840,7 @@ edit_completion_dialog (WEdit * edit, int max_len, int word_len, /* create the listbox */ compl_list = - listbox_new (1, 1, compl_dlg_w - 2, compl_dlg_h - 2, NULL); + listbox_new (1, 1, compl_dlg_h - 2, compl_dlg_w - 2, NULL); /* add the dialog */ add_widget (compl_dlg, compl_list); @@ -3078,7 +3078,7 @@ edit_select_definition_dialog (WEdit * edit, char *match_expr, int max_len, int DLG_COMPACT); /* create the listbox */ - def_list = listbox_new (1, 1, def_dlg_w - 2, def_dlg_h - 2, NULL); + def_list = listbox_new (1, 1, def_dlg_h - 2, def_dlg_w - 2, NULL); /* add the dialog */ add_widget (def_dlg, def_list); diff --git a/src/achown.c b/src/achown.c index 32c6d07db..444d7bc68 100644 --- a/src/achown.c +++ b/src/achown.c @@ -258,7 +258,7 @@ do_enter_key (Dlg_head * h, int f_pos) "[Advanced Chown]", title, DLG_COMPACT | DLG_REVERSE); /* get new listboxes */ - chl_list = listbox_new (1, 1, 15, 11, NULL); + chl_list = listbox_new (1, 1, 11, 15, NULL); listbox_add_item (chl_list, LISTBOX_APPEND_AT_END, 0, "", NULL); diff --git a/src/boxes.c b/src/boxes.c index a2cd88051..43c80b7c5 100644 --- a/src/boxes.c +++ b/src/boxes.c @@ -999,7 +999,7 @@ jobs_cmd (void) "[Background jobs]", _("Background Jobs"), DLG_CENTER | DLG_REVERSE); - bg_list = listbox_new (2, 3, JOBS_X-7, JOBS_Y-9, 0); + bg_list = listbox_new (2, 3, JOBS_Y - 9, JOBS_X - 7, NULL); add_widget (jobs_dlg, bg_list); i = n_buttons; diff --git a/src/chown.c b/src/chown.c index 9d27fe75b..f165c8df4 100644 --- a/src/chown.c +++ b/src/chown.c @@ -172,8 +172,8 @@ init_chown (void) } /* get new listboxes */ - l_user = listbox_new (UY + 1, UX + 1, 19, 10, NULL); - l_group = listbox_new (GY + 1, GX + 1, 19, 10, NULL); + l_user = listbox_new (UY + 1, UX + 1, 10, 19, NULL); + l_group = listbox_new (GY + 1, GX + 1, 10, 19, NULL); /* add fields for unknown names (numbers) */ listbox_add_item (l_user, 0, 0, _(""), NULL); diff --git a/src/complete.c b/src/complete.c index 4e62da6f6..ae6aa80f8 100644 --- a/src/complete.c +++ b/src/complete.c @@ -1088,7 +1088,7 @@ complete_engine (WInput *in, int what_to_do) query_dlg = create_dlg (y, x, query_height, query_width, dialog_colors, query_callback, "[Completion]", NULL, DLG_COMPACT); - query_list = listbox_new (1, 1, w - 2, h - 2, NULL); + query_list = listbox_new (1, 1, h - 2, w - 2, NULL); add_widget (query_dlg, query_list); for (p = in->completions + 1; *p; p++) listbox_add_item (query_list, 0, 0, *p, NULL); diff --git a/src/find.c b/src/find.c index eb38055d5..dcf283ce0 100644 --- a/src/find.c +++ b/src/find.c @@ -944,7 +944,7 @@ setup_gui (void) add_widget (find_dlg, status_label); find_list = - listbox_new (2, 2, FIND2_X - 4, FIND2_Y - 9, 0); + listbox_new (2, 2, FIND2_Y - 9, FIND2_X - 4, NULL); add_widget (find_dlg, find_list); } diff --git a/src/hotlist.c b/src/hotlist.c index 4c81ebb93..2d7bc7638 100644 --- a/src/hotlist.c +++ b/src/hotlist.c @@ -652,7 +652,7 @@ init_hotlist (int list_type) } /* get new listbox */ l_hotlist = - listbox_new (UY + 1, UX + 1, COLS - 2 * UX - 8, LINES - 14, + listbox_new (UY + 1, UX + 1, LINES - 14, COLS - 2 * UX - 8, l_call); /* Fill the hotlist with the active VFS or the hotlist */ @@ -700,8 +700,8 @@ init_movelist (int list_type, struct hotlist *item) add_widget (movelist_dlg, movelist_group); /* get new listbox */ l_movelist = - listbox_new (UY + 1, UX + 1, movelist_dlg->cols - 2 * UX - 2, - movelist_dlg->lines - 8, l_call); + listbox_new (UY + 1, UX + 1, movelist_dlg->lines - 8, + movelist_dlg->cols - 2 * UX - 2, l_call); fill_listbox (); diff --git a/src/layout.c b/src/layout.c index e325f6fee..da902f931 100644 --- a/src/layout.c +++ b/src/layout.c @@ -439,10 +439,10 @@ init_layout (void) dialog_colors, layout_callback, "[Layout]", _("Layout"), DLG_CENTER | DLG_REVERSE); - add_widget (layout_dlg, groupbox_new (4, 2, first_width, 6, title1)); - add_widget (layout_dlg, groupbox_new (4, 8, first_width, 4, title2)); + add_widget (layout_dlg, groupbox_new (2, 4, 6, first_width, title1)); + add_widget (layout_dlg, groupbox_new (8, 4, 4, first_width, title2)); add_widget (layout_dlg, - groupbox_new (5 + first_width, 2, second_width, 10, + groupbox_new (2, 5 + first_width, 10, second_width, title3)); add_widget (layout_dlg, diff --git a/src/listmode.c b/src/listmode.c index 69e499d70..8f4ccc3e4 100644 --- a/src/listmode.c +++ b/src/listmode.c @@ -173,10 +173,10 @@ init_listmode (char *oldlistformat) "Listing format edit", DLG_CENTER | DLG_REVERSE); add_widget (listmode_dlg, - groupbox_new (UX, UY, 63, 4, "General options")); - add_widget (listmode_dlg, groupbox_new (UX, UY + 4, 18, 11, "Items")); + groupbox_new (UY, UX, 4, 63, "General options")); + add_widget (listmode_dlg, groupbox_new (UY + 4, UX, 11, 18, "Items")); add_widget (listmode_dlg, - groupbox_new (UX + 20, UY + 4, 43, 11, "Item options")); + groupbox_new (UY + 4, UX + 20, 11, 43, "Item options")); for (i = 0; i < sizeof (listmode_but) / sizeof (struct listmode_button); i++) @@ -205,7 +205,7 @@ init_listmode (char *oldlistformat) radio_justify->sel = 1; /* get new listbox */ - l_listmode = listbox_new (UY + 5, UX + 1, 16, 9, NULL); + l_listmode = listbox_new (UY + 5, UX + 1, 9, 16, NULL); if (strncmp (oldlistformat, "full ", 5) == 0) { format_width = 1; diff --git a/src/option.c b/src/option.c index bc1bb2663..5d9d912b7 100644 --- a/src/option.c +++ b/src/option.c @@ -172,13 +172,13 @@ init_configure (void) _("Configure options"), DLG_CENTER | DLG_REVERSE); add_widget (conf_dlg, - groupbox_new (PX, PY, first_width, PANEL_OPTIONS + 2, title1)); + groupbox_new (PY, PX, PANEL_OPTIONS + 2, first_width, title1)); add_widget (conf_dlg, - groupbox_new (RX, RY, first_width, PAUSE_OPTIONS + 2, title2)); + groupbox_new (RY, RX, PAUSE_OPTIONS + 2, first_width, title2)); add_widget (conf_dlg, - groupbox_new (OX, OY, second_width, OTHER_OPTIONS + 2, title3)); + groupbox_new (OY, OX, OTHER_OPTIONS + 2, second_width, title3)); add_widget (conf_dlg, button_new (BY, b3, B_CANCEL, NORMAL_BUTTON, diff --git a/src/panelize.c b/src/panelize.c index 03358e55f..ee0076454 100644 --- a/src/panelize.c +++ b/src/panelize.c @@ -172,7 +172,7 @@ init_panelize (void) /* get new listbox */ l_panelize = - listbox_new (UY + 1, UX + 1, panelize_dlg->cols - 12, 10, NULL); + listbox_new (UY + 1, UX + 1, 10, panelize_dlg->cols - 12, NULL); while (current) { listbox_add_item (l_panelize, 0, 0, current->label, current); diff --git a/src/widget.c b/src/widget.c index ea8e394da..eb4d9ddb8 100644 --- a/src/widget.c +++ b/src/widget.c @@ -1059,7 +1059,7 @@ show_hist (GList *history, int widget_x, int widget_y) query_dlg = create_dlg (y, x, h, w, dialog_colors, NULL, "[History-query]", i18n_htitle (), DLG_COMPACT); - query_list = listbox_new (1, 1, w - 2, h - 2, 0); + query_list = listbox_new (1, 1, h - 2, w - 2, NULL); add_widget (query_dlg, query_list); hi = z; if (y < widget_y) { @@ -2278,7 +2278,7 @@ listbox_event (Gpm_Event *event, void *data) } WListbox * -listbox_new (int y, int x, int width, int height, lcback callback) +listbox_new (int y, int x, int height, int width, lcback callback) { WListbox *l = g_new (WListbox, 1); @@ -2635,7 +2635,7 @@ groupbox_callback (Widget *w, widget_msg_t msg, int parm) } WGroupbox * -groupbox_new (int x, int y, int width, int height, const char *title) +groupbox_new (int y, int x, int height, int width, const char *title) { WGroupbox *g = g_new (WGroupbox, 1); diff --git a/src/widget.h b/src/widget.h index 5252d0b32..8c2881548 100644 --- a/src/widget.h +++ b/src/widget.h @@ -167,8 +167,8 @@ WCheck *check_new (int y, int x, int state, const char *text); WInput *input_new (int y, int x, int color, int len, const char *text, const char *histname, INPUT_COMPLETE_FLAGS completion_flags); WLabel *label_new (int y, int x, const char *text); WGauge *gauge_new (int y, int x, int shown, int max, int current); -WListbox *listbox_new (int x, int y, int width, int height, lcback callback); -WGroupbox *groupbox_new (int x, int y, int width, int height, const char *title); +WListbox *listbox_new (int y, int x, int height, int width, lcback callback); +WGroupbox *groupbox_new (int y, int x, int height, int width, const char *title); /* Input lines */ void winput_set_origin (WInput *i, int x, int field_width); diff --git a/src/wtools.c b/src/wtools.c index a6f27a62b..a619acd58 100644 --- a/src/wtools.c +++ b/src/wtools.c @@ -66,7 +66,7 @@ create_listbox_window (int cols, int lines, const char *title, const char *help) create_dlg (ypos, xpos, lines + 6, cols + 4, dialog_colors, NULL, help, title, DLG_CENTER | DLG_REVERSE); - listbox->list = listbox_new (2, 2, cols, lines, 0); + listbox->list = listbox_new (2, 2, lines, cols, NULL); add_widget (listbox->dlg, button_new (lines + 3, (cols / 2 + 2) - len / 2, B_CANCEL, From 2fdb3689e2bed32acced527a54c6301db64b9ce4 Mon Sep 17 00:00:00 2001 From: Ilia Maslakov Date: Wed, 15 Apr 2009 18:14:17 +0000 Subject: [PATCH 09/12] fix: with status_string on utf-8 char --- edit/edit.c | 3 +-- edit/editdraw.c | 21 ++++++++++++++++----- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/edit/edit.c b/edit/edit.c index 27017b967..faf39db8f 100644 --- a/edit/edit.c +++ b/edit/edit.c @@ -164,12 +164,11 @@ int edit_get_utf (WEdit * edit, long byte_index, int *char_width) int width = 0; if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0) { - *char_width = 1; + *char_width = 0; return '\n'; } str = edit_get_byte_ptr (edit, byte_index); - res = g_utf8_get_char_validated (str, -1); if ( res < 0 ) { diff --git a/edit/editdraw.c b/edit/editdraw.c index 96ebdea44..c0b36ddca 100644 --- a/edit/editdraw.c +++ b/edit/editdraw.c @@ -73,11 +73,22 @@ static void status_string (WEdit * edit, char *s, int w) * as decimal and as hex. */ if (edit->curs1 < edit->last_byte) { - unsigned char cur_byte = edit_get_byte (edit, edit->curs1); - g_snprintf (byte_str, sizeof (byte_str), "%c %3d 0x%02X", - is_printable (cur_byte) ? cur_byte : '.', - (int) cur_byte, - (unsigned) cur_byte); + if ( !edit->utf8 ) { + unsigned char cur_byte = edit_get_byte (edit, edit->curs1); + g_snprintf (byte_str, sizeof (byte_str), "%c %3d 0x%02X", + is_printable (cur_byte) ? cur_byte : '.', + (int) cur_byte, + (unsigned) cur_byte); + } else { + int cw = 1; + unsigned int cur_utf = edit_get_utf (edit, edit->curs1, &cw); + if ( cw > 0 ) { + g_snprintf (byte_str, sizeof (byte_str), "%04d 0x%03X", + (unsigned) cur_utf, + (unsigned) cur_utf); + } + + } } else { strcpy (byte_str, ""); } From 7bcb0b193064eee8b29263c7fbbed98e4ad290c0 Mon Sep 17 00:00:00 2001 From: Ilia Maslakov Date: Wed, 15 Apr 2009 19:07:46 +0000 Subject: [PATCH 10/12] fix: edit_get_prev_utf segfault, add compare str with start of buf --- edit/edit.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/edit/edit.c b/edit/edit.c index faf39db8f..ce02b218d 100644 --- a/edit/edit.c +++ b/edit/edit.c @@ -168,6 +168,7 @@ int edit_get_utf (WEdit * edit, long byte_index, int *char_width) return '\n'; } + str = edit_get_byte_ptr (edit, byte_index); res = g_utf8_get_char_validated (str, -1); @@ -200,28 +201,27 @@ int edit_get_prev_utf (WEdit * edit, long byte_index, int *char_width) gunichar ch; gchar *next_ch = NULL; int width = 0; - gchar *prn_buf=NULL; if ( byte_index > 0 ) { byte_index--; } ch = edit_get_utf (edit, byte_index, &width); - if ( width == 1 ) { *char_width = width; return ch; } if ( byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0 ) { - *char_width = 1; + *char_width = 0; return 0; } str = edit_get_byte_ptr (edit, byte_index); buf = edit_get_buf_ptr (edit, byte_index); /* get prev utf8 char */ - str = g_utf8_find_prev_char (buf, str); + if ( str != buf ) + str = g_utf8_find_prev_char (buf, str); res = g_utf8_get_char_validated (str, -1); if ( res < 0 ) { From 7658246c35f3bf332e395687ca50f1668c6aaba7 Mon Sep 17 00:00:00 2001 From: Ilia Maslakov Date: Wed, 15 Apr 2009 20:08:44 +0000 Subject: [PATCH 11/12] fix: editor status bar --- edit/editdraw.c | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/edit/editdraw.c b/edit/editdraw.c index c0b36ddca..9f28d3d72 100644 --- a/edit/editdraw.c +++ b/edit/editdraw.c @@ -66,6 +66,9 @@ static void status_string (WEdit * edit, char *s, int w) { char byte_str[16]; + unsigned char cur_byte = 0; + unsigned int cur_utf = 0; + int cw = 1; /* * If we are at the end of file, print , @@ -74,28 +77,32 @@ static void status_string (WEdit * edit, char *s, int w) */ if (edit->curs1 < edit->last_byte) { if ( !edit->utf8 ) { - unsigned char cur_byte = edit_get_byte (edit, edit->curs1); - g_snprintf (byte_str, sizeof (byte_str), "%c %3d 0x%02X", - is_printable (cur_byte) ? cur_byte : '.', + cur_byte = edit_get_byte (edit, edit->curs1); + + g_snprintf (byte_str, sizeof (byte_str), "%4d 0x%03X", (int) cur_byte, (unsigned) cur_byte); } else { - int cw = 1; - unsigned int cur_utf = edit_get_utf (edit, edit->curs1, &cw); + cur_utf = edit_get_utf (edit, edit->curs1, &cw); if ( cw > 0 ) { g_snprintf (byte_str, sizeof (byte_str), "%04d 0x%03X", (unsigned) cur_utf, (unsigned) cur_utf); + } else { + cur_utf = edit_get_byte (edit, edit->curs1); + g_snprintf (byte_str, sizeof (byte_str), "%04d 0x%03X", + (int) cur_utf, + (unsigned) cur_utf); } } } else { - strcpy (byte_str, ""); + strcpy (byte_str, " "); } /* The field lengths just prevent the status line from shortening too much */ g_snprintf (s, w, - "[%c%c%c%c] %2ld L:[%3ld+%2ld %3ld/%3ld] *(%-4ld/%4ldb)= %s C:%s", + "[%c%c%c%c] %2ld L:[%3ld+%2ld %3ld/%3ld] *(%-4ld/%4ldb)= %s cp:%s", edit->mark1 != edit->mark2 ? ( column_highlighting ? 'C' : 'B') : '-', edit->modified ? 'M' : '-', edit->macro_i < 0 ? '-' : 'R', @@ -110,7 +117,9 @@ static void status_string (WEdit * edit, char *s, int w) edit->curs1, edit->last_byte, byte_str, - get_codepage_id( source_codepage )); + get_codepage_id ( source_codepage ) + ); + } static inline void From 6771a00b4cc67cb9d8ff48851205160361627880 Mon Sep 17 00:00:00 2001 From: Slava Zanko Date: Thu, 16 Apr 2009 12:34:38 +0300 Subject: [PATCH 12/12] add in core viewer codepage selection from list Author: Ilia Maslakov --- src/view.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/view.c b/src/view.c index 0ec1d0ce6..0f714ca1d 100644 --- a/src/view.c +++ b/src/view.c @@ -3652,12 +3652,13 @@ static void view_cmk_moveto_bottom (void *w, int n) { static void view_select_encoding (WView *view) { - char *enc; + char *enc = NULL; GIConv conv; struct cache_line *line; - enc = input_dialog ("Encoding", "Paste encoding", NULL, ""); + do_select_codepage (); + enc = g_strdup( get_codepage_id ( source_codepage ) ); if (enc != NULL) { conv = str_crt_conv_from (enc); if (conv != (iconv_t)(-1)) { @@ -3667,7 +3668,9 @@ view_select_encoding (WView *view) line = view_offset_to_line (view, view->dpy_start); view_set_first_showed (view, line); } + g_free(enc); } + }