diff options
Diffstat (limited to 'src/reader.c')
-rw-r--r-- | src/reader.c | 87 |
1 files changed, 27 insertions, 60 deletions
diff --git a/src/reader.c b/src/reader.c index b0b35387..1c9469af 100644 --- a/src/reader.c +++ b/src/reader.c @@ -19,7 +19,6 @@ #include "namespaces.h" #include "stack.h" #include "statement.h" -#include "system.h" #include "world.h" #include <errno.h> @@ -36,7 +35,7 @@ r_err(SerdReader* reader, SerdStatus st, const char* fmt, ...) { va_list args; va_start(args, fmt); - const SerdError e = {st, &reader->source.cur, fmt, &args}; + const SerdError e = {st, &reader->source->cur, fmt, &args}; serd_world_error(reader->world, &e); va_end(args); return st; @@ -123,7 +122,7 @@ emit_statement(SerdReader* reader, ReadContext ctx, SerdNode* o) serd_node_zero_pad(o); const SerdStatement statement = {{ctx.subject, ctx.predicate, o, ctx.graph}, - &reader->source.cur}; + &reader->source->cur}; const SerdStatus st = serd_sink_write_statement(reader->sink, *ctx.flags, &statement); @@ -141,7 +140,11 @@ read_statement(SerdReader* reader) SerdStatus serd_reader_read_document(SerdReader* reader) { - if (!reader->source.prepared) { + if (!reader->source) { + return SERD_ERR_BAD_CALL; + } + + if (!reader->source->prepared) { SerdStatus st = serd_reader_prepare(reader); if (st) { return st; @@ -221,12 +224,12 @@ serd_reader_add_blank_prefix(SerdReader* reader, const char* prefix) static SerdStatus skip_bom(SerdReader* me) { - if (serd_byte_source_peek(&me->source) == 0xEF) { - serd_byte_source_advance(&me->source); - if (serd_byte_source_peek(&me->source) != 0xBB || - serd_byte_source_advance(&me->source) || - serd_byte_source_peek(&me->source) != 0xBF || - serd_byte_source_advance(&me->source)) { + if (serd_byte_source_peek(me->source) == 0xEF) { + serd_byte_source_advance(me->source); + if (serd_byte_source_peek(me->source) != 0xBB || + serd_byte_source_advance(me->source) || + serd_byte_source_peek(me->source) != 0xBF || + serd_byte_source_advance(me->source)) { r_err(me, SERD_ERR_BAD_SYNTAX, "corrupt byte order mark\n"); return SERD_ERR_BAD_SYNTAX; } @@ -236,64 +239,23 @@ skip_bom(SerdReader* me) } SerdStatus -serd_reader_start_stream(SerdReader* reader, - SerdReadFunc read_func, - SerdStreamErrorFunc error_func, - void* stream, - const SerdNode* name, - size_t page_size) -{ - serd_reader_finish(reader); - return serd_byte_source_open_source( - &reader->source, read_func, error_func, NULL, stream, name, page_size); -} - -SerdStatus -serd_reader_start_file(SerdReader* reader, const char* uri, bool bulk) +serd_reader_start(SerdReader* reader, SerdByteSource* byte_source) { serd_reader_finish(reader); - char* const path = serd_parse_file_uri(uri, NULL); - if (!path) { - return SERD_ERR_BAD_ARG; - } - - FILE* fd = serd_world_fopen(reader->world, path, "rb"); - free(path); - if (!fd) { - return SERD_ERR_UNKNOWN; - } + reader->source = byte_source; - SerdNode* const name = serd_new_uri(SERD_MEASURE_STRING(uri)); - const SerdStatus st = serd_byte_source_open_source( - &reader->source, - bulk ? (SerdReadFunc)fread : serd_file_read_byte, - (SerdStreamErrorFunc)ferror, - (SerdStreamCloseFunc)fclose, - fd, - name, - bulk ? SERD_PAGE_SIZE : 1u); - serd_node_free(name); - return st; -} - -SerdStatus -serd_reader_start_string(SerdReader* reader, - const char* utf8, - const SerdNode* name) -{ - serd_reader_finish(reader); - return serd_byte_source_open_string(&reader->source, utf8, name); + return reader->source ? SERD_SUCCESS : SERD_ERR_BAD_ARG; } static SerdStatus serd_reader_prepare(SerdReader* reader) { - SerdStatus st = serd_byte_source_prepare(&reader->source); + SerdStatus st = serd_byte_source_prepare(reader->source); if (st == SERD_SUCCESS) { st = skip_bom(reader); } else if (st == SERD_FAILURE) { - reader->source.eof = true; + reader->source->eof = true; } else { r_err(reader, st, "error preparing read: %s\n", strerror(errno)); } @@ -304,10 +266,14 @@ SerdStatus serd_reader_read_chunk(SerdReader* reader) { SerdStatus st = SERD_SUCCESS; - if (!reader->source.prepared) { + if (!reader->source) { + return SERD_ERR_BAD_CALL; + } + + if (!reader->source->prepared) { st = serd_reader_prepare(reader); - } else if (reader->source.eof) { - st = serd_byte_source_advance(&reader->source); + } else if (reader->source->eof) { + st = serd_byte_source_advance(reader->source); } if (peek_byte(reader) == 0) { @@ -321,5 +287,6 @@ serd_reader_read_chunk(SerdReader* reader) SerdStatus serd_reader_finish(SerdReader* reader) { - return serd_byte_source_close(&reader->source); + reader->source = NULL; + return SERD_SUCCESS; } |