diff options
author | David Robillard <d@drobilla.net> | 2020-12-18 17:01:09 +0100 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2020-12-18 17:38:58 +0100 |
commit | f38626d31547445c1acb73d5838b1655cf8a2639 (patch) | |
tree | 37a6c4b1c4bf4996ec2f995f33c23ddbd4a97c2a /src | |
parent | 189435d25fe03148d4a701e2cd79a931bf26796c (diff) | |
download | lilv-f38626d31547445c1acb73d5838b1655cf8a2639.tar.gz lilv-f38626d31547445c1acb73d5838b1655cf8a2639.tar.bz2 lilv-f38626d31547445c1acb73d5838b1655cf8a2639.zip |
Windows: Prefer backslash as a path separator
This situation is, as always, a total nightmare. In an attempt to not make
weird paths with mixed separators, a heuristic is used here which uses forward
slash if it seems that the input paths do. Otherwise, backslash (the
"preferred" separator on Windows despite all good sense) is used.
Diffstat (limited to 'src')
-rw-r--r-- | src/filesystem.c | 25 |
1 files changed, 24 insertions, 1 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), |