aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2019-12-19 09:16:07 -0500
committerDavid Robillard <d@drobilla.net>2022-01-13 23:03:52 -0500
commita1c18e5585a1b3edbe62f53e768d270e54e4cac5 (patch)
treed50882062930a258b7d90d811ba0d237e5455a78
parentc4df0038bcfba1676bbaaa98badd241ec7d0b55e (diff)
downloadserd-a1c18e5585a1b3edbe62f53e768d270e54e4cac5.tar.gz
serd-a1c18e5585a1b3edbe62f53e768d270e54e4cac5.tar.bz2
serd-a1c18e5585a1b3edbe62f53e768d270e54e4cac5.zip
Add SerdField to public API
A statement field enum was previously only used by the writer internally, but will be used in the public API (without a sentinel value) in following commits.
-rw-r--r--include/serd/serd.h14
-rw-r--r--src/writer.c88
2 files changed, 55 insertions, 47 deletions
diff --git a/include/serd/serd.h b/include/serd/serd.h
index 3be32a65..820af22c 100644
--- a/include/serd/serd.h
+++ b/include/serd/serd.h
@@ -827,6 +827,20 @@ serd_node_equals(const SerdNode* SERD_NULLABLE a,
/**
@}
+ @defgroup serd_statement Statement
+ @{
+*/
+
+/// Index of a node in a statement
+typedef enum {
+ SERD_SUBJECT = 0, ///< Subject
+ SERD_PREDICATE = 1, ///< Predicate ("key")
+ SERD_OBJECT = 2, ///< Object ("value")
+ SERD_GRAPH = 3, ///< Graph ("context")
+} SerdField;
+
+/**
+ @}
@defgroup serd_world World
@{
*/
diff --git a/src/writer.c b/src/writer.c
index 6def5f73..8bbd6519 100644
--- a/src/writer.c
+++ b/src/writer.c
@@ -33,14 +33,6 @@
#include <stdlib.h>
#include <string.h>
-typedef enum {
- FIELD_NONE,
- FIELD_SUBJECT,
- FIELD_PREDICATE,
- FIELD_OBJECT,
- FIELD_GRAPH
-} Field;
-
typedef struct {
SerdNode* graph;
SerdNode* subject;
@@ -122,7 +114,7 @@ serd_writer_set_prefix(SerdWriter* writer,
static bool
write_node(SerdWriter* writer,
const SerdNode* node,
- Field field,
+ SerdField field,
SerdStatementFlags flags);
static bool
@@ -147,14 +139,14 @@ anon_stack_top(SerdWriter* const writer)
}
static SerdNode*
-ctx(SerdWriter* writer, const Field field)
+ctx(SerdWriter* writer, const SerdField field)
{
SerdNode* node = NULL;
- if (field == FIELD_SUBJECT) {
+ if (field == SERD_SUBJECT) {
node = writer->context.subject;
- } else if (field == FIELD_PREDICATE) {
+ } else if (field == SERD_PREDICATE) {
node = writer->context.predicate;
- } else if (field == FIELD_GRAPH) {
+ } else if (field == SERD_GRAPH) {
node = writer->context.graph;
}
@@ -494,11 +486,13 @@ free_context(const WriteContext* const ctx)
}
static bool
-is_inline_start(const SerdWriter* writer, Field field, SerdStatementFlags flags)
+is_inline_start(const SerdWriter* writer,
+ SerdField field,
+ SerdStatementFlags flags)
{
return (supports_abbrev(writer) &&
- ((field == FIELD_SUBJECT && (flags & SERD_ANON_S_BEGIN)) ||
- (field == FIELD_OBJECT && (flags & SERD_ANON_O_BEGIN))));
+ ((field == SERD_SUBJECT && (flags & SERD_ANON_S_BEGIN)) ||
+ (field == SERD_OBJECT && (flags & SERD_ANON_O_BEGIN))));
}
static bool
@@ -545,7 +539,7 @@ write_literal(SerdWriter* const writer,
sink(serd_node_string(lang), lang->length, writer);
} else if (type_uri) {
sink("^^", 2, writer);
- return write_node(writer, datatype, FIELD_NONE, flags);
+ return write_node(writer, datatype, (SerdField)-1, flags);
}
return true;
}
@@ -565,9 +559,9 @@ is_name(const char* buf, const size_t len)
}
static bool
-write_uri_node(SerdWriter* const writer,
- const SerdNode* node,
- const Field field)
+write_uri_node(SerdWriter* const writer,
+ const SerdNode* const node,
+ const SerdField field)
{
const SerdNode* prefix = NULL;
SerdStringView suffix = {NULL, 0};
@@ -575,7 +569,7 @@ write_uri_node(SerdWriter* const writer,
const char* node_str = serd_node_string(node);
const bool has_scheme = serd_uri_string_has_scheme(node_str);
if (supports_abbrev(writer)) {
- if (field == FIELD_PREDICATE && !strcmp(node_str, NS_RDF "type")) {
+ if (field == SERD_PREDICATE && !strcmp(node_str, NS_RDF "type")) {
return sink("a", 1, writer) == 1;
}
@@ -657,7 +651,7 @@ write_curie(SerdWriter* const writer, const SerdNode* const node)
static bool
write_blank(SerdWriter* const writer,
const SerdNode* node,
- const Field field,
+ const SerdField field,
const SerdStatementFlags flags)
{
const char* node_str = serd_node_string(node);
@@ -667,7 +661,7 @@ write_blank(SerdWriter* const writer,
return write_sep(writer, SEP_ANON_BEGIN);
}
- if (field == FIELD_SUBJECT && (flags & SERD_LIST_S_BEGIN)) {
+ if (field == SERD_SUBJECT && (flags & SERD_LIST_S_BEGIN)) {
assert(writer->list_depth == 0);
serd_node_set(&writer->list_subj, node);
++writer->list_depth;
@@ -675,14 +669,14 @@ write_blank(SerdWriter* const writer,
return write_sep(writer, SEP_LIST_BEGIN);
}
- if (field == FIELD_OBJECT && (flags & SERD_LIST_O_BEGIN)) {
+ if (field == SERD_OBJECT && (flags & SERD_LIST_O_BEGIN)) {
++writer->indent;
++writer->list_depth;
return write_sep(writer, SEP_LIST_BEGIN);
}
- if ((field == FIELD_SUBJECT && (flags & SERD_EMPTY_S)) ||
- (field == FIELD_OBJECT && (flags & SERD_EMPTY_O))) {
+ if ((field == SERD_SUBJECT && (flags & SERD_EMPTY_S)) ||
+ (field == SERD_OBJECT && (flags & SERD_EMPTY_O))) {
return sink("[]", 2, writer) == 2;
}
}
@@ -703,7 +697,7 @@ write_blank(SerdWriter* const writer,
static bool
write_node(SerdWriter* const writer,
const SerdNode* const node,
- const Field field,
+ const SerdField field,
const SerdStatementFlags flags)
{
bool ret = false;
@@ -734,7 +728,7 @@ is_resource(const SerdNode* node)
static void
write_pred(SerdWriter* writer, SerdStatementFlags flags, const SerdNode* pred)
{
- write_node(writer, pred, FIELD_PREDICATE, flags);
+ write_node(writer, pred, SERD_PREDICATE, flags);
write_sep(writer, SEP_P_O);
serd_node_set(&writer->context.predicate, pred);
}
@@ -753,7 +747,7 @@ write_list_obj(SerdWriter* const writer,
if (!strcmp(serd_node_string(predicate), NS_RDF "first")) {
write_sep(writer, SEP_LIST_SEP);
- write_node(writer, object, FIELD_OBJECT, flags);
+ write_node(writer, object, SERD_OBJECT, flags);
}
return false;
@@ -779,32 +773,32 @@ serd_writer_write_statement(SerdWriter* const writer,
} while (0)
if (writer->syntax == SERD_NTRIPLES || writer->syntax == SERD_NQUADS) {
- TRY(write_node(writer, subject, FIELD_SUBJECT, flags));
+ TRY(write_node(writer, subject, SERD_SUBJECT, flags));
sink(" ", 1, writer);
- TRY(write_node(writer, predicate, FIELD_PREDICATE, flags));
+ TRY(write_node(writer, predicate, SERD_PREDICATE, flags));
sink(" ", 1, writer);
- TRY(write_node(writer, object, FIELD_OBJECT, flags));
+ TRY(write_node(writer, object, SERD_OBJECT, flags));
if (writer->syntax == SERD_NQUADS && graph) {
sink(" ", 1, writer);
- TRY(write_node(writer, graph, FIELD_GRAPH, flags));
+ TRY(write_node(writer, graph, SERD_GRAPH, flags));
}
sink(" .\n", 3, writer);
return SERD_SUCCESS;
}
if ((graph && !serd_node_equals(graph, writer->context.graph)) ||
- (!graph && ctx(writer, FIELD_GRAPH))) {
+ (!graph && ctx(writer, SERD_GRAPH))) {
writer->indent = 0;
- if (ctx(writer, FIELD_SUBJECT)) {
+ if (ctx(writer, SERD_SUBJECT)) {
write_sep(writer, SEP_END_S);
}
- if (ctx(writer, FIELD_GRAPH)) {
+ if (ctx(writer, SERD_GRAPH)) {
write_sep(writer, SEP_GRAPH_END);
}
reset_context(writer, true);
if (graph) {
- TRY(write_node(writer, graph, FIELD_GRAPH, flags));
+ TRY(write_node(writer, graph, SERD_GRAPH, flags));
++writer->indent;
write_sep(writer, SEP_GRAPH_BEGIN);
serd_node_set(&writer->context.graph, graph);
@@ -829,20 +823,20 @@ serd_writer_write_statement(SerdWriter* const writer,
++writer->indent;
}
write_sep(writer, SEP_END_O);
- write_node(writer, object, FIELD_OBJECT, flags);
+ write_node(writer, object, SERD_OBJECT, flags);
if (!(flags & SERD_ANON_O_BEGIN)) {
--writer->indent;
}
} else {
// Abbreviate S
- Sep sep = ctx(writer, FIELD_PREDICATE) ? SEP_END_P : SEP_S_P;
+ Sep sep = ctx(writer, SERD_PREDICATE) ? SEP_END_P : SEP_S_P;
write_sep(writer, sep);
write_pred(writer, flags, predicate);
- write_node(writer, object, FIELD_OBJECT, flags);
+ write_node(writer, object, SERD_OBJECT, flags);
}
} else {
// No abbreviation
- if (ctx(writer, FIELD_SUBJECT)) {
+ if (ctx(writer, SERD_SUBJECT)) {
assert(writer->indent > 0);
--writer->indent;
if (serd_stack_is_empty(&writer->anon_stack)) {
@@ -853,7 +847,7 @@ serd_writer_write_statement(SerdWriter* const writer,
}
if (!(flags & SERD_ANON_CONT)) {
- write_node(writer, subject, FIELD_SUBJECT, flags);
+ write_node(writer, subject, SERD_SUBJECT, flags);
++writer->indent;
write_sep(writer, SEP_S_P);
} else {
@@ -867,7 +861,7 @@ serd_writer_write_statement(SerdWriter* const writer,
write_pred(writer, flags, predicate);
}
- write_node(writer, object, FIELD_OBJECT, flags);
+ write_node(writer, object, SERD_OBJECT, flags);
}
if (flags & (SERD_ANON_S_BEGIN | SERD_ANON_O_BEGIN)) {
@@ -923,10 +917,10 @@ serd_writer_end_anon(SerdWriter* writer, const SerdNode* node)
SerdStatus
serd_writer_finish(SerdWriter* writer)
{
- if (ctx(writer, FIELD_SUBJECT)) {
+ if (ctx(writer, SERD_SUBJECT)) {
write_sep(writer, SEP_END_S);
}
- if (ctx(writer, FIELD_GRAPH)) {
+ if (ctx(writer, SERD_GRAPH)) {
write_sep(writer, SEP_GRAPH_END);
}
serd_byte_sink_flush(&writer->byte_sink);
@@ -994,7 +988,7 @@ serd_writer_set_base_uri(SerdWriter* writer, const SerdNode* uri)
serd_env_set_base_uri(writer->env, serd_node_string_view(uri));
if (writer->syntax == SERD_TURTLE || writer->syntax == SERD_TRIG) {
- if (ctx(writer, FIELD_GRAPH) || ctx(writer, FIELD_SUBJECT)) {
+ if (ctx(writer, SERD_GRAPH) || ctx(writer, SERD_SUBJECT)) {
sink(" .\n\n", 4, writer);
reset_context(writer, true);
}
@@ -1040,7 +1034,7 @@ serd_writer_set_prefix(SerdWriter* writer,
}
if (writer->syntax == SERD_TURTLE || writer->syntax == SERD_TRIG) {
- if (ctx(writer, FIELD_GRAPH) || ctx(writer, FIELD_SUBJECT)) {
+ if (ctx(writer, SERD_GRAPH) || ctx(writer, SERD_SUBJECT)) {
sink(" .\n\n", 4, writer);
reset_context(writer, true);
}