From 1dac5f18c12c89933d6ea8264a10a2ef02304e74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonard=20Ikl=C3=A9?= Date: Fri, 2 Oct 2020 18:14:24 +0200 Subject: [PATCH] fixed cwk_path_get_absolute with a relative base path for windows --- CMakeLists.txt | 3 ++- docs/reference/cwk_path_get_absolute.md | 2 +- include/cwalk.h | 2 +- src/cwalk.c | 3 +++ test/absolute_test.c | 21 ++++++++++++++++++++- 5 files changed, 27 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 32ee05c..6ed50e1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -45,7 +45,8 @@ target_include_directories(${LIBRARY_TARGET} PUBLIC "${INCLUDE_DIRECTORY}") enable_testing() create_test(DEFAULT absolute simple) create_test(DEFAULT absolute absolute_path) -create_test(DEFAULT absolute relative_base) +create_test(DEFAULT absolute unix_relative_base) +create_test(DEFAULT absolute windows_relative_base) create_test(DEFAULT absolute mixed) create_test(DEFAULT absolute normalization) create_test(DEFAULT absolute too_far) diff --git a/docs/reference/cwk_path_get_absolute.md b/docs/reference/cwk_path_get_absolute.md index dfb80a6..2cadaff 100644 --- a/docs/reference/cwk_path_get_absolute.md +++ b/docs/reference/cwk_path_get_absolute.md @@ -15,7 +15,7 @@ size_t cwk_path_get_absolute(const char *base, const char *path, char *buffer, This function generates an absolute path based on a base path and another path. It is guaranteed to return an absolute path. If the second submitted path is absolute, it will override the base path. The result will be written to a buffer, which might be truncated if the buffer is not large enough to hold the full path. However, the truncated result will always be null-terminated. The returned value is the amount of characters which the resulting path would take if it was not truncated (excluding the null-terminating character). ## Parameters - * **base**: The base path on which the relative path will be applied. + * **base**: The absolute base path on which the relative path will be applied. * **path**: The relative path which will be applied on the base path. * **buffer**: The buffer where the result will be written to. * **buffer_size**: The size of the result buffer. diff --git a/include/cwalk.h b/include/cwalk.h index ed6c05a..3a94133 100644 --- a/include/cwalk.h +++ b/include/cwalk.h @@ -62,7 +62,7 @@ enum cwk_path_style * resulting path would take if it was not truncated (excluding the * null-terminating character). * - * @param base The base path on which the relative path will be applied. + * @param base The absolute base path on which the relative path will be applied. * @param path The relative path which will be applied on the base path. * @param buffer The buffer where the result will be written to. * @param buffer_size The size of the result buffer. diff --git a/src/cwalk.c b/src/cwalk.c index e953c42..7791152 100755 --- a/src/cwalk.c +++ b/src/cwalk.c @@ -650,6 +650,9 @@ size_t cwk_path_get_absolute(const char *base, const char *path, char *buffer, // root at the beginning. if (cwk_path_is_absolute(base)) { i = 0; + } else if (path_style == CWK_STYLE_WINDOWS) { + paths[0] = "\\"; + i = 1; } else { paths[0] = "/"; i = 1; diff --git a/test/absolute_test.c b/test/absolute_test.c index 7a0d4f5..1ef1470 100755 --- a/test/absolute_test.c +++ b/test/absolute_test.c @@ -91,7 +91,7 @@ int absolute_mixed() return EXIT_SUCCESS; } -int absolute_relative_base() +int absolute_unix_relative_base() { char buffer[FILENAME_MAX]; size_t length; @@ -110,6 +110,25 @@ int absolute_relative_base() return EXIT_SUCCESS; } +int absolute_windows_relative_base() +{ + char buffer[FILENAME_MAX]; + size_t length; + + cwk_path_set_style(CWK_STYLE_WINDOWS); + length = cwk_path_get_absolute("hello\\there", "test", buffer, sizeof(buffer)); + + if (length != 17) { + return EXIT_FAILURE; + } + + if (strcmp(buffer, "\\hello\\there\\test") != 0) { + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} + int absolute_absolute_path() { char buffer[FILENAME_MAX];