From 82eaf2ad3e5f2522e7bad633375c3929087fbda4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonard=20Ikl=C3=A9?= Date: Thu, 26 Nov 2020 11:26:39 +0100 Subject: [PATCH] #17 improved behaviour for relative path generation with unequal roots --- src/cwalk.c | 4 +- test/relative_test.c | 88 ++++++++++++++++++++++++-------------------- 2 files changed, 50 insertions(+), 42 deletions(-) diff --git a/src/cwalk.c b/src/cwalk.c index 84adee5..c20e8a7 100755 --- a/src/cwalk.c +++ b/src/cwalk.c @@ -186,11 +186,11 @@ static bool cwk_path_get_first_segment_without_root(const char *path, segment->segments = segments; segment->begin = segments; segment->end = segments; + segment->size = 0; // 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; } @@ -200,7 +200,6 @@ 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; } } @@ -731,6 +730,7 @@ size_t cwk_path_get_relative(const char *base_directory, const char *path, cwk_path_get_root(base_directory, &base_root_length); cwk_path_get_root(path, &path_root_length); if (!cwk_path_is_string_equal(base_directory, path, base_root_length)) { + cwk_path_terminate_output(buffer, buffer_size, pos); return pos; } diff --git a/test/relative_test.c b/test/relative_test.c index 55539a1..2563da4 100755 --- a/test/relative_test.c +++ b/test/relative_test.c @@ -6,6 +6,46 @@ #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) +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_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_check() { const char *relative_paths[] = {"..", "test", "test/test", "../another_test", @@ -38,11 +78,19 @@ int relative_different_roots() cwk_path_set_style(CWK_STYLE_WINDOWS); + *result = 1; + length = cwk_path_get_relative("C:/path/same", "D:/path/same", result, sizeof(result)); + if (length != 0) { return EXIT_FAILURE; } + + if (*result != '\0') { + return EXIT_FAILURE; + } + return EXIT_SUCCESS; } @@ -225,46 +273,6 @@ 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];