diff options
-rw-r--r-- | src/filesystem.c | 50 | ||||
-rw-r--r-- | src/filesystem.h | 10 | ||||
-rw-r--r-- | src/state.c | 6 | ||||
-rw-r--r-- | test/test_filesystem.c | 24 |
4 files changed, 3 insertions, 87 deletions
diff --git a/src/filesystem.c b/src/filesystem.c index 734e258..2305c6d 100644 --- a/src/filesystem.c +++ b/src/filesystem.c @@ -41,56 +41,6 @@ lilv_path_is_child(const char* path, const char* dir) } char* -lilv_path_relative_to(const char* path, const char* base) -{ - const size_t path_len = strlen(path); - const size_t base_len = strlen(base); - const size_t min_len = (path_len < base_len) ? path_len : base_len; - - // Find the last separator common to both paths - size_t last_shared_sep = 0; - for (size_t i = 0; i < min_len && path[i] == base[i]; ++i) { - if (lilv_is_dir_sep(path[i])) { - last_shared_sep = i; - } - } - - if (last_shared_sep == 0) { - // No common components, return path - return lilv_strdup(path); - } - - // Find the number of up references ("..") required - size_t up = 0; - for (size_t i = last_shared_sep + 1; i < base_len; ++i) { - if (lilv_is_dir_sep(base[i])) { - ++up; - } - } - -#ifdef _WIN32 - const bool use_slash = strchr(path, '/'); -#else - static const bool use_slash = true; -#endif - - // Write up references - const size_t suffix_len = path_len - last_shared_sep; - char* rel = (char*)calloc(1, suffix_len + (up * 3) + 1); - for (size_t i = 0; i < up; ++i) { - if (use_slash) { - memcpy(rel + (i * 3), "../", 3); - } else { - memcpy(rel + (i * 3), "..\\", 3); - } - } - - // Write suffix - memcpy(rel + (up * 3), path + last_shared_sep + 1, suffix_len); - return rel; -} - -char* lilv_path_parent(const char* path) { const char* s = path + strlen(path) - 1; // Last character diff --git a/src/filesystem.h b/src/filesystem.h index 5dfd3d7..5dbbdcf 100644 --- a/src/filesystem.h +++ b/src/filesystem.h @@ -8,16 +8,6 @@ bool lilv_path_is_child(const char* path, const char* dir); /** - Return `path` relative to `base` if possible. - - If `path` is not within `base`, a copy is returned. Otherwise, an - equivalent path relative to `base` is returned (which may contain - up-references). -*/ -char* -lilv_path_relative_to(const char* path, const char* base); - -/** Return the path to the directory that contains `path`. Returns the root path if `path` is the root path. diff --git a/src/state.c b/src/state.c index 571a591..d98b80b 100644 --- a/src/state.c +++ b/src/state.c @@ -284,10 +284,10 @@ abstract_path(LV2_State_Map_Path_Handle handle, const char* abs_path) if (lilv_path_is_child(real_path, state->dir)) { // File in state directory (loaded, or created by plugin during save) - path = lilv_path_relative_to(real_path, state->dir); + path = zix_path_lexically_relative(NULL, real_path, state->dir); } else if (lilv_path_is_child(real_path, state->scratch_dir)) { // File created by plugin earlier - path = lilv_path_relative_to(real_path, state->scratch_dir); + path = zix_path_lexically_relative(NULL, real_path, state->scratch_dir); if (state->copy_dir) { ZixStatus st = zix_create_directories(NULL, state->copy_dir); if (st) { @@ -1220,7 +1220,7 @@ lilv_state_make_links(const LilvState* state, const char* dir) } // Make a link in the save directory to the external link - char* target = lilv_path_relative_to(lpath, dir); + char* target = zix_path_lexically_relative(NULL, lpath, dir); maybe_symlink(lpath, path); free(target); free(lpath); diff --git a/test/test_filesystem.c b/test/test_filesystem.c index 4ed1d5e..99e4f3c 100644 --- a/test/test_filesystem.c +++ b/test/test_filesystem.c @@ -38,29 +38,6 @@ test_path_is_child(void) } static void -test_path_relative_to(void) -{ - assert(equals(lilv_path_relative_to("/a/b", "/a/"), "b")); - assert(equals(lilv_path_relative_to("/a", "/b/c/"), "/a")); - assert(equals(lilv_path_relative_to("/a/b/c", "/a/b/d/"), "../c")); - assert(equals(lilv_path_relative_to("/a/b/c", "/a/b/d/e/"), "../../c")); - -#ifdef _WIN32 - assert(equals(lilv_path_relative_to("C:/a/b", "C:/a/"), "b")); - assert(equals(lilv_path_relative_to("C:/a", "C:/b/c/"), "../../a")); - assert(equals(lilv_path_relative_to("C:/a/b/c", "C:/a/b/d/"), "../c")); - assert(equals(lilv_path_relative_to("C:/a/b/c", "C:/a/b/d/e/"), "../../c")); - - assert(equals(lilv_path_relative_to("C:\\a\\b", "C:\\a\\"), "b")); - assert(equals(lilv_path_relative_to("C:\\a", "C:\\b\\c\\"), "..\\..\\a")); - assert( - equals(lilv_path_relative_to("C:\\a\\b\\c", "C:\\a\\b\\d\\"), "..\\c")); - assert(equals(lilv_path_relative_to("C:\\a\\b\\c", "C:\\a\\b\\d\\e\\"), - "..\\..\\c")); -#endif -} - -static void test_path_parent(void) { assert(equals(lilv_path_parent("/"), "/")); @@ -102,7 +79,6 @@ int main(void) { test_path_is_child(); - test_path_relative_to(); test_path_parent(); test_path_filename(); |