From cae15d15c847faba203e40e2a327b23a1ebffb48 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 28 Jun 2020 19:46:47 +0200 Subject: Make Writer always write to a ByteSink --- include/serd/serd.h | 118 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 72 insertions(+), 46 deletions(-) (limited to 'include') diff --git a/include/serd/serd.h b/include/serd/serd.h index 776bf910..95555013 100644 --- a/include/serd/serd.h +++ b/include/serd/serd.h @@ -303,50 +303,6 @@ typedef size_t (*SerdWriteFunc)(const void* SERD_NONNULL buf, size_t nmemb, void* SERD_NONNULL stream); -/** - @} - @defgroup serd_streams Byte Streams - @{ -*/ - -/// A sink for bytes that receives text output -typedef struct SerdByteSinkImpl SerdByteSink; - -/** - Create a new byte sink. - - @param write_func Function called with bytes to consume. - @param stream Context parameter passed to `sink`. - @param block_size Number of bytes to write per call. -*/ -SERD_API -SerdByteSink* SERD_ALLOCATED -serd_byte_sink_new(SerdWriteFunc SERD_NONNULL write_func, - void* SERD_NULLABLE stream, - size_t block_size); - -/** - Write to `sink`. - - Compatible with SerdWriteFunc. -*/ -SERD_API -size_t -serd_byte_sink_write(const void* SERD_NONNULL buf, - size_t size, - size_t nmemb, - SerdByteSink* SERD_NONNULL sink); - -/// Flush any pending output in `sink` to the underlying write function -SERD_API -void -serd_byte_sink_flush(SerdByteSink* SERD_NONNULL sink); - -/// Free `sink` -SERD_API -void -serd_byte_sink_free(SerdByteSink* SERD_NULLABLE sink); - /** @} @defgroup serd_syntax Syntax Utilities @@ -1589,6 +1545,77 @@ SERD_API void serd_reader_free(SerdReader* SERD_NULLABLE reader); +/** + @} + @defgroup serd_byte_sink Byte Sink + @{ +*/ + +/// A sink for bytes that receives text output +typedef struct SerdByteSinkImpl SerdByteSink; + +/** + Create a new byte sink that writes to a buffer. + + The `buffer` is owned by the caller, but will be expanded as necessary. + + @param buffer Buffer to write output to. +*/ +SERD_API +SerdByteSink* SERD_ALLOCATED +serd_byte_sink_new_buffer(SerdBuffer* SERD_NONNULL buffer); + +/** + Create a new byte sink that writes to a file. + + An arbitrary `FILE*` can be used via serd_byte_sink_new_function() as well, + this is just a convenience function that opens the file properly and sets + flags for optimized I/O if possible. + + @param path Path of file to open and write to. + @param block_size Number of bytes to write per call. +*/ +SERD_API +SerdByteSink* SERD_ALLOCATED +serd_byte_sink_new_filename(const char* SERD_NONNULL path, size_t block_size); + +/** + Create a new byte sink that writes to a user-specified function. + + The `stream` will be passed to the `write_func`, which is compatible with + the standard C `fwrite` if `stream` is a `FILE*`. + + @param write_func Function called with bytes to consume. + @param stream Context parameter passed to `sink`. + @param block_size Number of bytes to write per call. +*/ +SERD_API +SerdByteSink* SERD_ALLOCATED +serd_byte_sink_new_function(SerdWriteFunc SERD_NONNULL write_func, + void* SERD_NULLABLE stream, + size_t block_size); + +/// Flush any pending output in `sink` to the underlying write function +SERD_API +void +serd_byte_sink_flush(SerdByteSink* SERD_NONNULL sink); + +/** + Close `sink`, including the underlying file if necessary. + + If `sink` was created with serd_byte_sink_new_filename(), then the file is + closed. If there was an error, then SERD_ERR_UNKNOWN is returned and + `errno` is set. +*/ +SERD_API +SerdStatus +serd_byte_sink_close(SerdByteSink* SERD_NONNULL sink); + +/// Free `sink`, flushing and closing first if necessary +SERD_API +void +serd_byte_sink_free(SerdByteSink* SERD_NULLABLE sink); + /** @} @defgroup serd_writer Writer @@ -1623,8 +1650,7 @@ serd_writer_new(SerdWorld* SERD_NONNULL world, SerdSyntax syntax, SerdWriterFlags flags, SerdEnv* SERD_NONNULL env, - SerdWriteFunc SERD_NONNULL write_func, - void* SERD_NULLABLE stream); + SerdByteSink* SERD_NONNULL byte_sink); /// Free `writer` SERD_API -- cgit v1.2.1