From c5addcfc449869eb1eaaa7f97bc49604623c4d8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonard=20Ikl=C3=A9?= Date: Thu, 26 Nov 2020 11:13:34 +0100 Subject: [PATCH] #17 fixed relative path generation with root path --- CMakeLists.txt | 2 ++ src/cwalk.c | 4 ++++ test/relative_test.c | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index b43d481..224f8f0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -185,6 +185,8 @@ if(ENABLE_TESTS) create_test(DEFAULT relative skip_all) create_test(DEFAULT relative different_roots) create_test(DEFAULT relative check) + create_test(DEFAULT relative root_path_unix) + create_test(DEFAULT relative root_path_windows) create_test(DEFAULT root absolute) create_test(DEFAULT root unc) create_test(DEFAULT root device_unc) diff --git a/src/cwalk.c b/src/cwalk.c index 7791152..84adee5 100755 --- a/src/cwalk.c +++ b/src/cwalk.c @@ -184,10 +184,13 @@ static bool cwk_path_get_first_segment_without_root(const char *path, // why this has to be done first. segment->path = path; segment->segments = segments; + segment->begin = segments; + segment->end = segments; // Now let's check whether this is an empty string. An empty string has no // segment it could use. if (*segments == '\0') { + segment->size = 0; return false; } @@ -197,6 +200,7 @@ static bool cwk_path_get_first_segment_without_root(const char *path, while (cwk_path_is_separator(segments)) { ++segments; if (*segments == '\0') { + segment->size = 0; return false; } } diff --git a/test/relative_test.c b/test/relative_test.c index edfb225..55539a1 100755 --- a/test/relative_test.c +++ b/test/relative_test.c @@ -225,6 +225,46 @@ int relative_relative() return EXIT_SUCCESS; } +int relative_root_path_unix() +{ + char result[FILENAME_MAX]; + size_t length; + + cwk_path_set_style(CWK_STYLE_UNIX); + + length = cwk_path_get_relative("/this/is/path_one", "/", result, + sizeof(result)); + if (length != 8) { + return EXIT_FAILURE; + } + + if (strcmp(result, "../../..") != 0) { + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} + +int relative_root_path_windows() +{ + char result[FILENAME_MAX]; + size_t length; + + cwk_path_set_style(CWK_STYLE_WINDOWS); + + length = cwk_path_get_relative("C:\\this\\is\\path_one", "C:\\", result, + sizeof(result)); + if (length != 8) { + return EXIT_FAILURE; + } + + if (strcmp(result, "..\\..\\..") != 0) { + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} + int relative_simple() { char result[FILENAME_MAX];