diff options
-rw-r--r-- | src/byte_source.c | 25 | ||||
-rw-r--r-- | src/byte_source.h | 24 | ||||
-rw-r--r-- | src/reader.c | 5 |
3 files changed, 20 insertions, 34 deletions
diff --git a/src/byte_source.c b/src/byte_source.c index 0e6ae2be..df757376 100644 --- a/src/byte_source.c +++ b/src/byte_source.c @@ -28,20 +28,23 @@ SerdStatus serd_byte_source_page(SerdByteSource* const source) { - source->read_head = 0; + uint8_t* const buf = + (source->page_size > 1 ? source->file_buf : &source->read_byte); + const size_t n_read = - source->read_func(source->file_buf, 1, source->page_size, source->stream); + source->read_func(buf, 1, source->page_size, source->stream); - if (n_read == 0) { - source->file_buf[0] = '\0'; - source->eof = true; - return (source->error_func(source->stream) ? SERD_ERR_UNKNOWN - : SERD_FAILURE); - } + source->buf_size = n_read; + source->read_head = 0; + source->eof = false; if (n_read < source->page_size) { - source->file_buf[n_read] = '\0'; - source->buf_size = n_read; + buf[n_read] = '\0'; + if (n_read == 0) { + source->eof = true; + return (source->error_func(source->stream) ? SERD_ERR_UNKNOWN + : SERD_FAILURE); + } } return SERD_SUCCESS; @@ -56,6 +59,7 @@ serd_byte_source_open_source(SerdByteSource* const source, const SerdNode* const name, const size_t page_size) { + assert(page_size > 0); memset(source, '\0', sizeof(*source)); source->read_func = read_func; source->error_func = error_func; @@ -100,6 +104,7 @@ serd_byte_source_open_string(SerdByteSource* const source, { memset(source, '\0', sizeof(*source)); + source->page_size = 1; source->name = name ? serd_node_copy(name) : serd_new_string(SERD_STRING("string")); diff --git a/src/byte_source.h b/src/byte_source.h index 6469ba95..1c9fbd1f 100644 --- a/src/byte_source.h +++ b/src/byte_source.h @@ -80,7 +80,8 @@ serd_byte_source_peek(SerdByteSource* source) static inline SerdStatus serd_byte_source_advance(SerdByteSource* source) { - SerdStatus st = SERD_SUCCESS; + SerdStatus st = SERD_SUCCESS; + const bool was_eof = source->eof; switch (serd_byte_source_peek(source)) { case '\n': @@ -91,27 +92,12 @@ serd_byte_source_advance(SerdByteSource* source) ++source->caret.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; - } + if (++source->read_head >= source->buf_size) { + st = serd_byte_source_page(source); } } else if (!source->eof) { - ++source->read_head; // Move to next character in string - if (source->read_buf[source->read_head] == '\0') { - source->eof = true; - } + source->eof = source->read_buf[++source->read_head] == '\0'; } return (was_eof && source->eof) ? SERD_FAILURE : st; diff --git a/src/reader.c b/src/reader.c index f0b81e7c..0f720d9b 100644 --- a/src/reader.c +++ b/src/reader.c @@ -327,11 +327,6 @@ serd_reader_read_chunk(SerdReader* const reader) st = serd_byte_source_advance(&reader->source); } - if (peek_byte(reader) == 0) { - // Skip leading null byte, for reading from a null-delimited socket - eat_byte_safe(reader, 0); - } - return st ? st : read_statement(reader); } |