src/strutil.h: created estr_t type for result of charset convertion functions.

Modified descriptions.

src/strutil.c (_str_convert): return value of estr_t type instead of int.
Check coder first. Removed redundant code.
(str_convert): simplified. Changed returned type to estr_t.
(str_nconvet): likewise.
(str_vfs_convert_to): likewise.
(str_vfs_convert_from): likewise. Used symbolic names instead of hardcoded values.
(str_translate_char): likewise.

src/strutil8bit.c (str_8bit_vfs_convert_to): likewise. Made function static.

src/strutilascii.c (str_ascii_vfs_convert_to): likewise.

src/strutilutf8.c: (str_utf8_vfs_convert_to): Used symbolic names
instead of hardcoded values.

src/charsets.c (convert_from_utf_to_current): likewise.
(convert_from_utf_to_current_c): likewise.
(convert_from_8bit_to_utf_c): likewise.
(convert_from_8bit_to_utf_c2): likewise.

src/view.c (view_get_char): likewise.

vfs/vfs.c (_vfs_translate_path): Changed returned type to estr_t.
Used symbolic names instead of hardcoded values.
(vfs_translate_path): likewise.
This commit is contained in:
Andrew Borodin 2009-04-22 20:35:32 +04:00
parent 2ad5d2e919
commit dc8150a521
8 changed files with 78 additions and 79 deletions

View File

@ -324,11 +324,11 @@ convert_from_utf_to_current (const char *str)
if (conv != INVALID_CONV) { if (conv != INVALID_CONV) {
switch (str_translate_char (conv, str, -1, buf_ch, sizeof(buf_ch))) { switch (str_translate_char (conv, str, -1, buf_ch, sizeof(buf_ch))) {
case 0: case ESTR_SUCCESS:
ch = buf_ch[0]; ch = buf_ch[0];
break; break;
case 1: case ESTR_PROBLEM:
case 2: case ESTR_FAILURE:
ch = '.'; ch = '.';
break; break;
} }
@ -360,11 +360,11 @@ convert_from_utf_to_current_c (const int input_char)
if (conv != INVALID_CONV) { if (conv != INVALID_CONV) {
switch (str_translate_char (conv, str, -1, buf_ch, sizeof(buf_ch))) { switch (str_translate_char (conv, str, -1, buf_ch, sizeof(buf_ch))) {
case 0: case ESTR_SUCCESS:
ch = buf_ch[0]; ch = buf_ch[0];
break; break;
case 1: case ESTR_PROBLEM:
case 2: case ESTR_FAILURE:
ch = '.'; ch = '.';
break; break;
} }
@ -390,7 +390,7 @@ convert_from_8bit_to_utf_c (const char input_char)
if (conv != INVALID_CONV) { if (conv != INVALID_CONV) {
switch (str_translate_char (conv, str, -1, buf_ch, sizeof(buf_ch))) { switch (str_translate_char (conv, str, -1, buf_ch, sizeof(buf_ch))) {
case 0: case ESTR_SUCCESS:
res = g_utf8_get_char_validated (buf_ch, -1); res = g_utf8_get_char_validated (buf_ch, -1);
if ( res < 0 ) { if ( res < 0 ) {
ch = buf_ch[0]; ch = buf_ch[0];
@ -398,8 +398,8 @@ convert_from_8bit_to_utf_c (const char input_char)
ch = res; ch = res;
} }
break; break;
case 1: case ESTR_PROBLEM:
case 2: case ESTR_FAILURE:
ch = '.'; ch = '.';
break; break;
} }
@ -426,7 +426,7 @@ convert_from_8bit_to_utf_c2 (const char input_char)
if (conv != INVALID_CONV) { if (conv != INVALID_CONV) {
switch (str_translate_char (conv, str, -1, buf_ch, sizeof(buf_ch))) { switch (str_translate_char (conv, str, -1, buf_ch, sizeof(buf_ch))) {
case 0: case ESTR_SUCCESS:
res = g_utf8_get_char_validated (buf_ch, -1); res = g_utf8_get_char_validated (buf_ch, -1);
if ( res < 0 ) { if ( res < 0 ) {
ch = buf_ch[0]; ch = buf_ch[0];
@ -434,8 +434,8 @@ convert_from_8bit_to_utf_c2 (const char input_char)
ch = res; ch = res;
} }
break; break;
case 1: case ESTR_PROBLEM:
case 2: case ESTR_FAILURE:
ch = '.'; ch = '.';
break; break;
} }

View File

@ -97,16 +97,19 @@ str_close_conv (GIConv conv)
g_iconv_close (conv); g_iconv_close (conv);
} }
static int static estr_t
_str_convert (GIConv coder, char *string, int size, GString * buffer) _str_convert (GIConv coder, char *string, int size, GString * buffer)
{ {
int state; estr_t state = ESTR_SUCCESS;
gchar *tmp_buff = NULL; gchar *tmp_buff = NULL;
gssize left; gssize left;
gsize bytes_read, bytes_written; gsize bytes_read, bytes_written;
GError *error = NULL; GError *error = NULL;
errno = 0; errno = 0;
if (coder == INVALID_CONV)
return ESTR_FAILURE;
if (string == NULL || buffer == NULL) if (string == NULL || buffer == NULL)
return ESTR_FAILURE; return ESTR_FAILURE;
@ -116,7 +119,6 @@ _str_convert (GIConv coder, char *string, int size, GString * buffer)
return ESTR_FAILURE; return ESTR_FAILURE;
} }
*/ */
state = 0;
if (size < 0) if (size < 0)
{ {
size = strlen (string); size = strlen (string);
@ -129,10 +131,6 @@ _str_convert (GIConv coder, char *string, int size, GString * buffer)
} }
left = size; left = size;
if (coder == (GIConv) (-1))
return ESTR_FAILURE;
g_iconv (coder, NULL, NULL, NULL, NULL); g_iconv (coder, NULL, NULL, NULL, NULL);
while (left) while (left)
@ -154,7 +152,7 @@ _str_convert (GIConv coder, char *string, int size, GString * buffer)
g_error_free (error); g_error_free (error);
error = NULL; error = NULL;
return ESTR_FAILURE; return ESTR_FAILURE;
break;
case G_CONVERT_ERROR_ILLEGAL_SEQUENCE: case G_CONVERT_ERROR_ILLEGAL_SEQUENCE:
/* Invalid byte sequence in conversion input. */ /* Invalid byte sequence in conversion input. */
if (tmp_buff){ if (tmp_buff){
@ -176,6 +174,7 @@ _str_convert (GIConv coder, char *string, int size, GString * buffer)
} }
state = ESTR_PROBLEM; state = ESTR_PROBLEM;
break; break;
case G_CONVERT_ERROR_PARTIAL_INPUT: case G_CONVERT_ERROR_PARTIAL_INPUT:
/* Partial character sequence at end of input. */ /* Partial character sequence at end of input. */
g_error_free (error); g_error_free (error);
@ -190,7 +189,7 @@ _str_convert (GIConv coder, char *string, int size, GString * buffer)
g_free (tmp_buff); g_free (tmp_buff);
} }
return ESTR_PROBLEM; return ESTR_PROBLEM;
break;
case G_CONVERT_ERROR_BAD_URI: /* Don't know how handle this error :( */ 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_NOT_ABSOLUTE_PATH: /* Don't know how handle this error :( */
case G_CONVERT_ERROR_FAILED: /* Conversion failed for some reason. */ case G_CONVERT_ERROR_FAILED: /* Conversion failed for some reason. */
@ -234,35 +233,27 @@ _str_convert (GIConv coder, char *string, int size, GString * buffer)
return state; return state;
} }
int estr_t
str_convert (GIConv coder, char *string, GString * buffer) str_convert (GIConv coder, char *string, GString * buffer)
{ {
int result; return _str_convert (coder, string, -1, buffer);
result = _str_convert (coder, string, -1, buffer);
return result;
} }
int estr_t
str_nconvert (GIConv coder, char *string, int size, GString * buffer) str_nconvert (GIConv coder, char *string, int size, GString * buffer)
{ {
int result; return _str_convert (coder, string, size, buffer);
result = _str_convert (coder, string, size, buffer);
return result;
} }
int estr_t
str_vfs_convert_from (GIConv coder, char *string, GString * buffer) str_vfs_convert_from (GIConv coder, char *string, GString * buffer)
{ {
int result; estr_t result;
if (coder == str_cnv_not_convert) if (coder == str_cnv_not_convert)
{ {
g_string_append (buffer, (string)?string:""); g_string_append (buffer, string != NULL ? string : "");
result = 0; result = ESTR_SUCCESS;
} }
else else
result = _str_convert (coder, string, -1, buffer); result = _str_convert (coder, string, -1, buffer);
@ -270,7 +261,7 @@ str_vfs_convert_from (GIConv coder, char *string, GString * buffer)
return result; return result;
} }
int estr_t
str_vfs_convert_to (GIConv coder, const char *string, int size, str_vfs_convert_to (GIConv coder, const char *string, int size,
GString * buffer) GString * buffer)
{ {
@ -301,7 +292,7 @@ str_insert_replace_char (GString * buffer)
used_class.insert_replace_char (buffer); used_class.insert_replace_char (buffer);
} }
int estr_t
str_translate_char (GIConv conv, const char *keys, size_t ch_size, str_translate_char (GIConv conv, const char *keys, size_t ch_size,
char *output, size_t out_size) char *output, size_t out_size)
{ {
@ -314,10 +305,10 @@ str_translate_char (GIConv conv, const char *keys, size_t ch_size,
cnv = g_iconv (conv, (gchar **) &keys, &left, &output, &out_size); cnv = g_iconv (conv, (gchar **) &keys, &left, &output, &out_size);
if (cnv == (size_t)(-1)) { if (cnv == (size_t)(-1)) {
if (errno == EINVAL) return ESTR_PROBLEM; else return ESTR_FAILURE; return (errno == EINVAL) ? ESTR_PROBLEM : ESTR_FAILURE;
} else { } else {
output[0] = '\0'; output[0] = '\0';
return 0; return ESTR_SUCCESS;
} }
} }

View File

@ -36,14 +36,21 @@
* decompose form. (used in do_search (screen.c)) * decompose form. (used in do_search (screen.c))
*/ */
/* errors for conversion function: /* results of conversion function
* problem means, that not every characters was successfully converted (They are */
typedef enum {
/* success means, that convertion has been finished successully
*/
ESTR_SUCCESS = 0,
/* problem means, that not every characters was successfully converted (They are
* replaced with questionmark). So is impossible convert string back. * replaced with questionmark). So is impossible convert string back.
* failure means, that conversion is not possible (example: wrong encoding */
ESTR_PROBLEM = 1,
/* failure means, that conversion is not possible (example: wrong encoding
* of input string) * of input string)
*/ */
#define ESTR_PROBLEM 1 ESTR_FAILURE = 2
#define ESTR_FAILURE 2 } estr_t;
/* constanst originally from screen.c /* constanst originally from screen.c
* used for alignment strings on terminal * used for alignment strings on terminal
@ -75,7 +82,7 @@ extern GIConv str_cnv_not_convert;
// all functions in str_class must be defined for every encoding // all functions in str_class must be defined for every encoding
struct str_class { struct str_class {
int (*vfs_convert_to) (GIConv coder, const char *string, estr_t (*vfs_convert_to) (GIConv coder, const char *string,
int size, GString *buffer); //I int size, GString *buffer); //I
void (*insert_replace_char) (GString *buffer); void (*insert_replace_char) (GString *buffer);
int (*is_valid_string) (const char *); //I int (*is_valid_string) (const char *); //I
@ -152,22 +159,22 @@ void str_close_conv (GIConv);
* return 0 if there was no problem. * return 0 if there was no problem.
* otherwise return ESTR_PROBLEM or ESTR_FAILURE * otherwise return ESTR_PROBLEM or ESTR_FAILURE
*/ */
int str_convert (GIConv, char *, GString *); estr_t str_convert (GIConv, char *, GString *);
int str_nconvert (GIConv, char *, int, GString *); estr_t str_nconvert (GIConv, char *, int, GString *);
/* return only 0 or ESTR_FAILURE, because vfs must be able to convert result to /* return only 0 or ESTR_FAILURE, because vfs must be able to convert result to
* original string. (so no replace with questionmark) * original string. (so no replace with questionmark)
* if coder is str_cnv_from_term or str_cnv_not_convert, string is only copied, * 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 * so is possible to show file, that is not valid in terminal encoding
*/ */
int str_vfs_convert_from (GIConv, char *, GString *); estr_t str_vfs_convert_from (GIConv, char *, GString *);
/* if coder is str_cnv_to_term or str_cnv_not_convert, string is only copied, /* if coder is str_cnv_to_term or str_cnv_not_convert, string is only copied,
* does replace with questionmark * does replace with questionmark
* I * I
*/ */
int str_vfs_convert_to (GIConv, const char *, int, GString *); estr_t str_vfs_convert_to (GIConv, const char *, int, GString *);
/* printf functin for str_buffer, append result of printf at the end of buffer /* printf functin for str_buffer, append result of printf at the end of buffer
*/ */
@ -191,10 +198,11 @@ void str_uninit_strings ();
/* try convert characters in ch to output using conv /* try convert characters in ch to output using conv
* ch_size is size of ch, can by (size_t)(-1) (-1 only for ASCII * ch_size is size of ch, can by (size_t)(-1) (-1 only for ASCII
* compatible encoding, for other must be set) * compatible encoding, for other must be set)
* return 0 if conversion was successfully, ESTR_PROBLEM if ch contains only * return ESTR_SUCCESS if conversion was successfully,
* part of characters, ESTR_FAILURE if conversion is not possible * ESTR_PROBLEM if ch contains only part of characters,
* ESTR_FAILURE if conversion is not possible
*/ */
int str_translate_char (GIConv conv, const char *ch, size_t ch_size, estr_t str_translate_char (GIConv conv, const char *ch, size_t ch_size,
char *output, size_t out_size); char *output, size_t out_size);
/* test, if text is valid in terminal encoding /* test, if text is valid in terminal encoding

View File

@ -163,16 +163,16 @@ str_8bit_length2 (const char *text, int size)
return (size >= 0) ? min (strlen (text), size) : strlen (text); return (size >= 0) ? min (strlen (text), size) : strlen (text);
} }
int static estr_t
str_8bit_vfs_convert_to (GIConv coder, const char *string, str_8bit_vfs_convert_to (GIConv coder, const char *string,
int size, GString * buffer) int size, GString * buffer)
{ {
int result; estr_t result;
if (coder == str_cnv_not_convert) if (coder == str_cnv_not_convert)
{ {
g_string_append_len (buffer, string, size); g_string_append_len (buffer, string, size);
result = 0; result = ESTR_SUCCESS;
} }
else else
result = str_nconvert (coder, (char *) string, size, buffer); result = str_nconvert (coder, (char *) string, size, buffer);

View File

@ -161,12 +161,12 @@ str_ascii_length2 (const char *text, int size)
return (size >= 0) ? min (strlen (text), size) : strlen (text); return (size >= 0) ? min (strlen (text), size) : strlen (text);
} }
int static estr_t
str_ascii_vfs_convert_to (GIConv coder, const char *string, str_ascii_vfs_convert_to (GIConv coder, const char *string,
int size, GString * buffer) int size, GString * buffer)
{ {
g_string_append_len (buffer, string, size); g_string_append_len (buffer, string, size);
return 0; return ESTR_SUCCESS;
} }

View File

@ -330,16 +330,16 @@ str_utf8_questmark_sustb (char **string, size_t * left, GString * buffer)
g_string_append_c (buffer, '?'); g_string_append_c (buffer, '?');
} }
static int static estr_t
str_utf8_vfs_convert_to (GIConv coder, const char *string, str_utf8_vfs_convert_to (GIConv coder, const char *string,
int size, GString * buffer) int size, GString * buffer)
{ {
int result; estr_t result;
if (coder == str_cnv_not_convert) if (coder == str_cnv_not_convert)
{ {
g_string_append_len (buffer, string, size); g_string_append_len (buffer, string, size);
result = 0; result = ESTR_SUCCESS;
} }
else else
result = str_nconvert (coder, (char *) string, size, buffer); result = str_nconvert (coder, (char *) string, size, buffer);

View File

@ -338,11 +338,11 @@ view_get_char (WView *view, offset_type from, char *ch, int size)
result++; result++;
buffer[result] = '\0'; buffer[result] = '\0';
switch (str_translate_char (view->converter, buffer, result, ch, size)) { switch (str_translate_char (view->converter, buffer, result, ch, size)) {
case 0: case ESTR_SUCCESS:
return (int) result; return (int) result;
case 1: case ESTR_PROBLEM:
break; break;
case 2: case ESTR_FAILURE:
ch[0] = '?'; ch[0] = '?';
ch[1] = '\0'; ch[1] = '\0';
return 1; return 1;

View File

@ -381,14 +381,14 @@ vfs_supported_enconding (const char *encoding) {
* #enc: subtring * #enc: subtring
* buffer - used to store result of translation * buffer - used to store result of translation
*/ */
static int static estr_t
_vfs_translate_path (const char *path, int size, _vfs_translate_path (const char *path, int size,
GIConv defcnv, GString *buffer) GIConv defcnv, GString *buffer)
{ {
const char *semi; const char *semi;
const char *ps; const char *ps;
const char *slash; const char *slash;
int state = 0; estr_t state = ESTR_SUCCESS;
static char encoding[16]; static char encoding[16];
GIConv coder; GIConv coder;
int ms; int ms;
@ -408,7 +408,8 @@ _vfs_translate_path (const char *path, int size,
state = _vfs_translate_path (path, ms, defcnv, buffer); state = _vfs_translate_path (path, ms, defcnv, buffer);
if (state != 0) return state; if (state != ESTR_SUCCESS)
return state;
/* now can be translated part after #enc: */ /* now can be translated part after #enc: */
semi+= 5; semi+= 5;
@ -445,21 +446,20 @@ _vfs_translate_path (const char *path, int size,
errno = EINVAL; errno = EINVAL;
return ESTR_FAILURE; return ESTR_FAILURE;
} }
} else { } else {
/* path can be translated whole at once */ /* path can be translated whole at once */
state = str_vfs_convert_to (defcnv, path, size, buffer); state = str_vfs_convert_to (defcnv, path, size, buffer);
return state; return state;
} }
return 0; return ESTR_SUCCESS;
} }
char * char *
vfs_translate_path (const char *path) vfs_translate_path (const char *path)
{ {
int state; estr_t state;
g_string_set_size(vfs_str_buffer,0); g_string_set_size(vfs_str_buffer,0);
state = _vfs_translate_path (path, -1, str_cnv_from_term, vfs_str_buffer); state = _vfs_translate_path (path, -1, str_cnv_from_term, vfs_str_buffer);
// strict version // strict version
@ -740,7 +740,7 @@ mc_readdir (DIR *dirp)
static struct dirent result; static struct dirent result;
struct dirent *entry = NULL; struct dirent *entry = NULL;
struct vfs_dirinfo *dirinfo; struct vfs_dirinfo *dirinfo;
int state; estr_t state;
if (!dirp) { if (!dirp) {
errno = EFAULT; errno = EFAULT;
@ -756,7 +756,7 @@ mc_readdir (DIR *dirp)
g_string_set_size(vfs_str_buffer,0); g_string_set_size(vfs_str_buffer,0);
state = str_vfs_convert_from (dirinfo->converter, state = str_vfs_convert_from (dirinfo->converter,
entry->d_name, vfs_str_buffer); entry->d_name, vfs_str_buffer);
// } while (state != 0); // } while (state != ESTR_SUCCESS);
memcpy (&result, entry, sizeof (struct dirent)); memcpy (&result, entry, sizeof (struct dirent));
g_strlcpy (result.d_name, vfs_str_buffer->str, NAME_MAX + 1); g_strlcpy (result.d_name, vfs_str_buffer->str, NAME_MAX + 1);
result.d_reclen = strlen (result.d_name); result.d_reclen = strlen (result.d_name);
@ -844,7 +844,7 @@ _vfs_get_cwd (void)
char *trans; char *trans;
const char *encoding; const char *encoding;
char *tmp; char *tmp;
int state; estr_t state;
struct stat my_stat, my_stat2; struct stat my_stat, my_stat2;
trans = vfs_translate_path_n (current_dir); //add check if NULL trans = vfs_translate_path_n (current_dir); //add check if NULL
@ -857,9 +857,9 @@ _vfs_get_cwd (void)
g_string_set_size(vfs_str_buffer,0); g_string_set_size(vfs_str_buffer,0);
state = str_vfs_convert_from (str_cnv_from_term, tmp, vfs_str_buffer); state = str_vfs_convert_from (str_cnv_from_term, tmp, vfs_str_buffer);
g_free (tmp); g_free (tmp);
sys_cwd = (state == 0) ? g_strdup (vfs_str_buffer->str) : NULL; sys_cwd = (state == ESTR_SUCCESS) ? g_strdup (vfs_str_buffer->str) : NULL;
if (!sys_cwd) if (!sys_cwd)
return current_dir; return current_dir;
/* Otherwise check if it is O.K. to use the current_dir */ /* Otherwise check if it is O.K. to use the current_dir */
if (!cd_symlinks || mc_stat (sys_cwd, &my_stat) if (!cd_symlinks || mc_stat (sys_cwd, &my_stat)