aboutsummaryrefslogtreecommitdiffstats
path: root/src/reader.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-02-10 13:32:06 +0100
committerDavid Robillard <d@drobilla.net>2018-12-30 17:56:22 -0500
commit0e24282852a889894dfc3e3afa61766a4b8f4ddb (patch)
tree7b2eac33b38ef20758a3998c25274b81dcb213d7 /src/reader.c
parent9f7d2de1ee0443c1d82a854ac459fc7dbe79749d (diff)
downloadserd-0e24282852a889894dfc3e3afa61766a4b8f4ddb.tar.gz
serd-0e24282852a889894dfc3e3afa61766a4b8f4ddb.tar.bz2
serd-0e24282852a889894dfc3e3afa61766a4b8f4ddb.zip
Simplify reader interface
Diffstat (limited to 'src/reader.c')
-rw-r--r--src/reader.c116
1 files changed, 37 insertions, 79 deletions
diff --git a/src/reader.c b/src/reader.c
index 7c262189..41fe4848 100644
--- a/src/reader.c
+++ b/src/reader.c
@@ -24,6 +24,8 @@
#include <stdlib.h>
#include <string.h>
+static SerdStatus serd_reader_prepare(SerdReader* reader);
+
int
r_err(SerdReader* reader, SerdStatus st, const char* fmt, ...)
{
@@ -59,22 +61,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,
SerdType type, const char* str, size_t n_bytes)
@@ -150,9 +136,16 @@ read_statement(SerdReader* reader)
}
}
-static bool
-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;
+ }
+ }
+
switch (reader->syntax) {
case SERD_NQUADS: return read_nquadsDoc(reader);
default: return read_turtleTrigDoc(reader);
@@ -265,10 +258,20 @@ serd_reader_read_file(SerdReader* reader,
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
@@ -289,30 +292,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
@@ -349,48 +343,12 @@ serd_reader_end_stream(SerdReader* reader)
}
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;
- } else if (!read_doc(reader)) {
- serd_reader_end_stream(reader);
- return SERD_ERR_UNKNOWN;
- }
-
- return serd_reader_end_stream(reader);
-}
-
-SerdStatus
serd_reader_read_string(SerdReader* reader, const char* utf8)
{
- serd_byte_source_open_string(&reader->source, utf8);
-
- SerdStatus st = serd_reader_prepare(reader);
- if (!st) {
- st = read_doc(reader) ? SERD_SUCCESS : SERD_ERR_UNKNOWN;
- }
+ serd_reader_start_string(reader, utf8);
- serd_byte_source_close(&reader->source);
+ const SerdStatus st = serd_reader_read_document(reader);
+ const SerdStatus est = serd_byte_source_close(&reader->source);
- return st;
+ return st ? st : est;
}