aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2019-03-10 16:21:56 +0100
committerDavid Robillard <d@drobilla.net>2019-04-13 19:48:23 +0200
commitedf7d6cbfd03184af131d419d65607ed9847a233 (patch)
tree82fc07aa921db72bc11b305e1f527561e189d834
parent7c2ce8b5593282ddbf42c103225e06a3ecacfbb8 (diff)
downloadserd-edf7d6cbfd03184af131d419d65607ed9847a233.tar.gz
serd-edf7d6cbfd03184af131d419d65607ed9847a233.tar.bz2
serd-edf7d6cbfd03184af131d419d65607ed9847a233.zip
Factor out pushing writer context
-rw-r--r--src/writer.c63
1 files changed, 34 insertions, 29 deletions
diff --git a/src/writer.c b/src/writer.c
index ffec736c..303385a4 100644
--- a/src/writer.c
+++ b/src/writer.c
@@ -720,6 +720,25 @@ write_list_obj(SerdWriter* writer,
return false;
}
+static WriteContext*
+push_context(SerdWriter* const writer,
+ const ContextType type,
+ const SerdNode* const g,
+ const SerdNode* const s,
+ const SerdNode* const p)
+{
+ WriteContext* old = (WriteContext*)serd_stack_push(&writer->anon_stack,
+ sizeof(WriteContext));
+
+ *old = writer->context;
+ const WriteContext new_context = {
+ type, serd_node_copy(g), serd_node_copy(s), serd_node_copy(p)
+ };
+ writer->context = new_context;
+
+ return old;
+}
+
static SerdStatus
serd_writer_write_statement(SerdWriter* writer,
SerdStatementFlags flags,
@@ -829,39 +848,25 @@ serd_writer_write_statement(SerdWriter* writer,
write_node(writer, object, SERD_OBJECT, flags);
}
- if (flags & (SERD_LIST_S)) {
- WriteContext* ctx = (WriteContext*)serd_stack_push(
- &writer->anon_stack, sizeof(WriteContext));
- *ctx = writer->context;
- WriteContext new_context = {
- CTX_LIST,
- serd_node_copy(graph), serd_node_copy(subject), NULL };
- writer->context = new_context;
+ WriteContext* old_ctx = NULL;
+ if (flags & SERD_LIST_S) {
+ old_ctx = push_context(writer, CTX_LIST, graph, subject, NULL);
}
- if (flags & (SERD_LIST_O)) {
- WriteContext* ctx = (WriteContext*)serd_stack_push(
- &writer->anon_stack, sizeof(WriteContext));
- *ctx = writer->context;
- WriteContext new_context = {
- CTX_LIST,
- serd_node_copy(graph), serd_node_copy(object), NULL };
- writer->context = new_context;
+ if (flags & SERD_LIST_O) {
+ old_ctx = push_context(writer, CTX_LIST, graph, object, NULL);
}
- if (flags & (SERD_ANON_S|SERD_ANON_O)) {
- WriteContext* ctx = (WriteContext*)serd_stack_push(
- &writer->anon_stack, sizeof(WriteContext));
- *ctx = writer->context;
- WriteContext new_context = {
- (flags & (SERD_LIST_S|SERD_LIST_O))
- ? CTX_LIST : CTX_BLANK,
- serd_node_copy(graph), serd_node_copy(subject), NULL };
- if ((flags & SERD_ANON_S)) {
- new_context.predicate = serd_node_copy(predicate);
- }
- writer->context = new_context;
- } else {
+ if (flags & (SERD_ANON_S | SERD_ANON_O)) {
+ old_ctx = push_context(
+ writer,
+ (flags & (SERD_LIST_S | SERD_LIST_O)) ? CTX_LIST : CTX_BLANK,
+ graph,
+ subject,
+ (flags & SERD_ANON_S) ? predicate : NULL);
+ }
+
+ if (!old_ctx) {
serd_node_set(&writer->context.graph, graph);
serd_node_set(&writer->context.subject, subject);
serd_node_set(&writer->context.predicate, predicate);