diff options
Diffstat (limited to 'src/byte_source.c')
-rw-r--r-- | src/byte_source.c | 89 |
1 files changed, 43 insertions, 46 deletions
diff --git a/src/byte_source.c b/src/byte_source.c index 8959b1e6..0dcb7615 100644 --- a/src/byte_source.c +++ b/src/byte_source.c @@ -38,28 +38,36 @@ serd_byte_source_page(SerdByteSource* const source) return SERD_SUCCESS; } -static void +static SerdStatus serd_byte_source_init_buffer(ZixAllocator* const allocator, SerdByteSource* const source) { if (source->block_size > 1) { - source->block = (uint8_t*)zix_aligned_alloc( - allocator, SERD_PAGE_SIZE, source->block_size); + void* const block = + zix_aligned_alloc(allocator, SERD_PAGE_SIZE, source->block_size); - if ((source->read_buf = source->block)) { - memset(source->block, '\0', source->block_size); + if (!block) { + return SERD_BAD_ALLOC; } + + source->block = (uint8_t*)block; + source->read_buf = source->block; + memset(source->block, '\0', source->block_size); } else { source->read_buf = &source->read_byte; } + + return SERD_SUCCESS; } -SerdByteSource* -serd_byte_source_new_input(ZixAllocator* const allocator, - SerdInputStream* const input, - const SerdNode* const name, - const size_t block_size) +SerdStatus +serd_byte_source_init(ZixAllocator* const allocator, + SerdByteSource* const source, + SerdInputStream* const input, + const SerdNode* const name, + const size_t block_size) { + assert(source); assert(input); assert(block_size); assert(input->stream); @@ -69,70 +77,59 @@ serd_byte_source_new_input(ZixAllocator* const allocator, : serd_node_new(allocator, serd_a_string("input")); if (!source_name) { - return NULL; - } - - SerdByteSource* source = - (SerdByteSource*)zix_calloc(allocator, 1, sizeof(SerdByteSource)); - - if (!source) { - serd_node_free(allocator, source_name); - return NULL; + return SERD_BAD_ALLOC; } - source->name = source_name; source->in = input; - source->block_size = block_size; - source->buf_size = block_size; - source->caret.document = source->name; + source->read_buf = NULL; + source->read_head = 0U; + source->block_size = (uint32_t)block_size; + source->buf_size = (uint32_t)block_size; + source->caret.document = source_name; source->caret.line = 1U; source->caret.col = 1U; + source->name = source_name; + source->block = NULL; + source->read_byte = 0U; + source->prepared = false; - serd_byte_source_init_buffer(allocator, source); - if (block_size > 1 && !source->block) { + if (serd_byte_source_init_buffer(allocator, source)) { serd_node_free(allocator, source_name); - zix_free(allocator, source); - return NULL; + memset(source, 0, sizeof(SerdByteSource)); + return SERD_BAD_ALLOC; } - return source; + return SERD_SUCCESS; } void -serd_byte_source_free(ZixAllocator* const allocator, - SerdByteSource* const source) +serd_byte_source_destroy(ZixAllocator* const allocator, + SerdByteSource* const source) { - if (source) { - if (source->block_size > 1) { - zix_aligned_free(allocator, source->block); - } - - serd_node_free(allocator, source->name); - zix_free(allocator, source); + if (source->block_size > 1) { + zix_aligned_free(allocator, source->block); } + + serd_node_free(allocator, source->name); + memset(source, 0, sizeof(SerdByteSource)); } SerdStatus serd_byte_source_prepare(SerdByteSource* const source) { source->prepared = true; - - if (source->block_size > 1) { - return serd_byte_source_page(source); - } - - return serd_byte_source_advance(source); + return serd_byte_source_page(source); } SerdStatus serd_byte_source_skip_bom(SerdByteSource* const source) { if (serd_byte_source_peek(source) == 0xEF) { - if (serd_byte_source_advance(source) || + if (serd_byte_source_advance_past(source, 0xEF) || serd_byte_source_peek(source) != 0xBB || - serd_byte_source_advance(source) || + serd_byte_source_advance_past(source, 0xBB) || serd_byte_source_peek(source) != 0xBF || - serd_byte_source_advance(source)) { + serd_byte_source_advance_past(source, 0xBF)) { return SERD_BAD_SYNTAX; } } |