diff --git a/lib/tests/Makefile.am b/lib/tests/Makefile.am index 20e92dd32..1f1ac742c 100644 --- a/lib/tests/Makefile.am +++ b/lib/tests/Makefile.am @@ -5,7 +5,8 @@ LIBS=@CHECK_LIBS@ $(top_builddir)/lib/libmc.la TESTS = \ library_independ \ - serialize + serialize \ + x_basename check_PROGRAMS = $(TESTS) @@ -14,3 +15,6 @@ library_independ_SOURCES = \ serialize_SOURCES = \ serialize.c + +x_basename_SOURCES = \ + x_basename.c diff --git a/lib/tests/x_basename.c b/lib/tests/x_basename.c new file mode 100644 index 000000000..1eb710e5f --- /dev/null +++ b/lib/tests/x_basename.c @@ -0,0 +1,98 @@ +/* lib/vfs - x_basename() 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_x_basename( input, etalon ) \ +{ \ + result = x_basename ( input ); \ + fail_unless(strcmp(result, etalon) == 0, \ + "\ninput (%s)\nactial (%s) not equal to\netalon (%s)", input, result, etalon); \ +} + +START_TEST (test_x_basename) +{ + const char *result; + check_x_basename ("/test/path/test2/path2", "path2"); + + check_x_basename ("/test/path/test2/path2#vfsprefix", "path2#vfsprefix"); + + check_x_basename ("/test/path/test2/path2/vfsprefix://", "path2/vfsprefix://"); + + + check_x_basename ("/test/path/test2/path2/vfsprefix://subdir", "subdir"); + + check_x_basename ("/test/path/test2/path2/vfsprefix://subdir/", "subdir/"); + + check_x_basename ("/test/path/test2/path2/vfsprefix://subdir/subdir2", "subdir2"); + + check_x_basename ("/test/path/test2/path2/vfsprefix:///", "/"); +} +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_x_basename); + /* *********************************** */ + + suite_add_tcase (s, tc_core); + sr = srunner_create (s); + srunner_set_log (sr, "x_basename.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.c b/lib/util.c index 25e0f726c..3a93a0cef 100644 --- a/lib/util.c +++ b/lib/util.c @@ -668,16 +668,29 @@ extract_line (const char *s, const char *top) const char * x_basename (const char *s) { - const char *where; + const char *url_delim, *path_sep; - where = g_strrstr (s, VFS_PATH_URL_DELIMITER); - if (where == NULL) - return ((where = strrchr (s, PATH_SEP))) ? where + 1 : s; + url_delim = g_strrstr (s, VFS_PATH_URL_DELIMITER); + path_sep = strrchr (s, PATH_SEP); - while (--where > s && *where != PATH_SEP); - while (--where > s && *where != PATH_SEP); + if (url_delim == NULL + || url_delim < path_sep - strlen (VFS_PATH_URL_DELIMITER) + || url_delim - s + strlen (VFS_PATH_URL_DELIMITER) < strlen (s)) + { + /* avoid trailing PATH_SEP, if present */ + if (s[strlen (s) - 1] == PATH_SEP) + { + while (--path_sep > s && *path_sep != PATH_SEP); + return (path_sep != s) ? path_sep + 1 : s; + } + else + return (path_sep != NULL) ? path_sep + 1 : s; + } - return (where == s ) ? s: where + 1; + while (--url_delim > s && *url_delim != PATH_SEP); + while (--url_delim > s && *url_delim != PATH_SEP); + + return (url_delim == s) ? s : url_delim + 1; } /* --------------------------------------------------------------------------------------------- */