From edf7d6cbfd03184af131d419d65607ed9847a233 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 10 Mar 2019 16:21:56 +0100 Subject: Factor out pushing writer context --- src/writer.c | 63 ++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 34 insertions(+), 29 deletions(-) (limited to 'src/writer.c') 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); -- cgit v1.2.1