aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-02-04 18:35:41 +0100
committerDavid Robillard <d@drobilla.net>2020-10-27 13:13:58 +0100
commit6295d10a7f076aa4ee403a20c28359ef3a747a68 (patch)
tree7ed98887457469cd90cb1b12d80d84ff7871c69a /src
parent26142937db25982ee2d94e5719acefff0cfaa5e0 (diff)
downloadserd-6295d10a7f076aa4ee403a20c28359ef3a747a68.tar.gz
serd-6295d10a7f076aa4ee403a20c28359ef3a747a68.tar.bz2
serd-6295d10a7f076aa4ee403a20c28359ef3a747a68.zip
Make nodes opaque
Diffstat (limited to 'src')
-rw-r--r--src/env.c81
-rw-r--r--src/node.c169
-rw-r--r--src/reader.c24
-rw-r--r--src/reader.h3
-rw-r--r--src/serdi.c13
-rw-r--r--src/writer.c143
6 files changed, 236 insertions, 197 deletions
diff --git a/src/env.c b/src/env.c
index aa5c3b45..488e52fe 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 "node.h"
+
#include "serd/serd.h"
#include <stdbool.h>
@@ -22,14 +24,14 @@
#include <string.h>
typedef struct {
- SerdNode name;
- SerdNode uri;
+ SerdNode* name;
+ SerdNode* uri;
} SerdPrefix;
struct SerdEnvImpl {
SerdPrefix* prefixes;
size_t n_prefixes;
- SerdNode base_uri_node;
+ SerdNode* base_uri_node;
SerdURI base_uri;
};
@@ -51,11 +53,11 @@ serd_env_free(SerdEnv* env)
}
for (size_t i = 0; i < env->n_prefixes; ++i) {
- serd_node_free(&env->prefixes[i].name);
- serd_node_free(&env->prefixes[i].uri);
+ serd_node_free(env->prefixes[i].name);
+ serd_node_free(env->prefixes[i].uri);
}
free(env->prefixes);
- serd_node_free(&env->base_uri_node);
+ serd_node_free(env->base_uri_node);
free(env);
}
@@ -65,7 +67,7 @@ serd_env_base_uri(const SerdEnv* env, SerdURI* out)
if (out) {
*out = env->base_uri;
}
- return &env->base_uri_node;
+ return env->base_uri_node;
}
SerdStatus
@@ -77,13 +79,13 @@ serd_env_set_base_uri(SerdEnv* env,
}
// Resolve base URI and create a new node and URI for it
- SerdURI base_uri;
- SerdNode base_uri_node = serd_node_new_uri_from_node(
+ SerdURI base_uri;
+ SerdNode* base_uri_node = serd_node_new_uri_from_node(
uri, &env->base_uri, &base_uri);
- if (base_uri_node.buf) {
+ if (base_uri_node) {
// Replace the current base URI
- serd_node_free(&env->base_uri_node);
+ serd_node_free(env->base_uri_node);
env->base_uri_node = base_uri_node;
env->base_uri = base_uri;
return SERD_SUCCESS;
@@ -97,7 +99,7 @@ serd_env_find(const SerdEnv* env,
size_t name_len)
{
for (size_t i = 0; i < env->n_prefixes; ++i) {
- const SerdNode* const prefix_name = &env->prefixes[i].name;
+ const SerdNode* const prefix_name = env->prefixes[i].name;
if (prefix_name->n_bytes == name_len) {
if (!memcmp(serd_node_string(prefix_name), name, name_len)) {
return &env->prefixes[i];
@@ -115,10 +117,10 @@ serd_env_add(SerdEnv* env,
const char* name_str = serd_node_string(name);
SerdPrefix* const prefix = serd_env_find(env, name_str, name->n_bytes);
if (prefix) {
- if (!serd_node_equals(&prefix->uri, uri)) {
- SerdNode old_prefix_uri = prefix->uri;
+ if (!serd_node_equals(prefix->uri, uri)) {
+ SerdNode* old_prefix_uri = prefix->uri;
prefix->uri = serd_node_copy(uri);
- serd_node_free(&old_prefix_uri);
+ serd_node_free(old_prefix_uri);
}
} else {
env->prefixes = (SerdPrefix*)realloc(
@@ -133,20 +135,20 @@ serd_env_set_prefix(SerdEnv* env,
const SerdNode* name,
const SerdNode* uri)
{
- if (!name->buf || uri->type != SERD_URI) {
+ if (!name || uri->type != SERD_URI) {
return SERD_ERR_BAD_ARG;
} else if (serd_uri_string_has_scheme(serd_node_string(uri))) {
// Set prefix to absolute URI
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(
+ SerdURI abs_uri;
+ SerdNode* abs_uri_node = serd_node_new_uri_from_node(
uri, &env->base_uri, &abs_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_node);
+ serd_node_free(abs_uri_node);
}
return SERD_SUCCESS;
}
@@ -156,20 +158,24 @@ serd_env_set_prefix_from_strings(SerdEnv* env,
const char* name,
const char* uri)
{
- const SerdNode name_node = serd_node_from_string(SERD_LITERAL, name);
- const SerdNode uri_node = serd_node_from_string(SERD_URI, uri);
+ SerdNode* name_node = serd_node_new_string(SERD_LITERAL, name);
+ SerdNode* uri_node = serd_node_new_string(SERD_URI, uri);
+
+ const SerdStatus st = serd_env_set_prefix(env, name_node, uri_node);
- return serd_env_set_prefix(env, &name_node, &uri_node);
+ serd_node_free(name_node);
+ serd_node_free(uri_node);
+ return st;
}
bool
-serd_env_qualify(const SerdEnv* env,
- const SerdNode* uri,
- SerdNode* prefix,
- SerdStringView* suffix)
+serd_env_qualify(const SerdEnv* env,
+ const SerdNode* uri,
+ const SerdNode** prefix,
+ SerdStringView* suffix)
{
for (size_t i = 0; i < env->n_prefixes; ++i) {
- const SerdNode* const prefix_uri = &env->prefixes[i].uri;
+ const SerdNode* const prefix_uri = env->prefixes[i].uri;
if (uri->n_bytes >= prefix_uri->n_bytes) {
const char* prefix_str = serd_node_string(prefix_uri);
const char* uri_str = serd_node_string(uri);
@@ -200,8 +206,8 @@ serd_env_expand(const SerdEnv* env,
const size_t name_len = (size_t)(colon - str);
const SerdPrefix* const prefix = serd_env_find(env, str, name_len);
if (prefix) {
- uri_prefix->buf = serd_node_string(&prefix->uri);
- uri_prefix->len = prefix->uri.n_bytes;
+ uri_prefix->buf = serd_node_string(prefix->uri);
+ uri_prefix->len = prefix->uri->n_bytes;
uri_suffix->buf = colon + 1;
uri_suffix->len = curie->n_bytes - name_len - 1;
return SERD_SUCCESS;
@@ -209,7 +215,7 @@ serd_env_expand(const SerdEnv* env,
return SERD_ERR_BAD_CURIE;
}
-SerdNode
+SerdNode*
serd_env_expand_node(const SerdEnv* env,
const SerdNode* node)
{
@@ -225,18 +231,19 @@ serd_env_expand_node(const SerdEnv* env,
SerdStringView prefix;
SerdStringView suffix;
if (serd_env_expand(env, node, &prefix, &suffix)) {
- return SERD_NODE_NULL;
+ return NULL;
}
const size_t len = prefix.len + suffix.len;
- char* buf = (char*)malloc(len + 1);
- SerdNode ret = { buf, len, 0, SERD_URI };
- snprintf(buf, ret.n_bytes + 1, "%s%s", prefix.buf, suffix.buf);
+ SerdNode* ret = serd_node_malloc(len, 0, SERD_URI);
+ char* buf = serd_node_buffer(ret);
+ snprintf(buf, len + 1, "%s%s", prefix.buf, suffix.buf);
+ ret->n_bytes = len;
return ret;
}
case SERD_BLANK:
break;
}
- return SERD_NODE_NULL;
+ return NULL;
}
void
@@ -245,6 +252,6 @@ serd_env_foreach(const SerdEnv* env,
void* handle)
{
for (size_t i = 0; i < env->n_prefixes; ++i) {
- func(handle, &env->prefixes[i].name, &env->prefixes[i].uri);
+ func(handle, env->prefixes[i].name, env->prefixes[i].uri);
}
}
diff --git a/src/node.c b/src/node.c
index 289e5b51..3bf013c4 100644
--- a/src/node.c
+++ b/src/node.c
@@ -38,53 +38,79 @@
# endif
#endif
-SerdNode
-serd_node_from_string(SerdType type, const char* str)
+SerdNode*
+serd_node_malloc(size_t n_bytes, SerdNodeFlags flags, SerdType type)
+{
+ SerdNode* node = (SerdNode*)calloc(1, sizeof(SerdNode) + n_bytes + 1);
+ node->n_bytes = 0;
+ node->flags = flags;
+ node->type = type;
+ return node;
+}
+
+void
+serd_node_set(SerdNode** dst, const SerdNode* src)
+{
+ if (src) {
+ if (!(*dst) || (*dst)->n_bytes < src->n_bytes) {
+ (*dst) = (SerdNode*)realloc(*dst, sizeof(SerdNode) + src->n_bytes + 1);
+ }
+
+ memcpy(*dst, src, sizeof(SerdNode) + src->n_bytes + 1);
+ } else if (*dst) {
+ memset(*dst, 0, sizeof(SerdNode));
+ }
+}
+
+SerdNode*
+serd_node_new_string(SerdType type, const char* str)
{
if (!str) {
- return SERD_NODE_NULL;
+ return NULL;
}
- SerdNodeFlags flags = 0;
- const size_t n_bytes = serd_strlen(str, &flags);
- const SerdNode ret = {str, n_bytes, flags, type};
- return ret;
+ SerdNodeFlags flags = 0;
+ const size_t n_bytes = serd_strlen(str, &flags);
+ SerdNode* node = serd_node_malloc(n_bytes, flags, type);
+ memcpy(serd_node_buffer(node), str, n_bytes);
+ node->n_bytes = n_bytes;
+ return node;
}
-SerdNode
-serd_node_from_substring(SerdType type, const char* str, const size_t len)
+SerdNode*
+serd_node_new_substring(SerdType type, const char* str, const size_t len)
{
if (!str) {
- return SERD_NODE_NULL;
+ return NULL;
}
- SerdNodeFlags flags = 0;
- const size_t n_bytes = serd_substrlen(str, len, &flags);
- const SerdNode ret = {str, n_bytes, flags, type};
- return ret;
+ SerdNodeFlags flags = 0;
+ const size_t n_bytes = serd_substrlen(str, len, &flags);
+ SerdNode* node = serd_node_malloc(n_bytes, flags, type);
+ memcpy(serd_node_buffer(node), str, n_bytes);
+ node->n_bytes = n_bytes;
+ return node;
}
-SerdNode
+SerdNode*
serd_node_copy(const SerdNode* node)
{
- if (!node || !node->buf) {
- return SERD_NODE_NULL;
+ if (!node) {
+ return NULL;
}
- SerdNode copy = *node;
- char* buf = (char*)malloc(copy.n_bytes + 1);
- memcpy(buf, node->buf, copy.n_bytes + 1);
- copy.buf = buf;
+ const size_t size = sizeof(SerdNode) + node->n_bytes + 1;
+ SerdNode* copy = (SerdNode*)malloc(size);
+ memcpy(copy, node, size);
return copy;
}
bool
serd_node_equals(const SerdNode* a, const SerdNode* b)
{
- return (a == b)
- || (a->type == b->type
- && a->n_bytes == b->n_bytes
- && ((a->buf == b->buf) || !memcmp(a->buf, b->buf, a->n_bytes + 1)));
+ return (a == b) ||
+ (a && b && a->type == b->type && a->n_bytes == b->n_bytes &&
+ !memcmp(serd_node_string(a), serd_node_string(b), a->n_bytes));
}
static size_t
@@ -113,7 +139,7 @@ string_sink(const void* buf, size_t len, void* stream)
return len;
}
-SerdNode
+SerdNode*
serd_node_new_uri_from_node(const SerdNode* uri_node,
const SerdURI* base,
SerdURI* out)
@@ -121,17 +147,17 @@ serd_node_new_uri_from_node(const SerdNode* uri_node,
const char* uri_str = serd_node_string(uri_node);
return (uri_node && uri_node->type == SERD_URI && uri_str)
? serd_node_new_uri_from_string(uri_str, base, out)
- : SERD_NODE_NULL;
+ : NULL;
}
-SerdNode
+SerdNode*
serd_node_new_uri_from_string(const char* str,
const SerdURI* base,
SerdURI* out)
{
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) : SERD_NODE_NULL;
+ return base ? serd_node_new_uri(base, NULL, out) : NULL;
}
SerdURI uri;
@@ -158,7 +184,7 @@ is_uri_path_char(const char c)
}
}
-SerdNode
+SerdNode*
serd_node_new_file_uri(const char* path,
const char* hostname,
SerdURI* out,
@@ -196,15 +222,17 @@ 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((const char*)buffer.buf, out);
+ serd_uri_parse(serd_node_buffer(node), out);
}
- return serd_node_from_substring(
- SERD_URI, (const char*)buffer.buf, buffer.len);
+ free(buffer.buf);
+ return node;
}
-SerdNode
+SerdNode*
serd_node_new_uri(const SerdURI* uri, const SerdURI* base, SerdURI* out)
{
SerdURI abs_uri = *uri;
@@ -213,22 +241,21 @@ serd_node_new_uri(const SerdURI* uri, const SerdURI* base, SerdURI* out)
}
const size_t len = serd_uri_string_length(&abs_uri);
- char* buf = (char*)malloc(len + 1);
- SerdNode node = { buf, len, 0, SERD_URI };
- char* ptr = buf;
+ SerdNode* node = serd_node_malloc(len, 0, SERD_URI);
+ char* ptr = serd_node_buffer(node);
const size_t actual_len = serd_uri_serialise(&abs_uri, string_sink, &ptr);
- buf[actual_len] = '\0';
- node.n_bytes = actual_len;
+ serd_node_buffer(node)[actual_len] = '\0';
+ node->n_bytes = actual_len;
if (out) {
- serd_uri_parse(buf, out); // TODO: cleverly avoid double parse
+ serd_uri_parse(serd_node_buffer(node), out); // TODO: avoid double parse
}
return node;
}
-SerdNode
+SerdNode*
serd_node_new_relative_uri(const SerdURI* uri,
const SerdURI* base,
const SerdURI* root,
@@ -236,17 +263,16 @@ serd_node_new_relative_uri(const SerdURI* uri,
{
const size_t uri_len = serd_uri_string_length(uri);
const size_t base_len = serd_uri_string_length(base);
- char* buf = (char*)malloc(uri_len + base_len + 1);
- SerdNode node = { buf, 0, 0, SERD_URI };
- char* ptr = buf;
+ 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);
- buf[actual_len] = '\0';
- node.n_bytes = actual_len;
+ serd_node_buffer(node)[actual_len] = '\0';
+ node->n_bytes = actual_len;
if (out) {
- serd_uri_parse(buf, out); // TODO: cleverly avoid double parse
+ serd_uri_parse(serd_node_buffer(node), out); // TODO: avoid double parse
}
return node;
@@ -259,18 +285,19 @@ serd_digits(double abs)
return lg < 1.0 ? 1U : (unsigned)lg;
}
-SerdNode
+SerdNode*
serd_node_new_decimal(double d, unsigned frac_digits)
{
if (isnan(d) || isinf(d)) {
- return SERD_NODE_NULL;
+ return NULL;
}
- const double abs_d = fabs(d);
- const unsigned int_digits = serd_digits(abs_d);
- char* buf = (char*)calloc(int_digits + frac_digits + 3, 1);
- SerdNode node = { buf, 0, 0, SERD_LITERAL };
- const double int_part = floor(abs_d);
+ const double abs_d = fabs(d);
+ const unsigned int_digits = serd_digits(abs_d);
+ const size_t len = int_digits + frac_digits + 3;
+ SerdNode* const node = serd_node_malloc(len, 0, SERD_LITERAL);
+ char* const buf = serd_node_buffer(node);
+ const double int_part = floor(abs_d);
// Point s to decimal point location
char* s = buf + int_digits;
@@ -286,13 +313,14 @@ serd_node_new_decimal(double d, unsigned frac_digits)
*t-- = (char)('0' + dec % 10);
} while ((dec /= 10) > 0);
+
*s++ = '.';
// Write fractional part (right to left)
double frac_part = fabs(d - int_part);
if (frac_part < DBL_EPSILON) {
*s++ = '0';
- node.n_bytes = (size_t)(s - buf);
+ node->n_bytes = (size_t)(s - buf);
} else {
uint64_t frac = (uint64_t)llround(frac_part * pow(10.0, (int)frac_digits));
s += frac_digits - 1;
@@ -301,7 +329,7 @@ serd_node_new_decimal(double d, unsigned frac_digits)
// Skip trailing zeros
for (; i < frac_digits - 1 && !(frac % 10); ++i, --s, frac /= 10) {}
- node.n_bytes = (size_t)(s - buf) + 1u;
+ node->n_bytes = (size_t)(s - buf) + 1u;
// Write digits from last trailing zero to decimal point
for (; i < frac_digits; ++i) {
@@ -313,13 +341,13 @@ serd_node_new_decimal(double d, unsigned frac_digits)
return node;
}
-SerdNode
+SerdNode*
serd_node_new_integer(int64_t i)
{
int64_t abs_i = (i < 0) ? -i : i;
const unsigned digits = serd_digits((double)abs_i);
- char* buf = (char*)calloc(digits + 2, 1);
- SerdNode node = { (const char*)buf, 0, 0, SERD_LITERAL };
+ SerdNode* node = serd_node_malloc(digits + 2, 0, SERD_LITERAL);
+ char* buf = serd_node_buffer(node);
// Point s to the end
char* s = buf + digits - 1;
@@ -328,7 +356,7 @@ serd_node_new_integer(int64_t i)
++s;
}
- node.n_bytes = (size_t)(s - buf) + 1u;
+ node->n_bytes = (size_t)(s - buf) + 1u;
// Write integer part (right to left)
do {
@@ -338,16 +366,22 @@ serd_node_new_integer(int64_t i)
return node;
}
-SerdNode
+SerdNode*
serd_node_new_blob(const void* buf, size_t size, bool wrap_lines)
{
+ if (!buf || !size) {
+ return NULL;
+ }
+
const size_t len = serd_base64_get_length(size, wrap_lines);
- uint8_t* str = (uint8_t*)calloc(len + 2, 1);
- SerdNode node = {(const char*)str, len, 0, SERD_LITERAL};
+ SerdNode* node = serd_node_malloc(len + 1, 0, SERD_LITERAL);
+ uint8_t* str = (uint8_t*)serd_node_buffer(node);
if (serd_base64_encode(str, buf, size, wrap_lines)) {
- node.flags |= SERD_HAS_NEWLINE;
+ node->flags |= SERD_HAS_NEWLINE;
}
+
+ node->n_bytes = len;
return node;
}
@@ -360,7 +394,7 @@ serd_node_type(const SerdNode* node)
const char*
serd_node_string(const SerdNode* node)
{
- return node ? node->buf : NULL;
+ return node ? (const char*)(node + 1) : NULL;
}
size_t
@@ -378,8 +412,5 @@ serd_node_flags(const SerdNode* node)
void
serd_node_free(SerdNode* node)
{
- if (node && node->buf) {
- free((char*)node->buf);
- node->buf = NULL;
- }
+ free(node);
}
diff --git a/src/reader.c b/src/reader.c
index 70fc8eb2..eef4d95b 100644
--- a/src/reader.c
+++ b/src/reader.c
@@ -42,9 +42,11 @@ void
set_blank_id(SerdReader* reader, Ref ref, size_t buf_size)
{
SerdNode* node = deref(reader, ref);
+ char* buf = (char*)(node + 1);
const char* prefix = reader->bprefix ? (const char*)reader->bprefix : "";
- node->n_bytes = (size_t)snprintf(
- (char*)node->buf, buf_size, "%sb%u", prefix, reader->next_id++);
+
+ node->n_bytes =
+ (size_t)snprintf(buf, buf_size, "%sb%u", prefix, reader->next_id++);
}
size_t
@@ -88,7 +90,6 @@ push_node_padded(SerdReader* reader, size_t maxlen,
node->n_bytes = n_bytes;
node->flags = 0;
node->type = type;
- node->buf = NULL;
char* buf = (char*)(node + 1);
memcpy(buf, str, n_bytes + 1);
@@ -110,12 +111,7 @@ push_node(SerdReader* reader, SerdType type, const char* str, size_t n_bytes)
SerdNode*
deref(SerdReader* reader, const Ref ref)
{
- if (ref) {
- SerdNode* node = (SerdNode*)(reader->stack.buf + ref);
- node->buf = (char*)node + sizeof(SerdNode);
- return node;
- }
- return NULL;
+ return ref ? (SerdNode*)(reader->stack.buf + ref) : NULL;
}
Ref
@@ -138,8 +134,8 @@ SerdStatus
emit_statement(SerdReader* reader, ReadContext ctx, Ref o, Ref d, Ref l)
{
SerdNode* graph = deref(reader, ctx.graph);
- if (!graph && reader->default_graph.buf) {
- graph = &reader->default_graph;
+ if (!graph && reader->default_graph) {
+ graph = reader->default_graph;
}
const SerdStatus st =
@@ -187,7 +183,7 @@ serd_reader_new(SerdSyntax syntax,
me->prefix_sink = prefix_sink;
me->statement_sink = statement_sink;
me->end_sink = end_sink;
- me->default_graph = SERD_NODE_NULL;
+ me->default_graph = NULL;
me->stack = serd_stack_new(SERD_PAGE_SIZE);
me->syntax = syntax;
me->next_id = 1;
@@ -225,7 +221,7 @@ serd_reader_free(SerdReader* reader)
pop_node(reader, reader->rdf_nil);
pop_node(reader, reader->rdf_rest);
pop_node(reader, reader->rdf_first);
- serd_node_free(&reader->default_graph);
+ serd_node_free(reader->default_graph);
#ifdef SERD_STACK_CHECK
free(reader->allocs);
@@ -262,7 +258,7 @@ void
serd_reader_set_default_graph(SerdReader* reader,
const SerdNode* graph)
{
- serd_node_free(&reader->default_graph);
+ serd_node_free(reader->default_graph);
reader->default_graph = serd_node_copy(graph);
}
diff --git a/src/reader.h b/src/reader.h
index 19e27993..dbe4350d 100644
--- a/src/reader.h
+++ b/src/reader.h
@@ -18,6 +18,7 @@
#define SERD_READER_H
#include "byte_source.h"
+#include "node.h"
#include "stack.h"
#include "serd/serd.h"
@@ -67,7 +68,7 @@ struct SerdReaderImpl {
Ref rdf_first;
Ref rdf_rest;
Ref rdf_nil;
- SerdNode default_graph;
+ SerdNode* default_graph;
SerdByteSource source;
SerdStack stack;
SerdSyntax syntax;
diff --git a/src/serdi.c b/src/serdi.c
index ed87bdfd..38646a67 100644
--- a/src/serdi.c
+++ b/src/serdi.c
@@ -258,8 +258,8 @@ main(int argc, char** argv)
: SERD_NQUADS);
}
- SerdURI base_uri = SERD_URI_NULL;
- SerdNode base = SERD_NODE_NULL;
+ SerdURI base_uri = SERD_URI_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);
@@ -268,7 +268,7 @@ main(int argc, char** argv)
}
FILE* out_fd = stdout;
- SerdEnv* env = serd_env_new(&base);
+ SerdEnv* env = serd_env_new(base);
int output_style = 0;
if (output_syntax == SERD_NTRIPLES || ascii) {
@@ -307,10 +307,11 @@ main(int argc, char** argv)
serd_writer_set_error_sink(writer, quiet_error_sink, NULL);
}
- SerdNode root = serd_node_from_string(SERD_URI, root_uri);
- serd_writer_set_root_uri(writer, &root);
+ SerdNode* root = serd_node_new_string(SERD_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);
+ serd_node_free(root);
SerdStatus st = SERD_SUCCESS;
if (!from_file) {
@@ -329,7 +330,7 @@ main(int argc, char** argv)
serd_writer_finish(writer);
serd_writer_free(writer);
serd_env_free(env);
- serd_node_free(&base);
+ serd_node_free(base);
if (from_file) {
fclose(in_fd);
diff --git a/src/writer.c b/src/writer.c
index ccf5e107..c3ccffb2 100644
--- a/src/writer.c
+++ b/src/writer.c
@@ -15,6 +15,7 @@
*/
#include "byte_sink.h"
+#include "node.h"
#include "serd_internal.h"
#include "stack.h"
#include "string_utils.h"
@@ -39,16 +40,12 @@ typedef enum {
} Field;
typedef struct {
- SerdNode graph;
- SerdNode subject;
- SerdNode predicate;
+ SerdNode* graph;
+ SerdNode* subject;
+ SerdNode* predicate;
} WriteContext;
-static const WriteContext WRITE_CONTEXT_NULL = {
- { 0, 0, 0, SERD_NOTHING },
- { 0, 0, 0, SERD_NOTHING },
- { 0, 0, 0, SERD_NOTHING }
-};
+static const WriteContext WRITE_CONTEXT_NULL = { NULL, NULL, NULL };
typedef enum {
SEP_NONE,
@@ -99,7 +96,7 @@ struct SerdWriterImpl {
SerdSyntax syntax;
SerdStyle style;
SerdEnv* env;
- SerdNode root_node;
+ SerdNode* root_node;
SerdURI root_uri;
SerdURI base_uri;
SerdStack anon_stack;
@@ -107,7 +104,7 @@ struct SerdWriterImpl {
SerdErrorSink error_sink;
void* error_handle;
WriteContext context;
- SerdNode list_subj;
+ SerdNode* list_subj;
unsigned list_depth;
unsigned indent;
char* bprefix;
@@ -159,18 +156,19 @@ anon_stack_top(SerdWriter* writer)
+ writer->anon_stack.size - sizeof(WriteContext));
}
-static void
-copy_node(SerdNode* dst, const SerdNode* src)
+static inline SerdNode*
+ctx(SerdWriter* writer, const Field field)
{
- if (src) {
- dst->buf = (char*)realloc((char*)dst->buf, src->n_bytes + 1);
- dst->n_bytes = src->n_bytes;
- dst->flags = src->flags;
- dst->type = src->type;
- memcpy((char*)dst->buf, src->buf, src->n_bytes + 1);
- } else {
- dst->type = SERD_NOTHING;
+ SerdNode* node = NULL;
+ if (field == FIELD_SUBJECT) {
+ node = writer->context.subject;
+ } else if (field == FIELD_PREDICATE) {
+ node = writer->context.predicate;
+ } else if (field == FIELD_GRAPH) {
+ node = writer->context.graph;
}
+
+ return node && node->type ? node : NULL;
}
static inline size_t
@@ -414,22 +412,26 @@ write_sep(SerdWriter* writer, const Sep sep)
static SerdStatus
reset_context(SerdWriter* writer, bool graph)
{
- if (graph) {
- writer->context.graph.type = SERD_NOTHING;
+ if (graph && writer->context.graph) {
+ memset(writer->context.graph, 0, sizeof(SerdNode));
+ }
+ if (writer->context.subject) {
+ memset(writer->context.subject, 0, sizeof(SerdNode));
}
- writer->context.subject.type = SERD_NOTHING;
- writer->context.predicate.type = SERD_NOTHING;
- writer->empty = false;
+ if (writer->context.predicate) {
+ memset(writer->context.predicate, 0, sizeof(SerdNode));
+ }
+ writer->empty = false;
return SERD_SUCCESS;
}
static SerdStatus
free_context(SerdWriter* writer)
{
- serd_node_free(&writer->context.graph);
- serd_node_free(&writer->context.subject);
- serd_node_free(&writer->context.predicate);
- return reset_context(writer, true);
+ serd_node_free(writer->context.graph);
+ serd_node_free(writer->context.subject);
+ serd_node_free(writer->context.predicate);
+ return SERD_SUCCESS;
}
static bool
@@ -507,8 +509,8 @@ write_uri_node(SerdWriter* const writer,
const Field field,
const SerdStatementFlags flags)
{
- SerdNode prefix;
- SerdStringView suffix;
+ const SerdNode* prefix = NULL;
+ SerdStringView suffix = {NULL, 0};
if (is_inline_start(writer, field, flags)) {
++writer->indent;
@@ -526,7 +528,7 @@ write_uri_node(SerdWriter* const writer,
} else if (has_scheme && (writer->style & SERD_STYLE_CURIED) &&
serd_env_qualify(writer->env, node, &prefix, &suffix) &&
is_name(suffix.buf, suffix.len)) {
- write_uri_from_node(writer, &prefix);
+ write_uri_from_node(writer, prefix);
sink(":", 1, writer);
write_uri(writer, suffix.buf, suffix.len);
return true;
@@ -613,7 +615,7 @@ write_blank(SerdWriter* const writer,
return write_sep(writer, SEP_ANON_BEGIN);
} else if (field == FIELD_SUBJECT && (flags & SERD_LIST_S_BEGIN)) {
assert(writer->list_depth == 0);
- copy_node(&writer->list_subj, node);
+ serd_node_set(&writer->list_subj, node);
++writer->list_depth;
++writer->indent;
return write_sep(writer, SEP_LIST_BEGIN);
@@ -680,7 +682,7 @@ write_pred(SerdWriter* writer, SerdStatementFlags flags, const SerdNode* pred)
{
write_node(writer, pred, NULL, NULL, FIELD_PREDICATE, flags);
write_sep(writer, SEP_P_O);
- copy_node(&writer->context.predicate, pred);
+ serd_node_set(&writer->context.predicate, pred);
}
static bool
@@ -712,9 +714,8 @@ serd_writer_write_statement(SerdWriter* writer,
const SerdNode* datatype,
const SerdNode* lang)
{
- if (!subject || !predicate || !object
- || !subject->buf || !predicate->buf || !object->buf
- || !is_resource(subject) || !is_resource(predicate)) {
+ if (!subject || !predicate || !object ||
+ !is_resource(subject) || !is_resource(predicate)) {
return SERD_ERR_BAD_ARG;
}
@@ -739,13 +740,13 @@ serd_writer_write_statement(SerdWriter* writer,
return SERD_SUCCESS;
}
- if ((graph && !serd_node_equals(graph, &writer->context.graph)) ||
- (!graph && writer->context.graph.type)) {
+ if ((graph && !serd_node_equals(graph, writer->context.graph)) ||
+ (!graph && ctx(writer, FIELD_GRAPH))) {
writer->indent = 0;
- if (writer->context.subject.type) {
+ if (ctx(writer, FIELD_SUBJECT)) {
write_sep(writer, SEP_END_S);
}
- if (writer->context.graph.type) {
+ if (ctx(writer, FIELD_GRAPH)) {
write_sep(writer, SEP_GRAPH_END);
}
@@ -754,23 +755,23 @@ serd_writer_write_statement(SerdWriter* writer,
TRY(write_node(writer, graph, datatype, lang, FIELD_GRAPH, flags));
++writer->indent;
write_sep(writer, SEP_GRAPH_BEGIN);
- copy_node(&writer->context.graph, graph);
+ serd_node_set(&writer->context.graph, graph);
}
}
if ((flags & SERD_LIST_CONT)) {
if (write_list_obj(writer, flags, predicate, object, datatype, lang)) {
// Reached end of list
- if (--writer->list_depth == 0 && writer->list_subj.type) {
+ if (--writer->list_depth == 0 && writer->list_subj) {
reset_context(writer, false);
- serd_node_free(&writer->context.subject);
+ serd_node_free(writer->context.subject);
writer->context.subject = writer->list_subj;
- writer->list_subj = SERD_NODE_NULL;
+ writer->list_subj = NULL;
}
return SERD_SUCCESS;
}
- } else if (serd_node_equals(subject, &writer->context.subject)) {
- if (serd_node_equals(predicate, &writer->context.predicate)) {
+ } else if (serd_node_equals(subject, writer->context.subject)) {
+ if (serd_node_equals(predicate, writer->context.predicate)) {
// Abbreviate S P
if (!(flags & SERD_ANON_O_BEGIN)) {
++writer->indent;
@@ -782,14 +783,14 @@ serd_writer_write_statement(SerdWriter* writer,
}
} else {
// Abbreviate S
- Sep sep = writer->context.predicate.type ? SEP_END_P : SEP_S_P;
+ Sep sep = ctx(writer, FIELD_PREDICATE) ? SEP_END_P : SEP_S_P;
write_sep(writer, sep);
write_pred(writer, flags, predicate);
write_node(writer, object, datatype, lang, FIELD_OBJECT, flags);
}
} else {
// No abbreviation
- if (writer->context.subject.type) {
+ if (ctx(writer, FIELD_SUBJECT)) {
assert(writer->indent > 0);
--writer->indent;
if (serd_stack_is_empty(&writer->anon_stack)) {
@@ -808,7 +809,7 @@ serd_writer_write_statement(SerdWriter* writer,
}
reset_context(writer, false);
- copy_node(&writer->context.subject, subject);
+ serd_node_set(&writer->context.subject, subject);
if (!(flags & SERD_LIST_S_BEGIN)) {
write_pred(writer, flags, predicate);
@@ -822,15 +823,15 @@ serd_writer_write_statement(SerdWriter* writer,
&writer->anon_stack, sizeof(WriteContext));
*ctx = writer->context;
WriteContext new_context = {
- serd_node_copy(graph), serd_node_copy(subject), SERD_NODE_NULL };
+ serd_node_copy(graph), serd_node_copy(subject), NULL };
if ((flags & SERD_ANON_S_BEGIN)) {
new_context.predicate = serd_node_copy(predicate);
}
writer->context = new_context;
} else {
- copy_node(&writer->context.graph, graph);
- copy_node(&writer->context.subject, subject);
- copy_node(&writer->context.predicate, predicate);
+ serd_node_set(&writer->context.graph, graph);
+ serd_node_set(&writer->context.subject, subject);
+ serd_node_set(&writer->context.predicate, predicate);
}
return SERD_SUCCESS;
@@ -853,10 +854,10 @@ serd_writer_end_anon(SerdWriter* writer,
free_context(writer);
writer->context = *anon_stack_top(writer);
serd_stack_pop(&writer->anon_stack, sizeof(WriteContext));
- const bool is_subject = serd_node_equals(node, &writer->context.subject);
+ const bool is_subject = serd_node_equals(node, writer->context.subject);
if (is_subject) {
- copy_node(&writer->context.subject, node);
- writer->context.predicate.type = SERD_NOTHING;
+ serd_node_set(&writer->context.subject, node);
+ memset(writer->context.predicate, 0, sizeof(SerdNode));
}
return SERD_SUCCESS;
}
@@ -864,15 +865,17 @@ serd_writer_end_anon(SerdWriter* writer,
SerdStatus
serd_writer_finish(SerdWriter* writer)
{
- if (writer->context.subject.type) {
+ if (ctx(writer, FIELD_SUBJECT)) {
write_sep(writer, SEP_END_S);
}
- if (writer->context.graph.type) {
+ if (ctx(writer, FIELD_GRAPH)) {
write_sep(writer, SEP_GRAPH_END);
}
serd_byte_sink_flush(&writer->byte_sink);
- writer->indent = 0;
- return free_context(writer);
+ free_context(writer);
+ writer->indent = 0;
+ writer->context = WRITE_CONTEXT_NULL;
+ return SERD_SUCCESS;
}
SerdWriter*
@@ -888,12 +891,12 @@ serd_writer_new(SerdSyntax syntax,
writer->syntax = syntax;
writer->style = style;
writer->env = env;
- writer->root_node = SERD_NODE_NULL;
+ 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 = SERD_NODE_NULL;
+ writer->list_subj = NULL;
writer->empty = true;
writer->byte_sink = serd_byte_sink_new(
ssink, stream, (style & SERD_STYLE_BULK) ? SERD_PAGE_SIZE : 1);
@@ -931,7 +934,7 @@ serd_writer_set_base_uri(SerdWriter* writer,
serd_env_base_uri(writer->env, &writer->base_uri);
if (writer->syntax == SERD_TURTLE || writer->syntax == SERD_TRIG) {
- if (writer->context.graph.type || writer->context.subject.type) {
+ if (ctx(writer, FIELD_GRAPH) || ctx(writer, FIELD_SUBJECT)) {
sink(" .\n\n", 4, writer);
reset_context(writer, true);
}
@@ -949,12 +952,12 @@ SerdStatus
serd_writer_set_root_uri(SerdWriter* writer,
const SerdNode* uri)
{
- serd_node_free(&writer->root_node);
- if (uri && uri->buf) {
+ serd_node_free(writer->root_node);
+ if (uri) {
writer->root_node = serd_node_copy(uri);
- serd_uri_parse(serd_node_string(uri), &writer->root_uri);
+ serd_uri_parse(serd_node_string(writer->root_node), &writer->root_uri);
} else {
- writer->root_node = SERD_NODE_NULL;
+ writer->root_node = NULL;
writer->root_uri = SERD_URI_NULL;
}
return SERD_SUCCESS;
@@ -967,7 +970,7 @@ serd_writer_set_prefix(SerdWriter* writer,
{
if (!serd_env_set_prefix(writer->env, name, uri)) {
if (writer->syntax == SERD_TURTLE || writer->syntax == SERD_TRIG) {
- if (writer->context.graph.type || writer->context.subject.type) {
+ if (ctx(writer, FIELD_GRAPH) || ctx(writer, FIELD_SUBJECT)) {
sink(" .\n\n", 4, writer);
reset_context(writer, true);
}
@@ -994,7 +997,7 @@ serd_writer_free(SerdWriter* writer)
serd_stack_free(&writer->anon_stack);
free(writer->bprefix);
serd_byte_sink_free(&writer->byte_sink);
- serd_node_free(&writer->root_node);
+ serd_node_free(writer->root_node);
free(writer);
}