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 2ac6b553c6
11 changed files with 484 additions and 648 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)
@ -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,14 +334,17 @@ 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) {
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 +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);
}
@ -822,4 +726,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
@ -155,57 +155,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;
}

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
@ -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;
}

View File

@ -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,17 +292,18 @@ 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)
int size, GString * buffer)
{
int state = 0;
size_t left;
@ -313,20 +314,30 @@ _str_utf8_vfs_convert_to (str_conv_t coder, const char *string,
errno = 0;
size = (size >= 0) ? size : strlen (string);
if (coder == (iconv_t) (-1)) return ESTR_FAILURE;
if (coder == (iconv_t) (-1))
return ESTR_FAILURE;
iconv (coder, NULL, NULL, NULL, NULL);
start = string;
while (size > 0) {
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);
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) {
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;
@ -341,26 +352,30 @@ _str_utf8_vfs_convert_to (str_conv_t coder, const char *string,
}
}
g_free (composed);
} else {
str_insert_string2 (start, end - start, buffer);
}
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)
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_utf8_vfs_convert_to (coder, string, size, buffer);
buffer->actual[0] = '\0';
}
else
result = str_nconvert (coder, (char *) string, size, buffer);
return result;
}
@ -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;
@ -774,13 +788,14 @@ 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 {
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);
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,35 +931,40 @@ 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) {
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_string_append (fixed, tmp);
g_free (tmp);
}
str_insert_char (end[0], fixed);
g_string_append_c (fixed, end[0]);
start = end + 1;
}
if (start == text) {
if (start == text)
{
result = g_utf8_normalize (text, -1, G_NORMALIZE_ALL);
} else {
if (start[0] != '\0' && start != end) {
}
else
{
if (start[0] != '\0' && start != end)
{
tmp = g_utf8_normalize (start, end - start, G_NORMALIZE_ALL);
str_insert_string (tmp, fixed);
g_string_append (fixed, tmp);
g_free (tmp);
}
result = g_strdup (fixed->data);
result = g_strdup (fixed->str);
}
str_release_buffer (fixed);
g_string_free (fixed, TRUE);
return result;
}
@ -952,41 +972,46 @@ str_utf8_normalize (const char *text)
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) {
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_string_append (fixed, tmp);
g_free (tmp);
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 = g_utf8_normalize (fold, -1, G_NORMALIZE_ALL);
g_free (fold);
} else {
if (start[0] != '\0' && start != end) {
}
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_string_append (fixed, tmp);
g_free (tmp);
g_free (fold);
}
result = g_strdup (fixed->data);
result = g_strdup (fixed->str);
}
str_release_buffer (fixed);
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;
}

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,11 @@ 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 +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

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);
}
/*