diff options
author | David Robillard <d@drobilla.net> | 2016-07-10 19:07:14 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2016-07-10 19:07:14 -0400 |
commit | 1e8c93c6e760c4453c4fd1f471feebd7886692d3 (patch) | |
tree | 0a7b77f7f27db3ceb401658cf18c883352dc7308 | |
parent | af9356818981e83d5eb9430d4717b19c7316c154 (diff) | |
download | serd-1e8c93c6e760c4453c4fd1f471feebd7886692d3.tar.gz serd-1e8c93c6e760c4453c4fd1f471feebd7886692d3.tar.bz2 serd-1e8c93c6e760c4453c4fd1f471feebd7886692d3.zip |
Add serd_node_new_relative_uri()
-rw-r--r-- | NEWS | 5 | ||||
-rw-r--r-- | serd/serd.h | 21 | ||||
-rw-r--r-- | src/node.c | 26 | ||||
-rw-r--r-- | tests/serd_test.c | 16 | ||||
-rw-r--r-- | wscript | 2 |
5 files changed, 64 insertions, 6 deletions
@@ -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 <d@drobilla.net> Sat, 09 Jul 2016 11:27:45 -0400 + -- David Robillard <d@drobilla.net> 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). @@ -475,6 +475,25 @@ 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. The resulting node will always contain a `.', start with a digit, and end @@ -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 <http://drobilla.net> + Copyright 2011-2016 David Robillard <http://drobilla.net> 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")); @@ -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 |