diff options
author | David Robillard <d@drobilla.net> | 2021-07-11 20:47:51 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2023-12-02 18:49:07 -0500 |
commit | 6eb1fa15a06ab7de08e33add1540a45b83c5f0d8 (patch) | |
tree | 86d3cfedbf2ff76878eba6d14dc7fc259d1cbf46 | |
parent | d1ba721d37af61f2b529faaa16bd20ba1e161b06 (diff) | |
download | serd-6eb1fa15a06ab7de08e33add1540a45b83c5f0d8.tar.gz serd-6eb1fa15a06ab7de08e33add1540a45b83c5f0d8.tar.bz2 serd-6eb1fa15a06ab7de08e33add1540a45b83c5f0d8.zip |
Add SerdWorld for shared library state
-rw-r--r-- | .clang-format | 5 | ||||
-rw-r--r-- | NEWS | 1 | ||||
-rw-r--r-- | doc/Doxyfile.in | 1 | ||||
-rw-r--r-- | doc/conf.py.in | 1 | ||||
-rw-r--r-- | include/serd/attributes.h | 3 | ||||
-rw-r--r-- | include/serd/reader.h | 16 | ||||
-rw-r--r-- | include/serd/serd.h | 1 | ||||
-rw-r--r-- | include/serd/world.h | 51 | ||||
-rw-r--r-- | include/serd/writer.h | 16 | ||||
-rw-r--r-- | meson.build | 2 | ||||
-rw-r--r-- | src/reader.c | 15 | ||||
-rw-r--r-- | src/reader.h | 2 | ||||
-rw-r--r-- | src/serd_internal.h | 15 | ||||
-rw-r--r-- | src/serdi.c | 18 | ||||
-rw-r--r-- | src/world.c | 29 | ||||
-rw-r--r-- | src/world.h | 14 | ||||
-rw-r--r-- | src/writer.c | 21 | ||||
-rw-r--r-- | test/test_free_null.c | 2 | ||||
-rw-r--r-- | test/test_overflow.c | 25 | ||||
-rw-r--r-- | test/test_reader_writer.c | 58 | ||||
-rw-r--r-- | test/test_writer.c | 35 |
21 files changed, 219 insertions, 112 deletions
diff --git a/.clang-format b/.clang-format index fabbc837..f6c9d6eb 100644 --- a/.clang-format +++ b/.clang-format @@ -28,13 +28,10 @@ AttributeMacros: - SERD_CONST_API - SERD_CONST_FUNC - SERD_FALLTHROUGH + - SERD_MALLOC_API - SERD_MALLOC_FUNC - - SERD_NODISCARD - - SERD_NONNULL - - SERD_NULLABLE - SERD_PURE_API - SERD_PURE_FUNC - - SERD_PURE_FUNC StatementMacros: - SERD_LOG_FUNC - _Pragma @@ -1,6 +1,7 @@ serd (1.1.1) unstable; urgency=medium * Add SerdBuffer for mutable buffers to keep SerdChunk const-correct + * Add SerdWorld for shared library state * Add support for xsd:float and xsd:double literals * Bring read/write interface closer to C standard * Make nodes opaque diff --git a/doc/Doxyfile.in b/doc/Doxyfile.in index f035564f..8952aa2c 100644 --- a/doc/Doxyfile.in +++ b/doc/Doxyfile.in @@ -28,6 +28,7 @@ PREDEFINED = SERD_ALLOCATED \ SERD_API \ SERD_CONST_API= \ SERD_CONST_FUNC= \ + SERD_MALLOC_API= \ SERD_NONNULL= \ SERD_NULLABLE= \ SERD_PURE_API= \ diff --git a/doc/conf.py.in b/doc/conf.py.in index 3ce284b1..5f0c026a 100644 --- a/doc/conf.py.in +++ b/doc/conf.py.in @@ -31,6 +31,7 @@ _opaque = [ "SerdNodeImpl", "SerdReaderImpl", "SerdSinkImpl", + "SerdWorldImpl", "SerdWriterImpl", "int64_t", "size_t", diff --git a/include/serd/attributes.h b/include/serd/attributes.h index 4e0d68e2..59063153 100644 --- a/include/serd/attributes.h +++ b/include/serd/attributes.h @@ -76,6 +76,9 @@ /// A const function in the public API that is pure and only reads parameters #define SERD_CONST_API SERD_API SERD_CONST_FUNC +/// A malloc function in the public API that returns allocated memory +#define SERD_MALLOC_API SERD_API SERD_MALLOC_FUNC + /** @} */ diff --git a/include/serd/reader.h b/include/serd/reader.h index e67717d7..26641801 100644 --- a/include/serd/reader.h +++ b/include/serd/reader.h @@ -5,12 +5,12 @@ #define SERD_READER_H #include "serd/attributes.h" -#include "serd/error.h" #include "serd/node.h" #include "serd/sink.h" #include "serd/status.h" #include "serd/stream.h" #include "serd/syntax.h" +#include "serd/world.h" #include <stdbool.h> #include <stddef.h> @@ -29,7 +29,8 @@ typedef struct SerdReaderImpl SerdReader; /// Create a new RDF reader SERD_API SerdReader* SERD_ALLOCATED -serd_reader_new(SerdSyntax syntax, +serd_reader_new(SerdWorld* SERD_NONNULL world, + SerdSyntax syntax, const SerdSink* SERD_NONNULL sink, size_t stack_size); @@ -44,17 +45,6 @@ SERD_API void serd_reader_set_strict(SerdReader* SERD_NONNULL reader, bool strict); /** - Set a function to be called when errors occur during reading. - - The `error_func` 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* SERD_NONNULL reader, - SerdErrorFunc SERD_NULLABLE error_func, - void* SERD_NULLABLE error_handle); - -/** Set a prefix to be added to all blank node identifiers. This is useful when multiple files are to be parsed into the same output (a diff --git a/include/serd/serd.h b/include/serd/serd.h index 03243c76..eb33d329 100644 --- a/include/serd/serd.h +++ b/include/serd/serd.h @@ -20,6 +20,7 @@ #include "serd/attributes.h" #include "serd/version.h" +#include "serd/world.h" /** @} diff --git a/include/serd/world.h b/include/serd/world.h new file mode 100644 index 00000000..22ae57ed --- /dev/null +++ b/include/serd/world.h @@ -0,0 +1,51 @@ +// Copyright 2011-2022 David Robillard <d@drobilla.net> +// SPDX-License-Identifier: ISC + +#ifndef SERD_WORLD_H +#define SERD_WORLD_H + +#include "serd/attributes.h" +#include "serd/error.h" + +SERD_BEGIN_DECLS + +/** + @defgroup serd_world World + @ingroup serd_library + @{ +*/ + +/// Global library state +typedef struct SerdWorldImpl SerdWorld; + +/** + Create a new Serd World. + + It is safe to use multiple worlds in one process, though no objects can be + shared between worlds. +*/ +SERD_MALLOC_API SerdWorld* SERD_ALLOCATED +serd_world_new(void); + +/// Free `world` +SERD_API void +serd_world_free(SerdWorld* SERD_NULLABLE world); + +/** + Set a function to be called when errors occur. + + The `error_func` 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_func(SerdWorld* SERD_NONNULL world, + SerdErrorFunc SERD_NULLABLE error_func, + void* SERD_NULLABLE handle); + +/** + @} +*/ + +SERD_END_DECLS + +#endif // SERD_WORLD_H diff --git a/include/serd/writer.h b/include/serd/writer.h index 5f1d0535..77ecac80 100644 --- a/include/serd/writer.h +++ b/include/serd/writer.h @@ -6,12 +6,12 @@ #include "serd/attributes.h" #include "serd/env.h" -#include "serd/error.h" #include "serd/node.h" #include "serd/sink.h" #include "serd/status.h" #include "serd/stream.h" #include "serd/syntax.h" +#include "serd/world.h" #include <stdint.h> @@ -47,7 +47,8 @@ typedef uint32_t SerdWriterFlags; /// Create a new RDF writer SERD_API SerdWriter* SERD_ALLOCATED -serd_writer_new(SerdSyntax syntax, +serd_writer_new(SerdWorld* SERD_NONNULL world, + SerdSyntax syntax, SerdWriterFlags flags, SerdEnv* SERD_NONNULL env, SerdWriteFunc SERD_NONNULL ssink, @@ -62,17 +63,6 @@ SERD_CONST_API const SerdSink* SERD_NONNULL serd_writer_sink(SerdWriter* SERD_NONNULL writer); /** - Set a function to be called when errors occur during writing. - - The `error_func` 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* SERD_NONNULL writer, - SerdErrorFunc SERD_NONNULL error_func, - void* SERD_NULLABLE error_handle); - -/** Set a prefix to be removed from matching blank node identifiers. This is the counterpart to serd_reader_add_blank_prefix() which can be used diff --git a/meson.build b/meson.build index e658426f..2dc91aa7 100644 --- a/meson.build +++ b/meson.build @@ -132,6 +132,7 @@ c_headers = files( 'include/serd/syntax.h', 'include/serd/uri.h', 'include/serd/version.h', + 'include/serd/world.h', 'include/serd/write_result.h', 'include/serd/writer.h', ) @@ -147,6 +148,7 @@ sources = files( 'src/syntax.c', 'src/system.c', 'src/uri.c', + 'src/world.c', 'src/writer.c', ) diff --git a/src/reader.c b/src/reader.c index 25c4b3b1..c2c20b82 100644 --- a/src/reader.c +++ b/src/reader.c @@ -28,7 +28,7 @@ r_err(SerdReader* const reader, const SerdStatus st, const char* const 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_func, reader->error_handle, &e); + serd_error(reader->world, &e); va_end(args); return st; } @@ -144,7 +144,8 @@ serd_reader_read_document(SerdReader* const reader) } SerdReader* -serd_reader_new(const SerdSyntax syntax, +serd_reader_new(SerdWorld* const world, + const SerdSyntax syntax, const SerdSink* const sink, const size_t stack_size) { @@ -154,6 +155,7 @@ serd_reader_new(const SerdSyntax syntax, SerdReader* me = (SerdReader*)calloc(1, sizeof(SerdReader)); + me->world = world; me->sink = sink; me->default_graph = NULL; me->stack = serd_stack_new(stack_size); @@ -183,15 +185,6 @@ serd_reader_set_strict(SerdReader* const reader, const bool strict) } void -serd_reader_set_error_sink(SerdReader* const reader, - const SerdErrorFunc error_func, - void* const error_handle) -{ - reader->error_func = error_func; - reader->error_handle = error_handle; -} - -void serd_reader_free(SerdReader* const reader) { if (!reader) { diff --git a/src/reader.h b/src/reader.h index c562ac6c..28889307 100644 --- a/src/reader.h +++ b/src/reader.h @@ -17,6 +17,7 @@ #include "serd/statement.h" #include "serd/status.h" #include "serd/syntax.h" +#include "serd/world.h" #include <assert.h> #include <stdbool.h> @@ -32,6 +33,7 @@ typedef struct { } ReadContext; struct SerdReaderImpl { + SerdWorld* world; const SerdSink* sink; SerdErrorFunc error_func; void* error_handle; diff --git a/src/serd_internal.h b/src/serd_internal.h index b3221ae7..0c9d4609 100644 --- a/src/serd_internal.h +++ b/src/serd_internal.h @@ -4,7 +4,10 @@ #ifndef SERD_SRC_SERD_INTERNAL_H #define SERD_SRC_SERD_INTERNAL_H +#include "world.h" + #include "serd/error.h" +#include "serd/world.h" #include <stdio.h> @@ -20,12 +23,16 @@ /* Error reporting */ static inline void -serd_error(SerdErrorFunc error_func, void* handle, const SerdError* e) +serd_error(const SerdWorld* world, const SerdError* e) { - if (error_func) { - error_func(handle, e); + if (world->error_func) { + world->error_func(world->error_handle, e); } else { - fprintf(stderr, "error: %s:%u:%u: ", e->filename, e->line, e->col); + if (e->filename) { + fprintf(stderr, "error: %s:%u:%u: ", e->filename, e->line, e->col); + } else { + fprintf(stderr, "error: "); + } vfprintf(stderr, e->fmt, *e->args); } } diff --git a/src/serdi.c b/src/serdi.c index bcd9cb8d..4649f57a 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -12,6 +12,7 @@ #include "serd/string_view.h" #include "serd/syntax.h" #include "serd/version.h" +#include "serd/world.h" #include "serd/writer.h" #ifdef _WIN32 @@ -84,7 +85,7 @@ missing_arg(const char* const name, const char opt) } static SerdStatus -quiet_error_sink(void* const handle, const SerdError* const e) +quiet_error_func(void* const handle, const SerdError* const e) { (void)handle; (void)e; @@ -272,20 +273,20 @@ main(int argc, char** argv) base = serd_new_file_uri(serd_string(input), serd_empty_string()); } - FILE* const out_fd = stdout; - SerdEnv* const env = + FILE* const out_fd = stdout; + SerdWorld* const world = serd_world_new(); + SerdEnv* const env = serd_env_new(base ? serd_node_string_view(base) : serd_empty_string()); - SerdWriter* writer = serd_writer_new( - output_syntax, writer_flags, env, (SerdWriteFunc)fwrite, out_fd); + SerdWriter* const writer = serd_writer_new( + world, output_syntax, writer_flags, env, (SerdWriteFunc)fwrite, out_fd); SerdReader* const reader = - serd_reader_new(input_syntax, serd_writer_sink(writer), stack_size); + serd_reader_new(world, input_syntax, serd_writer_sink(writer), stack_size); 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_func(world, quiet_error_func, NULL); } if (root_uri) { @@ -321,6 +322,7 @@ main(int argc, char** argv) serd_writer_free(writer); serd_env_free(env); serd_node_free(base); + serd_world_free(world); if (fclose(stdout)) { perror("serdi: write error"); diff --git a/src/world.c b/src/world.c new file mode 100644 index 00000000..eef70a14 --- /dev/null +++ b/src/world.c @@ -0,0 +1,29 @@ +// Copyright 2011-2022 David Robillard <d@drobilla.net> +// SPDX-License-Identifier: ISC + +#include "world.h" + +#include "serd/world.h" + +#include <stdlib.h> + +SerdWorld* +serd_world_new(void) +{ + return (SerdWorld*)calloc(1, sizeof(SerdWorld)); +} + +void +serd_world_free(SerdWorld* const world) +{ + free(world); +} + +void +serd_world_set_error_func(SerdWorld* world, + SerdErrorFunc error_func, + void* handle) +{ + world->error_func = error_func; + world->error_handle = handle; +} diff --git a/src/world.h b/src/world.h new file mode 100644 index 00000000..39fdf5e0 --- /dev/null +++ b/src/world.h @@ -0,0 +1,14 @@ +// Copyright 2011-2020 David Robillard <d@drobilla.net> +// SPDX-License-Identifier: ISC + +#ifndef SERD_SRC_WORLD_H +#define SERD_SRC_WORLD_H + +#include "serd/error.h" + +struct SerdWorldImpl { + SerdErrorFunc error_func; + void* error_handle; +}; + +#endif // SERD_SRC_WORLD_H diff --git a/src/writer.c b/src/writer.c index 5c05e244..f39363aa 100644 --- a/src/writer.c +++ b/src/writer.c @@ -23,6 +23,7 @@ #include "serd/string_view.h" #include "serd/syntax.h" #include "serd/uri.h" +#include "serd/world.h" #include "serd/writer.h" #include <assert.h> @@ -125,6 +126,7 @@ static const SepRule rules[] = { #undef SEP_EACH struct SerdWriterImpl { + SerdWorld* world; SerdSink iface; SerdSyntax syntax; SerdWriterFlags flags; @@ -133,8 +135,6 @@ struct SerdWriterImpl { SerdURIView root_uri; SerdStack anon_stack; SerdByteSink byte_sink; - SerdErrorFunc error_func; - void* error_handle; WriteContext context; char* bprefix; size_t bprefix_len; @@ -192,8 +192,8 @@ w_err(SerdWriter* writer, SerdStatus st, const char* fmt, ...) va_list args; // NOLINT(cppcoreguidelines-init-variables) va_start(args, fmt); - const SerdError e = {st, "", 0, 0, fmt, &args}; - serd_error(writer->error_func, writer->error_handle, &e); + const SerdError e = {st, NULL, 0, 0, fmt, &args}; + serd_error(writer->world, &e); va_end(args); return st; } @@ -1142,7 +1142,8 @@ serd_writer_finish(SerdWriter* writer) } SerdWriter* -serd_writer_new(SerdSyntax syntax, +serd_writer_new(SerdWorld* world, + SerdSyntax syntax, SerdWriterFlags flags, SerdEnv* env, SerdWriteFunc ssink, @@ -1151,6 +1152,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->flags = flags; writer->env = env; @@ -1171,15 +1173,6 @@ serd_writer_new(SerdSyntax syntax, } void -serd_writer_set_error_sink(SerdWriter* writer, - SerdErrorFunc error_func, - void* error_handle) -{ - writer->error_func = error_func; - writer->error_handle = error_handle; -} - -void serd_writer_chop_blank_prefix(SerdWriter* writer, const char* prefix) { free(writer->bprefix); diff --git a/test/test_free_null.c b/test/test_free_null.c index d3248cb5..72783328 100644 --- a/test/test_free_null.c +++ b/test/test_free_null.c @@ -8,6 +8,7 @@ #include "serd/node.h" #include "serd/reader.h" #include "serd/sink.h" +#include "serd/world.h" #include "serd/writer.h" #include <stddef.h> @@ -17,6 +18,7 @@ main(void) { serd_free(NULL); serd_node_free(NULL); + serd_world_free(NULL); serd_env_free(NULL); serd_sink_free(NULL); serd_reader_free(NULL); diff --git a/test/test_overflow.c b/test/test_overflow.c index ac4a490b..a304ae1c 100644 --- a/test/test_overflow.c +++ b/test/test_overflow.c @@ -12,12 +12,13 @@ static const size_t min_stack_size = 4U * sizeof(size_t) + 240U; static const size_t max_stack_size = 1024U; static SerdStatus -test_size(const char* const str, +test_size(SerdWorld* const world, + const char* const str, const SerdSyntax syntax, const size_t stack_size) { SerdSink* sink = serd_sink_new(NULL, NULL); - SerdReader* const reader = serd_reader_new(syntax, sink, stack_size); + SerdReader* const reader = serd_reader_new(world, syntax, sink, stack_size); if (!reader) { return SERD_BAD_STACK; } @@ -31,15 +32,17 @@ test_size(const char* const str, } static void -test_all_sizes(const char* const str, const SerdSyntax syntax) +test_all_sizes(SerdWorld* const world, + const char* const str, + const SerdSyntax syntax) { // Ensure reading with the maximum stack size succeeds - SerdStatus st = test_size(str, syntax, max_stack_size); + SerdStatus st = test_size(world, str, syntax, max_stack_size); assert(!st); // Test with an increasingly smaller stack for (size_t size = max_stack_size; size > min_stack_size; --size) { - if ((st = test_size(str, syntax, size))) { + if ((st = test_size(world, str, syntax, size))) { assert(st == SERD_BAD_STACK); } } @@ -55,9 +58,13 @@ test_ntriples_overflow(void) NULL, }; + SerdWorld* const world = serd_world_new(); + for (const char* const* t = test_strings; *t; ++t) { - test_all_sizes(*t, SERD_NTRIPLES); + test_all_sizes(world, *t, SERD_NTRIPLES); } + + serd_world_free(world); } static void @@ -138,9 +145,13 @@ test_turtle_overflow(void) NULL, }; + SerdWorld* const world = serd_world_new(); + for (const char* const* t = test_strings; *t; ++t) { - test_all_sizes(*t, SERD_TURTLE); + test_all_sizes(world, *t, SERD_TURTLE); } + + serd_world_free(world); } int diff --git a/test/test_reader_writer.c b/test/test_reader_writer.c index c1472e0a..c634bc79 100644 --- a/test/test_reader_writer.c +++ b/test/test_reader_writer.c @@ -5,7 +5,6 @@ #include "serd/buffer.h" #include "serd/env.h" -#include "serd/error.h" #include "serd/memory.h" #include "serd/node.h" #include "serd/reader.h" @@ -15,6 +14,7 @@ #include "serd/stream.h" #include "serd/string_view.h" #include "serd/syntax.h" +#include "serd/world.h" #include "serd/writer.h" #ifdef _WIN32 @@ -184,9 +184,10 @@ test_read_nquads_chunks(const char* const path) fseek(f, 0, SEEK_SET); + SerdWorld* const world = serd_world_new(); ReaderTest* const rt = (ReaderTest*)calloc(1, sizeof(ReaderTest)); SerdSink* const sink = serd_sink_new(rt, NULL); - SerdReader* const reader = serd_reader_new(SERD_NQUADS, sink, 4096); + SerdReader* const reader = serd_reader_new(world, SERD_NQUADS, sink, 4096); assert(reader); assert(sink); @@ -245,6 +246,7 @@ test_read_nquads_chunks(const char* const path) serd_reader_free(reader); serd_sink_free(sink); free(rt); + serd_world_free(world); fclose(f); remove(path); } @@ -266,9 +268,10 @@ test_read_turtle_chunks(const char* const path) fwrite(&null, sizeof(null), 1, f); fseek(f, 0, SEEK_SET); - ReaderTest* const rt = (ReaderTest*)calloc(1, sizeof(ReaderTest)); - SerdSink* const sink = serd_sink_new(rt, NULL); - SerdReader* const reader = serd_reader_new(SERD_TURTLE, sink, 4096); + SerdWorld* world = serd_world_new(); + ReaderTest* rt = (ReaderTest*)calloc(1, sizeof(ReaderTest)); + SerdSink* sink = serd_sink_new(rt, NULL); + SerdReader* reader = serd_reader_new(world, SERD_TURTLE, sink, 4096); assert(reader); assert(sink); @@ -343,6 +346,7 @@ test_read_turtle_chunks(const char* const path) serd_reader_free(reader); serd_sink_free(sink); free(rt); + serd_world_free(world); fclose(f); remove(path); } @@ -350,9 +354,10 @@ test_read_turtle_chunks(const char* const path) static void test_read_string(void) { + SerdWorld* world = serd_world_new(); ReaderTest* rt = (ReaderTest*)calloc(1, sizeof(ReaderTest)); SerdSink* sink = serd_sink_new(rt, NULL); - SerdReader* reader = serd_reader_new(SERD_TURTLE, sink, 4096); + SerdReader* reader = serd_reader_new(world, SERD_TURTLE, sink, 4096); assert(reader); assert(sink); @@ -378,6 +383,7 @@ test_read_string(void) serd_reader_free(reader); serd_sink_free(sink); free(rt); + serd_world_free(world); } static size_t @@ -404,18 +410,11 @@ faulty_sink(const void* const buf, return nmemb; } -static SerdStatus -quiet_error_sink(void* const handle, const SerdError* const e) -{ - (void)handle; - (void)e; - return SERD_SUCCESS; -} - static void test_write_errors(void) { - ErrorContext ctx = {0U, 0U}; + SerdWorld* const world = serd_world_new(); + ErrorContext ctx = {0U, 0U}; const SerdWriterFlags style = (SerdWriterFlags)(SERD_WRITE_STRICT | SERD_WRITE_CURIED); @@ -430,13 +429,10 @@ test_write_errors(void) SerdEnv* const env = serd_env_new(serd_empty_string()); SerdWriter* const writer = - serd_writer_new(syntax, style, env, faulty_sink, &ctx); + serd_writer_new(world, syntax, style, env, faulty_sink, &ctx); - const SerdSink* const sink = serd_writer_sink(writer); - SerdReader* const reader = serd_reader_new(SERD_TRIG, sink, 4096U); - - serd_reader_set_error_sink(reader, quiet_error_sink, NULL); - serd_writer_set_error_sink(writer, quiet_error_sink, NULL); + const SerdSink* const sink = serd_writer_sink(writer); + SerdReader* const reader = serd_reader_new(world, SERD_TRIG, sink, 4096U); SerdStatus st = serd_reader_start_string(reader, doc_string); assert(!st); @@ -448,6 +444,8 @@ test_write_errors(void) serd_env_free(env); } } + + serd_world_free(world); } static void @@ -457,8 +455,10 @@ test_writer(const char* const path) SerdEnv* env = serd_env_new(serd_empty_string()); assert(fd); + SerdWorld* world = serd_world_new(); + SerdWriter* writer = - serd_writer_new(SERD_TURTLE, 0, env, (SerdWriteFunc)fwrite, fd); + serd_writer_new(world, SERD_TURTLE, 0, env, (SerdWriteFunc)fwrite, fd); assert(writer); serd_writer_chop_blank_prefix(writer, "tmp"); @@ -527,7 +527,8 @@ test_writer(const char* const path) // Test buffer sink SerdBuffer buffer = {NULL, 0}; - writer = serd_writer_new(SERD_TURTLE, 0, env, serd_buffer_sink, &buffer); + writer = + serd_writer_new(world, SERD_TURTLE, 0, env, serd_buffer_sink, &buffer); SerdNode* const base = serd_new_uri(serd_string("http://example.org/base")); @@ -544,14 +545,16 @@ test_writer(const char* const path) serd_node_free(s); serd_env_free(env); + serd_world_free(world); fclose(fd); } static void test_reader(const char* path) { - ReaderTest rt = {0, 0, 0, 0, NULL}; - SerdSink* const sink = serd_sink_new(&rt, NULL); + SerdWorld* world = serd_world_new(); + ReaderTest rt = {0, 0, 0, 0, NULL}; + SerdSink* const sink = serd_sink_new(&rt, NULL); assert(sink); serd_sink_set_base_func(sink, test_base_sink); @@ -560,9 +563,9 @@ test_reader(const char* path) serd_sink_set_end_func(sink, test_end_sink); // Test that too little stack space fails gracefully - assert(!serd_reader_new(SERD_TURTLE, sink, 32)); + assert(!serd_reader_new(world, SERD_TURTLE, sink, 32)); - SerdReader* reader = serd_reader_new(SERD_TURTLE, sink, 4096); + SerdReader* reader = serd_reader_new(world, SERD_TURTLE, sink, 4096); assert(reader); assert(serd_reader_read_chunk(reader) == SERD_FAILURE); @@ -630,6 +633,7 @@ test_reader(const char* path) serd_reader_free(reader); serd_sink_free(sink); + serd_world_free(world); } int diff --git a/test/test_writer.c b/test/test_writer.c index 5a4e5ab0..db68454f 100644 --- a/test/test_writer.c +++ b/test/test_writer.c @@ -12,6 +12,7 @@ #include "serd/status.h" #include "serd/string_view.h" #include "serd/syntax.h" +#include "serd/world.h" #include "serd/writer.h" #include <assert.h> @@ -22,10 +23,11 @@ static void test_write_bad_prefix(void) { + SerdWorld* world = serd_world_new(); SerdEnv* env = serd_env_new(serd_empty_string()); SerdBuffer buffer = {NULL, 0}; SerdWriter* writer = - serd_writer_new(SERD_TURTLE, 0U, env, serd_buffer_sink, &buffer); + serd_writer_new(world, SERD_TURTLE, 0U, env, serd_buffer_sink, &buffer); assert(writer); @@ -44,15 +46,17 @@ test_write_bad_prefix(void) serd_node_free(name); serd_writer_free(writer); serd_env_free(env); + serd_world_free(world); } static void test_write_long_literal(void) { + SerdWorld* world = serd_world_new(); SerdEnv* env = serd_env_new(serd_empty_string()); SerdBuffer buffer = {NULL, 0}; SerdWriter* writer = - serd_writer_new(SERD_TURTLE, 0U, env, serd_buffer_sink, &buffer); + serd_writer_new(world, SERD_TURTLE, 0U, env, serd_buffer_sink, &buffer); assert(writer); @@ -76,6 +80,8 @@ test_write_long_literal(void) assert(!strcmp((char*)out, expected)); serd_free(out); + + serd_world_free(world); } static size_t @@ -93,9 +99,11 @@ null_sink(const void* const buf, static void test_writer_cleanup(void) { - SerdStatus st = SERD_SUCCESS; - SerdEnv* env = serd_env_new(serd_empty_string()); - SerdWriter* writer = serd_writer_new(SERD_TURTLE, 0U, env, null_sink, NULL); + SerdStatus st = SERD_SUCCESS; + SerdWorld* world = serd_world_new(); + SerdEnv* env = serd_env_new(serd_empty_string()); + SerdWriter* writer = + serd_writer_new(world, SERD_TURTLE, 0U, env, null_sink, NULL); const SerdSink* sink = serd_writer_sink(writer); @@ -134,6 +142,7 @@ test_writer_cleanup(void) serd_node_free(s); serd_writer_free(writer); serd_env_free(env); + serd_world_free(world); } static void @@ -143,9 +152,10 @@ test_strict_write(void) FILE* const fd = fopen(path, "wb"); assert(fd); + SerdWorld* world = serd_world_new(); SerdEnv* const env = serd_env_new(serd_empty_string()); SerdWriter* const writer = serd_writer_new( - SERD_TURTLE, (SerdWriterFlags)SERD_WRITE_STRICT, env, null_sink, fd); + world, SERD_TURTLE, (SerdWriterFlags)SERD_WRITE_STRICT, env, null_sink, fd); assert(writer); @@ -168,6 +178,7 @@ test_strict_write(void) serd_node_free(s); serd_writer_free(writer); serd_env_free(env); + serd_world_free(world); fclose(fd); remove(path); } @@ -189,14 +200,15 @@ error_sink(const void* const buf, static void test_write_error(void) { - SerdEnv* const env = serd_env_new(serd_empty_string()); - SerdWriter* writer = NULL; - SerdStatus st = SERD_SUCCESS; + SerdWorld* const world = serd_world_new(); + SerdEnv* const env = serd_env_new(serd_empty_string()); + SerdWriter* writer = NULL; + SerdStatus st = SERD_SUCCESS; SerdNode* u = serd_new_uri(serd_string("http://example.com/u")); - writer = - serd_writer_new(SERD_TURTLE, (SerdWriterFlags)0, env, error_sink, NULL); + writer = serd_writer_new( + world, SERD_TURTLE, (SerdWriterFlags)0, env, error_sink, NULL); assert(writer); const SerdSink* const sink = serd_writer_sink(writer); @@ -207,6 +219,7 @@ test_write_error(void) serd_node_free(u); serd_env_free(env); + serd_world_free(world); } int |