aboutsummaryrefslogtreecommitdiffstats
path: root/src/serd_internal.h
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2019-10-27 19:48:02 +0100
committerDavid Robillard <d@drobilla.net>2019-10-27 22:41:27 +0100
commitf7ffff1e75634909da60ea63a7c52f1a001220b8 (patch)
treed14587114d96a0be8408709c1f315412440d078d /src/serd_internal.h
parentcd6d4569c1c8819cc8e54eefdc0ac389d8efb4ea (diff)
downloadserd-f7ffff1e75634909da60ea63a7c52f1a001220b8.tar.gz
serd-f7ffff1e75634909da60ea63a7c52f1a001220b8.tar.bz2
serd-f7ffff1e75634909da60ea63a7c52f1a001220b8.zip
Fix EOF handling while reading in bulk or from strings
Diffstat (limited to 'src/serd_internal.h')
-rw-r--r--src/serd_internal.h11
1 files changed, 9 insertions, 2 deletions
diff --git a/src/serd_internal.h b/src/serd_internal.h
index eeb64687..9c58c151 100644
--- a/src/serd_internal.h
+++ b/src/serd_internal.h
@@ -92,6 +92,7 @@ typedef struct {
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
+ size_t buf_size; ///< Number of bytes in file_buf
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
@@ -140,28 +141,34 @@ serd_byte_source_advance(SerdByteSource* source)
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;
}
+ const bool was_eof = source->eof;
if (source->from_stream) {
source->eof = false;
if (source->page_size > 1) {
if (++source->read_head == source->page_size) {
st = serd_byte_source_page(source);
+ } else if (source->read_head == source->buf_size) {
+ source->eof = true;
}
} else {
if (!source->read_func(&source->read_byte, 1, 1, source->stream)) {
+ source->eof = true;
st = source->error_func(source->stream) ? SERD_ERR_UNKNOWN
: SERD_FAILURE;
}
}
} else if (!source->eof) {
++source->read_head; // Move to next character in string
+ if (source->read_buf[source->read_head] == '\0') {
+ source->eof = true;
+ }
}
- return source->eof ? SERD_FAILURE : st;
+ return (was_eof && source->eof) ? SERD_FAILURE : st;
}
/* Stack */