aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-05-12 17:56:27 +0200
committerDavid Robillard <d@drobilla.net>2022-01-13 23:03:50 -0500
commitc4df0038bcfba1676bbaaa98badd241ec7d0b55e (patch)
treeac16f53705066640efa3fd7eaaaf888923252017
parent1752a00ebefb22564fb805d4c89deb39ec5e218b (diff)
downloadserd-c4df0038bcfba1676bbaaa98badd241ec7d0b55e.tar.gz
serd-c4df0038bcfba1676bbaaa98badd241ec7d0b55e.tar.bz2
serd-c4df0038bcfba1676bbaaa98badd241ec7d0b55e.zip
Add serd_world_get_blank()
-rw-r--r--include/serd/serd.h10
-rw-r--r--src/world.c27
-rw-r--r--src/world.h3
-rw-r--r--test/meson.build1
-rw-r--r--test/test_world.c47
5 files changed, 86 insertions, 2 deletions
diff --git a/include/serd/serd.h b/include/serd/serd.h
index a04f1c8f..3be32a65 100644
--- a/include/serd/serd.h
+++ b/include/serd/serd.h
@@ -869,6 +869,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 e14a04c9..4413f433 100644
--- a/src/world.c
+++ b/src/world.c
@@ -16,6 +16,7 @@
#include "world.h"
+#include "node.h"
#include "serd_config.h"
#include "system.h"
@@ -27,6 +28,9 @@
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
+
+#define BLANK_CHARS 12
FILE*
serd_world_fopen(SerdWorld* world, const char* path, const char* mode)
@@ -81,13 +85,32 @@ serd_world_errorf(const SerdWorld* const world,
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_STRING("b00000000000"));
+
+ return world;
}
void
serd_world_free(SerdWorld* const world)
{
- free(world);
+ if (world) {
+ serd_node_free(world->blank_node);
+ free(world);
+ }
+}
+
+const SerdNode*
+serd_world_get_blank(SerdWorld* const world)
+{
+ char* buf = serd_node_buffer(world->blank_node);
+ memset(buf, 0, BLANK_CHARS + 1);
+
+ world->blank_node->length =
+ (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/meson.build b/test/meson.build
index 978bc779..05701f42 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -14,6 +14,7 @@ unit_tests = [
'sink',
'string',
'uri',
+ 'world',
'writer',
]
diff --git a/test/test_world.c b/test/test_world.c
new file mode 100644
index 00000000..a7728037
--- /dev/null
+++ b/test/test_world.c
@@ -0,0 +1,47 @@
+/*
+ Copyright 2011-2020 David Robillard <d@drobilla.net>
+
+ Permission to use, copy, modify, and/or distribute this software for any
+ purpose with or without fee is hereby granted, provided that the above
+ copyright notice and this permission notice appear in all copies.
+
+ THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+
+#undef NDEBUG
+
+#include "serd/serd.h"
+
+#include <assert.h>
+#include <stdio.h>
+#include <string.h>
+
+static void
+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);
+}
+
+int
+main(void)
+{
+ test_get_blank();
+
+ return 0;
+}