aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-05-12 17:56:27 +0200
committerDavid Robillard <d@drobilla.net>2019-12-19 20:52:38 -0500
commit9a2f489a7f6bc60e3f8ccc3881f3fbf8ebd87eda (patch)
tree1cfa97351e8e68a0a6c563cf4efd7ed79c460ae6
parent83ee262f7a952bf59b327982f5e66115036f9259 (diff)
downloadserd-9a2f489a7f6bc60e3f8ccc3881f3fbf8ebd87eda.tar.gz
serd-9a2f489a7f6bc60e3f8ccc3881f3fbf8ebd87eda.tar.bz2
serd-9a2f489a7f6bc60e3f8ccc3881f3fbf8ebd87eda.zip
Add serd_world_get_blank()
-rw-r--r--serd/serd.h10
-rw-r--r--src/world.c20
-rw-r--r--src/world.h2
-rw-r--r--tests/serd_test.c18
4 files changed, 49 insertions, 1 deletions
diff --git a/serd/serd.h b/serd/serd.h
index 5f1c4929..aceb9cdc 100644
--- a/serd/serd.h
+++ b/serd/serd.h
@@ -761,6 +761,16 @@ void
serd_world_free(SerdWorld* world);
/**
+ Return a unique blank node.
+
+ The returned node is valid only until the next time serd_world_get_blank()
+ is called or the world is destroyed.
+*/
+SERD_API
+const SerdNode*
+serd_world_get_blank(SerdWorld* world);
+
+/**
Set a function to be called when errors occur.
The `error_sink` will be called with `handle` as its first argument. If
diff --git a/src/world.c b/src/world.c
index f95accf4..9908e4d4 100644
--- a/src/world.c
+++ b/src/world.c
@@ -18,6 +18,7 @@
#include "world.h"
+#include "node.h"
#include "serd_config.h"
#include <errno.h>
@@ -29,6 +30,8 @@
# include <fcntl.h>
#endif
+#define BLANK_CHARS 11
+
FILE*
serd_world_fopen(SerdWorld* world, const char* path, const char* mode)
{
@@ -71,15 +74,30 @@ serd_world_errorf(const SerdWorld* world, SerdStatus st, const char* fmt, ...)
SerdWorld*
serd_world_new(void)
{
- return (SerdWorld*)calloc(1, sizeof(SerdWorld));
+ SerdWorld* world = (SerdWorld*)calloc(1, sizeof(SerdWorld));
+
+ world->blank_node = serd_node_new_blank("b0000000000");
+
+ return world;
}
void
serd_world_free(SerdWorld* world)
{
+ serd_node_free(world->blank_node);
free(world);
}
+const SerdNode*
+serd_world_get_blank(SerdWorld* world)
+{
+ char* buf = serd_node_buffer(world->blank_node);
+ memset(buf, 0, BLANK_CHARS + 1);
+ world->blank_node->n_bytes = snprintf(
+ buf, BLANK_CHARS, "b%u", ++world->next_blank_id);
+ return world->blank_node;
+}
+
void
serd_world_set_error_sink(SerdWorld* world,
SerdErrorSink error_sink,
diff --git a/src/world.h b/src/world.h
index 0aaea8fc..201d038f 100644
--- a/src/world.h
+++ b/src/world.h
@@ -25,6 +25,8 @@
struct SerdWorldImpl {
SerdErrorSink error_sink;
void* error_handle;
+ uint32_t next_blank_id;
+ SerdNode* blank_node;
};
FILE* serd_world_fopen(SerdWorld* world, const char* path, const char* mode);
diff --git a/tests/serd_test.c b/tests/serd_test.c
index f280c22a..81ae33d0 100644
--- a/tests/serd_test.c
+++ b/tests/serd_test.c
@@ -176,6 +176,23 @@ test_read_chunks(void)
serd_world_free(world);
}
+static int
+test_get_blank(void)
+{
+ SerdWorld* world = serd_world_new();
+ char expected[8];
+
+ for (unsigned i = 0; i < 32; ++i) {
+ const SerdNode* blank = serd_world_get_blank(world);
+
+ snprintf(expected, sizeof(expected), "b%u", i + 1);
+ assert(!strcmp(serd_node_get_string(blank), expected));
+ }
+
+ serd_world_free(world);
+ return 0;
+}
+
static void
test_string_to_double(void)
{
@@ -742,6 +759,7 @@ main(void)
test_blank();
test_env();
test_read_chunks();
+ test_get_blank();
const char* const path = "serd_test.ttl";
test_writer(path);