diff options
author | David Robillard <d@drobilla.net> | 2018-06-15 09:26:17 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2020-10-27 13:13:58 +0100 |
commit | ce6fbad56fb6970fe507186d6560ad4fac981d74 (patch) | |
tree | bb08acadb802660b221fd29b322e1cb083dd17e6 /src | |
parent | c5fb8c6326dfe8c46fe2247e611ae1bf94275da0 (diff) | |
download | serd-ce6fbad56fb6970fe507186d6560ad4fac981d74.tar.gz serd-ce6fbad56fb6970fe507186d6560ad4fac981d74.tar.bz2 serd-ce6fbad56fb6970fe507186d6560ad4fac981d74.zip |
Bring read/write interface closer to C standard
Diffstat (limited to 'src')
-rw-r--r-- | src/byte_sink.h | 18 | ||||
-rw-r--r-- | src/byte_source.c | 2 | ||||
-rw-r--r-- | src/byte_source.h | 4 | ||||
-rw-r--r-- | src/node.c | 16 | ||||
-rw-r--r-- | src/reader.c | 4 | ||||
-rw-r--r-- | src/serdi.c | 11 | ||||
-rw-r--r-- | src/uri.c | 40 | ||||
-rw-r--r-- | src/writer.c | 27 |
8 files changed, 62 insertions, 60 deletions
diff --git a/src/byte_sink.h b/src/byte_sink.h index f9b8a12a..1be84b48 100644 --- a/src/byte_sink.h +++ b/src/byte_sink.h @@ -26,15 +26,15 @@ #include <string.h> typedef struct SerdByteSinkImpl { - SerdSink sink; - void* stream; - char* buf; - size_t size; - size_t block_size; + SerdWriteFunc sink; + void* stream; + char* buf; + size_t size; + size_t block_size; } SerdByteSink; static inline SerdByteSink -serd_byte_sink_new(SerdSink sink, void* stream, size_t block_size) +serd_byte_sink_new(SerdWriteFunc sink, void* stream, size_t block_size) { SerdByteSink bsink; bsink.sink = sink; @@ -51,7 +51,7 @@ static inline void serd_byte_sink_flush(SerdByteSink* bsink) { if (bsink->block_size > 1 && bsink->size > 0) { - bsink->sink(bsink->buf, bsink->size, bsink->stream); + bsink->sink(bsink->buf, 1, bsink->size, bsink->stream); bsink->size = 0; } } @@ -70,7 +70,7 @@ serd_byte_sink_write(const void* buf, size_t len, SerdByteSink* bsink) if (len == 0) { return 0; } else if (bsink->block_size == 1) { - return bsink->sink(buf, len, bsink->stream); + return bsink->sink(buf, 1, len, bsink->stream); } const size_t orig_len = len; @@ -86,7 +86,7 @@ serd_byte_sink_write(const void* buf, size_t len, SerdByteSink* bsink) // Flush page if buffer is full if (bsink->size == bsink->block_size) { - bsink->sink(bsink->buf, bsink->block_size, bsink->stream); + bsink->sink(bsink->buf, 1, bsink->block_size, bsink->stream); bsink->size = 0; } } diff --git a/src/byte_source.c b/src/byte_source.c index 8006fcf5..9d245004 100644 --- a/src/byte_source.c +++ b/src/byte_source.c @@ -45,7 +45,7 @@ serd_byte_source_page(SerdByteSource* source) SerdStatus serd_byte_source_open_source(SerdByteSource* source, - SerdSource read_func, + SerdReadFunc read_func, SerdStreamErrorFunc error_func, void* stream, const char* name, diff --git a/src/byte_source.h b/src/byte_source.h index e5c4a8bf..1a59e408 100644 --- a/src/byte_source.h +++ b/src/byte_source.h @@ -32,7 +32,7 @@ typedef struct { } Cursor; typedef struct { - SerdSource read_func; ///< Read function (e.g. fread) + SerdReadFunc read_func; ///< Read function (e.g. fread) SerdStreamErrorFunc error_func; ///< Error function (e.g. ferror) void* stream; ///< Stream (e.g. FILE) size_t page_size; ///< Number of bytes to read at a time @@ -57,7 +57,7 @@ serd_byte_source_open_string(SerdByteSource* source, const char* utf8); SerdStatus serd_byte_source_open_source(SerdByteSource* source, - SerdSource read_func, + SerdReadFunc read_func, SerdStreamErrorFunc error_func, void* stream, const char* name, @@ -211,12 +211,12 @@ serd_uri_string_length(const SerdURI* uri) } static size_t -string_sink(const void* buf, size_t len, void* stream) +string_sink(const void* buf, size_t size, size_t nmemb, void* stream) { char** ptr = (char**)stream; - memcpy(*ptr, buf, len); - *ptr += len; - return len; + memcpy(*ptr, buf, size * nmemb); + *ptr += size * nmemb; + return nmemb; } SerdNode* @@ -286,18 +286,18 @@ serd_node_new_file_uri(const char* path, SerdBuffer buffer = { uri, uri_len }; for (size_t i = 0; i < path_len; ++i) { if (evil && path[i] == '\\') { - serd_buffer_sink("/", 1, &buffer); + serd_buffer_sink("/", 1, 1, &buffer); } else if (path[i] == '%') { - serd_buffer_sink("%%", 2, &buffer); + serd_buffer_sink("%%", 1, 2, &buffer); } else if (!escape || is_uri_path_char(path[i])) { - serd_buffer_sink(path + i, 1, &buffer); + serd_buffer_sink(path + i, 1, 1, &buffer); } else { char escape_str[4] = { '%', 0, 0, 0 }; snprintf(escape_str + 1, sizeof(escape_str) - 1, "%X", (unsigned)path[i]); - serd_buffer_sink(escape_str, 3, &buffer); + serd_buffer_sink(escape_str, 1, 3, &buffer); } } serd_buffer_sink_finish(&buffer); diff --git a/src/reader.c b/src/reader.c index c90f6355..e58982c0 100644 --- a/src/reader.c +++ b/src/reader.c @@ -268,7 +268,7 @@ serd_reader_read_file(SerdReader* reader, } SerdStatus st = serd_reader_start_stream( - reader, (SerdSource)fread, (SerdStreamErrorFunc)ferror, + reader, (SerdReadFunc)fread, (SerdStreamErrorFunc)ferror, fd, path, SERD_PAGE_SIZE); if (!st) { @@ -302,7 +302,7 @@ skip_bom(SerdReader* me) SerdStatus serd_reader_start_stream(SerdReader* reader, - SerdSource read_func, + SerdReadFunc read_func, SerdStreamErrorFunc error_func, void* stream, const char* name, diff --git a/src/serdi.c b/src/serdi.c index 99be638c..c16cd668 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -312,9 +312,12 @@ main(int argc, char** argv) output_style |= SERD_STYLE_BULK; } - SerdWriter* writer = serd_writer_new( - output_syntax, (SerdStyle)output_style, - env, &base_uri, serd_file_sink, out_fd); + SerdWriter* writer = serd_writer_new(output_syntax, + (SerdStyle)output_style, + env, + &base_uri, + (SerdWriteFunc)fwrite, + out_fd); SerdReader* reader = serd_reader_new( input_syntax, writer, NULL, @@ -341,7 +344,7 @@ main(int argc, char** argv) } else { st = serd_reader_start_stream( reader, - bulk_read ? (SerdSource)fread : serd_file_read_byte, + bulk_read ? (SerdReadFunc)fread : serd_file_read_byte, (SerdStreamErrorFunc)ferror, in_fd, in_name, @@ -55,18 +55,18 @@ serd_file_uri_parse(const char* uri, char** hostname) for (const char* s = path; *s; ++s) { if (*s == '%') { if (*(s + 1) == '%') { - serd_buffer_sink("%", 1, &buffer); + serd_buffer_sink("%", 1, 1, &buffer); ++s; } else if (is_hexdig(*(s + 1)) && is_hexdig(*(s + 2))) { const char code[3] = {*(s + 1), *(s + 2), 0}; const char c = (char)strtoul((const char*)code, NULL, 16); - serd_buffer_sink(&c, 1, &buffer); + serd_buffer_sink(&c, 1, 1, &buffer); s += 2; } else { s += 2; // Junk escape, ignore } } else { - serd_buffer_sink(s, 1, &buffer); + serd_buffer_sink(s, 1, 1, &buffer); } } return serd_buffer_sink_finish(&buffer); @@ -331,18 +331,18 @@ serd_uri_resolve(const SerdURI* r, const SerdURI* base, SerdURI* t) /** Write the path of `uri` starting at index `i` */ static size_t -write_path_tail(SerdSink sink, void* stream, const SerdURI* uri, size_t i) +write_path_tail(SerdWriteFunc sink, void* stream, const SerdURI* uri, size_t i) { size_t len = 0; if (i < uri->path_base.len) { - len += sink(uri->path_base.buf + i, uri->path_base.len - i, stream); + len += sink(uri->path_base.buf + i, 1, uri->path_base.len - i, stream); } if (uri->path.buf) { if (i < uri->path_base.len) { - len += sink(uri->path.buf, uri->path.len, stream); + len += sink(uri->path.buf, 1, uri->path.len, stream); } else { const size_t j = (i - uri->path_base.len); - len += sink(uri->path.buf + j, uri->path.len - j, stream); + len += sink(uri->path.buf + j, 1, uri->path.len - j, stream); } } return len; @@ -350,7 +350,7 @@ write_path_tail(SerdSink sink, void* stream, const SerdURI* uri, size_t i) /** Write the path of `uri` relative to the path of `base`. */ static size_t -write_rel_path(SerdSink sink, +write_rel_path(SerdWriteFunc sink, void* stream, const SerdURI* uri, const SerdURI* base) @@ -383,11 +383,11 @@ write_rel_path(SerdSink sink, // Write up references size_t len = 0; for (size_t u = 0; u < up; ++u) { - len += sink("../", 3, stream); + len += sink("../", 1, 3, stream); } if (last_shared_sep == 0 && up == 0) { - len += sink("/", 1, stream); + len += sink("/", 1, 1, stream); } // Write suffix @@ -407,7 +407,7 @@ size_t serd_uri_serialise_relative(const SerdURI* uri, const SerdURI* base, const SerdURI* root, - SerdSink sink, + SerdWriteFunc sink, void* stream) { size_t len = 0; @@ -419,35 +419,35 @@ serd_uri_serialise_relative(const SerdURI* uri, } if (!relative || (!len && base->query.buf)) { if (uri->scheme.buf) { - len += sink(uri->scheme.buf, uri->scheme.len, stream); - len += sink(":", 1, stream); + len += sink(uri->scheme.buf, 1, uri->scheme.len, stream); + len += sink(":", 1, 1, stream); } if (uri->authority.buf) { - len += sink("//", 2, stream); - len += sink(uri->authority.buf, uri->authority.len, stream); + len += sink("//", 1, 2, stream); + len += sink(uri->authority.buf, 1, uri->authority.len, stream); if (uri->authority.buf[uri->authority.len - 1] != '/' && serd_uri_path_starts_without_slash(uri)) { // Special case: ensure path begins with a slash // https://tools.ietf.org/html/rfc3986#section-3.2 - len += sink("/", 1, stream); + len += sink("/", 1, 1, stream); } } len += write_path_tail(sink, stream, uri, 0); } if (uri->query.buf) { - len += sink("?", 1, stream); - len += sink(uri->query.buf, uri->query.len, stream); + len += sink("?", 1, 1, stream); + len += sink(uri->query.buf, 1, uri->query.len, stream); } if (uri->fragment.buf) { // Note uri->fragment.buf includes the leading `#' - len += sink(uri->fragment.buf, uri->fragment.len, stream); + len += sink(uri->fragment.buf, 1, uri->fragment.len, stream); } return len; } /// See http://tools.ietf.org/html/rfc3986#section-5.3 size_t -serd_uri_serialise(const SerdURI* uri, SerdSink sink, void* stream) +serd_uri_serialise(const SerdURI* uri, SerdWriteFunc sink, void* stream) { return serd_uri_serialise_relative(uri, NULL, NULL, sink, stream); } diff --git a/src/writer.c b/src/writer.c index b2c6ad72..dbbdfbef 100644 --- a/src/writer.c +++ b/src/writer.c @@ -373,9 +373,11 @@ write_text(SerdWriter* writer, TextContext ctx, } static size_t -uri_sink(const void* buf, size_t len, void* stream) +uri_sink(const void* buf, size_t size, size_t nmemb, void* stream) { - return write_uri((SerdWriter*)stream, (const char*)buf, len); + (void)size; + assert(size == 1); + return write_uri((SerdWriter*)stream, (const char*)buf, nmemb); } static void @@ -873,7 +875,7 @@ serd_writer_new(SerdSyntax syntax, SerdStyle style, SerdEnv* env, const SerdURI* base_uri, - SerdSink ssink, + SerdWriteFunc ssink, void* stream) { const WriteContext context = WRITE_CONTEXT_NULL; @@ -998,24 +1000,21 @@ serd_writer_env(SerdWriter* writer) } size_t -serd_file_sink(const void* buf, size_t len, void* stream) +serd_buffer_sink(const void* buf, size_t size, size_t nmemb, void* stream) { - return fwrite(buf, 1, len, (FILE*)stream); -} + assert(size == 1); + (void)size; -size_t -serd_buffer_sink(const void* buf, size_t len, void* stream) -{ SerdBuffer* buffer = (SerdBuffer*)stream; - buffer->buf = (char*)realloc(buffer->buf, buffer->len + len); - memcpy((uint8_t*)buffer->buf + buffer->len, buf, len); - buffer->len += len; - return len; + buffer->buf = (char*)realloc(buffer->buf, buffer->len + nmemb); + memcpy((uint8_t*)buffer->buf + buffer->len, buf, nmemb); + buffer->len += nmemb; + return nmemb; } char* serd_buffer_sink_finish(SerdBuffer* stream) { - serd_buffer_sink("", 1, stream); + serd_buffer_sink("", 1, 1, stream); return (char*)stream->buf; } |