From 057a762e9f688bf02b17a7fa6824317385db1bc5 Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Mon, 30 Apr 2012 14:58:25 +0400 Subject: [PATCH] Ticket #2827: tweak and cleanup of code in case of --disable-charset option usage. First step: tweak vfs-related code. Signed-off-by: Andrew Borodin --- lib/vfs/interface.c | 12 +++++ lib/vfs/path.c | 41 +++++++++++++--- lib/vfs/path.h | 6 +++ lib/vfs/utilvfs.c | 2 + lib/vfs/vfs.c | 14 ++++-- lib/vfs/vfs.h | 2 + src/filemanager/cmd.c | 2 + src/filemanager/cmd.h | 2 + src/filemanager/midnight.c | 34 +++++++++---- src/filemanager/panel.c | 117 +++++++++++++++++++++++---------------------- src/filemanager/panel.h | 8 +++- 11 files changed, 162 insertions(+), 78 deletions(-) diff --git a/lib/vfs/interface.c b/lib/vfs/interface.c index 294646532..1eba70e43 100644 --- a/lib/vfs/interface.c +++ b/lib/vfs/interface.c @@ -436,10 +436,12 @@ mc_opendir (const vfs_path_t * vpath) path_element->dir.info = info; +#ifdef HAVE_CHARSET path_element->dir.converter = (path_element->encoding != NULL) ? str_crt_conv_from (path_element->encoding) : str_cnv_from_term; if (path_element->dir.converter == INVALID_CONV) path_element->dir.converter = str_cnv_from_term; +#endif handle = vfs_new_handle (path_element->class, vfs_path_element_clone (path_element)); @@ -457,7 +459,9 @@ mc_readdir (DIR * dirp) struct vfs_class *vfs; struct dirent *entry = NULL; vfs_path_element_t *vfs_path_element; +#ifdef HAVE_CHARSET estr_t state; +#endif if (!mc_readdir_result) { @@ -491,9 +495,14 @@ mc_readdir (DIR * dirp) entry = (*vfs->readdir) (vfs_path_element->dir.info); if (entry == NULL) return NULL; + g_string_set_size (vfs_str_buffer, 0); +#ifdef HAVE_CHARSET state = str_vfs_convert_from (vfs_path_element->dir.converter, entry->d_name, vfs_str_buffer); +#else + g_string_assign (vfs_str_buffer, entry->d_name); +#endif mc_readdir_result->d_ino = entry->d_ino; g_strlcpy (mc_readdir_result->d_name, vfs_str_buffer->str, MAXNAMLEN + 1); } @@ -516,11 +525,14 @@ mc_closedir (DIR * dirp) { vfs_path_element_t *vfs_path_element; vfs_path_element = vfs_class_data_find_by_handle (handle); + +#ifdef HAVE_CHARSET if (vfs_path_element->dir.converter != str_cnv_from_term) { str_close_conv (vfs_path_element->dir.converter); vfs_path_element->dir.converter = INVALID_CONV; } +#endif result = vfs->closedir ? (*vfs->closedir) (vfs_path_element->dir.info) : -1; vfs_free_handle (handle); diff --git a/lib/vfs/path.c b/lib/vfs/path.c index 05fef55ce..26aefd8e0 100644 --- a/lib/vfs/path.c +++ b/lib/vfs/path.c @@ -172,6 +172,8 @@ vfs_canon (const char *path) } /* --------------------------------------------------------------------------------------------- */ + +#ifdef HAVE_CHARSET /** get encoding after last #enc: or NULL, if part does not contain #enc: * * @param path string @@ -208,6 +210,7 @@ vfs_get_encoding (const char *path) return NULL; } } +#endif /* --------------------------------------------------------------------------------------------- */ /** Extract the hostname and username from the path @@ -380,9 +383,11 @@ vfs_path_from_str_deprecated_parser (char *path, vfs_path_flag_t flags) local = ""; element->path = vfs_translate_path_n (local); +#ifdef HAVE_CHARSET element->encoding = vfs_get_encoding (local); element->dir.converter = (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV; +#endif url_params = strchr (op, ':'); /* skip VFS prefix */ if (url_params != NULL) @@ -403,9 +408,11 @@ vfs_path_from_str_deprecated_parser (char *path, vfs_path_flag_t flags) element->class = g_ptr_array_index (vfs__classes_list, 0); element->path = vfs_translate_path_n (path); +#ifdef HAVE_CHARSET element->encoding = vfs_get_encoding (path); element->dir.converter = (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV; +#endif g_array_prepend_val (vpath->path, element); } @@ -463,8 +470,9 @@ vfs_path_from_str_uri_parser (char *path, vfs_path_flag_t flags) else { element->path = vfs_translate_path_n (slash_pointer + 1); +#ifdef HAVE_CHARSET element->encoding = vfs_get_encoding (slash_pointer); - +#endif *slash_pointer = '\0'; } vfs_path_url_split (element, url_delimiter); @@ -472,10 +480,14 @@ vfs_path_from_str_uri_parser (char *path, vfs_path_flag_t flags) else { element->path = vfs_translate_path_n (url_delimiter); +#ifdef HAVE_CHARSET element->encoding = vfs_get_encoding (url_delimiter); +#endif } +#ifdef HAVE_CHARSET element->dir.converter = (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV; +#endif g_array_prepend_val (vpath->path, element); if ((real_vfs_prefix_start > path && *(real_vfs_prefix_start) == PATH_SEP) || @@ -490,9 +502,11 @@ vfs_path_from_str_uri_parser (char *path, vfs_path_flag_t flags) element = g_new0 (vfs_path_element_t, 1); element->class = g_ptr_array_index (vfs__classes_list, 0); element->path = vfs_translate_path_n (path); +#ifdef HAVE_CHARSET element->encoding = vfs_get_encoding (path); element->dir.converter = (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV; +#endif g_array_prepend_val (vpath->path, element); } @@ -533,6 +547,8 @@ vfs_path_tokens_add_class_info (const vfs_path_element_t * element, GString * re g_free (url_str); } + +#ifdef HAVE_CHARSET if (element->encoding != NULL) { if (ret_tokens->len > 0 && ret_tokens->str[ret_tokens->len - 1] != PATH_SEP) @@ -541,6 +557,7 @@ vfs_path_tokens_add_class_info (const vfs_path_element_t * element, GString * re g_string_append (ret_tokens, element->encoding); g_string_append (ret_tokens, PATH_SEP_STR); } +#endif g_string_append (ret_tokens, element_tokens->str); } @@ -647,6 +664,7 @@ vfs_path_to_str_flags (const vfs_path_t * vpath, int elements_count, vfs_path_fl g_free (url_str); } +#ifdef HAVE_CHARSET if ((flags & VPF_RECODE) == 0 && vfs_path_element_need_cleanup_converter (element)) { if ((flags & VPF_HIDE_CHARSET) == 0) @@ -662,6 +680,7 @@ vfs_path_to_str_flags (const vfs_path_t * vpath, int elements_count, vfs_path_fl g_string_set_size (recode_buffer, 0); } else +#endif { vfs_append_from_path (element->path, is_relative); } @@ -847,12 +866,14 @@ vfs_path_element_clone (const vfs_path_element_t * element) new_element->port = element->port; new_element->path = g_strdup (element->path); new_element->class = element->class; - new_element->encoding = g_strdup (element->encoding); new_element->vfs_prefix = g_strdup (element->vfs_prefix); +#ifdef HAVE_CHARSET + new_element->encoding = g_strdup (element->encoding); if (vfs_path_element_need_cleanup_converter (element) && new_element->encoding != NULL) new_element->dir.converter = str_crt_conv_from (new_element->encoding); else new_element->dir.converter = element->dir.converter; +#endif new_element->dir.info = element->dir.info; return new_element; @@ -876,13 +897,14 @@ vfs_path_element_free (vfs_path_element_t * element) g_free (element->password); g_free (element->host); g_free (element->path); - g_free (element->encoding); g_free (element->vfs_prefix); +#ifdef HAVE_CHARSET + g_free (element->encoding); + if (vfs_path_element_need_cleanup_converter (element)) - { str_close_conv (element->dir.converter); - } +#endif g_free (element); } @@ -1010,12 +1032,13 @@ vfs_prefix_to_class (const char *prefix) * * @return TRUE if need cleanup converter or FALSE otherwise */ - +#ifdef HAVE_CHARSET gboolean vfs_path_element_need_cleanup_converter (const vfs_path_element_t * element) { return (element->dir.converter != str_cnv_from_term && element->dir.converter != INVALID_CONV); } +#endif /* --------------------------------------------------------------------------------------------- */ /** @@ -1051,8 +1074,9 @@ vfs_path_serialize (const vfs_path_t * vpath, GError ** error) mc_config_set_string_raw (cpath, groupname, "path", element->path); mc_config_set_string_raw (cpath, groupname, "class-name", element->class->name); +#ifdef HAVE_CHARSET mc_config_set_string_raw (cpath, groupname, "encoding", element->encoding); - +#endif mc_config_set_string_raw (cpath, groupname, "vfs_prefix", element->vfs_prefix); mc_config_set_string_raw (cpath, groupname, "user", element->user); @@ -1120,9 +1144,12 @@ vfs_path_deserialize (const char *data, GError ** error) g_free (cfg_value); element->path = mc_config_get_string_raw (cpath, groupname, "path", NULL); + +#ifdef HAVE_CHARSET element->encoding = mc_config_get_string_raw (cpath, groupname, "encoding", NULL); element->dir.converter = (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV; +#endif element->vfs_prefix = mc_config_get_string_raw (cpath, groupname, "vfs_prefix", NULL); diff --git a/lib/vfs/path.h b/lib/vfs/path.h index 98db436aa..2f962ebad 100644 --- a/lib/vfs/path.h +++ b/lib/vfs/path.h @@ -38,12 +38,16 @@ typedef struct int port; char *path; struct vfs_class *class; +#ifdef HAVE_CHARSET char *encoding; +#endif char *vfs_prefix; struct { +#ifdef HAVE_CHARSET GIConv converter; +#endif DIR *info; } dir; } vfs_path_element_t; @@ -77,7 +81,9 @@ void vfs_path_element_free (vfs_path_element_t * element); struct vfs_class *vfs_prefix_to_class (const char *prefix); +#ifdef HAVE_CHARSET gboolean vfs_path_element_need_cleanup_converter (const vfs_path_element_t * element); +#endif char *vfs_path_serialize (const vfs_path_t * vpath, GError ** error); vfs_path_t *vfs_path_deserialize (const char *data, GError ** error); diff --git a/lib/vfs/utilvfs.c b/lib/vfs/utilvfs.c index 6db577b72..bdc212473 100644 --- a/lib/vfs/utilvfs.c +++ b/lib/vfs/utilvfs.c @@ -328,7 +328,9 @@ vfs_url_split (const char *path, int default_port, vfs_url_flags_t flags) } path_element->host = g_strdup (rest); +#ifdef HAVE_CHARSET path_element->dir.converter = INVALID_CONV; +#endif return path_element; } diff --git a/lib/vfs/vfs.c b/lib/vfs/vfs.c index dfff016f2..e173bbfeb 100644 --- a/lib/vfs/vfs.c +++ b/lib/vfs/vfs.c @@ -94,6 +94,7 @@ static vfs_path_t *current_path = NULL; static GPtrArray *vfs_openfiles; static long vfs_free_handle_list = -1; +/* --------------------------------------------------------------------------------------------- */ /*** file scope functions ************************************************************************/ /* --------------------------------------------------------------------------------------------- */ /* now used only by vfs_translate_path, but could be used in other vfs @@ -108,9 +109,10 @@ static long vfs_free_handle_list = -1; static estr_t _vfs_translate_path (const char *path, int size, GIConv defcnv, GString * buffer) { + estr_t state = ESTR_SUCCESS; +#ifdef HAVE_CHARSET const char *semi; const char *slash; - estr_t state = ESTR_SUCCESS; if (size == 0) return ESTR_SUCCESS; @@ -148,10 +150,8 @@ _vfs_translate_path (const char *path, int size, GIConv defcnv, GString * buffer memcpy (encoding, semi, ms); encoding[ms] = '\0'; -#ifdef HAVE_CHARSET if (is_supported_encoding (encoding)) coder = str_crt_conv_to (encoding); -#endif if (coder != INVALID_CONV) { @@ -169,6 +169,12 @@ _vfs_translate_path (const char *path, int size, GIConv defcnv, GString * buffer /* path can be translated whole at once */ state = str_vfs_convert_to (defcnv, path, size, buffer); } +#else + (void) size; + (void) defcnv; + + g_string_assign (buffer, path); +#endif /* HAVE_CHARSET */ return state; } @@ -579,6 +585,7 @@ _vfs_get_cwd (void) /* --------------------------------------------------------------------------------------------- */ +#ifdef HAVE_CHARSET /** * Change encoding for last part (vfs_path_element_t) of vpath * @@ -607,6 +614,7 @@ vfs_change_encoding (vfs_path_t * vpath, const char *encoding) return vpath; } +#endif /* HAVE_CHARSET */ /* --------------------------------------------------------------------------------------------- */ diff --git a/lib/vfs/vfs.h b/lib/vfs/vfs.h index 6474216b7..82f72cf06 100644 --- a/lib/vfs/vfs.h +++ b/lib/vfs/vfs.h @@ -270,7 +270,9 @@ void vfs_free_handle (int handle); void vfs_setup_cwd (void); char *_vfs_get_cwd (void); +#ifdef HAVE_CHARSET vfs_path_t *vfs_change_encoding (vfs_path_t * vpath, const char *encoding); +#endif int vfs_preallocate (int dest_desc, off_t src_fsize, off_t dest_fsize); diff --git a/src/filemanager/cmd.c b/src/filemanager/cmd.c index 7b8018ea5..6ebe78885 100644 --- a/src/filemanager/cmd.c +++ b/src/filemanager/cmd.c @@ -1785,11 +1785,13 @@ toggle_listing_cmd (void) /* --------------------------------------------------------------------------------------------- */ +#ifdef HAVE_CHARSET void encoding_cmd (void) { if (SELECTED_IS_PANEL) panel_change_encoding (MENU_PANEL); } +#endif /* --------------------------------------------------------------------------------------------- */ diff --git a/src/filemanager/cmd.h b/src/filemanager/cmd.h index b68900a63..db953274b 100644 --- a/src/filemanager/cmd.h +++ b/src/filemanager/cmd.h @@ -98,7 +98,9 @@ void quick_cmd_no_menu (void); void info_cmd_no_menu (void); void quick_view_cmd (void); void toggle_listing_cmd (void); +#ifdef HAVE_CHARSET void encoding_cmd (void); +#endif /*** inline functions ****************************************************************************/ #endif /* MC__CMD_H */ diff --git a/src/filemanager/midnight.c b/src/filemanager/midnight.c index 55ef8bebd..b989580e8 100644 --- a/src/filemanager/midnight.c +++ b/src/filemanager/midnight.c @@ -681,20 +681,27 @@ static void put_current_path (void) { char *cwd_path; - vfs_path_t *cwd_vpath; if (!command_prompt) return; - cwd_vpath = remove_encoding_from_path (current_panel->cwd_vpath); - cwd_path = vfs_path_to_str (cwd_vpath); - command_insert (cmdline, cwd_path, FALSE); +#ifdef HAVE_CHARSET + { + vfs_path_t *cwd_vpath; + + cwd_vpath = remove_encoding_from_path (current_panel->cwd_vpath); + cwd_path = vfs_path_to_str (cwd_vpath); + vfs_path_free (cwd_vpath); + } +#else + cwd_path = vfs_path_to_str (current_panel->cwd_vpath); +#endif + command_insert (cmdline, cwd_path, FALSE); if (cwd_path[strlen (cwd_path) - 1] != PATH_SEP) command_insert (cmdline, PATH_SEP_STR, FALSE); g_free (cwd_path); - vfs_path_free (cwd_vpath); } /* --------------------------------------------------------------------------------------------- */ @@ -703,7 +710,6 @@ static void put_other_path (void) { char *cwd_path; - vfs_path_t *cwd_vpath; if (get_other_type () != view_listing) return; @@ -711,15 +717,23 @@ put_other_path (void) if (!command_prompt) return; - cwd_vpath = remove_encoding_from_path (other_panel->cwd_vpath); - cwd_path = vfs_path_to_str (cwd_vpath); - command_insert (cmdline, cwd_path, FALSE); +#ifdef HAVE_CHARSET + { + vfs_path_t *cwd_vpath; + + cwd_vpath = remove_encoding_from_path (other_panel->cwd_vpath); + cwd_path = vfs_path_to_str (cwd_vpath); + vfs_path_free (cwd_vpath); + } +#else + cwd_path = vfs_path_to_str (other_panel->cwd_vpath); +#endif + command_insert (cmdline, cwd_path, FALSE); if (cwd_path[strlen (cwd_path) - 1] != PATH_SEP) command_insert (cmdline, PATH_SEP_STR, FALSE); g_free (cwd_path); - vfs_path_free (cwd_vpath); } /* --------------------------------------------------------------------------------------------- */ diff --git a/src/filemanager/panel.c b/src/filemanager/panel.c index d51e0bc7e..56ecd7b21 100644 --- a/src/filemanager/panel.c +++ b/src/filemanager/panel.c @@ -1136,6 +1136,7 @@ panel_correct_path_to_show (WPanel * panel) * @return newly allocated string or NULL if path charset is same as system charset */ +#ifdef HAVE_CHARSET static char * panel_get_encoding_info_str (WPanel * panel) { @@ -1148,6 +1149,7 @@ panel_get_encoding_info_str (WPanel * panel) return ret_str; } +#endif /* --------------------------------------------------------------------------------------------- */ @@ -1183,6 +1185,7 @@ show_dir (WPanel * panel) if (panel->is_panelized) tty_printf (" %s ", _("Panelize")); +#ifdef HAVE_CHARSET else { tmp = panel_get_encoding_info_str (panel); @@ -1193,6 +1196,8 @@ show_dir (WPanel * panel) g_free (tmp); } } +#endif + if (panel->active) tty_setcolor (REVERSE_COLOR); @@ -3642,60 +3647,6 @@ update_one_panel (int which, panel_update_flags_t flags, const char *current_fil } /* --------------------------------------------------------------------------------------------- */ -/*** public functions ****************************************************************************/ -/* --------------------------------------------------------------------------------------------- */ -/** - * Remove encode info from last path element. - * - */ - -vfs_path_t * -remove_encoding_from_path (const vfs_path_t * vpath) -{ - vfs_path_t *ret_vpath; - GString *tmp_conv; - int indx; - - ret_vpath = vfs_path_new (); - - tmp_conv = g_string_new (""); - - for (indx = 0; indx < vfs_path_elements_count (vpath); indx++) - { - GIConv converter; - vfs_path_element_t *path_element; - - path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, indx)); - vfs_path_add_element (ret_vpath, path_element); - - if (path_element->encoding == NULL) - { - continue; - } - - converter = str_crt_conv_to (path_element->encoding); - if (converter == INVALID_CONV) - continue; - - g_free (path_element->encoding); - path_element->encoding = NULL; - - str_vfs_convert_from (converter, path_element->path, tmp_conv); - - g_free (path_element->path); - path_element->path = g_strdup (tmp_conv->str); - - g_string_set_size (tmp_conv, 0); - - str_close_conv (converter); - str_close_conv (path_element->dir.converter); - path_element->dir.converter = INVALID_CONV; - } - g_string_free (tmp_conv, TRUE); - return ret_vpath; -} - -/* --------------------------------------------------------------------------------------------- */ static void do_select (WPanel * panel, int i) @@ -3946,7 +3897,9 @@ panel_new_with_dir (const char *panel_name, const char *wpath) panel->panel_name = g_strdup (panel_name); panel->user_format = g_strdup (DEFAULT_USER_FORMAT); +#ifdef HAVE_CHARSET panel->codepage = SELECT_CHARSET_NO_TRANSLATE; +#endif for (i = 0; i < LIST_TYPES; i++) panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT); @@ -3981,7 +3934,9 @@ panel_new_with_dir (const char *panel_name, const char *wpath) if (mc_chdir (panel->cwd_vpath) != 0) { +#ifdef HAVE_CHARSET panel->codepage = SELECT_CHARSET_NO_TRANSLATE; +#endif vfs_setup_cwd (); vfs_path_free (panel->cwd_vpath); panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ()); @@ -4366,11 +4321,11 @@ panel_set_sort_order (WPanel * panel, const panel_field_t * sort_order) * @param panel WPanel object */ +#ifdef HAVE_CHARSET void panel_change_encoding (WPanel * panel) { const char *encoding = NULL; -#ifdef HAVE_CHARSET char *errmsg; int r; @@ -4403,7 +4358,6 @@ panel_change_encoding (WPanel * panel) } encoding = get_codepage_id (panel->codepage); -#endif if (encoding != NULL) { char *cd_path; @@ -4417,6 +4371,57 @@ panel_change_encoding (WPanel * panel) } /* --------------------------------------------------------------------------------------------- */ + +/** + * Remove encode info from last path element. + * + */ +vfs_path_t * +remove_encoding_from_path (const vfs_path_t * vpath) +{ + vfs_path_t *ret_vpath; + GString *tmp_conv; + int indx; + + ret_vpath = vfs_path_new (); + + tmp_conv = g_string_new (""); + + for (indx = 0; indx < vfs_path_elements_count (vpath); indx++) + { + GIConv converter; + vfs_path_element_t *path_element; + + path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, indx)); + vfs_path_add_element (ret_vpath, path_element); + + if (path_element->encoding == NULL) + continue; + + converter = str_crt_conv_to (path_element->encoding); + if (converter == INVALID_CONV) + continue; + + g_free (path_element->encoding); + path_element->encoding = NULL; + + str_vfs_convert_from (converter, path_element->path, tmp_conv); + + g_free (path_element->path); + path_element->path = g_strdup (tmp_conv->str); + + g_string_set_size (tmp_conv, 0); + + str_close_conv (converter); + str_close_conv (path_element->dir.converter); + path_element->dir.converter = INVALID_CONV; + } + g_string_free (tmp_conv, TRUE); + return ret_vpath; +} +#endif /* HAVE_CHARSET */ + +/* --------------------------------------------------------------------------------------------- */ /** * This routine reloads the directory in both panels. It tries to * select current_file in current_panel and other_file in other_panel. diff --git a/src/filemanager/panel.h b/src/filemanager/panel.h index 0f9351db5..9d94fd9bd 100644 --- a/src/filemanager/panel.h +++ b/src/filemanager/panel.h @@ -118,7 +118,9 @@ typedef struct WPanel char *panel_name; /* The panel name */ struct stat dir_stat; /* Stat of current dir: used by execute () */ +#ifdef HAVE_CHARSET int codepage; /* panel codepage */ +#endif gboolean searching; char search_buffer[MC_MAXFILENAMELEN]; @@ -144,7 +146,11 @@ void panel_clean_dir (WPanel * panel); void panel_reload (WPanel * panel); void panel_set_sort_order (WPanel * panel, const panel_field_t * sort_order); void panel_re_sort (WPanel * panel); + +#ifdef HAVE_CHARSET void panel_change_encoding (WPanel * panel); +vfs_path_t *remove_encoding_from_path (const vfs_path_t * vpath); +#endif void update_panels (panel_update_flags_t flags, const char *current_file); int set_panel_formats (WPanel * p); @@ -163,8 +169,6 @@ gboolean do_panel_cd (struct WPanel *panel, const vfs_path_t * new_dir_vpath, en void directory_history_add (struct WPanel *panel, const char *dir); -vfs_path_t *remove_encoding_from_path (const vfs_path_t * vpath); - gsize panel_get_num_of_sortable_fields (void); const char **panel_get_sortable_fields (gsize *); const panel_field_t *panel_get_field_by_id (const char *); -- 2.11.4.GIT