fixed x_basename function for handle URL-like paths

Signed-off-by: Slava Zanko <slavazanko@gmail.com>
This commit is contained in:
Slava Zanko 2011-06-17 18:32:51 +03:00
parent 04a4ed0ece
commit 1eb91d7921
3 changed files with 123 additions and 8 deletions

View File

@ -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

98
lib/tests/x_basename.c Normal file
View File

@ -0,0 +1,98 @@
/* lib/vfs - x_basename() function testing
Copyright (C) 2011 Free Software Foundation, Inc.
Written by:
Slava Zanko <slavazanko@gmail.com>, 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 <check.h>
#include <stdio.h>
#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;
}
/* --------------------------------------------------------------------------------------------- */

View File

@ -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;
}
/* --------------------------------------------------------------------------------------------- */