diff options
Diffstat (limited to 'src/byte_source.c')
-rw-r--r-- | src/byte_source.c | 107 |
1 files changed, 54 insertions, 53 deletions
diff --git a/src/byte_source.c b/src/byte_source.c index a62f7a47..1b461fd0 100644 --- a/src/byte_source.c +++ b/src/byte_source.c @@ -27,23 +27,23 @@ SerdStatus serd_byte_source_page(SerdByteSource* source) { - source->read_head = 0; - 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); - } - - if (n_read < source->page_size) { - source->file_buf[n_read] = '\0'; - source->buf_size = n_read; - } - - return SERD_SUCCESS; + source->read_head = 0; + 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); + } + + if (n_read < source->page_size) { + source->file_buf[n_read] = '\0'; + source->buf_size = n_read; + } + + return SERD_SUCCESS; } SerdStatus @@ -54,58 +54,59 @@ serd_byte_source_open_source(SerdByteSource* source, 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->buf_size = page_size; - source->cur = cur; - source->error_func = error_func; - source->read_func = read_func; - - if (page_size > 1) { - source->file_buf = (uint8_t*)serd_allocate_buffer(page_size); - source->read_buf = source->file_buf; - memset(source->file_buf, '\0', page_size); - } else { - source->read_buf = &source->read_byte; - } - - return SERD_SUCCESS; + const Cursor cur = {name, 1, 1}; + + memset(source, '\0', sizeof(*source)); + source->stream = stream; + source->from_stream = true; + source->page_size = page_size; + source->buf_size = page_size; + source->cur = cur; + source->error_func = error_func; + source->read_func = read_func; + + if (page_size > 1) { + source->file_buf = (uint8_t*)serd_allocate_buffer(page_size); + source->read_buf = source->file_buf; + memset(source->file_buf, '\0', page_size); + } else { + source->read_buf = &source->read_byte; + } + + return SERD_SUCCESS; } SerdStatus serd_byte_source_prepare(SerdByteSource* source) { - source->prepared = true; + source->prepared = true; - if (source->from_stream) { - return (source->page_size > 1 ? serd_byte_source_page(source) - : serd_byte_source_advance(source)); - } + if (source->from_stream) { + return (source->page_size > 1 ? serd_byte_source_page(source) + : serd_byte_source_advance(source)); + } - return SERD_SUCCESS; + return SERD_SUCCESS; } SerdStatus serd_byte_source_open_string(SerdByteSource* source, const uint8_t* utf8) { - const Cursor cur = { (const uint8_t*)"(string)", 1, 1 }; + const Cursor cur = {(const uint8_t*)"(string)", 1, 1}; - memset(source, '\0', sizeof(*source)); - source->cur = cur; - source->read_buf = utf8; - return SERD_SUCCESS; + memset(source, '\0', sizeof(*source)); + source->cur = cur; + source->read_buf = utf8; + return SERD_SUCCESS; } SerdStatus serd_byte_source_close(SerdByteSource* source) { - if (source->page_size > 1) { - serd_free_aligned(source->file_buf); - } - memset(source, '\0', sizeof(*source)); - return SERD_SUCCESS; + if (source->page_size > 1) { + serd_free_aligned(source->file_buf); + } + + memset(source, '\0', sizeof(*source)); + return SERD_SUCCESS; } |