diff options
author | David Robillard <d@drobilla.net> | 2018-05-12 17:56:27 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2020-10-27 13:13:58 +0100 |
commit | 8b20b3e3bfd186b7b0bbd082a979c7fb86a52479 (patch) | |
tree | a4a8d9722f751ad8dab1e7c021b86f106d33dee6 | |
parent | 40c90e819d20d6933504ccb639896be223e10c3c (diff) | |
download | serd-8b20b3e3bfd186b7b0bbd082a979c7fb86a52479.tar.gz serd-8b20b3e3bfd186b7b0bbd082a979c7fb86a52479.tar.bz2 serd-8b20b3e3bfd186b7b0bbd082a979c7fb86a52479.zip |
Add serd_world_get_blank()
-rw-r--r-- | serd/serd.h | 10 | ||||
-rw-r--r-- | src/world.c | 24 | ||||
-rw-r--r-- | src/world.h | 3 | ||||
-rw-r--r-- | tests/serd_test.c | 18 |
4 files changed, 53 insertions, 2 deletions
diff --git a/serd/serd.h b/serd/serd.h index b10d9ce9..513ac7dc 100644 --- a/serd/serd.h +++ b/serd/serd.h @@ -760,6 +760,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 90033ff5..ebafcfef 100644 --- a/src/world.c +++ b/src/world.c @@ -18,6 +18,7 @@ #include "world.h" +#include "node.h" #include "serd_config.h" #if defined(HAVE_POSIX_FADVISE) @@ -30,6 +31,8 @@ #include <stdlib.h> #include <string.h> +#define BLANK_CHARS 11 + FILE* serd_world_fopen(SerdWorld* world, const char* path, const char* mode) { @@ -76,13 +79,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) { - free(world); + if (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 diff --git a/src/world.h b/src/world.h index 7aaf3689..6d32b529 100644 --- a/src/world.h +++ b/src/world.h @@ -19,11 +19,14 @@ #include "serd/serd.h" +#include <stdint.h> #include <stdio.h> 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 0a65fbe9..168c4ee8 100644 --- a/tests/serd_test.c +++ b/tests/serd_test.c @@ -188,6 +188,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_string(blank), expected)); + } + + serd_world_free(world); + return 0; +} + static void test_read_string(void) { @@ -742,6 +759,7 @@ main(void) test_blank(); test_read_chunks(); test_read_string(); + test_get_blank(); const char* const path = "serd_test.ttl"; test_writer(path); |