From b631e7a3d6a807aaf4919396b62ba96e1a9f0bd9 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Fri, 15 Jun 2018 09:26:17 -0400 Subject: Bring read/write interface closer to the C standard --- include/serd/buffer.h | 7 ++++--- include/serd/reader.h | 4 ++-- include/serd/stream.h | 38 +++++++++++++++++++++++++------------- include/serd/uri.h | 6 +++--- include/serd/writer.h | 22 +++++----------------- 5 files changed, 39 insertions(+), 38 deletions(-) (limited to 'include/serd') diff --git a/include/serd/buffer.h b/include/serd/buffer.h index 90b51a48..f1bfa897 100644 --- a/include/serd/buffer.h +++ b/include/serd/buffer.h @@ -28,14 +28,15 @@ typedef struct { /** A convenience sink function for writing to a string. - This function can be used as a #SerdSink to write to a SerdBuffer which is - resized as necessary with realloc(). The `stream` parameter must point to + This function can be used as a #SerdWriteFunc to write to a SerdBuffer which + is resized as necessary with realloc(). The `stream` parameter must point to an initialized #SerdBuffer. When the write is finished, the string should be retrieved with serd_buffer_sink_finish(). */ SERD_API size_t serd_buffer_sink(const void* SERD_NONNULL buf, - size_t len, + size_t size, + size_t nmemb, void* SERD_NONNULL stream); /** diff --git a/include/serd/reader.h b/include/serd/reader.h index e4bd9b2f..63c2c5ac 100644 --- a/include/serd/reader.h +++ b/include/serd/reader.h @@ -113,7 +113,7 @@ serd_reader_start_stream(SerdReader* SERD_NONNULL reader, */ SERD_API SerdStatus serd_reader_start_source_stream(SerdReader* SERD_NONNULL reader, - SerdSource SERD_NONNULL read_func, + SerdReadFunc SERD_NONNULL read_func, SerdStreamErrorFunc SERD_NONNULL error_func, void* SERD_NONNULL stream, const char* SERD_NULLABLE name, @@ -143,7 +143,7 @@ serd_reader_read_file_handle(SerdReader* SERD_NONNULL reader, /// Read a user-specified byte source SERD_API SerdStatus serd_reader_read_source(SerdReader* SERD_NONNULL reader, - SerdSource SERD_NONNULL source, + SerdReadFunc SERD_NONNULL source, SerdStreamErrorFunc SERD_NONNULL error, void* SERD_NONNULL stream, const char* SERD_NULLABLE name, diff --git a/include/serd/stream.h b/include/serd/stream.h index 8b023add..992db552 100644 --- a/include/serd/stream.h +++ b/include/serd/stream.h @@ -32,26 +32,38 @@ SERD_BEGIN_DECLS typedef int (*SerdStreamErrorFunc)(void* SERD_NONNULL stream); /** - Source function for raw string input. + Function for reading input bytes from a stream. - Identical semantics to `fread`, but may set errno for more informative error - reporting than supported by SerdStreamErrorFunc. + This has identical semantics to `fread`, but may set `errno` for more + informative error reporting than supported by #SerdStreamErrorFunc. @param buf Output buffer. @param size Size of a single element of data in bytes (always 1). @param nmemb Number of elements to read. @param stream Stream to read from (FILE* for fread). - @return Number of elements (bytes) read. + @return Number of elements (bytes) read, which is short on error. */ -typedef size_t (*SerdSource)(void* SERD_NONNULL buf, - size_t size, - size_t nmemb, - void* SERD_NONNULL stream); - -/// Sink function for raw string output -typedef size_t (*SerdSink)(const void* SERD_NONNULL buf, - size_t len, - void* SERD_NONNULL stream); +typedef size_t (*SerdReadFunc)(void* SERD_NONNULL buf, + size_t size, + size_t nmemb, + void* SERD_NONNULL stream); + +/** + Function for writing output bytes to a stream. + + This has identical semantics to `fwrite`, but may set `errno` for more + informative error reporting than supported by #SerdStreamErrorFunc. + + @param buf Input buffer. + @param size Size of a single element of data in bytes (always 1). + @param nmemb Number of elements to read. + @param stream Stream to write to (FILE* for fread). + @return Number of elements (bytes) written, which is short on error. +*/ +typedef size_t (*SerdWriteFunc)(const void* SERD_NONNULL buf, + size_t size, + size_t nmemb, + void* SERD_NONNULL stream); /** @} diff --git a/include/serd/uri.h b/include/serd/uri.h index 7b5529dc..93409075 100644 --- a/include/serd/uri.h +++ b/include/serd/uri.h @@ -133,9 +133,9 @@ serd_uri_is_within(SerdURIView r, SerdURIView base); @return The number of bytes written. */ SERD_API size_t -serd_write_uri(SerdURIView uri, - SerdSink SERD_NONNULL sink, - void* SERD_NONNULL stream); +serd_write_uri(SerdURIView uri, + SerdWriteFunc SERD_NONNULL sink, + void* SERD_NONNULL stream); /** @} diff --git a/include/serd/writer.h b/include/serd/writer.h index 976ed5b7..94747309 100644 --- a/include/serd/writer.h +++ b/include/serd/writer.h @@ -13,7 +13,6 @@ #include "serd/stream.h" #include "serd/syntax.h" -#include #include SERD_BEGIN_DECLS @@ -48,11 +47,11 @@ typedef uint32_t SerdWriterFlags; /// Create a new RDF writer SERD_API SerdWriter* SERD_ALLOCATED -serd_writer_new(SerdSyntax syntax, - SerdWriterFlags flags, - SerdEnv* SERD_NONNULL env, - SerdSink SERD_NONNULL ssink, - void* SERD_NULLABLE stream); +serd_writer_new(SerdSyntax syntax, + SerdWriterFlags flags, + SerdEnv* SERD_NONNULL env, + SerdWriteFunc SERD_NONNULL ssink, + void* SERD_NULLABLE stream); /// Free `writer` SERD_API void @@ -62,17 +61,6 @@ serd_writer_free(SerdWriter* SERD_NULLABLE writer); SERD_PURE_API SerdEnv* SERD_NONNULL serd_writer_env(SerdWriter* SERD_NONNULL writer); -/** - A convenience sink function for writing to a FILE*. - - This function can be used as a #SerdSink when writing to a FILE*. The - `stream` parameter must be a FILE* opened for writing. -*/ -SERD_API size_t -serd_file_sink(const void* SERD_NONNULL buf, - size_t len, - void* SERD_NONNULL stream); - /** Set a function to be called when errors occur during writing. -- cgit v1.2.1