aboutsummaryrefslogtreecommitdiffstats
path: root/src/reader.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/reader.c')
-rw-r--r--src/reader.c105
1 files changed, 41 insertions, 64 deletions
diff --git a/src/reader.c b/src/reader.c
index d7c87c47..bf6d697f 100644
--- a/src/reader.c
+++ b/src/reader.c
@@ -16,12 +16,9 @@
#include "system.h"
#include "world.h"
-#include "serd/stream.h"
-#include "serd/string_view.h"
-#include "serd/uri.h"
+#include "serd/input_stream.h"
#include <assert.h>
-#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
@@ -35,7 +32,7 @@ r_err(SerdReader* const reader, const SerdStatus st, const char* const fmt, ...)
{
va_list args; // NOLINT(cppcoreguidelines-init-variables)
va_start(args, fmt);
- const SerdError e = {st, &reader->source.caret, fmt, &args};
+ const SerdError e = {st, &reader->source->caret, fmt, &args};
serd_world_error(reader->world, &e);
va_end(args);
return st;
@@ -90,7 +87,8 @@ tolerate_status(const SerdReader* const reader, const SerdStatus status)
}
if (status == SERD_BAD_STREAM || status == SERD_BAD_STACK ||
- status == SERD_BAD_WRITE || status == SERD_NO_DATA) {
+ status == SERD_BAD_WRITE || status == SERD_NO_DATA ||
+ status == SERD_BAD_CALL) {
return false;
}
@@ -174,7 +172,7 @@ emit_statement(SerdReader* const reader,
serd_node_zero_pad(o);
const SerdStatement statement = {{ctx.subject, ctx.predicate, o, ctx.graph},
- &reader->source.caret};
+ &reader->source->caret};
const SerdStatus st =
serd_sink_write_statement(reader->sink, *ctx.flags, &statement);
@@ -188,7 +186,11 @@ serd_reader_read_document(SerdReader* const reader)
{
assert(reader);
- if (reader->syntax != SERD_SYNTAX_EMPTY && !reader->source.prepared) {
+ if (!reader->source) {
+ return SERD_BAD_CALL;
+ }
+
+ if (reader->syntax != SERD_SYNTAX_EMPTY && !reader->source->prepared) {
SerdStatus st = serd_reader_prepare(reader);
if (st) {
return st;
@@ -256,7 +258,9 @@ serd_reader_free(SerdReader* const reader)
return;
}
- serd_reader_finish(reader);
+ if (reader->source) {
+ serd_reader_finish(reader);
+ }
serd_free_aligned(reader->stack.buf);
free(reader->bprefix);
@@ -283,12 +287,12 @@ serd_reader_add_blank_prefix(SerdReader* const reader, const char* const prefix)
static SerdStatus
skip_bom(SerdReader* const me)
{
- if (serd_byte_source_peek(&me->source) == 0xEF) {
- if (serd_byte_source_advance(&me->source) ||
- 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) {
+ if (serd_byte_source_advance(me->source) ||
+ 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_BAD_SYNTAX, "corrupt byte order mark");
return SERD_BAD_SYNTAX;
}
@@ -298,64 +302,31 @@ skip_bom(SerdReader* const me)
}
SerdStatus
-serd_reader_start_stream(SerdReader* const reader,
- const SerdReadFunc read_func,
- const SerdStreamErrorFunc error_func,
- void* const stream,
- const SerdNode* const name,
- const size_t page_size)
+serd_reader_start(SerdReader* const reader,
+ SerdInputStream* const input,
+ const SerdNode* const input_name,
+ const size_t block_size)
{
assert(reader);
+ assert(input);
- 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)
-{
- char* const path = serd_parse_file_uri(uri, NULL);
- if (!path) {
- return SERD_BAD_ARG;
- }
-
- FILE* fd = serd_world_fopen(reader->world, path, "rb");
- free(path);
- if (!fd) {
- return SERD_BAD_STREAM;
+ if (reader->source) {
+ return SERD_BAD_CALL;
}
- SerdNode* const name = serd_new_uri(serd_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;
-}
+ reader->source = serd_byte_source_new_input(input, input_name, block_size);
-SerdStatus
-serd_reader_start_string(SerdReader* const reader,
- const char* const utf8,
- const SerdNode* const name)
-{
- return serd_byte_source_open_string(&reader->source, utf8, name);
+ return reader->source ? SERD_SUCCESS : SERD_BAD_ARG;
}
static SerdStatus
serd_reader_prepare(SerdReader* const 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;
- } else {
- r_err(reader, st, "error preparing read: %s", strerror(errno));
+ reader->source->eof = true;
}
return st;
}
@@ -366,15 +337,19 @@ serd_reader_read_chunk(SerdReader* const reader)
assert(reader);
SerdStatus st = SERD_SUCCESS;
- if (!reader->source.prepared) {
+ if (!reader->source) {
+ return SERD_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) {
// Skip leading null byte, for reading from a null-delimited socket
- serd_byte_source_advance(&reader->source);
+ serd_byte_source_advance(reader->source);
return SERD_FAILURE;
}
@@ -402,5 +377,7 @@ serd_reader_finish(SerdReader* const reader)
{
assert(reader);
- return serd_byte_source_close(&reader->source);
+ serd_byte_source_free(reader->source);
+ reader->source = NULL;
+ return SERD_SUCCESS;
}