diff options
author | David Robillard <d@drobilla.net> | 2018-07-13 21:04:54 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2020-06-21 18:12:04 +0200 |
commit | 4f2640a2e4689cdda26a914c77aa2822a06da0f5 (patch) | |
tree | c124fe5fae4908f9e8e48697080db5b6debcfe7c | |
parent | a018e7a0c9e3146a6266e8de7a9af5069ef69bbd (diff) | |
download | serd-4f2640a2e4689cdda26a914c77aa2822a06da0f5.tar.gz serd-4f2640a2e4689cdda26a914c77aa2822a06da0f5.tar.bz2 serd-4f2640a2e4689cdda26a914c77aa2822a06da0f5.zip |
Separate ByteSink from Writer
-rw-r--r-- | serd/serd.h | 3 | ||||
-rw-r--r-- | src/serdi.c | 12 | ||||
-rw-r--r-- | src/writer.c | 13 | ||||
-rw-r--r-- | tests/serd_test.c | 14 |
4 files changed, 25 insertions, 17 deletions
diff --git a/serd/serd.h b/serd/serd.h index 21b0106c..119445fa 100644 --- a/serd/serd.h +++ b/serd/serd.h @@ -301,7 +301,6 @@ typedef struct { */ typedef enum { SERD_STYLE_ASCII = 1 << 0, /**< Escape all non-ASCII characters. */ - SERD_STYLE_BULK = 1 << 1 /**< Write output in pages. */ } SerdStyle; /** @@ -1233,7 +1232,7 @@ serd_writer_new(SerdWorld* world, SerdSyntax syntax, SerdStyleFlags style, SerdEnv* env, - SerdWriteFunc ssink, + SerdWriteFunc write_func, void* stream); /** diff --git a/src/serdi.c b/src/serdi.c index 86a315df..c2566b76 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -202,15 +202,17 @@ main(int argc, char** argv) SerdWorld* world = serd_world_new(); SerdEnv* env = serd_env_new(base); - const SerdStyleFlags output_style = ((ascii ? SERD_STYLE_ASCII : 0) | - (bulk_write ? SERD_STYLE_BULK : 0)); + const SerdStyleFlags output_style = (ascii ? SERD_STYLE_ASCII : 0); + + SerdByteSink* byte_sink = serd_byte_sink_new( + (SerdWriteFunc)fwrite, out_fd, bulk_write ? 4096 : 1); SerdWriter* writer = serd_writer_new(world, output_syntax, output_style, env, - (SerdWriteFunc)fwrite, - out_fd); + (SerdWriteFunc)serd_byte_sink_write, + byte_sink); SerdReader* reader = serd_reader_new( world, input_syntax, serd_writer_get_sink(writer), stack_size); @@ -249,8 +251,8 @@ main(int argc, char** argv) serd_reader_finish(reader); serd_reader_free(reader); - serd_writer_finish(writer); serd_writer_free(writer); + serd_byte_sink_free(byte_sink); serd_env_free(env); serd_node_free(base); serd_world_free(world); diff --git a/src/writer.c b/src/writer.c index 9a03d24c..776660f6 100644 --- a/src/writer.c +++ b/src/writer.c @@ -95,7 +95,8 @@ struct SerdWriterImpl { SerdNode* root_node; SerdURI root_uri; SerdStack anon_stack; - SerdByteSink* byte_sink; + SerdWriteFunc write_func; + void* stream; SerdErrorSink error_sink; void* error_handle; WriteContext context; @@ -156,7 +157,7 @@ ctx(SerdWriter* writer, const SerdField field) static inline size_t sink(const void* buf, size_t len, SerdWriter* writer) { - return serd_byte_sink_write(buf, 1, len, writer->byte_sink); + return writer->write_func(buf, 1, len, writer->stream); } // Write a single character, as an escape for single byte characters @@ -856,7 +857,6 @@ serd_writer_finish(SerdWriter* writer) if (ctx(writer, SERD_GRAPH)) { write_sep(writer, SEP_GRAPH_END); } - serd_byte_sink_flush(writer->byte_sink); free_context(writer); writer->indent = 0; writer->context = WRITE_CONTEXT_NULL; @@ -868,7 +868,7 @@ serd_writer_new(SerdWorld* world, SerdSyntax syntax, SerdStyleFlags style, SerdEnv* env, - SerdWriteFunc ssink, + SerdWriteFunc write_func, void* stream) { const WriteContext context = WRITE_CONTEXT_NULL; @@ -880,11 +880,11 @@ serd_writer_new(SerdWorld* world, writer->root_node = NULL; writer->root_uri = SERD_URI_NULL; writer->anon_stack = serd_stack_new(SERD_PAGE_SIZE); + writer->write_func = write_func; + writer->stream = stream; writer->context = context; writer->list_subj = NULL; writer->empty = true; - writer->byte_sink = serd_byte_sink_new( - ssink, stream, (style & SERD_STYLE_BULK) ? SERD_PAGE_SIZE : 1); writer->iface.handle = writer; writer->iface.base = (SerdBaseSink)serd_writer_set_base_uri; @@ -974,7 +974,6 @@ serd_writer_free(SerdWriter* writer) serd_writer_finish(writer); serd_stack_free(&writer->anon_stack); free(writer->bprefix); - serd_byte_sink_free(writer->byte_sink); serd_node_free(writer->root_node); free(writer); } diff --git a/tests/serd_test.c b/tests/serd_test.c index d5b8d33b..a50ee846 100644 --- a/tests/serd_test.c +++ b/tests/serd_test.c @@ -703,15 +703,23 @@ test_writer(const char* const path) serd_node_free(urn_Type); // Test buffer sink - SerdBuffer buffer = { NULL, 0 }; - writer = serd_writer_new( - world, SERD_TURTLE, 0, env, serd_buffer_sink, &buffer); + SerdBuffer buffer = { NULL, 0 }; + SerdByteSink* byte_sink = + serd_byte_sink_new((SerdWriteFunc)serd_buffer_sink, &buffer, 1); + + writer = serd_writer_new(world, + SERD_TURTLE, + 0, + env, + (SerdWriteFunc)serd_byte_sink_write, + byte_sink); o = serd_new_uri("http://example.org/base"); assert(!serd_writer_set_base_uri(writer, o)); serd_node_free(o); serd_writer_free(writer); + serd_byte_sink_free(byte_sink); char* out = serd_buffer_sink_finish(&buffer); assert(!strcmp(out, "@base <http://example.org/base> .\n")); |