summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/filesystem.c25
-rw-r--r--test/test_filesystem.c34
2 files changed, 55 insertions, 4 deletions
diff --git a/src/filesystem.c b/src/filesystem.c
index abf40e9..bed9f81 100644
--- a/src/filesystem.c
+++ b/src/filesystem.c
@@ -172,11 +172,21 @@ lilv_path_relative_to(const char* path, const char* base)
}
}
+#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) {
- memcpy(rel + (i * 3), "../", 3);
+ if (use_slash) {
+ memcpy(rel + (i * 3), "../", 3);
+ } else {
+ memcpy(rel + (i * 3), "..\\", 3);
+ }
}
// Write suffix
@@ -237,7 +247,20 @@ lilv_path_join(const char* a, const char* b)
const size_t pre_len = a_len - (a_end_is_sep ? 1 : 0);
char* path = (char*)calloc(1, a_len + b_len + 2);
memcpy(path, a, pre_len);
+
+#ifdef _WIN32
+ // Use forward slash if it seems that the input paths do
+ const bool a_has_slash = strchr(a, '/');
+ const bool b_has_slash = b && strchr(b, '/');
+ if (a_has_slash || b_has_slash) {
+ path[pre_len] = '/';
+ } else {
+ path[pre_len] = '\\';
+ }
+#else
path[pre_len] = '/';
+#endif
+
if (b) {
memcpy(path + pre_len + 1,
b + (lilv_is_dir_sep(b[0]) ? 1 : 0),
diff --git a/test/test_filesystem.c b/test/test_filesystem.c
index 4faaf15..7e1968d 100644
--- a/test/test_filesystem.c
+++ b/test/test_filesystem.c
@@ -138,6 +138,18 @@ test_path_relative_to(void)
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
@@ -183,14 +195,25 @@ test_path_join(void)
{
assert(lilv_path_join(NULL, NULL) == NULL);
assert(lilv_path_join(NULL, "") == NULL);
+
+#ifdef _WIN32
+ assert(equals(lilv_path_join("", NULL), "\\"));
+ assert(equals(lilv_path_join("", ""), "\\"));
+ assert(equals(lilv_path_join("a", ""), "a\\"));
+ assert(equals(lilv_path_join("a", NULL), "a\\"));
+ assert(equals(lilv_path_join("a", "b"), "a\\b"));
+#else
assert(equals(lilv_path_join("", NULL), "/"));
assert(equals(lilv_path_join("", ""), "/"));
+ assert(equals(lilv_path_join("a", ""), "a/"));
+ assert(equals(lilv_path_join("a", NULL), "a/"));
+ assert(equals(lilv_path_join("a", "b"), "a/b"));
+#endif
assert(equals(lilv_path_join("/a", ""), "/a/"));
assert(equals(lilv_path_join("/a/b", ""), "/a/b/"));
assert(equals(lilv_path_join("/a/", ""), "/a/"));
assert(equals(lilv_path_join("/a/b/", ""), "/a/b/"));
- assert(equals(lilv_path_join("a", ""), "a/"));
assert(equals(lilv_path_join("a/b", ""), "a/b/"));
assert(equals(lilv_path_join("a/", ""), "a/"));
assert(equals(lilv_path_join("a/b/", ""), "a/b/"));
@@ -199,20 +222,25 @@ test_path_join(void)
assert(equals(lilv_path_join("/a/b", NULL), "/a/b/"));
assert(equals(lilv_path_join("/a/", NULL), "/a/"));
assert(equals(lilv_path_join("/a/b/", NULL), "/a/b/"));
- assert(equals(lilv_path_join("a", NULL), "a/"));
assert(equals(lilv_path_join("a/b", NULL), "a/b/"));
assert(equals(lilv_path_join("a/", NULL), "a/"));
assert(equals(lilv_path_join("a/b/", NULL), "a/b/"));
assert(equals(lilv_path_join("/a", "b"), "/a/b"));
assert(equals(lilv_path_join("/a/", "b"), "/a/b"));
- assert(equals(lilv_path_join("a", "b"), "a/b"));
assert(equals(lilv_path_join("a/", "b"), "a/b"));
assert(equals(lilv_path_join("/a", "b/"), "/a/b/"));
assert(equals(lilv_path_join("/a/", "b/"), "/a/b/"));
assert(equals(lilv_path_join("a", "b/"), "a/b/"));
assert(equals(lilv_path_join("a/", "b/"), "a/b/"));
+
+#ifdef _WIN32
+ assert(equals(lilv_path_join("C:/a", "b"), "C:/a/b"));
+ assert(equals(lilv_path_join("C:\\a", "b"), "C:\\a\\b"));
+ assert(equals(lilv_path_join("C:/a", "b/"), "C:/a/b/"));
+ assert(equals(lilv_path_join("C:\\a", "b\\"), "C:\\a\\b\\"));
+#endif
}
static void