diff options
Diffstat (limited to 'src')
-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 |
7 files changed, 77 insertions, 37 deletions
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); |