diff options
author | David Robillard <d@drobilla.net> | 2021-07-11 20:47:51 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2022-01-13 23:03:46 -0500 |
commit | 71c950bb749c3581ab389edfff9771cb06242e29 (patch) | |
tree | a1437913131978133e48b21078d900859ce30c43 /src | |
parent | 30f3e6fc2c1e24c429d5d0b7100dc449ade6703f (diff) | |
download | serd-71c950bb749c3581ab389edfff9771cb06242e29.tar.gz serd-71c950bb749c3581ab389edfff9771cb06242e29.tar.bz2 serd-71c950bb749c3581ab389edfff9771cb06242e29.zip |
Add SerdWorld for shared library state
Diffstat (limited to 'src')
-rw-r--r-- | src/attributes.h | 4 | ||||
-rw-r--r-- | src/reader.c | 15 | ||||
-rw-r--r-- | src/reader.h | 1 | ||||
-rw-r--r-- | src/serd_internal.h | 14 | ||||
-rw-r--r-- | src/serdi.c | 17 | ||||
-rw-r--r-- | src/system.h | 8 | ||||
-rw-r--r-- | src/world.c | 40 | ||||
-rw-r--r-- | src/world.h | 27 | ||||
-rw-r--r-- | src/writer.c | 20 |
9 files changed, 104 insertions, 42 deletions
diff --git a/src/attributes.h b/src/attributes.h index 8628a868..25e5b505 100644 --- a/src/attributes.h +++ b/src/attributes.h @@ -18,9 +18,9 @@ #define SERD_ATTRIBUTES_H #ifdef __GNUC__ -# define SERD_MALLOC_FUNC __attribute__((malloc)) +# define SERD_I_MALLOC_FUNC __attribute__((malloc)) #else -# define SERD_MALLOC_FUNC +# define SERD_I_MALLOC_FUNC #endif #endif // SERD_ATTRIBUTES_H diff --git a/src/reader.c b/src/reader.c index 354ff478..c35f5fbc 100644 --- a/src/reader.c +++ b/src/reader.c @@ -38,7 +38,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; } @@ -160,7 +160,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) { @@ -170,6 +171,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); @@ -199,15 +201,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 632d5257..39ab4628 100644 --- a/src/reader.h +++ b/src/reader.h @@ -43,6 +43,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 e16a2c12..1bb2a3bc 100644 --- a/src/serd_internal.h +++ b/src/serd_internal.h @@ -17,6 +17,8 @@ #ifndef SERD_INTERNAL_H #define SERD_INTERNAL_H +#include "world.h" + #include "serd/serd.h" #include <stdio.h> @@ -33,12 +35,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 e46ef5e5..fcddac6c 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -80,7 +80,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; @@ -258,20 +258,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) { @@ -307,6 +307,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/system.h b/src/system.h index 7dae6a96..944c8942 100644 --- a/src/system.h +++ b/src/system.h @@ -27,16 +27,18 @@ FILE* serd_fopen(const char* path, const char* mode); /// Allocate a buffer aligned to `alignment` bytes -SERD_MALLOC_FUNC void* +SERD_I_MALLOC_FUNC +void* serd_malloc_aligned(size_t alignment, size_t size); /// Allocate a zeroed buffer aligned to `alignment` bytes -SERD_MALLOC_FUNC +SERD_I_MALLOC_FUNC void* serd_calloc_aligned(size_t alignment, size_t size); /// Allocate an aligned buffer for I/O -SERD_MALLOC_FUNC void* +SERD_I_MALLOC_FUNC +void* serd_allocate_buffer(size_t size); /// Free a buffer allocated with an aligned allocation function diff --git a/src/world.c b/src/world.c new file mode 100644 index 00000000..5663c182 --- /dev/null +++ b/src/world.c @@ -0,0 +1,40 @@ +/* + 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. +*/ + +#include "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..3d994df9 --- /dev/null +++ b/src/world.h @@ -0,0 +1,27 @@ +/* + 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. +*/ + +#ifndef SERD_WORLD_H +#define SERD_WORLD_H + +#include "serd/serd.h" + +struct SerdWorldImpl { + SerdErrorFunc error_func; + void* error_handle; +}; + +#endif // SERD_WORLD_H diff --git a/src/writer.c b/src/writer.c index b132dc83..70d22485 100644 --- a/src/writer.c +++ b/src/writer.c @@ -93,6 +93,7 @@ static const SepRule rules[] = {{NULL, 0, 0, 0, 0}, {"\n", 1, 0, 1, 0}}; struct SerdWriterImpl { + SerdWorld* world; SerdSink iface; SerdSyntax syntax; SerdWriterFlags flags; @@ -101,8 +102,6 @@ struct SerdWriterImpl { SerdURIView root_uri; SerdStack anon_stack; SerdByteSink byte_sink; - SerdErrorFunc error_func; - void* error_handle; WriteContext context; SerdNode* list_subj; unsigned list_depth; @@ -149,8 +148,8 @@ w_err(SerdWriter* writer, SerdStatus st, const char* fmt, ...) va_list args; 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); } @@ -948,7 +947,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, @@ -957,6 +957,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; @@ -979,15 +980,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); |