aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--serd/serd.h97
-rw-r--r--src/env.c42
-rw-r--r--src/env.h24
-rw-r--r--src/node.c146
-rw-r--r--src/node.h1
-rw-r--r--src/serdi.c12
-rw-r--r--src/string_utils.h16
-rw-r--r--src/writer.c31
-rw-r--r--tests/serd_test.c162
9 files changed, 279 insertions, 252 deletions
diff --git a/serd/serd.h b/serd/serd.h
index 0f937b24..89f612a3 100644
--- a/serd/serd.h
+++ b/serd/serd.h
@@ -439,18 +439,11 @@ serd_uri_serialise_relative(const SerdURI* uri,
*/
/**
- Create a new node from `str`.
+ Create a new plain literal string node from `str`.
*/
SERD_API
SerdNode*
-serd_node_new_string(SerdType type, const char* str);
-
-/**
- Create a new node from a prefix of `str`.
-*/
-SERD_API
-SerdNode*
-serd_node_new_substring(SerdType type, const char* str, size_t len);
+serd_node_new_string(const char* str);
/**
Create a new literal node from `str`.
@@ -466,15 +459,19 @@ serd_node_new_literal(const char* str,
/**
Create a new blank node.
-
- Note this function measures `str`, which is a common bottleneck.
- Use sord_node_from_serd_node() instead if `str` is already measured.
*/
SERD_API
SerdNode*
serd_node_new_blank(const char* str);
/**
+ Create a new CURIE node.
+*/
+SERD_API
+SerdNode*
+serd_node_new_curie(const char* str);
+
+/**
Return a deep copy of `node`.
*/
SERD_API
@@ -489,72 +486,60 @@ bool
serd_node_equals(const SerdNode* a, const SerdNode* b);
/**
- Simple wrapper for serd_node_new_uri() to resolve a URI node.
+ Create a new URI from a string.
*/
SERD_API
SerdNode*
-serd_node_new_uri_from_node(const SerdNode* uri_node,
- const SerdURI* base,
- SerdURI* out);
+serd_node_new_uri(const char* str);
/**
- Simple wrapper for serd_node_new_uri() to resolve a URI string.
+ Create a new URI from a string, resolved against a base URI.
*/
SERD_API
SerdNode*
-serd_node_new_uri_from_string(const char* str,
- const SerdURI* base,
- SerdURI* out);
+serd_node_new_resolved_uri(const char* str, const SerdNode* base);
/**
- Create a new file URI node from a file system path and optional hostname.
+ Resolve `node` against `base`.
- Backslashes in Windows paths will be converted and '%' will always be
- percent encoded. If `escape` is true, all other invalid characters will be
- percent encoded as well.
-
- If `path` is relative, `hostname` is ignored.
- If `out` is not NULL, it will be set to the parsed URI.
+ If `node` is not a relative URI, an equivalent new node is returned.
*/
SERD_API
SerdNode*
-serd_node_new_file_uri(const char* path,
- const char* hostname,
- SerdURI* out,
- bool escape);
+serd_node_resolve(const SerdNode* node, const SerdNode* base);
/**
- Create a new node by serialising `uri` into a new string.
-
- @param uri The URI to serialise.
+ Create a new file URI node from a file system path and optional hostname.
- @param base Base URI to resolve `uri` against (or NULL for no resolution).
+ Backslashes in Windows paths will be converted and '%' will always be
+ percent encoded. If `escape` is true, all other invalid characters will be
+ percent encoded as well.
- @param out Set to the parsing of the new URI (i.e. points only to
- memory owned by the new returned node).
+ If `path` is relative, `hostname` is ignored.
+ If `out` is not NULL, it will be set to the parsed URI.
*/
SERD_API
SerdNode*
-serd_node_new_uri(const SerdURI* uri, const SerdURI* base, SerdURI* out);
+serd_node_new_file_uri(const char* path, const char* hostname, bool escape);
/**
- Create a new node by serialising `uri` into a new relative URI.
+ Create a new URI from a string, relative to a base URI.
- @param uri The URI to serialise.
+ @param str URI string.
- @param base Base URI to make `uri` relative to, if possible.
+ @param base Base URI to make `str` relative to, if possible.
- @param root Root URI for resolution (see serd_uri_serialise_relative()).
+ @param root Optional root URI for resolution.
- @param out Set to the parsing of the new URI (i.e. points only to
- memory owned by the new returned node).
+ The URI is made relative iff if it a child of `base` and `root`. The
+ optional `root` parameter must be a prefix of `base` and can be used keep
+ up-references ("../") within a certain namespace.
*/
SERD_API
SerdNode*
-serd_node_new_relative_uri(const SerdURI* uri,
- const SerdURI* base,
- const SerdURI* root,
- SerdURI* out);
+serd_node_new_relative_uri(const char* str,
+ const SerdNode* base,
+ const SerdNode* root);
/**
Create a new node by serialising `d` into an xsd:decimal string.
@@ -772,8 +757,7 @@ serd_env_free(SerdEnv* env);
*/
SERD_API
const SerdNode*
-serd_env_get_base_uri(const SerdEnv* env,
- SerdURI* out);
+serd_env_get_base_uri(const SerdEnv* env);
/**
Set the current base URI.
@@ -986,13 +970,12 @@ serd_reader_free(SerdReader* reader);
*/
SERD_API
SerdWriter*
-serd_writer_new(SerdWorld* world,
- SerdSyntax syntax,
- SerdStyle style,
- SerdEnv* env,
- const SerdURI* base_uri,
- SerdSink ssink,
- void* stream);
+serd_writer_new(SerdWorld* world,
+ SerdSyntax syntax,
+ SerdStyle style,
+ SerdEnv* env,
+ SerdSink ssink,
+ void* stream);
/**
Free `writer`.
diff --git a/src/env.c b/src/env.c
index c81b62b0..7723c70d 100644
--- a/src/env.c
+++ b/src/env.c
@@ -14,6 +14,8 @@
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
+#include "env.h"
+
#include "node.h"
#include "serd/serd.h"
@@ -57,13 +59,15 @@ serd_env_free(SerdEnv* env)
free(env);
}
+const SerdURI*
+serd_env_get_parsed_base_uri(const SerdEnv* env)
+{
+ return &env->base_uri;
+}
+
const SerdNode*
-serd_env_get_base_uri(const SerdEnv* env,
- SerdURI* out)
+serd_env_get_base_uri(const SerdEnv* env)
{
- if (out) {
- *out = env->base_uri;
- }
return env->base_uri_node;
}
@@ -71,14 +75,16 @@ SerdStatus
serd_env_set_base_uri(SerdEnv* env,
const SerdNode* uri)
{
- if (!env || !uri) {
+ if (!env || !uri || uri->type != SERD_URI) {
return SERD_ERR_BAD_ARG;
}
// Resolve base URI and create a new node and URI for it
+ SerdNode* base_uri_node = serd_node_new_resolved_uri_i(
+ serd_node_get_string(uri), &env->base_uri);
+
SerdURI base_uri;
- SerdNode* base_uri_node = serd_node_new_uri_from_node(
- uri, &env->base_uri, &base_uri);
+ serd_uri_parse(serd_node_get_string(base_uri_node), &base_uri);
if (base_uri_node) {
// Replace the current base URI
@@ -137,13 +143,12 @@ serd_env_set_prefix(SerdEnv* env,
serd_env_add(env, name, uri);
} else {
// Resolve relative URI and create a new node and URI for it
- SerdURI abs_uri;
- SerdNode* abs_uri_node = serd_node_new_uri_from_node(
- uri, &env->base_uri, &abs_uri);
+ SerdNode* abs_uri = serd_node_new_resolved_uri_i(
+ serd_node_get_string(uri), &env->base_uri);
// Set prefix to resolved (absolute) URI
- serd_env_add(env, name, abs_uri_node);
- serd_node_free(abs_uri_node);
+ serd_env_add(env, name, abs_uri);
+ serd_node_free(abs_uri);
}
return SERD_SUCCESS;
}
@@ -153,8 +158,8 @@ serd_env_set_prefix_from_strings(SerdEnv* env,
const char* name,
const char* uri)
{
- SerdNode* name_node = serd_node_new_string(SERD_LITERAL, name);
- SerdNode* uri_node = serd_node_new_string(SERD_URI, uri);
+ SerdNode* name_node = serd_node_new_string(name);
+ SerdNode* uri_node = serd_node_new_uri(uri);
const SerdStatus st = serd_env_set_prefix(env, name_node, uri_node);
@@ -228,10 +233,9 @@ serd_env_expand_node(const SerdEnv* env,
ret->n_bytes = len;
return ret;
}
- case SERD_URI: {
- SerdURI ignored;
- return serd_node_new_uri_from_node(node, &env->base_uri, &ignored);
- }
+ case SERD_URI:
+ return serd_node_new_resolved_uri_i(
+ serd_node_get_string(node), &env->base_uri);
default:
return NULL;
}
diff --git a/src/env.h b/src/env.h
new file mode 100644
index 00000000..ecb6cc15
--- /dev/null
+++ b/src/env.h
@@ -0,0 +1,24 @@
+/*
+ Copyright 2011-2018 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
+ copyright notice and this permission notice appear in all copies.
+
+ THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+
+#ifndef SERD_ENV_H
+#define SERD_ENV_H
+
+#include "serd/serd.h"
+
+const SerdURI* serd_env_get_parsed_base_uri(const SerdEnv* env);
+
+#endif // SERD_ENV_H
diff --git a/src/node.c b/src/node.c
index fcea6bea..7f124f13 100644
--- a/src/node.c
+++ b/src/node.c
@@ -38,6 +38,9 @@
static const size_t serd_node_align = sizeof(SerdNode);
+static SerdNode*
+serd_node_new_from_uri(const SerdURI* uri, const SerdURI* base);
+
static size_t
serd_node_pad_size(const size_t n_bytes)
{
@@ -84,31 +87,30 @@ serd_node_set(SerdNode** dst, const SerdNode* src)
}
}
-SerdNode*
-serd_node_new_string(SerdType type, const char* str)
+static SerdNode*
+serd_node_new_simple(SerdType type, const char* str)
{
if (!str) {
return NULL;
}
- uint32_t flags = 0;
- const size_t n_bytes = serd_strlen(str, &flags);
- SerdNode* node = serd_node_malloc(n_bytes, flags, type);
+ const size_t n_bytes = strlen(str);
+ SerdNode* node = serd_node_malloc(n_bytes, 0, type);
memcpy(serd_node_buffer(node), str, n_bytes);
node->n_bytes = n_bytes;
return node;
}
SerdNode*
-serd_node_new_substring(SerdType type, const char* str, const size_t len)
+serd_node_new_string(const char* str)
{
if (!str) {
return NULL;
}
uint32_t flags = 0;
- const size_t n_bytes = serd_substrlen(str, len, &flags);
- SerdNode* node = serd_node_malloc(n_bytes, flags, type);
+ const size_t n_bytes = serd_strlen(str, &flags);
+ SerdNode* node = serd_node_malloc(n_bytes, flags, SERD_LITERAL);
memcpy(serd_node_buffer(node), str, n_bytes);
node->n_bytes = n_bytes;
return node;
@@ -164,15 +166,19 @@ serd_node_new_literal(const char* str,
SerdNode*
serd_node_new_blank(const char* str)
{
- if (!str) {
- return NULL;
- }
+ return serd_node_new_simple(SERD_BLANK, str);
+}
- const size_t n_bytes = strlen(str);
- SerdNode* node = serd_node_malloc(n_bytes, 0, SERD_BLANK);
- memcpy(serd_node_buffer(node), str, n_bytes);
- node->n_bytes = n_bytes;
- return node;
+SerdNode*
+serd_node_new_curie(const char* str)
+{
+ return serd_node_new_simple(SERD_CURIE, str);
+}
+
+SerdNode*
+serd_node_new_uri(const char* str)
+{
+ return serd_node_new_simple(SERD_URI, str);
}
SERD_API
@@ -232,29 +238,43 @@ string_sink(const void* buf, size_t len, void* stream)
}
SerdNode*
-serd_node_new_uri_from_node(const SerdNode* uri_node,
- const SerdURI* base,
- SerdURI* out)
+serd_node_new_resolved_uri(const char* str, const SerdNode* base)
{
- const char* uri_str = serd_node_get_string(uri_node);
- return (uri_node->type == SERD_URI && uri_str)
- ? serd_node_new_uri_from_string(uri_str, base, out)
- : NULL;
+ if (!base || base->type != SERD_URI) {
+ return NULL;
+ }
+
+ SerdURI base_uri;
+ serd_uri_parse(serd_node_get_string(base), &base_uri);
+ return serd_node_new_resolved_uri_i(str, &base_uri);
}
SerdNode*
-serd_node_new_uri_from_string(const char* str,
- const SerdURI* base,
- SerdURI* out)
+serd_node_resolve(const SerdNode* node, const SerdNode* base)
+{
+ if (!node || !base || node->type != SERD_URI || base->type != SERD_URI) {
+ return NULL;
+ }
+
+ SerdURI uri;
+ SerdURI base_uri;
+ serd_uri_parse(serd_node_get_string(node), &uri);
+ serd_uri_parse(serd_node_get_string(base), &base_uri);
+
+ return serd_node_new_from_uri(&uri, &base_uri);
+}
+
+SerdNode*
+serd_node_new_resolved_uri_i(const char* str, const SerdURI* base)
{
if (!str || str[0] == '\0') {
// Empty URI => Base URI, or nothing if no base is given
- return base ? serd_node_new_uri(base, NULL, out) : NULL;
+ return base ? serd_node_new_from_uri(base, NULL) : NULL;
}
SerdURI uri;
serd_uri_parse(str, &uri);
- return serd_node_new_uri(&uri, base, out); // Resolve/Serialise
+ return serd_node_new_from_uri(&uri, base);
}
static inline bool
@@ -277,10 +297,7 @@ is_uri_path_char(const char c)
}
SerdNode*
-serd_node_new_file_uri(const char* path,
- const char* hostname,
- SerdURI* out,
- bool escape)
+serd_node_new_file_uri(const char* path, const char* hostname, bool escape)
{
const size_t path_len = strlen(path);
const size_t hostname_len = hostname ? strlen(hostname) : 0;
@@ -311,18 +328,13 @@ serd_node_new_file_uri(const char* path,
}
serd_buffer_sink_finish(&buffer);
- SerdNode* node = serd_node_new_substring(
- SERD_URI, (const char*)buffer.buf, buffer.len);
- if (out) {
- serd_uri_parse(serd_node_buffer(node), out);
- }
-
+ SerdNode* node = serd_node_new_uri((const char*)buffer.buf);
free(buffer.buf);
return node;
}
-SerdNode*
-serd_node_new_uri(const SerdURI* uri, const SerdURI* base, SerdURI* out)
+static SerdNode*
+serd_node_new_from_uri(const SerdURI* uri, const SerdURI* base)
{
SerdURI abs_uri = *uri;
if (base) {
@@ -337,33 +349,36 @@ serd_node_new_uri(const SerdURI* uri, const SerdURI* base, SerdURI* out)
serd_node_buffer(node)[actual_len] = '\0';
node->n_bytes = actual_len;
- if (out) {
- serd_uri_parse(serd_node_buffer(node), out); // TODO: avoid double parse
- }
-
return node;
}
SerdNode*
-serd_node_new_relative_uri(const SerdURI* uri,
- const SerdURI* base,
- const SerdURI* root,
- SerdURI* out)
+serd_node_new_relative_uri(const char* str,
+ const SerdNode* base,
+ const SerdNode* root)
{
- const size_t uri_len = serd_uri_string_length(uri);
- const size_t base_len = serd_uri_string_length(base);
+ SerdURI uri = SERD_URI_NULL;
+ SerdURI base_uri = SERD_URI_NULL;
+ SerdURI root_uri = SERD_URI_NULL;
+
+ serd_uri_parse(str, &uri);
+ if (base) {
+ serd_uri_parse(serd_node_get_string(base), &base_uri);
+ }
+ if (root) {
+ serd_uri_parse(serd_node_get_string(root), &root_uri);
+ }
+
+ const size_t uri_len = serd_uri_string_length(&uri);
+ const size_t base_len = serd_uri_string_length(&base_uri);
SerdNode* node = serd_node_malloc(uri_len + base_len, 0, SERD_URI);
char* ptr = serd_node_buffer(node);
const size_t actual_len = serd_uri_serialise_relative(
- uri, base, root, string_sink, &ptr);
+ &uri, &base_uri, root ? &root_uri : NULL, string_sink, &ptr);
serd_node_buffer(node)[actual_len] = '\0';
node->n_bytes = actual_len;
- if (out) {
- serd_uri_parse(serd_node_buffer(node), out); // TODO: avoid double parse
- }
-
return node;
}
@@ -516,6 +531,15 @@ serd_node_get_length(const SerdNode* node)
return node ? node->n_bytes : 0;
}
+static const SerdNode*
+serd_node_get_meta_node(const SerdNode* node)
+{
+ const size_t len = serd_node_pad_size(node->n_bytes);
+ assert((intptr_t)node % serd_node_align == 0);
+ assert(len % serd_node_align == 0);
+ return node + 1 + (len / serd_node_align);
+}
+
const SerdNode*
serd_node_get_datatype(const SerdNode* node)
{
@@ -523,11 +547,7 @@ serd_node_get_datatype(const SerdNode* node)
return NULL;
}
- const size_t len = serd_node_pad_size(node->n_bytes);
- assert((intptr_t)node % serd_node_align == 0);
- assert(len % serd_node_align == 0);
-
- const SerdNode* const datatype = node + 1 + (len / serd_node_align);
+ const SerdNode* const datatype = serd_node_get_meta_node(node);
assert(datatype->type == SERD_URI || datatype->type == SERD_CURIE);
return datatype;
}
@@ -539,11 +559,7 @@ serd_node_get_language(const SerdNode* node)
return NULL;
}
- const size_t len = serd_node_pad_size(node->n_bytes);
- assert((intptr_t)node % serd_node_align == 0);
- assert(len % serd_node_align == 0);
-
- const SerdNode* const lang = node + 1 + (len / serd_node_align);
+ const SerdNode* const lang = serd_node_get_meta_node(node);
assert(lang->type == SERD_LITERAL);
return lang;
}
diff --git a/src/node.h b/src/node.h
index 025e29de..91202db1 100644
--- a/src/node.h
+++ b/src/node.h
@@ -43,5 +43,6 @@ serd_node_buffer_c(const SerdNode* node)
SerdNode* serd_node_malloc(size_t n_bytes, SerdNodeFlags flags, SerdType type);
void serd_node_set(SerdNode** dst, const SerdNode* src);
size_t serd_node_total_size(const SerdNode* node);
+SerdNode* serd_node_new_resolved_uri_i(const char* str, const SerdURI* base);
#endif // SERD_NODE_H
diff --git a/src/serdi.c b/src/serdi.c
index ad52de3d..6efce26e 100644
--- a/src/serdi.c
+++ b/src/serdi.c
@@ -212,13 +212,11 @@ main(int argc, char** argv)
: SERD_NQUADS);
}
- SerdURI base_uri = SERD_URI_NULL;
- SerdNode* base = NULL;
+ SerdNode* base = NULL;
if (a < argc) { // Base URI given on command line
- base = serd_node_new_uri_from_string(
- (const char*)argv[a], NULL, &base_uri);
+ base = serd_node_new_uri((const char*)argv[a]);
} else if (!from_string && !from_stdin) { // Use input file URI
- base = serd_node_new_file_uri(input, NULL, &base_uri, true);
+ base = serd_node_new_file_uri(input, NULL, true);
}
FILE* out_fd = stdout;
@@ -247,7 +245,7 @@ main(int argc, char** argv)
SerdWriter* writer = serd_writer_new(
world, output_syntax, (SerdStyle)output_style,
- env, &base_uri, serd_file_sink, out_fd);
+ env, serd_file_sink, out_fd);
SerdReader* reader = serd_reader_new(
world, input_syntax, serd_writer_get_sink_interface(writer));
@@ -257,7 +255,7 @@ main(int argc, char** argv)
serd_world_set_error_sink(world, quiet_error_sink, NULL);
}
- SerdNode* root = serd_node_new_string(SERD_URI, root_uri);
+ SerdNode* root = serd_node_new_uri(root_uri);
serd_writer_set_root_uri(writer, root);
serd_writer_chop_blank_prefix(writer, chop_prefix);
serd_reader_add_blank_prefix(reader, add_prefix);
diff --git a/src/string_utils.h b/src/string_utils.h
index ab6ece0e..5f0dec01 100644
--- a/src/string_utils.h
+++ b/src/string_utils.h
@@ -102,22 +102,6 @@ serd_update_flags(const uint8_t c, SerdNodeFlags* const flags)
}
}
-static inline size_t
-serd_substrlen(const char* const str,
- const size_t len,
- SerdNodeFlags* const flags)
-{
- if (flags) {
- size_t i = 0;
- *flags = 0;
- for (; i < len && str[i]; ++i) {
- serd_update_flags(str[i], flags);
- }
- return i;
- }
- return strlen(str);
-}
-
static inline int
serd_strncasecmp(const char* s1, const char* s2, size_t n)
{
diff --git a/src/writer.c b/src/writer.c
index 2ee3412a..2f17d86d 100644
--- a/src/writer.c
+++ b/src/writer.c
@@ -15,6 +15,7 @@
*/
#include "byte_sink.h"
+#include "env.h"
#include "node.h"
#include "serd_internal.h"
#include "stack.h"
@@ -99,7 +100,6 @@ struct SerdWriterImpl {
SerdEnv* env;
SerdNode* root_node;
SerdURI root_uri;
- SerdURI base_uri;
SerdStack anon_stack;
SerdByteSink byte_sink;
SerdErrorSink error_sink;
@@ -527,19 +527,18 @@ write_uri_node(SerdWriter* const writer,
write_sep(writer, SEP_URI_BEGIN);
if (writer->style & SERD_STYLE_RESOLVED) {
- SerdURI in_base_uri, uri, abs_uri;
- serd_env_get_base_uri(writer->env, &in_base_uri);
+ const SerdURI* base_uri = serd_env_get_parsed_base_uri(writer->env);
+ SerdURI uri, abs_uri;
serd_uri_parse(node_str, &uri);
- serd_uri_resolve(&uri, &in_base_uri, &abs_uri);
- bool rooted = uri_is_under(&writer->base_uri, &writer->root_uri);
- SerdURI* root = rooted ? &writer->root_uri : & writer->base_uri;
+ serd_uri_resolve(&uri, base_uri, &abs_uri);
+ bool rooted = uri_is_under(base_uri, &writer->root_uri);
+ const SerdURI* root = rooted ? &writer->root_uri : base_uri;
if (!uri_is_under(&abs_uri, root) ||
writer->syntax == SERD_NTRIPLES ||
writer->syntax == SERD_NQUADS) {
serd_uri_serialise(&abs_uri, uri_sink, writer);
} else {
- serd_uri_serialise_relative(
- &uri, &writer->base_uri, root, uri_sink, writer);
+ serd_uri_serialise_relative(&uri, base_uri, root, uri_sink, writer);
}
} else {
write_uri_from_node(writer, node);
@@ -863,13 +862,12 @@ serd_writer_finish(SerdWriter* writer)
}
SerdWriter*
-serd_writer_new(SerdWorld* world,
- SerdSyntax syntax,
- SerdStyle style,
- SerdEnv* env,
- const SerdURI* base_uri,
- SerdSink ssink,
- void* stream)
+serd_writer_new(SerdWorld* world,
+ SerdSyntax syntax,
+ SerdStyle style,
+ SerdEnv* env,
+ SerdSink ssink,
+ void* stream)
{
const WriteContext context = WRITE_CONTEXT_NULL;
SerdWriter* writer = (SerdWriter*)calloc(1, sizeof(SerdWriter));
@@ -879,7 +877,6 @@ serd_writer_new(SerdWorld* world,
writer->env = env;
writer->root_node = NULL;
writer->root_uri = SERD_URI_NULL;
- writer->base_uri = base_uri ? *base_uri : SERD_URI_NULL;
writer->anon_stack = serd_stack_new(4 * sizeof(WriteContext));
writer->context = context;
writer->list_subj = NULL;
@@ -915,8 +912,6 @@ serd_writer_set_base_uri(SerdWriter* writer,
const SerdNode* uri)
{
if (!serd_env_set_base_uri(writer->env, uri)) {
- serd_env_get_base_uri(writer->env, &writer->base_uri);
-
if (writer->syntax == SERD_TURTLE || writer->syntax == SERD_TRIG) {
if (ctx(writer, FIELD_GRAPH) || ctx(writer, FIELD_SUBJECT)) {
sink(" .\n\n", 4, writer);
diff --git a/tests/serd_test.c b/tests/serd_test.c
index 9d203a15..01f1e60b 100644
--- a/tests/serd_test.c
+++ b/tests/serd_test.c
@@ -91,7 +91,7 @@ check_file_uri(const char* hostname,
expected_path = path;
}
- SerdNode* node = serd_node_new_file_uri(path, hostname, 0, escape);
+ SerdNode* node = serd_node_new_file_uri(path, hostname, escape);
const char* node_str = serd_node_get_string(node);
char* out_hostname = NULL;
char* out_path = serd_file_uri_parse(node_str, &out_hostname);
@@ -113,6 +113,23 @@ check_file_uri(const char* hostname,
return ret;
}
+static int
+check_rel_uri(const char* uri,
+ const SerdNode* base,
+ const SerdNode* root,
+ const char* expected)
+{
+ SerdNode* rel = serd_node_new_relative_uri(uri, base, root);
+ const int ret = strcmp(serd_node_get_string(rel), expected);
+ serd_node_free(rel);
+ if (ret) {
+ FAILF("Bad relative URI `%s' (expected `%s')\n",
+ serd_node_get_string(rel),
+ expected);
+ }
+ return ret;
+}
+
int
main(void)
{
@@ -273,14 +290,14 @@ main(void)
// Test serd_node_equals
const uint8_t replacement_char_str[] = { 0xEF, 0xBF, 0xBD, 0 };
- SerdNode* lhs = serd_node_new_string(SERD_LITERAL, (const char*)replacement_char_str);
- SerdNode* rhs = serd_node_new_string(SERD_LITERAL, "123");
+ SerdNode* lhs = serd_node_new_string((const char*)replacement_char_str);
+ SerdNode* rhs = serd_node_new_string("123");
if (serd_node_equals(lhs, rhs)) {
FAILF("%s == %s\n",
serd_node_get_string(lhs), serd_node_get_string(rhs));
}
- SerdNode* qnode = serd_node_new_string(SERD_CURIE, "foo:bar");
+ SerdNode* qnode = serd_node_new_curie("foo:bar");
if (serd_node_equals(lhs, qnode)) {
FAILF("%s == %s\n",
serd_node_get_string(lhs), serd_node_get_string(qnode));
@@ -301,39 +318,17 @@ main(void)
// Test serd_node_new_string
- SerdNode* hello = serd_node_new_string(SERD_LITERAL, "hello\"");
+ SerdNode* hello = serd_node_new_string("hello\"");
if (serd_node_get_length(hello) != 6 ||
serd_node_get_flags(hello) != SERD_HAS_QUOTE ||
strcmp(serd_node_get_string(hello), "hello\"")) {
FAILF("Bad node %s\n", serd_node_get_string(hello));
}
- if (serd_node_new_string(SERD_URI, NULL)) {
+ if (serd_node_new_string(NULL)) {
FAIL("Successfully created node from NULL string\n");
}
- // Test serd_node_new_substring
-
- if (serd_node_new_substring(SERD_LITERAL, NULL, 32)) {
- FAIL("Successfully created node from NULL substring\n");
- }
-
- SerdNode* a_b = serd_node_new_substring(SERD_LITERAL, "a\"bc", 3);
- if (serd_node_get_length(a_b) != 3 ||
- serd_node_get_flags(a_b) != SERD_HAS_QUOTE ||
- strncmp(serd_node_get_string(a_b), "a\"b", 3)) {
- FAILF("Bad node %s\n", serd_node_get_string(a_b));
- }
-
- serd_node_free(a_b);
- a_b = serd_node_new_substring(SERD_LITERAL, "a\"bc", 10);
- if (serd_node_get_length(a_b) != 4 ||
- serd_node_get_flags(a_b) != SERD_HAS_QUOTE ||
- strncmp(serd_node_get_string(a_b), "a\"bc", 4)) {
- FAILF("Bad node %s\n", serd_node_get_string(a_b));
- }
- serd_node_free(a_b);
-
// Test serd_node_new_literal
if (serd_node_new_literal(NULL, NULL, NULL)) {
@@ -357,8 +352,7 @@ main(void)
}
serd_node_free(hello_l);
- SerdNode* eg_Thing =
- serd_node_new_uri_from_string("http://example.org/Thing", NULL, NULL);
+ SerdNode* eg_Thing = serd_node_new_uri("http://example.org/Thing");
SerdNode* hello_dt =
serd_node_new_literal("hello_dt\"", eg_Thing, NULL);
@@ -372,43 +366,71 @@ main(void)
serd_node_free(hello_dt);
serd_node_free(eg_Thing);
- // Test serd_node_new_uri_from_string
+ // Test absolute URI creation
- if (serd_node_new_uri_from_string(NULL, NULL, NULL)) {
+ if (serd_node_new_uri(NULL)) {
FAIL("Successfully created NULL URI\n");
}
- SerdURI base_uri;
- SerdNode* base = serd_node_new_uri_from_string("http://example.org/",
- NULL, &base_uri);
- SerdNode* nil = serd_node_new_uri_from_string(NULL, &base_uri, NULL);
- SerdNode* nil2 = serd_node_new_uri_from_string("", &base_uri, NULL);
- if (serd_node_get_type(nil) != SERD_URI ||
- strcmp(serd_node_get_string(nil), serd_node_get_string(base)) ||
- serd_node_get_type(nil2) != SERD_URI ||
- strcmp(serd_node_get_string(nil2), serd_node_get_string(base))) {
+ SerdNode* not_a_uri = serd_node_new_string("hello");
+ SerdNode* root = serd_node_new_uri("http://example.org/a/b/");
+ SerdNode* base = serd_node_new_uri("http://example.org/a/b/c/");
+ SerdNode* nil = serd_node_new_resolved_uri(NULL, base);
+ SerdNode* nil2 = serd_node_new_resolved_uri("", base);
+ if (serd_node_new_resolved_uri("", NULL)) {
+ FAIL("Successfully created URI resolved against NULL\n");
+ } else if (serd_node_new_resolved_uri("", not_a_uri)) {
+ FAIL("Successfully created URI resolved against non-URI\n");
+ } else if (serd_node_get_type(nil) != SERD_URI ||
+ strcmp(serd_node_get_string(nil), serd_node_get_string(base)) ||
+ serd_node_get_type(nil2) != SERD_URI ||
+ strcmp(serd_node_get_string(nil2), serd_node_get_string(base))) {
FAILF("URI %s != base %s\n",
serd_node_get_string(nil),
serd_node_get_string(base));
}
- // Test serd_node_new_relative_uri
- SerdNode* abs = serd_node_new_string(SERD_URI, "http://example.org/foo/bar");
- SerdURI abs_uri;
- serd_uri_parse(serd_node_get_string(abs), &abs_uri);
+ // Test relative URI creation
+
+ if (check_rel_uri("http://example.org/a/b/c/foo", base, NULL, "foo") ||
+ check_rel_uri("http://example.org/a/", base, NULL, "../../") ||
+ check_rel_uri("http://example.org/a/", base, root,
+ "http://example.org/a/") ||
+ check_rel_uri("http://example.org/", base, NULL,
+ "http://example.org/") ||
+ check_rel_uri("http://drobilla.net/a", base, NULL,
+ "http://drobilla.net/a")) {
+ return 1;
+ }
+
+ // Test URI resolution
+
+ if (serd_node_resolve(NULL, base)) {
+ FAIL("Successfully resolved null URI\n");
+ } else if (serd_node_resolve(nil, NULL)) {
+ FAIL("Successfully resolved against null URI\n");
+ } else if (serd_node_resolve(not_a_uri, base)) {
+ FAIL("Successfully resolved a non-URI\n");
+ } else if (serd_node_resolve(nil, not_a_uri)) {
+ FAIL("Successfully resolved against a non-URI\n");
+ }
- SerdURI rel_uri;
- SerdNode* rel = serd_node_new_relative_uri(&abs_uri, &base_uri, NULL, &rel_uri);
- if (strcmp(serd_node_get_string(rel), "/foo/bar")) {
- FAILF("Bad relative URI %s (expected '/foo/bar')\n",
- serd_node_get_string(rel));
+ SerdNode* rel = serd_node_new_relative_uri(
+ "http://example.org/a/b/c/foo", base, NULL);
+ SerdNode* resolved = serd_node_resolve(rel, base);
+ if (strcmp(serd_node_get_string(resolved),
+ "http://example.org/a/b/c/foo")) {
+ FAILF("Bad resolved URI %s (expected 'http://example.org/a/b/c/foo'\n",
+ serd_node_get_string(resolved));
}
- serd_node_free(abs);
serd_node_free(nil);
serd_node_free(nil2);
+ serd_node_free(not_a_uri);
+ serd_node_free(resolved);
serd_node_free(rel);
serd_node_free(base);
+ serd_node_free(root);
// Test serd_node_new_blank
@@ -428,13 +450,13 @@ main(void)
SerdWorld* world = serd_world_new();
- SerdNode* u = serd_node_new_string(SERD_URI, "http://example.org/foo");
- SerdNode* b = serd_node_new_string(SERD_CURIE, "invalid");
- SerdNode* c = serd_node_new_string(SERD_CURIE, "eg.2:b");
+ SerdNode* u = serd_node_new_uri("http://example.org/foo");
+ SerdNode* b = serd_node_new_curie("invalid");
+ SerdNode* c = serd_node_new_curie("eg.2:b");
SerdEnv* env = serd_env_new(NULL);
serd_env_set_prefix_from_strings(env, "eg.2", "http://example.org/");
- if (serd_env_get_base_uri(env, NULL)) {
+ if (serd_env_get_base_uri(env)) {
FAIL("Unexpected initial base URI\n");
}
@@ -446,7 +468,7 @@ main(void)
FAILF("Set base URI to %s\n", serd_node_get_string(hello));
}
- if (serd_env_get_base_uri(env, NULL)) {
+ if (serd_env_get_base_uri(env)) {
FAIL("Unexpected base URI\n");
}
@@ -468,7 +490,7 @@ main(void)
}
serd_node_free(xu);
- SerdNode* badpre = serd_node_new_string(SERD_CURIE, "hm:what");
+ SerdNode* badpre = serd_node_new_curie("hm:what");
SerdNode* xbadpre = serd_env_expand_node(env, badpre);
if (xbadpre) {
FAILF("Expanded invalid curie %s\n", serd_node_get_string(badpre));
@@ -486,7 +508,7 @@ main(void)
FAIL("Set NULL prefix\n");
}
- SerdNode* lit = serd_node_new_string(SERD_LITERAL, "hello");
+ SerdNode* lit = serd_node_new_string("hello");
if (!serd_env_set_prefix(env, b, lit)) {
FAIL("Set prefix to literal\n");
}
@@ -498,7 +520,7 @@ main(void)
FAILF("Bad prefix count %d\n", n_prefixes);
}
- SerdNode* shorter_uri = serd_node_new_string(SERD_URI, "urn:foo");
+ SerdNode* shorter_uri = serd_node_new_uri("urn:foo");
const SerdNode* prefix_name;
if (serd_env_qualify(env, shorter_uri, &prefix_name, &suffix)) {
FAILF("Qualified %s\n", serd_node_get_string(shorter_uri));
@@ -517,7 +539,7 @@ main(void)
}
SerdWriter* writer = serd_writer_new(
- world, SERD_TURTLE, (SerdStyle)0, env, NULL, serd_file_sink, fd);
+ world, SERD_TURTLE, (SerdStyle)0, env, serd_file_sink, fd);
if (!writer) {
FAIL("Failed to create writer\n");
}
@@ -538,9 +560,9 @@ main(void)
}
uint8_t buf[] = { 0xEF, 0xBF, 0xBD, 0 };
- SerdNode* s = serd_node_new_string(SERD_URI, "");
- SerdNode* p = serd_node_new_string(SERD_URI, "http://example.org/pred");
- SerdNode* o = serd_node_new_string(SERD_LITERAL, (char*)buf);
+ SerdNode* s = serd_node_new_uri("");
+ SerdNode* p = serd_node_new_uri("http://example.org/pred");
+ SerdNode* o = serd_node_new_string((char*)buf);
// Write 3 invalid statements (should write nothing)
const SerdNode* junk[][5] = { { s, p, NULL },
@@ -561,7 +583,7 @@ main(void)
}
}
- SerdNode* urn_Type = serd_node_new_uri_from_string("urn:Type", NULL, NULL);
+ SerdNode* urn_Type = serd_node_new_uri("urn:Type");
SerdNode* t = serd_node_new_literal((char*)buf, urn_Type, NULL);
SerdNode* l = serd_node_new_literal((char*)buf, NULL, "en");
@@ -584,8 +606,8 @@ main(void)
// Write statements with bad UTF-8 (should be replaced)
const char bad_str[] = { (char)0xFF, (char)0x90, 'h', 'i', 0 };
- SerdNode* bad_lit = serd_node_new_string(SERD_LITERAL, bad_str);
- SerdNode* bad_uri = serd_node_new_string(SERD_URI, bad_str);
+ SerdNode* bad_lit = serd_node_new_string(bad_str);
+ SerdNode* bad_uri = serd_node_new_uri(bad_str);
if (iface->statement(iface->handle, 0, NULL, s, p, bad_lit)) {
FAIL("Failed to write junk UTF-8 literal\n");
} else if (iface->statement(iface->handle, 0, NULL, s, p, bad_uri)) {
@@ -596,7 +618,7 @@ main(void)
// Write 1 valid statement
serd_node_free(o);
- o = serd_node_new_string(SERD_LITERAL, "hello");
+ o = serd_node_new_string("hello");
if (iface->statement(iface->handle, 0, NULL, s, p, o)) {
FAIL("Failed to write valid statement\n");
}
@@ -613,9 +635,9 @@ main(void)
// Test buffer sink
SerdBuffer buffer = { NULL, 0 };
writer = serd_writer_new(
- world, SERD_TURTLE, (SerdStyle)0, env, NULL, serd_buffer_sink, &buffer);
+ world, SERD_TURTLE, (SerdStyle)0, env, serd_buffer_sink, &buffer);
- o = serd_node_new_string(SERD_URI, "http://example.org/base");
+ o = serd_node_new_uri("http://example.org/base");
if (serd_writer_set_base_uri(writer, o)) {
FAIL("Failed to write to chunk sink\n");
}
@@ -640,7 +662,7 @@ main(void)
FAIL("Failed to create reader\n");
}
- SerdNode* g = serd_node_new_string(SERD_URI, "http://example.org/");
+ SerdNode* g = serd_node_new_uri("http://example.org/");
serd_reader_set_default_graph(reader, g);
serd_reader_add_blank_prefix(reader, "tmp");
serd_reader_add_blank_prefix(reader, NULL);