aboutsummaryrefslogtreecommitdiffstats
path: root/src/statement.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-06-03 19:24:59 +0200
committerDavid Robillard <d@drobilla.net>2023-12-02 18:49:07 -0500
commit40534e6c42daabb6cc8ec2d49785bec088d1d3a8 (patch)
tree593e37de3e9bce98e291ec098a7081e6e26240b0 /src/statement.c
parent38f85ad8c53c618033e3d0b7cb1fe782c38cf0a3 (diff)
downloadserd-40534e6c42daabb6cc8ec2d49785bec088d1d3a8.tar.gz
serd-40534e6c42daabb6cc8ec2d49785bec088d1d3a8.tar.bz2
serd-40534e6c42daabb6cc8ec2d49785bec088d1d3a8.zip
Add SerdStatement
Diffstat (limited to 'src/statement.c')
-rw-r--r--src/statement.c142
1 files changed, 142 insertions, 0 deletions
diff --git a/src/statement.c b/src/statement.c
new file mode 100644
index 00000000..430194b7
--- /dev/null
+++ b/src/statement.c
@@ -0,0 +1,142 @@
+// Copyright 2011-2020 David Robillard <d@drobilla.net>
+// SPDX-License-Identifier: ISC
+
+#include "statement.h"
+
+#include "caret.h"
+#include "node.h"
+
+#include "serd/statement.h"
+
+#include <assert.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
+
+static bool
+is_resource(const SerdNode* const node)
+{
+ const SerdNodeType type = node ? serd_node_type(node) : (SerdNodeType)0;
+ return type == SERD_URI || type == SERD_CURIE || type == SERD_BLANK;
+}
+
+bool
+serd_statement_is_valid(const SerdNode* const subject,
+ const SerdNode* const predicate,
+ const SerdNode* const object,
+ const SerdNode* const graph)
+{
+ return is_resource(subject) && is_resource(predicate) && object &&
+ serd_node_type(predicate) != SERD_BLANK &&
+ (!graph || is_resource(graph));
+}
+
+SerdStatement*
+serd_statement_new(const SerdNode* const s,
+ const SerdNode* const p,
+ const SerdNode* const o,
+ const SerdNode* const g,
+ const SerdCaret* const caret)
+{
+ assert(s);
+ assert(p);
+ assert(o);
+
+ if (!serd_statement_is_valid(s, p, o, g)) {
+ return NULL;
+ }
+
+ SerdStatement* statement = (SerdStatement*)malloc(sizeof(SerdStatement));
+ if (statement) {
+ statement->nodes[0] = s;
+ statement->nodes[1] = p;
+ statement->nodes[2] = o;
+ statement->nodes[3] = g;
+ statement->caret = serd_caret_copy(caret);
+ }
+ return statement;
+}
+
+SerdStatement*
+serd_statement_copy(const SerdStatement* const statement)
+{
+ if (!statement) {
+ return NULL;
+ }
+
+ SerdStatement* copy = (SerdStatement*)malloc(sizeof(SerdStatement));
+ memcpy(copy, statement, sizeof(SerdStatement));
+ if (statement->caret) {
+ copy->caret = (SerdCaret*)malloc(sizeof(SerdCaret));
+ memcpy(copy->caret, statement->caret, sizeof(SerdCaret));
+ }
+ return copy;
+}
+
+void
+serd_statement_free(SerdStatement* const statement)
+{
+ if (statement) {
+ free(statement->caret);
+ free(statement);
+ }
+}
+
+const SerdNode*
+serd_statement_node(const SerdStatement* const statement, const SerdField field)
+{
+ return statement->nodes[field];
+}
+
+const SerdNode*
+serd_statement_subject(const SerdStatement* const statement)
+{
+ return statement->nodes[SERD_SUBJECT];
+}
+
+const SerdNode*
+serd_statement_predicate(const SerdStatement* const statement)
+{
+ return statement->nodes[SERD_PREDICATE];
+}
+
+const SerdNode*
+serd_statement_object(const SerdStatement* const statement)
+{
+ return statement->nodes[SERD_OBJECT];
+}
+
+const SerdNode*
+serd_statement_graph(const SerdStatement* const statement)
+{
+ return statement->nodes[SERD_GRAPH];
+}
+
+const SerdCaret*
+serd_statement_caret(const SerdStatement* const statement)
+{
+ return statement->caret;
+}
+
+bool
+serd_statement_equals(const SerdStatement* const a,
+ const SerdStatement* const b)
+{
+ return (a == b || (a && b && serd_node_equals(a->nodes[0], b->nodes[0]) &&
+ serd_node_equals(a->nodes[1], b->nodes[1]) &&
+ serd_node_equals(a->nodes[2], b->nodes[2]) &&
+ serd_node_equals(a->nodes[3], b->nodes[3])));
+}
+
+bool
+serd_statement_matches(const SerdStatement* const statement,
+ const SerdNode* const subject,
+ const SerdNode* const predicate,
+ const SerdNode* const object,
+ const SerdNode* const graph)
+{
+ return (serd_node_pattern_match(statement->nodes[0], subject) &&
+ serd_node_pattern_match(statement->nodes[1], predicate) &&
+ serd_node_pattern_match(statement->nodes[2], object) &&
+ serd_node_pattern_match(statement->nodes[3], graph));
+}