diff options
author | David Robillard <d@drobilla.net> | 2020-06-28 19:46:47 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2020-10-27 13:13:59 +0100 |
commit | ba2dc6b53c8dd840651fc9e2c10790989b9cee9f (patch) | |
tree | a824e5de81d054097cd8b2021d3d3f7340f096d4 /serd | |
parent | 834ca36d4cbbfd63789f7894ab2d5d370347d76f (diff) | |
download | serd-ba2dc6b53c8dd840651fc9e2c10790989b9cee9f.tar.gz serd-ba2dc6b53c8dd840651fc9e2c10790989b9cee9f.tar.bz2 serd-ba2dc6b53c8dd840651fc9e2c10790989b9cee9f.zip |
WIP: Make Writer always write to a ByteSink
Diffstat (limited to 'serd')
-rw-r--r-- | serd/serd.h | 56 |
1 files changed, 42 insertions, 14 deletions
diff --git a/serd/serd.h b/serd/serd.h index 78a1d0cf..fe2a08b2 100644 --- a/serd/serd.h +++ b/serd/serd.h @@ -443,34 +443,63 @@ typedef size_t (*SerdWriteFunc)(const void* buf, void* stream); /** - Create a new byte sink + Create a new byte sink that writes to a buffer - @param write_func Function called with bytes to consume. - @param stream Context parameter passed to `sink`. + The `buffer` is owned by the caller, but will be expanded as necessary. + + @param buffer Buffer to write output to. +*/ +SERD_API +SerdByteSink* +serd_byte_sink_new_buffer(SerdBuffer* 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_byte_sink_new(SerdWriteFunc write_func, void* stream, size_t block_size); +serd_byte_sink_new_filename(const char* path, size_t block_size); /** - Write to `sink` + 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*`. - Compatible with SerdWriteFunc. + @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 -size_t -serd_byte_sink_write(const void* buf, - size_t size, - size_t nmemb, - SerdByteSink* sink); +SerdByteSink* +serd_byte_sink_new_function(SerdWriteFunc write_func, + void* stream, + size_t block_size); /// Flush any pending output in `sink` to the underlying write function SERD_API void serd_byte_sink_flush(SerdByteSink* sink); -/// Free `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* sink); + +/// Free `sink`, flushing and closing first if necessary SERD_API void serd_byte_sink_free(SerdByteSink* sink); @@ -1368,8 +1397,7 @@ serd_writer_new(SerdWorld* world, SerdSyntax syntax, SerdWriterFlags flags, SerdEnv* env, - SerdWriteFunc write_func, - void* stream); + SerdByteSink* byte_sink); /// Free `writer` SERD_API |