From 9a8e06aa5bdc62ed589bd8ed5789bd059cec0700 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 16 Aug 2020 12:42:58 +0200 Subject: Simplify reader interface --- src/n3.c | 3 +- src/reader.c | 124 ++++++++++++++++++++--------------------------------------- src/serdi.c | 39 +++++++++++++++---- 3 files changed, 74 insertions(+), 92 deletions(-) (limited to 'src') diff --git a/src/n3.c b/src/n3.c index fe6da47f..4ea6adbe 100644 --- a/src/n3.c +++ b/src/n3.c @@ -1683,8 +1683,9 @@ read_nquadsDoc(SerdReader* reader) } if (peek_byte(reader) == '@') { - return r_err( + r_err( reader, SERD_ERR_BAD_SYNTAX, "syntax does not support directives\n"); + return SERD_ERR_BAD_SYNTAX; } // subject predicate object diff --git a/src/reader.c b/src/reader.c index 6e814d93..194c295b 100644 --- a/src/reader.c +++ b/src/reader.c @@ -23,11 +23,13 @@ #include #include -#include #include #include #include +static SerdStatus +serd_reader_prepare(SerdReader* reader); + SerdStatus r_err(SerdReader* reader, SerdStatus st, const char* fmt, ...) { @@ -65,22 +67,6 @@ blank_id(SerdReader* reader) return ref; } -/** fread-like wrapper for getc (which is faster). */ -static size_t -serd_file_read_byte(void* buf, size_t size, size_t nmemb, void* stream) -{ - (void)size; - (void)nmemb; - - const int c = getc((FILE*)stream); - if (c == EOF) { - *((uint8_t*)buf) = 0; - return 0; - } - *((uint8_t*)buf) = (uint8_t)c; - return 1; -} - Ref push_node_padded(SerdReader* reader, size_t maxlen, @@ -165,9 +151,16 @@ read_statement(SerdReader* reader) return read_n3_statement(reader); } -static SerdStatus -read_doc(SerdReader* reader) +SerdStatus +serd_reader_read_document(SerdReader* reader) { + if (!reader->source.prepared) { + SerdStatus st = serd_reader_prepare(reader); + if (st) { + return st; + } + } + return ((reader->syntax == SERD_NQUADS) ? read_nquadsDoc(reader) : read_turtleTrigDoc(reader)); } @@ -281,10 +274,23 @@ serd_reader_read_file(SerdReader* reader, const char* uri) return SERD_ERR_UNKNOWN; } - SerdStatus ret = serd_reader_read_file_handle(reader, fd, path); + SerdStatus st = serd_reader_start_stream(reader, + (SerdSource)fread, + (SerdStreamErrorFunc)ferror, + fd, + path, + SERD_PAGE_SIZE); + + if (!st) { + st = serd_reader_read_document(reader); + } + + const SerdStatus est = serd_reader_end_stream(reader); + fclose(fd); free(path); - return ret; + + return st ? st : est; } static SerdStatus @@ -305,30 +311,21 @@ skip_bom(SerdReader* me) } SerdStatus -serd_reader_start_stream(SerdReader* reader, - FILE* file, - const char* name, - bool bulk) +serd_reader_start_stream(SerdReader* reader, + SerdSource read_func, + SerdStreamErrorFunc error_func, + void* stream, + const char* name, + size_t page_size) { - return serd_reader_start_source_stream(reader, - bulk ? (SerdSource)fread - : serd_file_read_byte, - (SerdStreamErrorFunc)ferror, - file, - name, - bulk ? SERD_PAGE_SIZE : 1); + return serd_byte_source_open_source( + &reader->source, read_func, error_func, stream, name, page_size); } SerdStatus -serd_reader_start_source_stream(SerdReader* reader, - SerdSource read_func, - SerdStreamErrorFunc error_func, - void* stream, - const char* name, - size_t page_size) +serd_reader_start_string(SerdReader* reader, const char* utf8) { - return serd_byte_source_open_source( - &reader->source, read_func, error_func, stream, name, page_size); + return serd_byte_source_open_string(&reader->source, utf8); } static SerdStatus @@ -369,52 +366,13 @@ serd_reader_end_stream(SerdReader* reader) return serd_byte_source_close(&reader->source); } -SerdStatus -serd_reader_read_file_handle(SerdReader* reader, FILE* file, const char* name) -{ - return serd_reader_read_source(reader, - (SerdSource)fread, - (SerdStreamErrorFunc)ferror, - file, - name, - SERD_PAGE_SIZE); -} - -SerdStatus -serd_reader_read_source(SerdReader* reader, - SerdSource source, - SerdStreamErrorFunc error, - void* stream, - const char* name, - size_t page_size) -{ - SerdStatus st = serd_reader_start_source_stream( - reader, source, error, stream, name, page_size); - - if (st || (st = serd_reader_prepare(reader))) { - serd_reader_end_stream(reader); - return st; - } - - if ((st = read_doc(reader))) { - serd_reader_end_stream(reader); - return st; - } - - return serd_reader_end_stream(reader); -} - SerdStatus serd_reader_read_string(SerdReader* reader, const char* utf8) { - serd_byte_source_open_string(&reader->source, utf8); + serd_reader_start_string(reader, utf8); - SerdStatus st = serd_reader_prepare(reader); - if (!st) { - st = read_doc(reader); - } + const SerdStatus st = serd_reader_read_document(reader); + const SerdStatus est = serd_byte_source_close(&reader->source); - serd_byte_source_close(&reader->source); - - return st; + return st ? st : est; } diff --git a/src/serdi.c b/src/serdi.c index f0cf8f4b..0e59c5ee 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -35,6 +35,7 @@ #include #include +#include #include #include #include @@ -150,6 +151,22 @@ serd_fopen(const char* path, const char* mode) return fd; } +/** fread-like wrapper for getc (which is faster). */ +static size_t +serd_file_read_byte(void* buf, size_t size, size_t nmemb, void* stream) +{ + (void)size; + (void)nmemb; + + const int c = getc((FILE*)stream); + if (c == EOF) { + *((uint8_t*)buf) = 0; + return 0; + } + *((uint8_t*)buf) = (uint8_t)c; + return 1; +} + static SerdWriterFlags choose_style(const SerdSyntax input_syntax, const SerdSyntax output_syntax, @@ -345,17 +362,23 @@ main(int argc, char** argv) SerdStatus st = SERD_SUCCESS; if (!from_file) { - st = serd_reader_read_string(reader, input); - } else if (bulk_read) { - st = serd_reader_read_file_handle(reader, in_fd, in_name); + st = serd_reader_start_string(reader, input); } else { - st = serd_reader_start_stream(reader, in_fd, in_name, false); - while (!st) { - st = serd_reader_read_chunk(reader); - } - serd_reader_end_stream(reader); + st = serd_reader_start_stream(reader, + bulk_read ? (SerdSource)fread + : serd_file_read_byte, + (SerdStreamErrorFunc)ferror, + in_fd, + in_name, + bulk_read ? 4096 : 1); + } + + if (!st) { + st = serd_reader_read_document(reader); } + serd_reader_end_stream(reader); + serd_reader_free(reader); serd_writer_finish(writer); serd_writer_free(writer); -- cgit v1.2.1