From 1beaecdf1201945be6c0e95bc921aad7dae9f967 Mon Sep 17 00:00:00 2001 From: Slava Zanko Date: Fri, 17 Jun 2011 20:17:16 +0300 Subject: [PATCH] Added function mc_build_filename() for processing URL-paths as well Signed-off-by: Slava Zanko --- lib/tests/Makefile.am | 4 ++ lib/tests/mc_build_filename.c | 100 ++++++++++++++++++++++++++++++++++++++++++ lib/util.h | 2 + lib/utilunix.c | 52 ++++++++++++++++++++++ src/editor/edit.c | 4 +- src/editor/editcmd.c | 4 +- src/editor/etags.c | 2 +- src/filemanager/file.c | 4 +- src/filemanager/find.c | 4 +- src/viewer/lib.c | 4 +- 10 files changed, 169 insertions(+), 11 deletions(-) create mode 100644 lib/tests/mc_build_filename.c diff --git a/lib/tests/Makefile.am b/lib/tests/Makefile.am index 1f1ac742c..4a1d61416 100644 --- a/lib/tests/Makefile.am +++ b/lib/tests/Makefile.am @@ -5,6 +5,7 @@ LIBS=@CHECK_LIBS@ $(top_builddir)/lib/libmc.la TESTS = \ library_independ \ + mc_build_filename \ serialize \ x_basename @@ -13,6 +14,9 @@ check_PROGRAMS = $(TESTS) library_independ_SOURCES = \ library_independ.c +mc_build_filename_SOURCES = \ + mc_build_filename.c + serialize_SOURCES = \ serialize.c diff --git a/lib/tests/mc_build_filename.c b/lib/tests/mc_build_filename.c new file mode 100644 index 000000000..e9568ce30 --- /dev/null +++ b/lib/tests/mc_build_filename.c @@ -0,0 +1,100 @@ +/* lib/vfs - mc_build_filename() function testing + + Copyright (C) 2011 Free Software Foundation, Inc. + + Written by: + Slava Zanko , 2011 + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License + as published by the Free Software Foundation; either version 2 of + the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#define TEST_SUITE_NAME "/lib" + +#include + +#include + +#include "lib/global.h" +#include "lib/strutil.h" +#include "lib/util.h" + + +static void +setup (void) +{ +} + +static void +teardown (void) +{ +} + +/* --------------------------------------------------------------------------------------------- */ +#define check_mc_build_filename( inargs, etalon ) \ +{ \ + result = mc_build_filename inargs; \ + fail_unless( strcmp (result, etalon) == 0, \ + "\nactial (%s) not equal to\netalon (%s)", result, etalon); \ + g_free (result); \ +} + +START_TEST (test_mc_build_filename) +{ + char *result; + + check_mc_build_filename(("test", "path", NULL), "/test/path"); + + check_mc_build_filename(("/test", "path/", NULL), "/test/path"); + + check_mc_build_filename(("/test", "pa/th", NULL), "/test/pa/th"); + + check_mc_build_filename(("/test", "#vfsprefix:", "path ", NULL), "/test/#vfsprefix:/path "); + + check_mc_build_filename(("/test", "vfsprefix://", "path ", NULL), "/test/vfsprefix://path "); + + check_mc_build_filename(("/test", "vfs/../prefix:///", "p\\///ath", NULL), "/test/prefix://p\\/ath"); + + check_mc_build_filename(("/test", "path", "..", "/test", "path/", NULL), "/test/test/path"); + +} +END_TEST + +/* --------------------------------------------------------------------------------------------- */ + +int +main (void) +{ + int number_failed; + + Suite *s = suite_create (TEST_SUITE_NAME); + TCase *tc_core = tcase_create ("Core"); + SRunner *sr; + + tcase_add_checked_fixture (tc_core, setup, teardown); + + /* Add new tests here: *************** */ + tcase_add_test (tc_core, test_mc_build_filename); + /* *********************************** */ + + suite_add_tcase (s, tc_core); + sr = srunner_create (s); + srunner_set_log (sr, "mc_build_filename.log"); + srunner_run_all (sr, CK_NORMAL); + number_failed = srunner_ntests_failed (sr); + srunner_free (sr); + return (number_failed == 0) ? 0 : 1; +} + +/* --------------------------------------------------------------------------------------------- */ diff --git a/lib/util.h b/lib/util.h index 95b4af607..3f67277ce 100644 --- a/lib/util.h +++ b/lib/util.h @@ -205,6 +205,8 @@ gboolean mc_util_unlink_backup_if_possible (const char *, const char *); char *guess_message_value (void); +char *mc_build_filename (const char *first_element, ...); + /*** inline functions **************************************************/ static inline gboolean diff --git a/lib/utilunix.c b/lib/utilunix.c index ef284da3a..26f3977f6 100644 --- a/lib/utilunix.c +++ b/lib/utilunix.c @@ -1016,3 +1016,55 @@ get_user_permissions (struct stat *st) } /* --------------------------------------------------------------------------------------------- */ +/** + * Build filename from arguments. + * Like to g_build_filename(), but respect VFS_PATH_URL_DELIMITER + */ + +char * +mc_build_filename (const char *first_element, ...) +{ + va_list args; + const char *element = first_element; + GString *path; + char *ret; + + if (element == NULL) + return NULL; + + path = g_string_new (""); + va_start (args, first_element); + + do + { + char *tmp_element; + size_t len; + const char *start; + + tmp_element = g_strdup (element); + + element = va_arg (args, char *); + + canonicalize_pathname (tmp_element); + len = strlen (tmp_element); + start = (tmp_element[0] == PATH_SEP) ? tmp_element + 1 : tmp_element; + + g_string_append (path, start); + if (tmp_element[len - 1] != PATH_SEP && element != NULL) + g_string_append_c (path, PATH_SEP); + + g_free (tmp_element); + } + while (element != NULL); + + va_end (args); + + g_string_prepend_c (path, PATH_SEP); + + ret = g_string_free (path, FALSE); + canonicalize_pathname (ret); + + return ret; +} + +/* --------------------------------------------------------------------------------------------- */ diff --git a/src/editor/edit.c b/src/editor/edit.c index 6287ca674..04d1553d3 100644 --- a/src/editor/edit.c +++ b/src/editor/edit.c @@ -4275,7 +4275,7 @@ edit_unlock_file (WEdit * edit) char *fullpath; unsigned int ret; - fullpath = g_build_filename (edit->dir, edit->filename, (char *) NULL); + fullpath = mc_build_filename (edit->dir, edit->filename, (char *) NULL); ret = unlock_file (fullpath); g_free (fullpath); @@ -4290,7 +4290,7 @@ edit_lock_file (WEdit * edit) char *fullpath; unsigned int ret; - fullpath = g_build_filename (edit->dir, edit->filename, (char *) NULL); + fullpath = mc_build_filename (edit->dir, edit->filename, (char *) NULL); ret = lock_file (fullpath); g_free (fullpath); diff --git a/src/editor/editcmd.c b/src/editor/editcmd.c index e940bb9bb..d35fd07a8 100644 --- a/src/editor/editcmd.c +++ b/src/editor/editcmd.c @@ -492,7 +492,7 @@ edit_load_file_from_filename (WEdit * edit, char *exp) { char *fullpath; - fullpath = g_build_filename (edit->dir, prev_filename, (char *) NULL); + fullpath = mc_build_filename (edit->dir, prev_filename, (char *) NULL); unlock_file (fullpath); g_free (fullpath); } @@ -3193,7 +3193,7 @@ edit_get_match_keyword_cmd (WEdit * edit) g_free (path); path = ptr; g_free (tagfile); - tagfile = g_build_filename (path, TAGS_NAME, (char *) NULL); + tagfile = mc_build_filename (path, TAGS_NAME, (char *) NULL); if (exist_file (tagfile)) break; } diff --git a/src/editor/etags.c b/src/editor/etags.c index 60ef00426..20ec36fe3 100644 --- a/src/editor/etags.c +++ b/src/editor/etags.c @@ -225,7 +225,7 @@ etags_set_definition_hash (const char *tagfile, const char *start_path, if (num < MAX_DEFINITIONS - 1) { def_hash[num].filename_len = strlen (filename); - def_hash[num].fullpath = g_build_filename (start_path, filename, (char *) NULL); + def_hash[num].fullpath = mc_build_filename (start_path, filename, (char *) NULL); canonicalize_pathname (def_hash[num].fullpath); def_hash[num].filename = g_strdup (filename); diff --git a/src/filemanager/file.c b/src/filemanager/file.c index 432878a47..8aa9e1a6b 100644 --- a/src/filemanager/file.c +++ b/src/filemanager/file.c @@ -2547,7 +2547,7 @@ panel_operate (void *source_panel, FileOperation operation, gboolean force_singl if (g_path_is_absolute (source)) source_with_path = g_strdup (source); else - source_with_path = g_build_filename (panel->cwd, source, (char *) NULL); + source_with_path = mc_build_filename (panel->cwd, source, (char *) NULL); #endif /* WITH_FULL_PATHS */ if (panel_operate_init_totals (operation, panel, source_with_path, ctx) == FILE_CONT) @@ -2641,7 +2641,7 @@ panel_operate (void *source_panel, FileOperation operation, gboolean force_singl if (g_path_is_absolute (source)) source_with_path = g_strdup (source); else - source_with_path = g_build_filename (panel->cwd, source, (char *) NULL); + source_with_path = mc_build_filename (panel->cwd, source, (char *) NULL); #endif /* WITH_FULL_PATHS */ if (operation == OP_DELETE) diff --git a/src/filemanager/find.c b/src/filemanager/find.c index adc2e436d..5dfcc382f 100644 --- a/src/filemanager/find.c +++ b/src/filemanager/find.c @@ -714,7 +714,7 @@ find_parameters (char **start_dir, char **ignore_dirs, char **pattern, char **co else if (g_path_is_absolute (*start_dir)) *start_dir = g_strdup (*start_dir); else - *start_dir = g_build_filename (current_panel->cwd, *start_dir, (char *) NULL); + *start_dir = mc_build_filename (current_panel->cwd, *start_dir, (char *) NULL); canonicalize_pathname (*start_dir); @@ -1246,7 +1246,7 @@ do_search (Dlg_head * h) { char *tmp_name; - tmp_name = g_build_filename (directory, dp->d_name, (char *) NULL); + tmp_name = mc_build_filename (directory, dp->d_name, (char *) NULL); if (mc_lstat (tmp_name, &tmp_stat) == 0 && S_ISDIR (tmp_stat.st_mode)) { diff --git a/src/viewer/lib.c b/src/viewer/lib.c index 9d5db0c54..a45c0353e 100644 --- a/src/viewer/lib.c +++ b/src/viewer/lib.c @@ -437,7 +437,7 @@ mcview_lock_file (mcview_t * view) char *fullpath; gboolean ret; - fullpath = g_build_filename (view->workdir, view->filename, (char *) NULL); + fullpath = mc_build_filename (view->workdir, view->filename, (char *) NULL); ret = lock_file (fullpath); g_free (fullpath); @@ -452,7 +452,7 @@ mcview_unlock_file (mcview_t * view) char *fullpath; gboolean ret; - fullpath = g_build_filename (view->workdir, view->filename, (char *) NULL); + fullpath = mc_build_filename (view->workdir, view->filename, (char *) NULL); ret = unlock_file (fullpath); g_free (fullpath); -- 2.11.4.GIT