diff options
author | David Robillard <d@drobilla.net> | 2018-05-12 17:56:27 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2021-03-08 23:23:05 -0500 |
commit | f0f4bfb37f36fce6d6e216a90162b81691f61e83 (patch) | |
tree | c3038f393641334ba2c04bf82acc545f1038803f | |
parent | fd276bcf70bad4520ca1d6f3c73f88073b3b58cf (diff) | |
download | serd-f0f4bfb37f36fce6d6e216a90162b81691f61e83.tar.gz serd-f0f4bfb37f36fce6d6e216a90162b81691f61e83.tar.bz2 serd-f0f4bfb37f36fce6d6e216a90162b81691f61e83.zip |
Add serd_world_get_blank()
-rw-r--r-- | include/serd/serd.h | 10 | ||||
-rw-r--r-- | src/world.c | 26 | ||||
-rw-r--r-- | src/world.h | 3 | ||||
-rw-r--r-- | test/test_reader_writer.c | 18 |
4 files changed, 55 insertions, 2 deletions
diff --git a/include/serd/serd.h b/include/serd/serd.h index d57f1470..3d08eb07 100644 --- a/include/serd/serd.h +++ b/include/serd/serd.h @@ -871,6 +871,16 @@ void serd_world_free(SerdWorld* SERD_NULLABLE 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_NONNULL +serd_world_get_blank(SerdWorld* SERD_NONNULL world); + +/** Set a function to be called when errors occur. The `error_func` will be called with `handle` as its first argument. If diff --git a/src/world.c b/src/world.c index 73bc9927..9503c578 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(USE_POSIX_FADVISE) @@ -30,6 +31,8 @@ #include <stdlib.h> #include <string.h> +#define BLANK_CHARS 12 + FILE* serd_world_fopen(SerdWorld* world, const char* path, const char* mode) { @@ -78,13 +81,32 @@ 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_new_blank(SERD_STATIC_STRING("b00000000000")); + + 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 = + (size_t)snprintf(buf, BLANK_CHARS + 1, "b%u", ++world->next_blank_id); + + return world->blank_node; } void diff --git a/src/world.h b/src/world.h index ab7aad80..99f0c92d 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 { SerdErrorFunc error_func; void* error_handle; + uint32_t next_blank_id; + SerdNode* blank_node; }; /// Open a file configured for fast sequential reading diff --git a/test/test_reader_writer.c b/test/test_reader_writer.c index 48f20247..43b820db 100644 --- a/test/test_reader_writer.c +++ b/test/test_reader_writer.c @@ -140,6 +140,23 @@ test_read_chunks(void) serd_world_free(world); } +static int +test_get_blank(void) +{ + SerdWorld* world = serd_world_new(); + char expected[12]; + + 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) { @@ -358,6 +375,7 @@ main(void) { test_read_chunks(); test_read_string(); + test_get_blank(); const char* const path = "serd_test.ttl"; test_writer(path); |