From b58174f0ebe31fb56fe891bfbcf10979079053e3 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Fri, 18 Dec 2020 16:54:14 +0100 Subject: Add lilv_path_absolute_child() --- src/filesystem.c | 10 ++++++++++ src/filesystem.h | 9 +++++++++ test/test_filesystem.c | 21 +++++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/src/filesystem.c b/src/filesystem.c index 0ec9802..0482336 100644 --- a/src/filesystem.c +++ b/src/filesystem.c @@ -134,6 +134,16 @@ lilv_path_absolute(const char* path) } } +char* +lilv_path_absolute_child(const char* path, const char* parent) +{ + if (lilv_path_is_absolute(path)) { + return lilv_strdup(path); + } + + return lilv_path_join(parent, path); +} + char* lilv_path_relative_to(const char* path, const char* base) { diff --git a/src/filesystem.h b/src/filesystem.h index 319dd49..f888f5f 100644 --- a/src/filesystem.h +++ b/src/filesystem.h @@ -42,6 +42,15 @@ lilv_path_current(void); char* lilv_path_absolute(const char* path); +/** + Return `path` as an absolute path relative to `parent`. + + If `path` is absolute, an identical copy of it is returned. Otherwise, the + returned path is relative to `parent`. +*/ +char* +lilv_path_absolute_child(const char* path, const char* parent); + /** Return `path` relative to `base` if possible. diff --git a/test/test_filesystem.c b/test/test_filesystem.c index a3288ed..01ca5ae 100644 --- a/test/test_filesystem.c +++ b/test/test_filesystem.c @@ -104,6 +104,26 @@ test_path_absolute(void) free(cwd); } +static void +test_path_absolute_child(void) +{ + const char* const parent = "/parent"; + const char* const short_path = "a"; + const char* const long_path = "a/b/c"; + + char* const expected_short = lilv_path_join(parent, short_path); + char* const expected_long = lilv_path_join(parent, long_path); + + assert(equals(lilv_path_absolute_child(short_path, parent), + expected_short)); + + assert(equals(lilv_path_absolute_child(long_path, parent), + expected_long)); + + free(expected_long); + free(expected_short); +} + static void test_path_relative_to(void) { @@ -478,6 +498,7 @@ main(void) test_path_is_child(); test_path_current(); test_path_absolute(); + test_path_absolute_child(); test_path_relative_to(); test_path_parent(); test_path_filename(); -- cgit v1.2.1