From 1e8c93c6e760c4453c4fd1f471feebd7886692d3 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 10 Jul 2016 19:07:14 -0400 Subject: Add serd_node_new_relative_uri() --- NEWS | 5 +++-- serd/serd.h | 21 ++++++++++++++++++++- src/node.c | 26 ++++++++++++++++++++++++++ tests/serd_test.c | 16 ++++++++++++++-- wscript | 2 +- 5 files changed, 64 insertions(+), 6 deletions(-) diff --git a/NEWS b/NEWS index e469e9f6..0b22d3e0 100644 --- a/NEWS +++ b/NEWS @@ -1,5 +1,6 @@ -serd (0.22.4) unstable; +serd (0.23.0) unstable; + * Add serd_node_new_relative_uri() * Fix construction and comparison of URIs with UTF-8 characters * Report I/O errors with message and return appropriate status code * Fix potential out of bounds read @@ -7,7 +8,7 @@ serd (0.22.4) unstable; * Fix documentation generation * Update serdi man page - -- David Robillard Sat, 09 Jul 2016 11:27:45 -0400 + -- David Robillard Sun, 10 Jul 2016 18:55:37 -0400 serd (0.22.0) stable; diff --git a/serd/serd.h b/serd/serd.h index c34eed01..9e0bf8e9 100644 --- a/serd/serd.h +++ b/serd/serd.h @@ -463,7 +463,7 @@ serd_node_new_file_uri(const uint8_t* path, /** Create a new node by serialising `uri` into a new string. - @param uri The URI to parse and serialise. + @param uri The URI to serialise. @param base Base URI to resolve `uri` against (or NULL for no resolution). @@ -474,6 +474,25 @@ SERD_API SerdNode serd_node_new_uri(const SerdURI* uri, const SerdURI* base, SerdURI* out); +/** + Create a new node by serialising `uri` into a new relative URI. + + @param uri The URI to serialise. + + @param base Base URI to make `uri` relative to, if possible. + + @param root Root URI for resolution (see serd_uri_serialise_relative()). + + @param out Set to the parsing of the new URI (i.e. points only to + memory owned by the new returned node). +*/ +SERD_API +SerdNode +serd_node_new_relative_uri(const SerdURI* uri, + const SerdURI* base, + const SerdURI* root, + SerdURI* out); + /** Create a new node by serialising `d` into an xsd:decimal string. diff --git a/src/node.c b/src/node.c index 7d50f326..2a3a44b2 100644 --- a/src/node.c +++ b/src/node.c @@ -212,6 +212,32 @@ serd_node_new_uri(const SerdURI* uri, const SerdURI* base, SerdURI* out) return node; } +SERD_API +SerdNode +serd_node_new_relative_uri(const SerdURI* uri, + const SerdURI* base, + const SerdURI* root, + SerdURI* out) +{ + const size_t uri_len = serd_uri_string_length(uri); + const size_t base_len = serd_uri_string_length(base); + uint8_t* buf = (uint8_t*)malloc(uri_len + base_len + 1); + SerdNode node = { buf, 0, 0, 0, SERD_URI }; + uint8_t* ptr = buf; + const size_t actual_len = serd_uri_serialise_relative( + uri, base, root, string_sink, &ptr); + + buf[actual_len] = '\0'; + node.n_bytes = actual_len; + node.n_chars = serd_strlen(buf, NULL, NULL); + + if (out) { + serd_uri_parse(buf, out); // TODO: cleverly avoid double parse + } + + return node; +} + static inline unsigned serd_digits(double abs) { diff --git a/tests/serd_test.c b/tests/serd_test.c index a0a6c47f..14534878 100644 --- a/tests/serd_test.c +++ b/tests/serd_test.c @@ -1,5 +1,5 @@ /* - Copyright 2011-2015 David Robillard + Copyright 2011-2016 David Robillard Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above @@ -415,10 +415,22 @@ main(void) nil2.type != SERD_URI || strcmp((const char*)nil2.buf, (const char*)base.buf)) { return failure("URI %s != base %s\n", nil.buf, base.buf); } - serd_node_free(&base); serd_node_free(&nil); serd_node_free(&nil2); + // Test serd_node_new_relative_uri + SerdNode abs = serd_node_from_string(SERD_URI, USTR("http://example.org/foo/bar")); + SerdURI abs_uri; + serd_uri_parse(abs.buf, &abs_uri); + + SerdURI rel_uri; + SerdNode rel = serd_node_new_relative_uri(&abs_uri, &base_uri, NULL, &rel_uri); + if (strcmp((const char*)rel.buf, "/foo/bar")) { + return failure("Bad relative URI %s (expected '/foo/bar')\n", rel.buf); + } + + serd_node_free(&base); + // Test SerdEnv SerdNode u = serd_node_from_string(SERD_URI, USTR("http://example.org/foo")); diff --git a/wscript b/wscript index 289ba0c5..308e1013 100644 --- a/wscript +++ b/wscript @@ -11,7 +11,7 @@ import waflib.extras.autowaf as autowaf # major increment <=> incompatible changes # minor increment <=> compatible changes (additions) # micro increment <=> no interface changes -SERD_VERSION = '0.22.4' +SERD_VERSION = '0.23.0' SERD_MAJOR_VERSION = '0' # Mandatory waf variables -- cgit v1.2.1