aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2019-12-19 09:16:07 -0500
committerDavid Robillard <d@drobilla.net>2023-12-02 18:49:07 -0500
commit3b89559e4c08202bee228e25ea42a4371e333c9b (patch)
treea84406674c7237ab5abeecf56e64f83dfa90f6bb
parent8c4e3097517e95c8c6ff67f0a972525bfeb3bb1d (diff)
downloadserd-3b89559e4c08202bee228e25ea42a4371e333c9b.tar.gz
serd-3b89559e4c08202bee228e25ea42a4371e333c9b.tar.bz2
serd-3b89559e4c08202bee228e25ea42a4371e333c9b.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/statement.h8
-rw-r--r--src/writer.c74
2 files changed, 41 insertions, 41 deletions
diff --git a/include/serd/statement.h b/include/serd/statement.h
index 321aace4..33911c41 100644
--- a/include/serd/statement.h
+++ b/include/serd/statement.h
@@ -16,6 +16,14 @@ SERD_BEGIN_DECLS
@{
*/
+/// Index of a node in a statement
+typedef enum {
+ SERD_SUBJECT = 0U, ///< Subject
+ SERD_PREDICATE = 1U, ///< Predicate ("key")
+ SERD_OBJECT = 2U, ///< Object ("value")
+ SERD_GRAPH = 3U, ///< Graph ("context")
+} SerdField;
+
/// Flags indicating inline abbreviation information for a statement
typedef enum {
SERD_EMPTY_S = 1U << 1U, ///< Empty blank node subject
diff --git a/src/writer.c b/src/writer.c
index ced64adc..5419e565 100644
--- a/src/writer.c
+++ b/src/writer.c
@@ -42,14 +42,6 @@ typedef enum {
CTX_LIST, ///< Anonymous list
} ContextType;
-typedef enum {
- FIELD_NONE,
- FIELD_SUBJECT,
- FIELD_PREDICATE,
- FIELD_OBJECT,
- FIELD_GRAPH,
-} Field;
-
typedef struct {
ContextType type;
SerdNode* graph;
@@ -154,7 +146,7 @@ serd_writer_set_prefix(SerdWriter* writer,
SERD_NODISCARD static SerdStatus
write_node(SerdWriter* writer,
const SerdNode* node,
- Field field,
+ SerdField field,
SerdStatementFlags flags);
SERD_NODISCARD static bool
@@ -201,12 +193,12 @@ w_err(SerdWriter* writer, SerdStatus st, const char* fmt, ...)
}
static inline SerdNode*
-ctx(SerdWriter* writer, const Field field)
+ctx(SerdWriter* writer, const SerdField field)
{
- SerdNode* node = (field == FIELD_SUBJECT) ? writer->context.subject
- : (field == FIELD_PREDICATE) ? writer->context.predicate
- : (field == FIELD_GRAPH) ? writer->context.graph
- : NULL;
+ SerdNode* node = (field == SERD_SUBJECT) ? writer->context.subject
+ : (field == SERD_PREDICATE) ? writer->context.predicate
+ : (field == SERD_GRAPH) ? writer->context.graph
+ : NULL;
return node && node->type ? node : NULL;
}
@@ -639,7 +631,7 @@ write_sep(SerdWriter* writer, const Sep sep)
// Reset context and write a blank line after ends of subjects
if (sep == SEP_END_S) {
- writer->indent = ctx(writer, FIELD_GRAPH) ? 1 : 0;
+ writer->indent = ctx(writer, SERD_GRAPH) ? 1 : 0;
writer->context.predicates = false;
writer->context.comma_indented = false;
TRY(st, esink("\n", 1, writer));
@@ -730,7 +722,7 @@ write_literal(SerdWriter* const writer,
TRY(st, esink(serd_node_string(lang), lang->length, writer));
} else if (type_uri) {
TRY(st, esink("^^", 2, writer));
- return write_node(writer, datatype, FIELD_NONE, flags);
+ return write_node(writer, datatype, (SerdField)-1, flags);
}
return st;
@@ -751,9 +743,9 @@ is_name(const char* buf, const size_t len)
}
SERD_NODISCARD static SerdStatus
-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)
{
SerdStatus st = SERD_SUCCESS;
const SerdNode* prefix = NULL;
@@ -762,7 +754,7 @@ write_uri_node(SerdWriter* const writer,
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 esink("a", 1, writer);
}
@@ -843,25 +835,25 @@ write_curie(SerdWriter* const writer, const SerdNode* const node)
SERD_NODISCARD static SerdStatus
write_blank(SerdWriter* const writer,
const SerdNode* node,
- const Field field,
+ const SerdField field,
const SerdStatementFlags flags)
{
SerdStatus st = SERD_SUCCESS;
const char* const node_str = serd_node_string(node);
if (supports_abbrev(writer)) {
- if ((field == FIELD_SUBJECT && (flags & SERD_ANON_S_BEGIN)) ||
- (field == FIELD_OBJECT && (flags & SERD_ANON_O_BEGIN))) {
+ if ((field == SERD_SUBJECT && (flags & SERD_ANON_S_BEGIN)) ||
+ (field == SERD_OBJECT && (flags & SERD_ANON_O_BEGIN))) {
return write_sep(writer, SEP_ANON_BEGIN);
}
- if ((field == FIELD_SUBJECT && (flags & SERD_LIST_S_BEGIN)) ||
- (field == FIELD_OBJECT && (flags & SERD_LIST_O_BEGIN))) {
+ if ((field == SERD_SUBJECT && (flags & SERD_LIST_S_BEGIN)) ||
+ (field == SERD_OBJECT && (flags & SERD_LIST_O_BEGIN))) {
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 esink("[]", 2, writer);
}
}
@@ -883,7 +875,7 @@ write_blank(SerdWriter* const writer,
SERD_NODISCARD static SerdStatus
write_node(SerdWriter* const writer,
const SerdNode* const node,
- const Field field,
+ const SerdField field,
const SerdStatementFlags flags)
{
SerdStatus st = SERD_SUCCESS;
@@ -921,7 +913,7 @@ write_pred(SerdWriter* writer, SerdStatementFlags flags, const SerdNode* pred)
{
SerdStatus st = SERD_SUCCESS;
- TRY(st, write_node(writer, pred, FIELD_PREDICATE, flags));
+ TRY(st, write_node(writer, pred, SERD_PREDICATE, flags));
TRY(st, write_sep(writer, SEP_P_O));
serd_node_set(&writer->context.predicate, pred);
@@ -944,7 +936,7 @@ write_list_next(SerdWriter* const writer,
}
if (!strcmp(serd_node_string(predicate), NS_RDF "first")) {
- TRY(st, write_node(writer, object, FIELD_OBJECT, flags));
+ TRY(st, write_node(writer, object, SERD_OBJECT, flags));
} else {
TRY(st, write_sep(writer, SEP_LIST_SEP));
}
@@ -984,14 +976,14 @@ serd_writer_write_statement(SerdWriter* const writer,
// Simple case: write a line of NTriples or NQuads
if (writer->syntax == SERD_NTRIPLES || writer->syntax == SERD_NQUADS) {
- TRY(st, write_node(writer, subject, FIELD_SUBJECT, flags));
+ TRY(st, write_node(writer, subject, SERD_SUBJECT, flags));
TRY(st, esink(" ", 1, writer));
- TRY(st, write_node(writer, predicate, FIELD_PREDICATE, flags));
+ TRY(st, write_node(writer, predicate, SERD_PREDICATE, flags));
TRY(st, esink(" ", 1, writer));
- TRY(st, write_node(writer, object, FIELD_OBJECT, flags));
+ TRY(st, write_node(writer, object, SERD_OBJECT, flags));
if (writer->syntax == SERD_NQUADS && graph) {
TRY(st, esink(" ", 1, writer));
- TRY(st, write_node(writer, graph, FIELD_GRAPH, flags));
+ TRY(st, write_node(writer, graph, SERD_GRAPH, flags));
}
TRY(st, esink(" .\n", 3, writer));
return SERD_SUCCESS;
@@ -999,12 +991,12 @@ serd_writer_write_statement(SerdWriter* const writer,
// Separate graphs if necessary
if ((graph && !serd_node_equals(graph, writer->context.graph)) ||
- (!graph && ctx(writer, FIELD_GRAPH))) {
+ (!graph && ctx(writer, SERD_GRAPH))) {
TRY(st, terminate_context(writer));
reset_context(writer, RESET_GRAPH | RESET_INDENT);
if (graph) {
TRY(st, write_newline(writer));
- TRY(st, write_node(writer, graph, FIELD_GRAPH, flags));
+ TRY(st, write_node(writer, graph, SERD_GRAPH, flags));
TRY(st, write_sep(writer, SEP_GRAPH_BEGIN));
serd_node_set(&writer->context.graph, graph);
}
@@ -1046,17 +1038,17 @@ serd_writer_write_statement(SerdWriter* const writer,
writer->context.comma_indented = false;
}
- const bool first = !ctx(writer, FIELD_PREDICATE);
+ const bool first = !ctx(writer, SERD_PREDICATE);
TRY(st, write_sep(writer, first ? SEP_S_P : SEP_END_P));
TRY(st, write_pred(writer, flags, predicate));
}
- TRY(st, write_node(writer, object, FIELD_OBJECT, flags));
+ TRY(st, write_node(writer, object, SERD_OBJECT, flags));
} else {
// No abbreviation
if (serd_stack_is_empty(&writer->anon_stack)) {
- if (ctx(writer, FIELD_SUBJECT)) {
+ if (ctx(writer, SERD_SUBJECT)) {
TRY(st, write_sep(writer, SEP_END_S));
}
@@ -1064,7 +1056,7 @@ serd_writer_write_statement(SerdWriter* const writer,
TRY(st, write_newline(writer));
}
- TRY(st, write_node(writer, subject, FIELD_SUBJECT, flags));
+ TRY(st, write_node(writer, subject, SERD_SUBJECT, flags));
if ((flags & (SERD_ANON_S_BEGIN | SERD_LIST_S_BEGIN))) {
TRY(st, write_sep(writer, SEP_ANON_S_P));
} else {
@@ -1082,7 +1074,7 @@ serd_writer_write_statement(SerdWriter* const writer,
TRY(st, write_pred(writer, flags, predicate));
}
- TRY(st, write_node(writer, object, FIELD_OBJECT, flags));
+ TRY(st, write_node(writer, object, SERD_OBJECT, flags));
}
if (flags & (SERD_ANON_S_BEGIN | SERD_LIST_S_BEGIN)) {