From fdedfb5d3ba07e76081fdeccea9984bf1cafe752 Mon Sep 17 00:00:00 2001 From: Slava Zanko Date: Sun, 25 Sep 2011 14:36:44 +0300 Subject: [PATCH] Added new fnction for manipulate vpath objects: * vfs_path_append_vpath_new () * vfs_path_vtokens_get () Signed-off-by: Slava Zanko --- lib/vfs/path.c | 70 ++++++++++++++++++++++++++++++++++++++ lib/vfs/path.h | 4 ++- tests/lib/vfs/path_manipulations.c | 23 +++++++++++++ 3 files changed, 96 insertions(+), 1 deletion(-) diff --git a/lib/vfs/path.c b/lib/vfs/path.c index 84584d506..b9eab52c1 100644 --- a/lib/vfs/path.c +++ b/lib/vfs/path.c @@ -1152,6 +1152,46 @@ vfs_path_append_new (const vfs_path_t * vpath, const char *first_element, ...) } /* --------------------------------------------------------------------------------------------- */ + +/** + * Append vpath_t tokens to path object + * + * @param ... NULL-terminated vpath objects + * + * @return newly allocated path object + */ + +vfs_path_t * +vfs_path_append_vpath_new (const vfs_path_t * first_vpath, ...) +{ + va_list args; + vfs_path_t *ret_vpath; + const vfs_path_t *current_vpath = first_vpath; + + if (first_vpath == NULL) + return NULL; + + ret_vpath = vfs_path_new (); + + va_start (args, first_vpath); + do + { + int vindex; + + for (vindex = 0; vindex < vfs_path_elements_count (current_vpath); vindex++) + ret_vpath->path = + g_list_append (ret_vpath->path, + vfs_path_element_clone (vfs_path_get_by_index + (current_vpath, vindex))); + current_vpath = va_arg (args, const vfs_path_t *); + } + while (current_vpath != NULL); + va_end (args); + + return ret_vpath; +} + +/* --------------------------------------------------------------------------------------------- */ /** * get tockens count in path. * @@ -1207,6 +1247,9 @@ vfs_path_tokens_get (const vfs_path_t * vpath, ssize_t start_position, size_t le int element_index; size_t tokens_count = vfs_path_tokens_count (vpath); + if (vpath == NULL) + return NULL; + if (length == 0) length = tokens_count; @@ -1266,3 +1309,30 @@ vfs_path_tokens_get (const vfs_path_t * vpath, ssize_t start_position, size_t le } /* --------------------------------------------------------------------------------------------- */ +/** + * 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 path object with path tokens separated by slash + */ + +vfs_path_t * +vfs_path_vtokens_get (const vfs_path_t * vpath, ssize_t start_position, size_t length) +{ + char *str_tokens; + vfs_path_t *ret_vpath = NULL; + + str_tokens = vfs_path_tokens_get (vpath, start_position, length); + if (str_tokens != NULL) + { + ret_vpath = vfs_path_from_str_flags (str_tokens, VPF_NO_CANON); + g_free (str_tokens); + } + return ret_vpath; +} + +/* --------------------------------------------------------------------------------------------- */ diff --git a/lib/vfs/path.h b/lib/vfs/path.h index a2266cd74..600ae5f84 100644 --- a/lib/vfs/path.h +++ b/lib/vfs/path.h @@ -58,9 +58,11 @@ char *vfs_path_to_str_elements_count (const vfs_path_t * path, int elements_coun 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, ...); +vfs_path_t *vfs_path_append_new (const vfs_path_t * vpath, const char *first_element, ...); +vfs_path_t *vfs_path_append_vpath_new (const vfs_path_t * first_vpath, ...); 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_t *vfs_path_vtokens_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); diff --git a/tests/lib/vfs/path_manipulations.c b/tests/lib/vfs/path_manipulations.c index 74e5e9bb3..b4823196a 100644 --- a/tests/lib/vfs/path_manipulations.c +++ b/tests/lib/vfs/path_manipulations.c @@ -209,6 +209,28 @@ END_TEST /* --------------------------------------------------------------------------------------------- */ +START_TEST (test_vfs_path_append_vpath) +{ + vfs_path_t *vpath1, *vpath2, *vpath3; + + vpath1 = 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"); + vpath2 = vfs_path_from_str("/local/path/test1://user:pass@some.host:12345/bla-bla/some/path/"); + + vpath3 = vfs_path_append_vpath_new (vpath1, vpath2, NULL); + + fail_unless (vfs_path_elements_count(vpath3) == 6, + "\nvpath elements count should be %d, actial is %d\n", + 6, + vfs_path_elements_count(vpath3) + ); + vfs_path_free (vpath1); + vfs_path_free (vpath2); + vfs_path_free (vpath3); + +} +END_TEST + +/* --------------------------------------------------------------------------------------------- */ int main (void) @@ -224,6 +246,7 @@ main (void) /* Add new tests here: *************** */ tcase_add_test (tc_core, test_vfs_path_tokens_count); tcase_add_test (tc_core, test_vfs_path_tokens_get); + tcase_add_test (tc_core, test_vfs_path_append_vpath); /* *********************************** */ suite_add_tcase (s, tc_core); -- 2.11.4.GIT