Rework strutils for usage GString instread of self-made buffers.

This commit is contained in:
Slava Zanko 2009-04-14 13:29:01 +03:00
parent 21c88729a4
commit dbf8dd1bc2
11 changed files with 1806 additions and 1715 deletions

View File

@ -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);
@ -1100,12 +1100,11 @@ get_random_hint (int force)
/* 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);
}

View File

@ -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;
GIConv conv;
GString *translated_data;
if (translated_data == NULL) translated_data = str_get_buffer ();
translated_data = g_string_new ("");
str_reset_buffer (translated_data);
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

View File

@ -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);
g_string_append_c (tkn_buf, c);
}
return TKN_COMMENT;
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:
@ -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;
}
}

View File

@ -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 ())
@ -94,7 +93,7 @@ info_show_info (struct WInfo *info)
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 */
@ -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)

View File

@ -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,274 +76,194 @@ 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)) {
if (used_class.is_valid_string (string))
{
state = 0;
left = 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--;
if (buffer->remain <= 0) {
str_incrase_buffer (buffer);
if (size < 0)
{
size = strlen (string);
}
else
{
left = strlen (string);
if (left < size)
size = left;
}
left = size;
if (coder == (GIConv) (-1))
return ESTR_FAILURE;
g_iconv (coder, NULL, NULL, NULL, NULL);
while (left)
{
tmp_buff = g_convert_with_iconv ((const gchar *) string,
left,
coder,
&bytes_read,
&bytes_written, &error);
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;
}
buffer->actual[0] = '?';
buffer->actual++;
buffer->remain--;
state = ESTR_PROBLEM;
break;
case E2BIG:
str_incrase_buffer (buffer);
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 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;
}
else
return ESTR_FAILURE;
}
int
str_convert (str_conv_t coder, char *string, struct str_buffer *buffer)
str_convert (GIConv coder, char *string, GString * buffer)
{
int result;
result = _str_convert (coder, string, buffer);
buffer->actual[0] = '\0';
result = _str_convert (coder, string, -1, buffer);
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_nconvert (GIConv coder, char *string, int size, GString * buffer)
{
int result;
if (coder == str_cnv_not_convert) {
str_insert_string (string, buffer);
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_vfs_convert_from (coder, string, buffer);
buffer->actual[0] = '\0';
}
else
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_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)
@ -354,12 +273,18 @@ str_translate_char (str_conv_t conv, char *keys, size_t ch_size,
iconv (conv, NULL, NULL, NULL, NULL);
left = (ch_size == (size_t)(-1)) ? strlen (keys) : ch_size;
left = (ch_size == (size_t) (-1)) ? strlen (keys) : ch_size;
cnv = iconv (conv, &keys, &left, &output, &out_size);
if (cnv == (size_t)(-1)) {
if (errno == EINVAL) return ESTR_PROBLEM; else return ESTR_FAILURE;
} else {
if (cnv == (size_t) (-1))
{
if (errno == EINVAL)
return ESTR_PROBLEM;
else
return ESTR_FAILURE;
}
else
{
output[0] = '\0';
return 0;
}
@ -369,7 +294,7 @@ str_translate_char (str_conv_t conv, char *keys, size_t ch_size,
static const char *
str_detect_termencoding ()
{
return (nl_langinfo(CODESET));
return (nl_langinfo (CODESET));
}
static int
@ -378,8 +303,9 @@ str_test_encoding_class (const char *encoding, const char **table)
int t;
int result = 0;
for (t = 0; table[t] != NULL; t++) {
result+= (g_ascii_strncasecmp (encoding, table[t],
for (t = 0; table[t] != NULL; t++)
{
result += (g_ascii_strncasecmp (encoding, table[t],
strlen (table[t])) == 0);
}
@ -389,11 +315,16 @@ str_test_encoding_class (const char *encoding, const char **table)
static void
str_choose_str_functions ()
{
if (str_test_encoding_class (codeset, str_utf8_encodings)) {
if (str_test_encoding_class (codeset, str_utf8_encodings))
{
used_class = str_utf8_init ();
} else if (str_test_encoding_class (codeset, str_8bit_encodings)) {
}
else if (str_test_encoding_class (codeset, str_8bit_encodings))
{
used_class = str_8bit_init ();
} else {
}
else
{
used_class = str_ascii_init ();
}
}
@ -402,7 +333,8 @@ int
str_isutf8 (char *codeset_name)
{
int result = 0;
if (str_test_encoding_class (codeset_name, str_utf8_encodings)) {
if (str_test_encoding_class (codeset_name, str_utf8_encodings))
{
result = 1;
}
return result;
@ -412,18 +344,20 @@ void
str_init_strings (const char *termenc)
{
codeset = g_strdup ((termenc != NULL)
? termenc
: str_detect_termencoding ());
? termenc : str_detect_termencoding ());
str_cnv_not_convert = iconv_open (codeset, codeset);
if (str_cnv_not_convert == INVALID_CONV) {
if (termenc != NULL) {
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 (str_cnv_not_convert == INVALID_CONV)
{
g_free (codeset);
codeset = g_strdup ("ascii");
str_cnv_not_convert = iconv_open (codeset, codeset);
@ -436,26 +370,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);
}
@ -493,7 +410,7 @@ char *
str_get_next_char (char *text)
{
used_class.cnext_char ((const char **)&text);
used_class.cnext_char ((const char **) &text);
return text;
}
@ -643,25 +560,25 @@ str_term_char_width (const char *text)
}
int
str_offset_to_pos (const char* text, size_t length)
str_offset_to_pos (const char *text, size_t length)
{
return used_class.offset_to_pos (text, length);
}
int
str_length (const char* text)
str_length (const char *text)
{
return used_class.length (text);
}
int
str_length2 (const char* text, int size)
str_length2 (const char *text, int size)
{
return used_class.length2 (text, size);
}
int
str_length_noncomb (const char* text)
str_length_noncomb (const char *text)
{
return used_class.length_noncomb (text);
}
@ -697,13 +614,13 @@ str_isdigit (const char *ch)
}
int
str_toupper (const char *ch, char **out, size_t *remain)
str_toupper (const char *ch, char **out, size_t * remain)
{
return used_class.toupper (ch, out, remain);
}
int
str_tolower (const char *ch, char **out, size_t *remain)
str_tolower (const char *ch, char **out, size_t * remain)
{
return used_class.tolower (ch, out, remain);
}
@ -822,4 +739,3 @@ str_release_key (char *key, int case_sen)
{
used_class.release_key (key, case_sen);
}

View File

@ -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,84 +133,51 @@ 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);
/* rewind "count" characters buffer back
*/
void str_backward_buffer (struct str_buffer *buffer, int count);
void str_insert_replace_char (GString *);
/* init strings and set terminal encoding,
* if is termenc NULL, detect terminal encoding

View File

@ -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
@ -72,19 +72,25 @@ str_8bit_cprev_char (const char **text)
static int
str_8bit_cnext_noncomb_char (const char **text)
{
if (*text[0] != '\0') {
if (*text[0] != '\0')
{
(*text)++;
return 1;
} else return 0;
}
else
return 0;
}
static int
str_8bit_cprev_noncomb_char (const char **text, const char *begin)
{
if ((*text) != begin) {
if ((*text) != begin)
{
(*text)--;
return 1;
} else return 0;
}
else
return 0;
}
static int
@ -124,9 +130,10 @@ str_8bit_iscombiningmark (const char *text)
}
static int
str_8bit_toupper (const char *text, char **out, size_t *remain)
str_8bit_toupper (const char *text, char **out, size_t * remain)
{
if (*remain <= 1) return 0;
if (*remain <= 1)
return 0;
(*out)[0] = toupper ((unsigned char) text[0]);
(*out)++;
(*remain)--;
@ -134,9 +141,10 @@ str_8bit_toupper (const char *text, char **out, size_t *remain)
}
static int
str_8bit_tolower (const char *text, char **out, size_t *remain)
str_8bit_tolower (const char *text, char **out, size_t * remain)
{
if (*remain <= 1) return 0;
if (*remain <= 1)
return 0;
(*out)[0] = tolower ((unsigned char) text[0]);
(*out)++;
(*remain)--;
@ -155,57 +163,19 @@ 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)
int size, GString * buffer)
{
int result;
if (coder == str_cnv_not_convert) {
str_insert_string2 (string, size, buffer);
if (coder == str_cnv_not_convert)
{
g_string_append_len (buffer, string, size);
result = 0;
} else result = _str_8bit_vfs_convert_to (coder, (char*)string, size, buffer);
buffer->actual[0] = '\0';
}
else
result = str_nconvert (coder, (char *) string, size, buffer);
return result;
}
@ -224,7 +194,8 @@ str_8bit_term_form (const char *text)
remain = sizeof (result);
length = strlen (text);
for (; pos < length && remain > 1; pos++, actual++, remain--) {
for (; pos < length && remain > 1; pos++, actual++, remain--)
{
actual[0] = isprint (text[pos]) ? text[pos] : '.';
}
@ -244,11 +215,13 @@ str_8bit_fit_to_term (const char *text, int width, int just_mode)
length = strlen (text);
actual = result;
remain = sizeof(result);
remain = sizeof (result);
if (length <= width) {
if (length <= width)
{
ident = 0;
switch (HIDE_FIT (just_mode)) {
switch (HIDE_FIT (just_mode))
{
case J_CENTER_LEFT:
case J_CENTER:
ident = (width - length) / 2;
@ -258,41 +231,54 @@ str_8bit_fit_to_term (const char *text, int width, int just_mode)
break;
}
if (remain <= ident) goto finally;
if (remain <= ident)
goto finally;
memset (actual, ' ', ident);
actual+= ident;
remain-= ident;
actual += ident;
remain -= ident;
for (; pos < length && remain > 1; pos++, actual++, remain--) {
for (; pos < length && remain > 1; pos++, actual++, remain--)
{
actual[0] = isprint (text[pos]) ? text[pos] : '.';
}
if (width - length - ident > 0) {
if (remain <= width - length - ident) goto finally;
if (width - length - ident > 0)
{
if (remain <= width - length - ident)
goto finally;
memset (actual, ' ', width - length - ident);
actual+= width - length - ident;
remain-= width - length - ident;
actual += width - length - ident;
remain -= width - length - ident;
}
} else {
if (IS_FIT (just_mode)) {
}
else
{
if (IS_FIT (just_mode))
{
for (; pos + 1 <= width / 2 && remain > 1;
actual++, pos++, remain--) {
actual++, pos++, remain--)
{
actual[0] = isprint (text[pos]) ? text[pos] : '.';
}
if (remain <= 1) goto finally;
if (remain <= 1)
goto finally;
actual[0] = '~';
actual++;
remain--;
pos+= length - width + 1;
pos += length - width + 1;
for (; pos < length && remain > 1; pos++, actual++, remain--) {
for (; pos < length && remain > 1; pos++, actual++, remain--)
{
actual[0] = isprint (text[pos]) ? text[pos] : '.';
}
} else {
}
else
{
ident = 0;
switch (HIDE_FIT (just_mode)) {
switch (HIDE_FIT (just_mode))
{
case J_CENTER:
ident = (length - width) / 2;
break;
@ -301,9 +287,10 @@ str_8bit_fit_to_term (const char *text, int width, int just_mode)
break;
}
pos+= ident;
pos += ident;
for (; pos < ident + width && remain > 1;
pos++, actual++, remain--) {
pos++, actual++, remain--)
{
actual[0] = isprint (text[pos]) ? text[pos] : '.';
}
@ -328,24 +315,32 @@ str_8bit_term_trim (const char *text, int width)
actual = result;
remain = sizeof (result);
if (width < length) {
if (width <= 3) {
if (width < length)
{
if (width <= 3)
{
memset (actual, '.', width);
actual+= width;
remain-= width;
} else {
actual += width;
remain -= width;
}
else
{
memset (actual, '.', 3);
actual+= 3;
remain-= 3;
actual += 3;
remain -= 3;
pos+= length - width + 3;
pos += length - width + 3;
for (; pos < length && remain > 1; pos++, actual++, remain--) {
for (; pos < length && remain > 1; pos++, actual++, remain--)
{
actual[0] = isprint (text[pos]) ? text[pos] : '.';
}
}
} else {
for (; pos < length && remain > 1; pos++, actual++, remain--) {
}
else
{
for (; pos < length && remain > 1; pos++, actual++, remain--)
{
actual[0] = isprint (text[pos]) ? text[pos] : '.';
}
}
@ -357,15 +352,14 @@ str_8bit_term_trim (const char *text, int width)
static int
str_8bit_term_width2 (const char *text, size_t length)
{
return (length != (size_t)(-1))
? min (strlen (text), length)
: strlen (text);
return (length != (size_t) (-1))
? min (strlen (text), length) : strlen (text);
}
static int
str_8bit_term_width1 (const char *text)
{
return str_8bit_term_width2 (text, (size_t)(-1));
return str_8bit_term_width2 (text, (size_t) (-1));
}
static int
@ -386,15 +380,18 @@ str_8bit_msg_term_size (const char *text, int *lines, int *columns)
int width;
p = tmp;
for (;;) {
for (;;)
{
q = strchr (p, '\n');
if (q != NULL) {
if (q != NULL)
{
c = q[0];
q[0] = '\0';
}
width = str_8bit_term_width1 (p);
if (width > (*columns)) (*columns) = width;
if (width > (*columns))
(*columns) = width;
if (q == NULL)
break;
@ -418,16 +415,19 @@ str_8bit_term_substring (const char *text, int start, int width)
remain = sizeof (result);
length = strlen (text);
if (start < length) {
pos+= start;
if (start < length)
{
pos += start;
for (; pos < length && width > 0 && remain > 1;
pos++, width--, actual++, remain--) {
pos++, width--, actual++, remain--)
{
actual[0] = isprint (text[pos]) ? text[pos] : '.';
}
}
for (; width > 0 && remain > 1; actual++, remain--, width--) {
for (; width > 0 && remain > 1; actual++, remain--, width--)
{
actual[0] = ' ';
}
@ -448,23 +448,30 @@ str_8bit_trunc (const char *text, int width)
remain = sizeof (result);
length = strlen (text);
if (length > width) {
for (; pos + 1 <= width / 2 && remain > 1; actual++, pos++, remain--) {
if (length > width)
{
for (; pos + 1 <= width / 2 && remain > 1; actual++, pos++, remain--)
{
actual[0] = isprint (text[pos]) ? text[pos] : '.';
}
if (remain <= 1) goto finally;
if (remain <= 1)
goto finally;
actual[0] = '~';
actual++;
remain--;
pos+= length - width + 1;
pos += length - width + 1;
for (; pos < length && remain > 1; pos++, actual++, remain--) {
for (; pos < length && remain > 1; pos++, actual++, remain--)
{
actual[0] = isprint (text[pos]) ? text[pos] : '.';
}
} else {
for (; pos < length && remain > 1; pos++, actual++, remain--) {
}
else
{
for (; pos < length && remain > 1; pos++, actual++, remain--)
{
actual[0] = isprint (text[pos]) ? text[pos] : '.';
}
}
@ -477,19 +484,19 @@ str_8bit_trunc (const char *text, int width)
static int
str_8bit_offset_to_pos (const char *text, size_t length)
{
return (int)length;
return (int) length;
}
static int
str_8bit_column_to_pos (const char *text, size_t pos)
{
return (int)pos;
return (int) pos;
}
static char *
str_8bit_create_search_needle (const char *needle, int case_sen)
{
return (char*) needle;
return (char *) needle;
}
static void
@ -505,16 +512,18 @@ str_8bit_search_first (const char *text, const char *search, int case_sen)
const char *match;
size_t offsset;
fold_text = (case_sen) ? (char*) text : g_strdown (g_strdup (text));
fold_search = (case_sen) ? (char*) text : g_strdown (g_strdup (search));
fold_text = (case_sen) ? (char *) text : g_strdown (g_strdup (text));
fold_search = (case_sen) ? (char *) text : g_strdown (g_strdup (search));
match = g_strstr_len (fold_text, -1, fold_search);
if (match != NULL) {
if (match != NULL)
{
offsset = match - fold_text;
match = text + offsset;
}
if (!case_sen) {
if (!case_sen)
{
g_free (fold_text);
g_free (fold_search);
}
@ -530,16 +539,18 @@ str_8bit_search_last (const char *text, const char *search, int case_sen)
const char *match;
size_t offsset;
fold_text = (case_sen) ? (char*) text : g_strdown (g_strdup (text));
fold_search = (case_sen) ? (char*) text : g_strdown (g_strdup (search));
fold_text = (case_sen) ? (char *) text : g_strdown (g_strdup (text));
fold_search = (case_sen) ? (char *) text : g_strdown (g_strdup (search));
match = g_strrstr_len (fold_text, -1, fold_search);
if (match != NULL) {
if (match != NULL)
{
offsset = match - fold_text;
match = text + offsset;
}
if (!case_sen) {
if (!case_sen)
{
g_free (fold_text);
g_free (fold_search);
}
@ -585,8 +596,7 @@ str_8bit_caseprefix (const char *text, const char *prefix)
{
int result;
for (result = 0; text[result] != '\0' && prefix[result] != '\0'
&& toupper (text[result]) == toupper (prefix[result]);
result++);
&& toupper (text[result]) == toupper (prefix[result]); result++);
return result;
}
@ -600,20 +610,23 @@ str_8bit_fix_string (char *text)
static char *
str_8bit_create_key (const char *text, int case_sen)
{
return (case_sen) ? (char*)text : g_strdown (g_strdup (text));
return (case_sen) ? (char *) text : g_strdown (g_strdup (text));
}
static int
str_8bit_key_collate (const char *t1, const char *t2, int case_sen)
{
if (case_sen) return strcmp (t1, t2);
else return strcoll (t1, t2);
if (case_sen)
return strcmp (t1, t2);
else
return strcoll (t1, t2);
}
static void
str_8bit_release_key (char *key, int case_sen)
{
if (!case_sen) g_free (key);
if (!case_sen)
g_free (key);
}
struct str_class

View File

@ -38,9 +38,9 @@
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
@ -70,19 +70,25 @@ str_ascii_cprev_char (const char **text)
static int
str_ascii_cnext_noncomb_char (const char **text)
{
if (*text[0] != '\0') {
if (*text[0] != '\0')
{
(*text)++;
return 1;
} else return 0;
}
else
return 0;
}
static int
str_ascii_cprev_noncomb_char (const char **text, const char *begin)
{
if ((*text) != begin) {
if ((*text) != begin)
{
(*text)--;
return 1;
} else return 0;
}
else
return 0;
}
static int
@ -122,9 +128,10 @@ str_ascii_iscombiningmark (const char *text)
}
static int
str_ascii_toupper (const char *text, char **out, size_t *remain)
str_ascii_toupper (const char *text, char **out, size_t * remain)
{
if (*remain <= 1) return 0;
if (*remain <= 1)
return 0;
(*out)[0] = (char) g_ascii_toupper ((gchar) text[0]);
(*out)++;
(*remain)--;
@ -132,9 +139,10 @@ str_ascii_toupper (const char *text, char **out, size_t *remain)
}
static int
str_ascii_tolower (const char *text, char **out, size_t *remain)
str_ascii_tolower (const char *text, char **out, size_t * remain)
{
if (*remain <= 1) return 0;
if (*remain <= 1)
return 0;
(*out)[0] = (char) g_ascii_tolower ((gchar) text[0]);
(*out)++;
(*remain)--;
@ -154,10 +162,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;
}
@ -176,8 +184,9 @@ str_ascii_term_form (const char *text)
length = strlen (text);
/* go throw all characters and check, if they are ascii and printable */
for (; pos < length && remain > 1; pos++, actual++, remain--) {
actual[0] = isascii((unsigned char)text[pos]) ? text[pos] : '?';
for (; pos < length && remain > 1; pos++, actual++, remain--)
{
actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
}
@ -197,11 +206,13 @@ str_ascii_fit_to_term (const char *text, int width, int just_mode)
length = strlen (text);
actual = result;
remain = sizeof(result);
remain = sizeof (result);
if (length <= width) {
if (length <= width)
{
ident = 0;
switch (HIDE_FIT (just_mode)) {
switch (HIDE_FIT (just_mode))
{
case J_CENTER_LEFT:
case J_CENTER:
ident = (width - length) / 2;
@ -212,52 +223,65 @@ str_ascii_fit_to_term (const char *text, int width, int just_mode)
}
/* add space before text */
if (remain <= ident) goto finally;
if (remain <= ident)
goto finally;
memset (actual, ' ', ident);
actual+= ident;
remain-= ident;
actual += ident;
remain -= ident;
/* copy all characters */
for (; pos < length && remain > 1; pos++, actual++, remain--) {
actual[0] = isascii((unsigned char)text[pos]) ? text[pos] : '?';
for (; pos < length && remain > 1; pos++, actual++, remain--)
{
actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
}
/* add space after text */
if (width - length - ident > 0) {
if (remain <= width - length - ident) goto finally;
if (width - length - ident > 0)
{
if (remain <= width - length - ident)
goto finally;
memset (actual, ' ', width - length - ident);
actual+= width - length - ident;
remain-= width - length - ident;
actual += width - length - ident;
remain -= width - length - ident;
}
} else {
if (IS_FIT (just_mode)) {
}
else
{
if (IS_FIT (just_mode))
{
/* copy prefix of text, that is not wider than width / 2 */
for (; pos + 1 <= width / 2 && remain > 1;
actual++, pos++, remain--) {
actual[0] = isascii((unsigned char)text[pos])
actual++, pos++, remain--)
{
actual[0] = isascii ((unsigned char) text[pos])
? text[pos] : '?';
actual[0] = g_ascii_isprint ((gchar) actual[0])
? actual[0] : '.';
}
if (remain <= 1) goto finally;
if (remain <= 1)
goto finally;
actual[0] = '~';
actual++;
remain--;
pos+= length - width + 1;
pos += length - width + 1;
/* copy suffix of text */
for (; pos < length && remain > 1; pos++, actual++, remain--) {
actual[0] = isascii((unsigned char)text[pos])
for (; pos < length && remain > 1; pos++, actual++, remain--)
{
actual[0] = isascii ((unsigned char) text[pos])
? text[pos] : '?';
actual[0] = g_ascii_isprint ((gchar) actual[0])
? actual[0] : '.';
}
} else {
}
else
{
ident = 0;
switch (HIDE_FIT (just_mode)) {
switch (HIDE_FIT (just_mode))
{
case J_CENTER:
ident = (length - width) / 2;
break;
@ -268,10 +292,11 @@ str_ascii_fit_to_term (const char *text, int width, int just_mode)
/* copy substring text, substring start from ident and take width
* characters from text */
pos+= ident;
pos += ident;
for (; pos < ident + width && remain > 1;
pos++, actual++, remain--) {
actual[0] = isascii((unsigned char)text[pos])
pos++, actual++, remain--)
{
actual[0] = isascii ((unsigned char) text[pos])
? text[pos] : '?';
actual[0] = g_ascii_isprint ((gchar) actual[0])
? actual[0] : '.';
@ -297,30 +322,38 @@ str_ascii_term_trim (const char *text, int width)
actual = result;
remain = sizeof (result);
if (width < length) {
if (width <= 3) {
if (width < length)
{
if (width <= 3)
{
memset (actual, '.', width);
actual+= width;
remain-= width;
} else {
actual += width;
remain -= width;
}
else
{
memset (actual, '.', 3);
actual+= 3;
remain-= 3;
actual += 3;
remain -= 3;
pos+= length - width + 3;
pos += length - width + 3;
/* copy suffix of text*/
for (; pos < length && remain > 1; pos++, actual++, remain--) {
actual[0] = isascii((unsigned char)text[pos])
/* copy suffix of text */
for (; pos < length && remain > 1; pos++, actual++, remain--)
{
actual[0] = isascii ((unsigned char) text[pos])
? text[pos] : '?';
actual[0] = g_ascii_isprint ((gchar) actual[0])
? actual[0] : '.';
}
}
} else {
}
else
{
/* copy all characters */
for (; pos < length && remain > 1; pos++, actual++, remain--) {
actual[0] = isascii((unsigned char)text[pos]) ? text[pos] : '?';
for (; pos < length && remain > 1; pos++, actual++, remain--)
{
actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
}
}
@ -332,15 +365,14 @@ str_ascii_term_trim (const char *text, int width)
static int
str_ascii_term_width2 (const char *text, size_t length)
{
return (length != (size_t)(-1))
? min (strlen (text), length)
: strlen (text);
return (length != (size_t) (-1))
? min (strlen (text), length) : strlen (text);
}
static int
str_ascii_term_width1 (const char *text)
{
return str_ascii_term_width2 (text, (size_t)(-1));
return str_ascii_term_width2 (text, (size_t) (-1));
}
static int
@ -361,15 +393,18 @@ str_ascii_msg_term_size (const char *text, int *lines, int *columns)
int width;
p = tmp;
for (;;) {
for (;;)
{
q = strchr (p, '\n');
if (q != NULL) {
if (q != NULL)
{
c = q[0];
q[0] = '\0';
}
width = str_ascii_term_width1 (p);
if (width > (*columns)) (*columns) = width;
if (width > (*columns))
(*columns) = width;
if (q == NULL)
break;
@ -393,19 +428,22 @@ str_ascii_term_substring (const char *text, int start, int width)
remain = sizeof (result);
length = strlen (text);
if (start < length) {
pos+= start;
if (start < length)
{
pos += start;
/* copy at most width characters from text from start */
for (; pos < length && width > 0 && remain > 1;
pos++, width--, actual++, remain--) {
pos++, width--, actual++, remain--)
{
actual[0] = isascii((unsigned char)text[pos]) ? text[pos] : '?';
actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
}
}
/* if text is shorter then width, add space to the end */
for (; width > 0 && remain > 1; actual++, remain--, width--) {
for (; width > 0 && remain > 1; actual++, remain--, width--)
{
actual[0] = ' ';
}
@ -426,29 +464,36 @@ str_ascii_trunc (const char *text, int width)
remain = sizeof (result);
length = strlen (text);
if (length > width) {
if (length > width)
{
/* copy prefix of text */
for (; pos + 1 <= width / 2 && remain > 1; actual++, pos++, remain--) {
actual[0] = isascii((unsigned char)text[pos]) ? text[pos] : '?';
for (; pos + 1 <= width / 2 && remain > 1; actual++, pos++, remain--)
{
actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
}
if (remain <= 1) goto finally;
if (remain <= 1)
goto finally;
actual[0] = '~';
actual++;
remain--;
pos+= length - width + 1;
pos += length - width + 1;
/* copy suffix of text */
for (; pos < length && remain > 1; pos++, actual++, remain--) {
actual[0] = isascii((unsigned char)text[pos]) ? text[pos] : '?';
for (; pos < length && remain > 1; pos++, actual++, remain--)
{
actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
}
} else {
}
else
{
/* copy all characters */
for (; pos < length && remain > 1; pos++, actual++, remain--) {
actual[0] = isascii((unsigned char)text[pos]) ? text[pos] : '?';
for (; pos < length && remain > 1; pos++, actual++, remain--)
{
actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
}
}
@ -461,19 +506,19 @@ str_ascii_trunc (const char *text, int width)
static int
str_ascii_offset_to_pos (const char *text, size_t length)
{
return (int)length;
return (int) length;
}
static int
str_ascii_column_to_pos (const char *text, size_t pos)
{
return (int)pos;
return (int) pos;
}
static char *
str_ascii_create_search_needle (const char *needle, int case_sen)
{
return (char*) needle;
return (char *) needle;
}
static void
@ -489,16 +534,18 @@ str_ascii_search_first (const char *text, const char *search, int case_sen)
const char *match;
size_t offset;
fold_text = (case_sen) ? (char*) text : g_ascii_strdown (text, -1);
fold_search = (case_sen) ? (char*) search : g_ascii_strdown (search, -1);
fold_text = (case_sen) ? (char *) text : g_ascii_strdown (text, -1);
fold_search = (case_sen) ? (char *) search : g_ascii_strdown (search, -1);
match = g_strstr_len (fold_text, -1, fold_search);
if (match != NULL) {
if (match != NULL)
{
offset = match - fold_text;
match = text + offset;
}
if (!case_sen) {
if (!case_sen)
{
g_free (fold_text);
g_free (fold_search);
}
@ -514,16 +561,18 @@ str_ascii_search_last (const char *text, const char *search, int case_sen)
const char *match;
size_t offset;
fold_text = (case_sen) ? (char*) text : g_ascii_strdown (text, -1);
fold_search = (case_sen) ? (char*) search : g_ascii_strdown (search, -1);
fold_text = (case_sen) ? (char *) text : g_ascii_strdown (text, -1);
fold_search = (case_sen) ? (char *) search : g_ascii_strdown (search, -1);
match = g_strrstr_len (fold_text, -1, fold_search);
if (match != NULL) {
if (match != NULL)
{
offset = match - fold_text;
match = text + offset;
}
if (!case_sen) {
if (!case_sen)
{
g_free (fold_text);
g_free (fold_search);
}
@ -540,7 +589,7 @@ str_ascii_compare (const char *t1, const char *t2)
static int
str_ascii_ncompare (const char *t1, const char *t2)
{
return strncmp (t1, t2, min (strlen (t1), strlen(t2)));
return strncmp (t1, t2, min (strlen (t1), strlen (t2)));
}
static int
@ -558,15 +607,16 @@ str_ascii_ncasecmp (const char *t1, const char *t2)
static void
str_ascii_fix_string (char *text)
{
for (; text[0] != '\0'; text++) {
text[0] = ((unsigned char)text[0] < 128) ? text[0] : '?';
for (; text[0] != '\0'; text++)
{
text[0] = ((unsigned char) text[0] < 128) ? text[0] : '?';
}
}
static char *
str_ascii_create_key (const char *text, int case_sen)
{
return (char*)text;
return (char *) text;
}
static int
@ -594,8 +644,8 @@ str_ascii_caseprefix (const char *text, const char *prefix)
{
int result;
for (result = 0; text[result] != '\0' && prefix[result] != '\0'
&& g_ascii_toupper (text[result]) == g_ascii_toupper (prefix[result]);
result++);
&& g_ascii_toupper (text[result]) ==
g_ascii_toupper (prefix[result]); result++);
return result;
}

File diff suppressed because it is too large Load Diff

View File

@ -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,18 +2458,18 @@ 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)) {
@ -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,7 +2766,7 @@ 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 {
@ -2780,11 +2780,12 @@ view_get_line_at (WView *view, offset_type from, struct str_buffer * buffer,
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;
@ -2981,7 +2982,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 +2997,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 +3023,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 +3059,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

View File

@ -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",
@ -383,7 +383,7 @@ vfs_supported_enconding (const char *encoding) {
*/
static int
_vfs_translate_path (const char *path, int size,
str_conv_t defcnv, struct str_buffer *buffer)
str_conv_t defcnv, GString *buffer)
{
const char *semi;
const char *ps;
@ -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;
@ -460,11 +460,11 @@ 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);
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() */
@ -1219,7 +1219,7 @@ vfs_shut (void)
g_slist_free (vfs_openfiles);
str_release_buffer (vfs_str_buffer);
g_string_free (vfs_str_buffer, TRUE);
}
/*