From ebe0b8697bca69abb7e7be1c731da4d54b10e462 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 3 Aug 2024 12:31:51 -0400 Subject: Fix reading chunks from files without trailing newlines --- src/byte_source.c | 15 +++++++-------- src/byte_source.h | 2 +- 2 files changed, 8 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/byte_source.c b/src/byte_source.c index 5443f4dd..122dc0f9 100644 --- a/src/byte_source.c +++ b/src/byte_source.c @@ -19,18 +19,17 @@ serd_byte_source_page(SerdByteSource* const source) const size_t n_read = source->read_func(source->file_buf, 1, source->page_size, source->stream); + source->buf_size = n_read; + if (n_read < source->page_size) { + source->file_buf[n_read] = '\0'; + } + if (n_read == 0) { - source->file_buf[0] = '\0'; - source->eof = true; + source->eof = true; return (source->error_func(source->stream) ? SERD_ERR_UNKNOWN : SERD_FAILURE); } - if (n_read < source->page_size) { - source->file_buf[n_read] = '\0'; - source->buf_size = n_read; - } - return SERD_SUCCESS; } @@ -52,7 +51,7 @@ serd_byte_source_open_source(SerdByteSource* const source, source->stream = stream; source->from_stream = true; source->page_size = page_size; - source->buf_size = page_size; + source->buf_size = 0U; source->cur = cur; source->error_func = error_func; source->read_func = read_func; diff --git a/src/byte_source.h b/src/byte_source.h index bd6701c9..afd9ccb9 100644 --- a/src/byte_source.h +++ b/src/byte_source.h @@ -83,7 +83,7 @@ serd_byte_source_advance(SerdByteSource* source) 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) { + } else if (source->read_head >= source->buf_size) { source->eof = true; } } else { -- cgit v1.2.1