mirror of
https://github.com/likle/cwalk.git
synced 2026-10-28 09:44:01 +00:00
#17 improved behaviour for relative path generation with unequal roots
This commit is contained in:
1 parent
c5addcfc44
commit
82eaf2ad3e
2 files changed
+50
-42
No files matched your search
+2
-2
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
+48
-40
@@ -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];
|
||||
|
||||
Reference in new issue
Block a user