aboutsummaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2020-06-28 19:46:47 +0200
committerDavid Robillard <d@drobilla.net>2022-01-13 23:04:17 -0500
commitcae15d15c847faba203e40e2a327b23a1ebffb48 (patch)
tree17d1e637673ef45c3afdc3c43b67f0b023b8d851 /include
parent900d360027d570da085d495becde1ad50f050048 (diff)
downloadserd-cae15d15c847faba203e40e2a327b23a1ebffb48.tar.gz
serd-cae15d15c847faba203e40e2a327b23a1ebffb48.tar.bz2
serd-cae15d15c847faba203e40e2a327b23a1ebffb48.zip
Make Writer always write to a ByteSink
Diffstat (limited to 'include')
-rw-r--r--include/serd/serd.h118
1 files changed, 72 insertions, 46 deletions
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
@@ -305,50 +305,6 @@ typedef size_t (*SerdWriteFunc)(const void* SERD_NONNULL buf,
/**
@}
- @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
@{
*/
@@ -1591,6 +1547,77 @@ 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