From 1ae793464e0cfb71dedc9807d97132fdc6145005 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Fri, 14 Apr 2017 10:51:46 +0200 Subject: Factor out file reading from reader --- serd/serd.h | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'serd') diff --git a/serd/serd.h b/serd/serd.h index e7f6ec3b..0ba3dffd 100644 --- a/serd/serd.h +++ b/serd/serd.h @@ -376,6 +376,32 @@ SERD_API void serd_uri_resolve(const SerdURI* uri, const SerdURI* base, SerdURI* out); +/** + Function to detect I/O stream errors. + + Identical semantics to `ferror`. + + @return Non-zero if `stream` has encountered an error. +*/ +typedef int (*SerdStreamErrorFunc)(void* stream); + +/** + Source function for raw string input. + + 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. +*/ +typedef size_t (*SerdSource)(void* buf, + size_t size, + size_t nmemb, + void* stream); + /** Sink function for raw string output. */ @@ -798,6 +824,21 @@ serd_reader_start_stream(SerdReader* me, const uint8_t* name, bool bulk); +/** + Start an incremental read from a user-specified source. + + Iff `bulk` is true, `source` will be read a page at a time. Otherwise, + `source` is guaranteed to only be called for single bytes. +*/ +SERD_API +SerdStatus +serd_reader_start_source_stream(SerdReader* me, + SerdSource read_func, + SerdStreamErrorFunc error_func, + void* stream, + const uint8_t* name, + bool bulk); + /** Read a single "chunk" of data during an incremental read. @@ -826,6 +867,17 @@ serd_reader_read_file_handle(SerdReader* reader, FILE* file, const uint8_t* name); +/** + Read a user-specified byte source. +*/ +SERD_API +SerdStatus +serd_reader_read_source(SerdReader* reader, + SerdSource source, + SerdStreamErrorFunc error, + void* stream, + const uint8_t* name); + /** Read `utf8`. */ -- cgit v1.2.1