Byte Sink¶
-
typedef struct SerdByteSinkImpl
SerdByteSink¶ A sink for bytes that receives text output.
-
typedef size_t (*
SerdWriteFunc)(const void *buf, size_t size, size_t nmemb, void *stream)¶ Sink function for raw string output.
Identical semantics to
fwrite, but may set errno for more informative error reporting than supported by SerdStreamErrorFunc.- Parameters
buf – Input buffer.
size – Size of a single element of data in bytes (always 1).
nmemb – Number of elements to read.
stream – Stream to write to (FILE* for fread).
- Returns
Number of elements (bytes) written, which is short on error.
-
SerdByteSink *
serd_byte_sink_new_buffer(SerdBuffer *buffer)¶ Create a new byte sink that writes to a buffer.
The
bufferis owned by the caller, but will be expanded as necessary.- Parameters
buffer – Buffer to write output to.
-
SerdByteSink *
serd_byte_sink_new_filename(const char *path, size_t block_size)¶ Create a new byte sink that writes to a file.
An arbitrary
FILE*can be used viaserd_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.- Parameters
path – Path of file to open and write to.
block_size – Number of bytes to write per call.
-
SerdByteSink *
serd_byte_sink_new_function(SerdWriteFunc write_func, void *stream, size_t block_size)¶ Create a new byte sink that writes to a user-specified function.
The
streamwill be passed to thewrite_func, which is compatible with the standard Cfwriteifstreamis aFILE*.- Parameters
write_func – Function called with bytes to consume.
stream – Context parameter passed to
sink.block_size – Number of bytes to write per call.
-
void
serd_byte_sink_flush(SerdByteSink *sink)¶ Flush any pending output in
sinkto the underlying write function.
-
SerdStatus
serd_byte_sink_close(SerdByteSink *sink)¶ Close
sink, including the underlying file if necessary.If
sinkwas created withserd_byte_sink_new_filename(), then the file is closed. If there was an error, then SERD_ERR_UNKNOWN is returned anderrnois set.
-
void
serd_byte_sink_free(SerdByteSink *sink)¶ Free
sink, flushing and closing first if necessary.