Skip to content

Commit

Permalink
fixed cwk_path_get_absolute with a relative base path for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
likle committed Oct 2, 2020
1 parent ff447b5 commit 1dac5f1
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/cwk_path_get_absolute.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion include/cwalk.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions src/cwalk.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
21 changes: 20 additions & 1 deletion test/absolute_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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];
Expand Down

0 comments on commit 1dac5f1

Please sign in to comment.