mirror of
https://github.com/MidnightCommander/mc
synced 2024-12-22 20:36:50 +03:00
Added vfs_path_tokens_count() and vfs_path_tokens_get() functions
...for easy work with vfs_path_t tokens. Signed-off-by: Slava Zanko <slavazanko@gmail.com>
This commit is contained in:
parent
4267c04b69
commit
133cf183b5
181
lib/vfs/path.c
181
lib/vfs/path.c
@ -524,7 +524,8 @@ vfs_path_from_str_uri_parser (char *path)
|
||||
(element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
|
||||
vpath->path = g_list_prepend (vpath->path, element);
|
||||
|
||||
if (real_vfs_prefix_start > path && *(real_vfs_prefix_start) == PATH_SEP)
|
||||
if ((real_vfs_prefix_start > path && *(real_vfs_prefix_start) == PATH_SEP) ||
|
||||
(real_vfs_prefix_start == path && *(real_vfs_prefix_start) != PATH_SEP))
|
||||
*real_vfs_prefix_start = '\0';
|
||||
else
|
||||
*(real_vfs_prefix_start + 1) = '\0';
|
||||
@ -544,6 +545,52 @@ vfs_path_from_str_uri_parser (char *path)
|
||||
return vpath;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
/**
|
||||
* Add element's class info to result string (such as VFS name, host, encoding etc)
|
||||
* This function used as helper only in vfs_path_tokens_get() function
|
||||
*
|
||||
* @param element current path element
|
||||
* @param ret_tokens total tikens for return
|
||||
* @param element_tokens accumulated element-only tokens
|
||||
*/
|
||||
|
||||
static void
|
||||
vfs_path_tokens_add_class_info (vfs_path_element_t * element, GString * ret_tokens,
|
||||
GString * element_tokens)
|
||||
{
|
||||
if (((element->class->flags & VFSF_LOCAL) == 0 || ret_tokens->len > 0)
|
||||
&& element_tokens->len > 0)
|
||||
{
|
||||
char *url_str;
|
||||
|
||||
if (ret_tokens->len > 0 && ret_tokens->str[ret_tokens->len - 1] != PATH_SEP)
|
||||
g_string_append_c (ret_tokens, PATH_SEP);
|
||||
|
||||
g_string_append (ret_tokens, element->vfs_prefix);
|
||||
g_string_append (ret_tokens, VFS_PATH_URL_DELIMITER);
|
||||
|
||||
url_str = vfs_path_build_url_params_str (element);
|
||||
if (*url_str != '\0')
|
||||
{
|
||||
g_string_append (ret_tokens, url_str);
|
||||
g_string_append_c (ret_tokens, PATH_SEP);
|
||||
}
|
||||
|
||||
g_free (url_str);
|
||||
}
|
||||
if (element->encoding != NULL)
|
||||
{
|
||||
if (ret_tokens->len > 0 && ret_tokens->str[ret_tokens->len - 1] != PATH_SEP)
|
||||
g_string_append (ret_tokens, PATH_SEP_STR);
|
||||
g_string_append (ret_tokens, VFS_ENCODING_PREFIX);
|
||||
g_string_append (ret_tokens, element->encoding);
|
||||
g_string_append (ret_tokens, PATH_SEP_STR);
|
||||
}
|
||||
|
||||
g_string_append (ret_tokens, element_tokens->str);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
/*** public functions ****************************************************************************/
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
@ -592,8 +639,8 @@ vfs_path_to_str_elements_count (const vfs_path_t * vpath, int elements_count)
|
||||
{
|
||||
char *url_str;
|
||||
|
||||
if (buffer->str[buffer->len - 1] != '/')
|
||||
g_string_append_c (buffer, '/');
|
||||
if (buffer->len == 0 || buffer->str[buffer->len - 1] != PATH_SEP)
|
||||
g_string_append_c (buffer, PATH_SEP);
|
||||
|
||||
g_string_append (buffer, element->vfs_prefix);
|
||||
g_string_append (buffer, VFS_PATH_URL_DELIMITER);
|
||||
@ -1047,8 +1094,10 @@ vfs_path_deserialize (const char *data, GError ** error)
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
/**
|
||||
* Build vfs_path_t object from arguments.
|
||||
*
|
||||
* return newly allocated vfs_path_t object
|
||||
*
|
||||
* @param ... path tokens, terminated by NULL
|
||||
*
|
||||
* @return newly allocated vfs_path_t object
|
||||
*/
|
||||
|
||||
vfs_path_t *
|
||||
@ -1070,6 +1119,14 @@ vfs_path_build_filename (const char *first_element, ...)
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
/**
|
||||
* Append tokens to path object
|
||||
*
|
||||
* @param vpath path object
|
||||
* @param ... NULL-terminated strings
|
||||
*
|
||||
* @return newly allocated path object
|
||||
*/
|
||||
|
||||
vfs_path_t *
|
||||
vfs_path_append_new (const vfs_path_t * vpath, const char *first_element, ...)
|
||||
@ -1095,3 +1152,117 @@ vfs_path_append_new (const vfs_path_t * vpath, const char *first_element, ...)
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
/**
|
||||
* get tockens count in path.
|
||||
*
|
||||
* @param vpath path object
|
||||
*
|
||||
* @return count of tokens
|
||||
*/
|
||||
|
||||
size_t
|
||||
vfs_path_tokens_count (const vfs_path_t * vpath)
|
||||
{
|
||||
size_t count_tokens = 0;
|
||||
int element_index;
|
||||
|
||||
if (vpath == NULL)
|
||||
return 0;
|
||||
|
||||
for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
|
||||
{
|
||||
vfs_path_element_t *element;
|
||||
char **path_tokens, **iterator;
|
||||
|
||||
element = vfs_path_get_by_index (vpath, element_index);
|
||||
path_tokens = iterator = g_strsplit (element->path, PATH_SEP_STR, -1);
|
||||
|
||||
while (*iterator != NULL)
|
||||
{
|
||||
if (**iterator != '\0')
|
||||
count_tokens++;
|
||||
iterator++;
|
||||
}
|
||||
g_strfreev (path_tokens);
|
||||
}
|
||||
return count_tokens;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
/**
|
||||
* Get subpath by tokens
|
||||
*
|
||||
* @param vpath path object
|
||||
* @param start_position first token for got/ Started from 0.
|
||||
* If negative, then position will be relative to end of path
|
||||
* @param length count of tokens
|
||||
*
|
||||
* @return newly allocated string with path tokens separated by slash
|
||||
*/
|
||||
|
||||
char *
|
||||
vfs_path_tokens_get (const vfs_path_t * vpath, ssize_t start_position, size_t length)
|
||||
{
|
||||
GString *ret_tokens, *element_tokens;
|
||||
int element_index;
|
||||
size_t tokens_count = vfs_path_tokens_count (vpath);
|
||||
|
||||
if (length == 0)
|
||||
length = tokens_count;
|
||||
|
||||
if (start_position < 0)
|
||||
start_position = (ssize_t) tokens_count + start_position;
|
||||
|
||||
if (start_position < 0)
|
||||
return NULL;
|
||||
|
||||
if (start_position >= (ssize_t) tokens_count)
|
||||
return NULL;
|
||||
|
||||
if (start_position + (ssize_t) length > (ssize_t) tokens_count)
|
||||
length = tokens_count - start_position;
|
||||
|
||||
ret_tokens = g_string_sized_new (32);
|
||||
element_tokens = g_string_sized_new (32);
|
||||
|
||||
for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
|
||||
{
|
||||
vfs_path_element_t *element;
|
||||
char **path_tokens, **iterator;
|
||||
|
||||
g_string_assign (element_tokens, "");
|
||||
element = vfs_path_get_by_index (vpath, element_index);
|
||||
path_tokens = iterator = g_strsplit (element->path, PATH_SEP_STR, -1);
|
||||
|
||||
while (*iterator != NULL)
|
||||
{
|
||||
if (**iterator != '\0')
|
||||
{
|
||||
if (start_position == 0)
|
||||
{
|
||||
if (length == 0)
|
||||
{
|
||||
vfs_path_tokens_add_class_info (element, ret_tokens, element_tokens);
|
||||
g_string_free (element_tokens, TRUE);
|
||||
g_strfreev (path_tokens);
|
||||
return g_string_free (ret_tokens, FALSE);
|
||||
}
|
||||
length--;
|
||||
if (element_tokens->len != 0)
|
||||
g_string_append_c (element_tokens, PATH_SEP);
|
||||
g_string_append (element_tokens, *iterator);
|
||||
}
|
||||
else
|
||||
start_position--;
|
||||
}
|
||||
iterator++;
|
||||
}
|
||||
g_strfreev (path_tokens);
|
||||
vfs_path_tokens_add_class_info (element, ret_tokens, element_tokens);
|
||||
}
|
||||
|
||||
g_string_free (element_tokens, TRUE);
|
||||
return g_string_free (ret_tokens, !(start_position == 0 && length == 0));
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
|
@ -59,6 +59,8 @@ vfs_path_t *vfs_path_from_str (const char *path_str);
|
||||
vfs_path_t *vfs_path_from_str_flags (const char *path_str, vfs_path_flag_t flags);
|
||||
vfs_path_t *vfs_path_build_filename (const char *first_element, ...);
|
||||
vfs_path_t *vfs_path_append_new (const vfs_path_t *, const char *first_element, ...);
|
||||
size_t vfs_path_tokens_count (const vfs_path_t *);
|
||||
char *vfs_path_tokens_get (const vfs_path_t * vpath, ssize_t start_position, size_t length);
|
||||
|
||||
vfs_path_element_t *vfs_path_get_by_index (const vfs_path_t * path, int element_index);
|
||||
vfs_path_element_t *vfs_path_element_clone (const vfs_path_element_t * element);
|
||||
|
@ -11,6 +11,7 @@ LIBS=@CHECK_LIBS@ \
|
||||
TESTS = \
|
||||
canonicalize_pathname \
|
||||
current_dir \
|
||||
path_manipulations \
|
||||
path_recode \
|
||||
path_serialize \
|
||||
tempdir \
|
||||
@ -28,6 +29,9 @@ canonicalize_pathname_SOURCES = \
|
||||
current_dir_SOURCES = \
|
||||
current_dir.c
|
||||
|
||||
path_manipulations_SOURCES = \
|
||||
path_manipulations.c
|
||||
|
||||
path_recode_SOURCES = \
|
||||
path_recode.c
|
||||
|
||||
|
238
tests/lib/vfs/path_manipulations.c
Normal file
238
tests/lib/vfs/path_manipulations.c
Normal file
@ -0,0 +1,238 @@
|
||||
/* lib/vfs - test vfs_path_t manipulation functions
|
||||
|
||||
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/vfs"
|
||||
|
||||
#include <check.h>
|
||||
|
||||
#include "lib/global.c"
|
||||
|
||||
#ifndef HAVE_CHARSET
|
||||
#define HAVE_CHARSET 1
|
||||
#endif
|
||||
|
||||
#include "lib/charsets.h"
|
||||
|
||||
#include "lib/strutil.h"
|
||||
#include "lib/vfs/xdirentry.h"
|
||||
#include "lib/vfs/path.h"
|
||||
|
||||
#include "src/vfs/local/local.c"
|
||||
|
||||
|
||||
struct vfs_s_subclass test_subclass1, test_subclass2, test_subclass3;
|
||||
struct vfs_class vfs_test_ops1, vfs_test_ops2, vfs_test_ops3;
|
||||
|
||||
static void
|
||||
setup (void)
|
||||
{
|
||||
|
||||
str_init_strings (NULL);
|
||||
|
||||
vfs_init ();
|
||||
init_localfs ();
|
||||
vfs_setup_work_dir ();
|
||||
|
||||
|
||||
test_subclass1.flags = VFS_S_REMOTE;
|
||||
vfs_s_init_class (&vfs_test_ops1, &test_subclass1);
|
||||
|
||||
vfs_test_ops1.name = "testfs1";
|
||||
vfs_test_ops1.flags = VFSF_NOLINKS;
|
||||
vfs_test_ops1.prefix = "test1";
|
||||
vfs_register_class (&vfs_test_ops1);
|
||||
|
||||
vfs_s_init_class (&vfs_test_ops2, &test_subclass2);
|
||||
vfs_test_ops2.name = "testfs2";
|
||||
vfs_test_ops2.prefix = "test2";
|
||||
vfs_register_class (&vfs_test_ops2);
|
||||
|
||||
vfs_s_init_class (&vfs_test_ops3, &test_subclass3);
|
||||
vfs_test_ops3.name = "testfs3";
|
||||
vfs_test_ops3.prefix = "test3";
|
||||
vfs_test_ops3.flags = VFSF_LOCAL;
|
||||
vfs_register_class (&vfs_test_ops3);
|
||||
|
||||
mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
|
||||
load_codepages_list ();
|
||||
}
|
||||
|
||||
static void
|
||||
teardown (void)
|
||||
{
|
||||
free_codepages_list ();
|
||||
|
||||
vfs_shut ();
|
||||
str_uninit_strings ();
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
|
||||
START_TEST (test_vfs_path_tokens_count)
|
||||
{
|
||||
size_t tokens_count;
|
||||
vfs_path_t *vpath;
|
||||
|
||||
vpath = vfs_path_from_str ("/");
|
||||
tokens_count = vfs_path_tokens_count(vpath);
|
||||
fail_unless (tokens_count == 0, "actual: %zu; expected: 0\n", tokens_count);
|
||||
vfs_path_free (vpath);
|
||||
|
||||
vpath = vfs_path_from_str ("/path");
|
||||
tokens_count = vfs_path_tokens_count(vpath);
|
||||
fail_unless (tokens_count == 1, "actual: %zu; expected: 1\n", tokens_count);
|
||||
vfs_path_free (vpath);
|
||||
|
||||
vpath = vfs_path_from_str ("/path1/path2/path3");
|
||||
tokens_count = vfs_path_tokens_count(vpath);
|
||||
fail_unless (tokens_count == 3, "actual: %zu; expected: 3\n", tokens_count);
|
||||
vfs_path_free (vpath);
|
||||
|
||||
vpath = vfs_path_from_str_flags ("test3://path1/path2/path3/path4", VPF_NO_CANON);
|
||||
tokens_count = vfs_path_tokens_count(vpath);
|
||||
fail_unless (tokens_count == 4, "actual: %zu; expected: 4\n", tokens_count);
|
||||
vfs_path_free (vpath);
|
||||
|
||||
vpath = vfs_path_from_str_flags ("path1/path2/path3", VPF_NO_CANON);
|
||||
tokens_count = vfs_path_tokens_count(vpath);
|
||||
fail_unless (tokens_count == 3, "actual: %zu; expected: 3\n", tokens_count);
|
||||
vfs_path_free (vpath);
|
||||
|
||||
vpath = vfs_path_from_str ("/path1/path2/path3/");
|
||||
tokens_count = vfs_path_tokens_count(vpath);
|
||||
fail_unless (tokens_count == 3, "actual: %zu; expected: 3\n", tokens_count);
|
||||
vfs_path_free (vpath);
|
||||
|
||||
vpath = vfs_path_from_str ("/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/");
|
||||
tokens_count = vfs_path_tokens_count(vpath);
|
||||
fail_unless (tokens_count == 5, "actual: %zu; expected: 5\n", tokens_count);
|
||||
vfs_path_free (vpath);
|
||||
|
||||
vpath = vfs_path_from_str (
|
||||
"/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/test2://#enc:KOI8-R/bla-bla/some/path/test3://111/22/33"
|
||||
);
|
||||
tokens_count = vfs_path_tokens_count(vpath);
|
||||
fail_unless (tokens_count == 11, "actual: %zu; expected: 11\n", tokens_count);
|
||||
vfs_path_free (vpath);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
|
||||
#define check_invalid_token_str(input, start, length) { \
|
||||
vpath = vfs_path_from_str (input); \
|
||||
path_tokens = vfs_path_tokens_get(vpath, start, length); \
|
||||
fail_unless (path_tokens == NULL, "path_tokens should be NULL!\n"); \
|
||||
g_free (path_tokens); \
|
||||
vfs_path_free (vpath); \
|
||||
}
|
||||
|
||||
#define check_token_str(input, start, length, etalon) { \
|
||||
vpath = vfs_path_from_str_flags (input, VPF_NO_CANON); \
|
||||
path_tokens = vfs_path_tokens_get(vpath, start, length); \
|
||||
fail_unless (path_tokens != NULL, "path_tokens shouldn't equal to NULL!\n"); \
|
||||
if (path_tokens != NULL) \
|
||||
fail_unless (strcmp(path_tokens, etalon) == 0, "\nactual: '%s'\netalon: '%s'", path_tokens, etalon); \
|
||||
g_free (path_tokens); \
|
||||
vfs_path_free (vpath); \
|
||||
}
|
||||
|
||||
START_TEST (test_vfs_path_tokens_get)
|
||||
{
|
||||
vfs_path_t *vpath;
|
||||
char *path_tokens;
|
||||
|
||||
/* Invalid start position */
|
||||
check_invalid_token_str ("/" , 2, 1);
|
||||
|
||||
/* Invalid negative position */
|
||||
check_invalid_token_str ("/path" , -3, 1);
|
||||
|
||||
/* Count of tokens is zero. Count should be autocorrected */
|
||||
check_token_str ("/path", 0, 0, "path");
|
||||
|
||||
/* get 'path2/path3' by 1,2 */
|
||||
check_token_str ("/path1/path2/path3/path4", 1, 2, "path2/path3");
|
||||
|
||||
/* get 'path2/path3' by 1,2 from LOCAL VFS */
|
||||
check_token_str ("test3://path1/path2/path3/path4", 1, 2, "path2/path3");
|
||||
|
||||
/* get 'path2/path3' by 1,2 from LOCAL VFS with encoding */
|
||||
check_token_str ("test3://path1/path2/test3://#enc:KOI8-R/path3/path4", 1, 2, "path2/test3://#enc:KOI8-R/path3");
|
||||
|
||||
/* get 'path2/path3' by 1,2 with encoding */
|
||||
check_token_str ("#enc:KOI8-R/path1/path2/path3/path4", 1, 2, "#enc:KOI8-R/path2/path3");
|
||||
|
||||
/* get 'path2/path3' by 1,2 from non-LOCAL VFS */
|
||||
check_token_str ("test2://path1/path2/path3/path4", 1, 2, "test2://path2/path3");
|
||||
|
||||
/* get 'path2/path3' by 1,2 throught non-LOCAL VFS */
|
||||
check_token_str ("/path1/path2/test1://user:pass@some.host:12345/path3/path4", 1, 2, "path2/test1://user:pass@some.host:12345/path3");
|
||||
|
||||
/* get 'path2/path3' by 1,2 from LOCAL VFS */
|
||||
/* TODO: currently this test don't passed. Probably broken string URI parser */
|
||||
/* check_token_str ("test3://path1/path2/test2://test3://path3/path4", 1, 2, "path2/path3"); */
|
||||
|
||||
/* get 'path2/path3' by 1,2 where path2 it's LOCAL VFS */
|
||||
check_token_str ("test3://path1/path2/test2://path3/path4", 1, 2, "path2/test2://path3");
|
||||
|
||||
/* get 'path2/path3' by 1,2 where path3 it's LOCAL VFS */
|
||||
check_token_str ("test2://path1/path2/test3://path3/path4", 1, 2, "test2://path2/test3://path3");
|
||||
|
||||
/* get 'path4' by -1,1 */
|
||||
check_token_str ("/path1/path2/path3/path4", -1, 1, "path4");
|
||||
|
||||
/* get 'path2/path3/path4' by -3,0 */
|
||||
check_token_str ("/path1/path2/path3/path4", -3, 0, "path2/path3/path4");
|
||||
|
||||
}
|
||||
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_vfs_path_tokens_count);
|
||||
tcase_add_test (tc_core, test_vfs_path_tokens_get);
|
||||
/* *********************************** */
|
||||
|
||||
suite_add_tcase (s, tc_core);
|
||||
sr = srunner_create (s);
|
||||
srunner_set_log (sr, "path_manipulations.log");
|
||||
srunner_run_all (sr, CK_NORMAL);
|
||||
number_failed = srunner_ntests_failed (sr);
|
||||
srunner_free (sr);
|
||||
return (number_failed == 0) ? 0 : 1;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
Loading…
Reference in New Issue
Block a user