aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-04-29 15:24:05 +0200
committerDavid Robillard <d@drobilla.net>2018-05-27 18:21:57 +0200
commit40268925466dba7a37b468be0bba951e7f6ca5d1 (patch)
tree066808e4923a0fb0cce7d4609e021e0b0907b5ea
parent129bc1d39254ff4c56e9cffab6d064bc5863ea2f (diff)
downloadserd-40268925466dba7a37b468be0bba951e7f6ca5d1.tar.gz
serd-40268925466dba7a37b468be0bba951e7f6ca5d1.tar.bz2
serd-40268925466dba7a37b468be0bba951e7f6ca5d1.zip
Add SerdWorld for shared library state
-rw-r--r--serd/serd.h72
-rw-r--r--src/reader.c15
-rw-r--r--src/reader.h1
-rw-r--r--src/serd_internal.h8
-rw-r--r--src/serdi.c13
-rw-r--r--src/world.c40
-rw-r--r--src/world.h27
-rw-r--r--src/writer.c16
-rw-r--r--tests/read_chunk_test.c4
-rw-r--r--tests/serd_test.c9
-rw-r--r--wscript1
11 files changed, 145 insertions, 61 deletions
diff --git a/serd/serd.h b/serd/serd.h
index 4e9b5d48..715ff22a 100644
--- a/serd/serd.h
+++ b/serd/serd.h
@@ -56,6 +56,13 @@ extern "C" {
*/
/**
+ World.
+
+ The World represents all library state shared between various objects.
+*/
+typedef struct SerdWorldImpl SerdWorld;
+
+/**
Environment.
Represents the state required to resolve a CURIE or relative URI, e.g. the
@@ -695,6 +702,41 @@ typedef struct SerdSinkInterface {
/**
@}
+ @name World
+ @{
+*/
+
+/**
+ Create a new Serd World.
+
+ It is safe to use multiple worlds in one process, though no objects can be
+ shared between worlds.
+*/
+SERD_API
+SerdWorld*
+serd_world_new(void);
+
+/**
+ Free `world`.
+*/
+SERD_API
+void
+serd_world_free(SerdWorld* world);
+
+/**
+ Set a function to be called when errors occur.
+
+ The `error_sink` will be called with `handle` as its first argument. If
+ no error function is set, errors are printed to stderr.
+*/
+SERD_API
+void
+serd_world_set_error_sink(SerdWorld* world,
+ SerdErrorSink error_sink,
+ void* handle);
+
+/**
+ @}
@name Environment
@{
*/
@@ -800,7 +842,8 @@ serd_env_foreach(const SerdEnv* env,
*/
SERD_API
SerdReader*
-serd_reader_new(SerdSyntax syntax,
+serd_reader_new(SerdWorld* world,
+ SerdSyntax syntax,
const SerdSinkInterface* sink);
/**
@@ -815,18 +858,6 @@ void
serd_reader_set_strict(SerdReader* reader, bool strict);
/**
- Set a function to be called when errors occur during reading.
-
- The `error_sink` will be called with `handle` as its first argument. If
- no error function is set, errors are printed to stderr in GCC style.
-*/
-SERD_API
-void
-serd_reader_set_error_sink(SerdReader* reader,
- SerdErrorSink error_sink,
- void* error_handle);
-
-/**
Return the `handle` passed to serd_reader_new().
*/
SERD_API
@@ -944,7 +975,8 @@ serd_reader_free(SerdReader* reader);
*/
SERD_API
SerdWriter*
-serd_writer_new(SerdSyntax syntax,
+serd_writer_new(SerdWorld* world,
+ SerdSyntax syntax,
SerdStyle style,
SerdEnv* env,
const SerdURI* base_uri,
@@ -1005,18 +1037,6 @@ char*
serd_buffer_sink_finish(SerdBuffer* stream);
/**
- Set a function to be called when errors occur during writing.
-
- The `error_sink` will be called with `handle` as its first argument. If
- no error function is set, errors are printed to stderr.
-*/
-SERD_API
-void
-serd_writer_set_error_sink(SerdWriter* writer,
- SerdErrorSink error_sink,
- void* error_handle);
-
-/**
Set a prefix to be removed from matching blank node identifiers.
*/
SERD_API
diff --git a/src/reader.c b/src/reader.c
index 8c3988d0..27950da6 100644
--- a/src/reader.c
+++ b/src/reader.c
@@ -35,7 +35,7 @@ r_err(SerdReader* reader, SerdStatus st, const char* fmt, ...)
va_start(args, fmt);
const Cursor* const cur = &reader->source.cur;
const SerdError e = { st, cur->filename, cur->line, cur->col, fmt, &args };
- serd_error(reader->error_sink, reader->error_handle, &e);
+ serd_error(reader->world, &e);
va_end(args);
return 0;
}
@@ -155,10 +155,12 @@ serd_reader_read_document(SerdReader* reader)
}
SerdReader*
-serd_reader_new(SerdSyntax syntax,
+serd_reader_new(SerdWorld* world,
+ SerdSyntax syntax,
const SerdSinkInterface* sink)
{
SerdReader* me = (SerdReader*)calloc(1, sizeof(SerdReader));
+ me->world = world;
me->sink = sink;
me->default_graph = NULL;
me->stack = serd_stack_new(SERD_PAGE_SIZE);
@@ -180,15 +182,6 @@ serd_reader_set_strict(SerdReader* reader, bool strict)
}
void
-serd_reader_set_error_sink(SerdReader* reader,
- SerdErrorSink error_sink,
- void* error_handle)
-{
- reader->error_sink = error_sink;
- reader->error_handle = error_handle;
-}
-
-void
serd_reader_free(SerdReader* reader)
{
pop_node(reader, reader->rdf_nil);
diff --git a/src/reader.h b/src/reader.h
index ec2532b0..2c915ada 100644
--- a/src/reader.h
+++ b/src/reader.h
@@ -46,6 +46,7 @@ typedef struct {
} ReadContext;
struct SerdReaderImpl {
+ SerdWorld* world;
const SerdSinkInterface* sink;
SerdErrorSink error_sink;
void* error_handle;
diff --git a/src/serd_internal.h b/src/serd_internal.h
index fbf493e9..172343b8 100644
--- a/src/serd_internal.h
+++ b/src/serd_internal.h
@@ -27,6 +27,8 @@
#include "serd/serd.h"
#include "serd_config.h"
+#include "world.h"
+
#if defined(HAVE_POSIX_FADVISE) && defined(HAVE_FILENO)
# include <fcntl.h>
#endif
@@ -68,10 +70,10 @@ serd_bufalloc(size_t size)
}
static inline void
-serd_error(SerdErrorSink error_sink, void* handle, const SerdError* e)
+serd_error(const SerdWorld* world, const SerdError* e)
{
- if (error_sink) {
- error_sink(handle, e);
+ if (world->error_sink) {
+ world->error_sink(world->error_handle, e);
} else {
fprintf(stderr, "error: %s:%u:%u: ", e->filename, e->line, e->col);
vfprintf(stderr, e->fmt, *e->args);
diff --git a/src/serdi.c b/src/serdi.c
index dc8e3207..01484c8c 100644
--- a/src/serdi.c
+++ b/src/serdi.c
@@ -249,8 +249,9 @@ main(int argc, char** argv)
base = serd_node_new_file_uri(input, NULL, &base_uri, true);
}
- FILE* out_fd = stdout;
- SerdEnv* env = serd_env_new(base);
+ FILE* out_fd = stdout;
+ SerdWorld* world = serd_world_new();
+ SerdEnv* env = serd_env_new(base);
int output_style = 0;
if (output_syntax == SERD_NTRIPLES || ascii) {
@@ -273,16 +274,15 @@ main(int argc, char** argv)
}
SerdWriter* writer = serd_writer_new(
- output_syntax, (SerdStyle)output_style,
+ world, output_syntax, (SerdStyle)output_style,
env, &base_uri, serd_file_sink, out_fd);
SerdReader* reader = serd_reader_new(
- input_syntax, serd_writer_get_sink_interface(writer));
+ world, input_syntax, serd_writer_get_sink_interface(writer));
serd_reader_set_strict(reader, !lax);
if (quiet) {
- serd_reader_set_error_sink(reader, quiet_error_sink, NULL);
- serd_writer_set_error_sink(writer, quiet_error_sink, NULL);
+ serd_world_set_error_sink(world, quiet_error_sink, NULL);
}
SerdNode* root = serd_node_new_string(SERD_URI, root_uri);
@@ -313,6 +313,7 @@ main(int argc, char** argv)
serd_writer_free(writer);
serd_env_free(env);
serd_node_free(base);
+ serd_world_free(world);
free(input_path);
if (from_file) {
diff --git a/src/world.c b/src/world.c
new file mode 100644
index 00000000..07439137
--- /dev/null
+++ b/src/world.c
@@ -0,0 +1,40 @@
+/*
+ Copyright 2011-2018 David Robillard <http://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.
+*/
+
+#include "serd_internal.h"
+
+#include "world.h"
+
+SerdWorld*
+serd_world_new(void)
+{
+ return (SerdWorld*)calloc(1, sizeof(SerdWorld));
+}
+
+void
+serd_world_free(SerdWorld* world)
+{
+ free(world);
+}
+
+void
+serd_world_set_error_sink(SerdWorld* world,
+ SerdErrorSink error_sink,
+ void* handle)
+{
+ world->error_sink = error_sink;
+ world->error_handle = handle;
+}
diff --git a/src/world.h b/src/world.h
new file mode 100644
index 00000000..88241b0e
--- /dev/null
+++ b/src/world.h
@@ -0,0 +1,27 @@
+/*
+ Copyright 2011-2018 David Robillard <http://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.
+*/
+
+#ifndef SERD_WORLD_H
+#define SERD_WORLD_H
+
+#include "serd_internal.h"
+
+struct SerdWorldImpl {
+ SerdErrorSink error_sink;
+ void* error_handle;
+};
+
+#endif // SERD_WORLD_H
diff --git a/src/writer.c b/src/writer.c
index c57323b0..f478524e 100644
--- a/src/writer.c
+++ b/src/writer.c
@@ -89,6 +89,7 @@ static const SepRule rules[] = {
};
struct SerdWriterImpl {
+ SerdWorld* world;
SerdSinkInterface iface;
SerdSyntax syntax;
SerdStyle style;
@@ -144,7 +145,7 @@ w_err(SerdWriter* writer, SerdStatus st, const char* fmt, ...)
va_list args;
va_start(args, fmt);
const SerdError e = { st, NULL, 0, 0, fmt, &args };
- serd_error(writer->error_sink, writer->error_handle, &e);
+ serd_error(writer->world, &e);
va_end(args);
}
@@ -874,7 +875,8 @@ serd_writer_finish(SerdWriter* writer)
}
SerdWriter*
-serd_writer_new(SerdSyntax syntax,
+serd_writer_new(SerdWorld* world,
+ SerdSyntax syntax,
SerdStyle style,
SerdEnv* env,
const SerdURI* base_uri,
@@ -883,6 +885,7 @@ serd_writer_new(SerdSyntax syntax,
{
const WriteContext context = WRITE_CONTEXT_NULL;
SerdWriter* writer = (SerdWriter*)calloc(1, sizeof(SerdWriter));
+ writer->world = world;
writer->syntax = syntax;
writer->style = style;
writer->env = env;
@@ -906,15 +909,6 @@ serd_writer_new(SerdSyntax syntax,
}
void
-serd_writer_set_error_sink(SerdWriter* writer,
- SerdErrorSink error_sink,
- void* error_handle)
-{
- writer->error_sink = error_sink;
- writer->error_handle = error_handle;
-}
-
-void
serd_writer_chop_blank_prefix(SerdWriter* writer,
const char* prefix)
{
diff --git a/tests/read_chunk_test.c b/tests/read_chunk_test.c
index ed648e5f..1e41ca8a 100644
--- a/tests/read_chunk_test.c
+++ b/tests/read_chunk_test.c
@@ -59,8 +59,9 @@ on_end(void* handle, const SerdNode* node)
int
main(int argc, char** argv)
{
+ SerdWorld* world = serd_world_new();
SerdSinkInterface sink = { 0, on_base, on_prefix, on_statement, on_end };
- SerdReader* reader = serd_reader_new(SERD_TURTLE, &sink);
+ SerdReader* reader = serd_reader_new(world, SERD_TURTLE, &sink);
if (!reader) {
FAIL("Failed to create reader\n");
}
@@ -99,5 +100,6 @@ main(int argc, char** argv)
}
serd_reader_free(reader);
+ serd_world_free(world);
return 0;
}
diff --git a/tests/serd_test.c b/tests/serd_test.c
index 9fd52852..ccb3c5b6 100644
--- a/tests/serd_test.c
+++ b/tests/serd_test.c
@@ -407,6 +407,8 @@ main(void)
// Test SerdEnv
+ SerdWorld* world = serd_world_new();
+
SerdNode* u = serd_node_new_string(SERD_URI, "http://example.org/foo");
SerdNode* b = serd_node_new_string(SERD_CURIE, "invalid");
SerdNode* c = serd_node_new_string(SERD_CURIE, "eg.2:b");
@@ -496,7 +498,7 @@ main(void)
}
SerdWriter* writer = serd_writer_new(
- SERD_TURTLE, (SerdStyle)0, env, NULL, serd_file_sink, fd);
+ world, SERD_TURTLE, (SerdStyle)0, env, NULL, serd_file_sink, fd);
if (!writer) {
FAIL("Failed to create writer\n");
}
@@ -589,7 +591,7 @@ main(void)
// Test buffer sink
SerdBuffer buffer = { NULL, 0 };
writer = serd_writer_new(
- SERD_TURTLE, (SerdStyle)0, env, NULL, serd_buffer_sink, &buffer);
+ world, SERD_TURTLE, (SerdStyle)0, env, NULL, serd_buffer_sink, &buffer);
o = serd_node_new_string(SERD_URI, "http://example.org/base");
if (serd_writer_set_base_uri(writer, o)) {
@@ -611,7 +613,7 @@ main(void)
ReaderTest* rt = (ReaderTest*)calloc(1, sizeof(ReaderTest));
SerdSinkInterface sink = { rt, NULL, NULL, test_sink, NULL };
- SerdReader* reader = serd_reader_new(SERD_TURTLE, &sink);
+ SerdReader* reader = serd_reader_new(world, SERD_TURTLE, &sink);
if (!reader) {
FAIL("Failed to create reader\n");
}
@@ -648,6 +650,7 @@ main(void)
fclose(fd);
serd_env_free(env);
+ serd_world_free(world);
printf("Success\n");
return 0;
diff --git a/wscript b/wscript
index 26efaa35..11052e76 100644
--- a/wscript
+++ b/wscript
@@ -82,6 +82,7 @@ lib_source = ['src/byte_source.c',
'src/reader.c',
'src/string.c',
'src/uri.c',
+ 'src/world.c',
'src/writer.c']
def build(bld):