diff options
author | David Robillard <d@drobilla.net> | 2019-12-19 13:16:56 -0500 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2020-10-27 13:13:59 +0100 |
commit | 641158b887c02bfda17702c6ff6c2dba4e53f466 (patch) | |
tree | 2c7cf9f2b2b993d01826199ee883eeeb99f13eea | |
parent | a1e6f53715ba7be340d4a59a9878d0ca2c7c6c3f (diff) | |
download | serd-641158b887c02bfda17702c6ff6c2dba4e53f466.tar.gz serd-641158b887c02bfda17702c6ff6c2dba4e53f466.tar.bz2 serd-641158b887c02bfda17702c6ff6c2dba4e53f466.zip |
Simplify ByteSource implementation
-rw-r--r-- | src/byte_source.c | 34 | ||||
-rw-r--r-- | src/byte_source.h | 25 |
2 files changed, 27 insertions, 32 deletions
diff --git a/src/byte_source.c b/src/byte_source.c index eac74c63..0b233bc9 100644 --- a/src/byte_source.c +++ b/src/byte_source.c @@ -28,18 +28,26 @@ SerdStatus serd_byte_source_page(SerdByteSource* 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); - if (n_read == 0) { - source->file_buf[0] = '\0'; - source->eof = true; - return (source->error_func(source->stream) - ? SERD_ERR_UNKNOWN : SERD_FAILURE); - } else if (n_read < source->page_size) { - source->file_buf[n_read] = '\0'; - source->buf_size = n_read; + buf, 1, source->page_size, source->stream); + + source->buf_size = n_read; + source->read_head = 0; + source->eof = false; + + if (n_read < source->page_size) { + 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; } @@ -52,6 +60,7 @@ serd_byte_source_open_source(SerdByteSource* source, const SerdNode* name, size_t page_size) { + assert(page_size > 0); memset(source, '\0', sizeof(*source)); source->read_func = read_func; source->error_func = error_func; @@ -97,8 +106,9 @@ serd_byte_source_open_string(SerdByteSource* source, { memset(source, '\0', sizeof(*source)); - source->name = name ? serd_node_copy(name) : serd_new_string("string"); - source->read_buf = (const uint8_t*)utf8; + source->page_size = 1; + source->name = name ? serd_node_copy(name) : serd_new_string("string"); + source->read_buf = (const uint8_t*)utf8; const SerdCursor cur = {source->name, 1, 1}; source->cur = cur; diff --git a/src/byte_source.h b/src/byte_source.h index d0a73fb9..acda0812 100644 --- a/src/byte_source.h +++ b/src/byte_source.h @@ -79,35 +79,20 @@ 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': ++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 { - source->eof = false; - 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; |