From 8f1192bb2361fcd7907b926e86c41a171ed04ad0 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Tue, 10 Nov 2020 09:45:15 +0100 Subject: Add serd_new_real_file_uri() --- src/node.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'src/node.c') diff --git a/src/node.c b/src/node.c index 566e8839..76c12235 100644 --- a/src/node.c +++ b/src/node.c @@ -14,6 +14,8 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#define _XOPEN_SOURCE 600 /* for realpath */ + #include "node.h" #include "namespaces.h" @@ -24,6 +26,11 @@ #include "exess/exess.h" #include "serd/serd.h" +#ifdef _WIN32 +# define WIN32_LEAN_AND_MEAN 1 +# include +#endif + #include #include #include @@ -539,6 +546,32 @@ is_uri_path_char(const char c) } } +static char* +serd_realpath(const char* const path) +{ + if (!path) { + return NULL; + } + +#ifdef _WIN32 + const DWORD size = GetFullPathName(path, 0, NULL, NULL); + if (size == 0) { + return NULL; + } + + char* const out = (char*)calloc(size, 1); + const DWORD ret = GetFullPathName(path, MAX_PATH, out, NULL); + if (ret == 0 || ret >= size) { + free(out); + return NULL; + } + + return out; +#else + return realpath(path, NULL); +#endif +} + SerdNode* serd_new_file_uri(const SerdStringView path, const SerdStringView hostname) { @@ -586,6 +619,20 @@ serd_new_file_uri(const SerdStringView path, const SerdStringView hostname) return node; } +SerdNode* +serd_new_real_file_uri(const char* const path, const char* const hostname) +{ + char* const real_path = serd_realpath(path); + if (!real_path) { + return NULL; + } + + SerdNode* const node = serd_new_file_uri(SERD_MEASURE_STRING(real_path), + SERD_MEASURE_STRING(hostname)); + free(real_path); + return node; +} + typedef size_t (*SerdWriteLiteralFunc)(const void* const user_data, const size_t buf_size, char* const buf); -- cgit v1.2.1