From dc8150a5212f56b46120fd990ce1d7e282ca1924 Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Wed, 22 Apr 2009 20:35:32 +0400 Subject: [PATCH] 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. --- src/charsets.c | 24 +++++++++++------------ src/strutil.c | 49 +++++++++++++++++++--------------------------- src/strutil.h | 34 ++++++++++++++++++++------------ src/strutil8bit.c | 6 +++--- src/strutilascii.c | 4 ++-- src/strutilutf8.c | 6 +++--- src/view.c | 6 +++--- vfs/vfs.c | 28 +++++++++++++------------- 8 files changed, 78 insertions(+), 79 deletions(-) diff --git a/src/charsets.c b/src/charsets.c index 8ca8ec5fb..6928f8c64 100644 --- a/src/charsets.c +++ b/src/charsets.c @@ -324,11 +324,11 @@ convert_from_utf_to_current (const char *str) if (conv != INVALID_CONV) { switch (str_translate_char (conv, str, -1, buf_ch, sizeof(buf_ch))) { - case 0: + case ESTR_SUCCESS: ch = buf_ch[0]; break; - case 1: - case 2: + case ESTR_PROBLEM: + case ESTR_FAILURE: ch = '.'; break; } @@ -360,11 +360,11 @@ convert_from_utf_to_current_c (const int input_char) if (conv != INVALID_CONV) { switch (str_translate_char (conv, str, -1, buf_ch, sizeof(buf_ch))) { - case 0: + case ESTR_SUCCESS: ch = buf_ch[0]; break; - case 1: - case 2: + case ESTR_PROBLEM: + case ESTR_FAILURE: ch = '.'; break; } @@ -390,7 +390,7 @@ convert_from_8bit_to_utf_c (const char input_char) if (conv != INVALID_CONV) { 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); if ( res < 0 ) { ch = buf_ch[0]; @@ -398,8 +398,8 @@ convert_from_8bit_to_utf_c (const char input_char) ch = res; } break; - case 1: - case 2: + case ESTR_PROBLEM: + case ESTR_FAILURE: ch = '.'; break; } @@ -426,7 +426,7 @@ convert_from_8bit_to_utf_c2 (const char input_char) if (conv != INVALID_CONV) { 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); if ( res < 0 ) { ch = buf_ch[0]; @@ -434,8 +434,8 @@ convert_from_8bit_to_utf_c2 (const char input_char) ch = res; } break; - case 1: - case 2: + case ESTR_PROBLEM: + case ESTR_FAILURE: ch = '.'; break; } diff --git a/src/strutil.c b/src/strutil.c index 4b9501b9e..379a9632d 100644 --- a/src/strutil.c +++ b/src/strutil.c @@ -97,16 +97,19 @@ str_close_conv (GIConv conv) g_iconv_close (conv); } -static int +static estr_t _str_convert (GIConv coder, char *string, int size, GString * buffer) { - int state; + estr_t state = ESTR_SUCCESS; gchar *tmp_buff = NULL; gssize left; gsize bytes_read, bytes_written; GError *error = NULL; errno = 0; + if (coder == INVALID_CONV) + return ESTR_FAILURE; + if (string == NULL || buffer == NULL) return ESTR_FAILURE; @@ -116,7 +119,6 @@ _str_convert (GIConv coder, char *string, int size, GString * buffer) return ESTR_FAILURE; } */ - state = 0; if (size < 0) { size = strlen (string); @@ -129,10 +131,6 @@ _str_convert (GIConv coder, char *string, int size, GString * buffer) } left = size; - - if (coder == (GIConv) (-1)) - return ESTR_FAILURE; - g_iconv (coder, NULL, NULL, NULL, NULL); while (left) @@ -154,7 +152,7 @@ _str_convert (GIConv coder, char *string, int size, GString * buffer) g_error_free (error); error = NULL; return ESTR_FAILURE; - break; + case G_CONVERT_ERROR_ILLEGAL_SEQUENCE: /* Invalid byte sequence in conversion input. */ if (tmp_buff){ @@ -176,6 +174,7 @@ _str_convert (GIConv coder, char *string, int size, GString * buffer) } state = ESTR_PROBLEM; break; + case G_CONVERT_ERROR_PARTIAL_INPUT: /* Partial character sequence at end of input. */ g_error_free (error); @@ -190,7 +189,7 @@ _str_convert (GIConv coder, char *string, int size, GString * buffer) 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. */ @@ -234,35 +233,27 @@ _str_convert (GIConv coder, char *string, int size, GString * buffer) return state; } -int +estr_t str_convert (GIConv coder, char *string, GString * buffer) { - int result; - - result = _str_convert (coder, string, -1, buffer); - - return result; + return _str_convert (coder, string, -1, buffer); } -int +estr_t str_nconvert (GIConv coder, char *string, int size, GString * buffer) { - int result; - - result = _str_convert (coder, string, size, buffer); - - return result; + return _str_convert (coder, string, size, buffer); } -int +estr_t str_vfs_convert_from (GIConv coder, char *string, GString * buffer) { - int result; + estr_t result; if (coder == str_cnv_not_convert) { - g_string_append (buffer, (string)?string:""); - result = 0; + g_string_append (buffer, string != NULL ? string : ""); + result = ESTR_SUCCESS; } else result = _str_convert (coder, string, -1, buffer); @@ -270,7 +261,7 @@ str_vfs_convert_from (GIConv coder, char *string, GString * buffer) return result; } -int +estr_t str_vfs_convert_to (GIConv coder, const char *string, int size, GString * buffer) { @@ -301,7 +292,7 @@ str_insert_replace_char (GString * buffer) used_class.insert_replace_char (buffer); } -int +estr_t str_translate_char (GIConv conv, const char *keys, size_t ch_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); if (cnv == (size_t)(-1)) { - if (errno == EINVAL) return ESTR_PROBLEM; else return ESTR_FAILURE; + return (errno == EINVAL) ? ESTR_PROBLEM : ESTR_FAILURE; } else { output[0] = '\0'; - return 0; + return ESTR_SUCCESS; } } diff --git a/src/strutil.h b/src/strutil.h index a327ae65b..c1c5fce9c 100644 --- a/src/strutil.h +++ b/src/strutil.h @@ -36,14 +36,21 @@ * decompose form. (used in do_search (screen.c)) */ -/* errors for conversion function: - * problem means, that not every characters was successfully converted (They are +/* results of conversion function + */ +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. - * 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) */ -#define ESTR_PROBLEM 1 -#define ESTR_FAILURE 2 + ESTR_FAILURE = 2 +} estr_t; /* constanst originally from screen.c * 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 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 void (*insert_replace_char) (GString *buffer); int (*is_valid_string) (const char *); //I @@ -152,22 +159,22 @@ void str_close_conv (GIConv); * return 0 if there was no problem. * 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 * 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 (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, * does replace with questionmark * 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 */ @@ -191,10 +198,11 @@ void str_uninit_strings (); /* try convert characters in ch to output using conv * ch_size is size of ch, can by (size_t)(-1) (-1 only for ASCII * compatible encoding, for other must be set) - * return 0 if conversion was successfully, ESTR_PROBLEM if ch contains only - * part of characters, ESTR_FAILURE if conversion is not possible + * return ESTR_SUCCESS if conversion was successfully, + * 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); /* test, if text is valid in terminal encoding diff --git a/src/strutil8bit.c b/src/strutil8bit.c index d1d2f7f32..22c4d5bf0 100644 --- a/src/strutil8bit.c +++ b/src/strutil8bit.c @@ -163,16 +163,16 @@ str_8bit_length2 (const char *text, int size) return (size >= 0) ? min (strlen (text), size) : strlen (text); } -int +static estr_t str_8bit_vfs_convert_to (GIConv coder, const char *string, int size, GString * buffer) { - int result; + estr_t result; if (coder == str_cnv_not_convert) { g_string_append_len (buffer, string, size); - result = 0; + result = ESTR_SUCCESS; } else result = str_nconvert (coder, (char *) string, size, buffer); diff --git a/src/strutilascii.c b/src/strutilascii.c index 0920929c9..1967b4a5c 100644 --- a/src/strutilascii.c +++ b/src/strutilascii.c @@ -161,12 +161,12 @@ str_ascii_length2 (const char *text, int size) return (size >= 0) ? min (strlen (text), size) : strlen (text); } -int +static estr_t str_ascii_vfs_convert_to (GIConv coder, const char *string, int size, GString * buffer) { g_string_append_len (buffer, string, size); - return 0; + return ESTR_SUCCESS; } diff --git a/src/strutilutf8.c b/src/strutilutf8.c index a17fa72b5..ade2b4747 100644 --- a/src/strutilutf8.c +++ b/src/strutilutf8.c @@ -330,16 +330,16 @@ str_utf8_questmark_sustb (char **string, size_t * left, GString * buffer) g_string_append_c (buffer, '?'); } -static int +static estr_t str_utf8_vfs_convert_to (GIConv coder, const char *string, int size, GString * buffer) { - int result; + estr_t result; if (coder == str_cnv_not_convert) { g_string_append_len (buffer, string, size); - result = 0; + result = ESTR_SUCCESS; } else result = str_nconvert (coder, (char *) string, size, buffer); diff --git a/src/view.c b/src/view.c index 7824151fb..2d111f254 100644 --- a/src/view.c +++ b/src/view.c @@ -338,11 +338,11 @@ view_get_char (WView *view, offset_type from, char *ch, int size) result++; buffer[result] = '\0'; switch (str_translate_char (view->converter, buffer, result, ch, size)) { - case 0: + case ESTR_SUCCESS: return (int) result; - case 1: + case ESTR_PROBLEM: break; - case 2: + case ESTR_FAILURE: ch[0] = '?'; ch[1] = '\0'; return 1; diff --git a/vfs/vfs.c b/vfs/vfs.c index d5cfbf426..cc2920f14 100644 --- a/vfs/vfs.c +++ b/vfs/vfs.c @@ -381,14 +381,14 @@ vfs_supported_enconding (const char *encoding) { * #enc: subtring * buffer - used to store result of translation */ -static int +static estr_t _vfs_translate_path (const char *path, int size, GIConv defcnv, GString *buffer) { const char *semi; const char *ps; const char *slash; - int state = 0; + estr_t state = ESTR_SUCCESS; static char encoding[16]; GIConv coder; int ms; @@ -408,7 +408,8 @@ _vfs_translate_path (const char *path, int size, 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: */ semi+= 5; @@ -445,21 +446,20 @@ _vfs_translate_path (const char *path, int size, errno = EINVAL; return ESTR_FAILURE; } - } else { /* path can be translated whole at once */ state = str_vfs_convert_to (defcnv, path, size, buffer); return state; } - - return 0; + + return ESTR_SUCCESS; } char * vfs_translate_path (const char *path) { - int state; - + estr_t state; + g_string_set_size(vfs_str_buffer,0); state = _vfs_translate_path (path, -1, str_cnv_from_term, vfs_str_buffer); // strict version @@ -740,7 +740,7 @@ mc_readdir (DIR *dirp) static struct dirent result; struct dirent *entry = NULL; struct vfs_dirinfo *dirinfo; - int state; + estr_t state; if (!dirp) { errno = EFAULT; @@ -756,7 +756,7 @@ mc_readdir (DIR *dirp) g_string_set_size(vfs_str_buffer,0); state = str_vfs_convert_from (dirinfo->converter, entry->d_name, vfs_str_buffer); -// } while (state != 0); +// } while (state != ESTR_SUCCESS); memcpy (&result, entry, sizeof (struct dirent)); g_strlcpy (result.d_name, vfs_str_buffer->str, NAME_MAX + 1); result.d_reclen = strlen (result.d_name); @@ -844,7 +844,7 @@ _vfs_get_cwd (void) char *trans; const char *encoding; char *tmp; - int state; + estr_t state; struct stat my_stat, my_stat2; 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); 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->str) : NULL; - if (!sys_cwd) - return current_dir; + sys_cwd = (state == ESTR_SUCCESS) ? g_strdup (vfs_str_buffer->str) : NULL; + if (!sys_cwd) + return current_dir; /* Otherwise check if it is O.K. to use the current_dir */ if (!cd_symlinks || mc_stat (sys_cwd, &my_stat)