aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/byte_source.c14
-rw-r--r--src/reader.c28
-rw-r--r--src/serd_internal.h8
3 files changed, 25 insertions, 25 deletions
diff --git a/src/byte_source.c b/src/byte_source.c
index 8346785f..e2bcf196 100644
--- a/src/byte_source.c
+++ b/src/byte_source.c
@@ -38,12 +38,16 @@ serd_byte_source_open_source(SerdByteSource* source,
SerdSource read_func,
SerdStreamErrorFunc error_func,
void* stream,
+ const uint8_t* name,
size_t page_size)
{
+ const Cursor cur = { name, 1, 1 };
+
memset(source, '\0', sizeof(*source));
source->stream = stream;
source->from_stream = true;
source->page_size = page_size;
+ source->cur = cur;
source->error_func = error_func;
source->read_func = read_func;
@@ -76,7 +80,10 @@ serd_byte_source_prepare(SerdByteSource* source)
SerdStatus
serd_byte_source_open_string(SerdByteSource* source, const uint8_t* utf8)
{
+ const Cursor cur = { (const uint8_t*)"(string)", 1, 1 };
+
memset(source, '\0', sizeof(*source));
+ source->cur = cur;
source->read_buf = utf8;
source->prepared = true;
return SERD_SUCCESS;
@@ -97,6 +104,13 @@ serd_byte_source_advance(SerdByteSource* source)
{
const bool paging = source->page_size > 1;
SerdStatus st = SERD_SUCCESS;
+
+ switch (serd_byte_source_peek(source)) {
+ case '\0': break;
+ case '\n': ++source->cur.line; source->cur.col = 0; break;
+ default: ++source->cur.col;
+ }
+
if (source->from_stream && !paging) {
if (source->read_func(&source->read_byte, 1, 1, source->stream) == 0) {
return (source->error_func(source->stream)
diff --git a/src/reader.c b/src/reader.c
index 5fab0b3c..36d01cae 100644
--- a/src/reader.c
+++ b/src/reader.c
@@ -35,12 +35,6 @@
# define SERD_STACK_ASSERT_TOP(reader, ref)
#endif
-typedef struct {
- const uint8_t* filename;
- unsigned line;
- unsigned col;
-} Cursor;
-
/* Reference to a node in the stack (we can not use pointers since the
stack may be reallocated, invalidating any pointers to elements).
*/
@@ -73,7 +67,6 @@ struct SerdReaderImpl {
SerdStack stack;
SerdSyntax syntax;
unsigned next_id;
- Cursor cur;
SerdStatus status;
uint8_t* buf;
uint8_t* bprefix;
@@ -97,9 +90,8 @@ r_err(SerdReader* reader, SerdStatus st, const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
- const SerdError e = {
- st, reader->cur.filename, reader->cur.line, reader->cur.col, fmt, &args
- };
+ const Cursor* const cur = &reader->source.cur;
+ const SerdError e = { st, cur->filename, cur->line, cur->col, fmt, &args };
serd_error(reader->error_sink, reader->error_handle, &e);
va_end(args);
return 0;
@@ -128,12 +120,6 @@ static inline uint8_t
eat_byte_safe(SerdReader* reader, const uint8_t byte)
{
assert(peek_byte(reader) == byte);
- switch (byte) {
- case '\0': break;
- case '\n': ++reader->cur.line; reader->cur.col = 0; break;
- default: ++reader->cur.col;
- }
-
const SerdStatus st = serd_byte_source_advance(&reader->source);
if (st) {
reader->status = st;
@@ -1753,7 +1739,6 @@ serd_reader_new(SerdSyntax syntax,
SerdStatementSink statement_sink,
SerdEndSink end_sink)
{
- const Cursor cur = { NULL, 0, 0 };
SerdReader* me = (SerdReader*)calloc(1, sizeof(SerdReader));
me->handle = handle;
me->free_handle = free_handle;
@@ -1764,7 +1749,6 @@ serd_reader_new(SerdSyntax syntax,
me->default_graph = SERD_NODE_NULL;
me->stack = serd_stack_new(SERD_PAGE_SIZE);
me->syntax = syntax;
- me->cur = cur;
me->next_id = 1;
me->strict = true;
@@ -1905,11 +1889,8 @@ serd_reader_start_source_stream(SerdReader* reader,
const uint8_t* name,
size_t page_size)
{
- const Cursor cur = { name, 1, 1 };
- reader->cur = cur;
-
return serd_byte_source_open_source(
- &reader->source, read_func, error_func, stream, page_size);
+ &reader->source, read_func, error_func, stream, name, page_size);
}
static SerdStatus
@@ -1989,10 +1970,7 @@ SERD_API
SerdStatus
serd_reader_read_string(SerdReader* reader, const uint8_t* utf8)
{
- const Cursor cur = { (const uint8_t*)"(string)", 1, 1 };
-
serd_byte_source_open_string(&reader->source, utf8);
- reader->cur = cur;
SerdStatus st = serd_reader_prepare(reader);
if (!st) {
diff --git a/src/serd_internal.h b/src/serd_internal.h
index 7c758cb8..9523af91 100644
--- a/src/serd_internal.h
+++ b/src/serd_internal.h
@@ -75,10 +75,17 @@ serd_bufalloc(size_t size)
/* Byte source */
typedef struct {
+ const uint8_t* filename;
+ unsigned line;
+ unsigned col;
+} Cursor;
+
+typedef struct {
SerdSource 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
+ Cursor cur; ///< Cursor for error reporting
uint8_t* file_buf; ///< Buffer iff reading pages from a file
const uint8_t* read_buf; ///< Pointer to file_buf or read_byte
size_t read_head; ///< Offset into read_buf
@@ -101,6 +108,7 @@ serd_byte_source_open_source(SerdByteSource* source,
SerdSource read_func,
SerdStreamErrorFunc error_func,
void* stream,
+ const uint8_t* name,
size_t page_size);
SerdStatus