aboutsummaryrefslogtreecommitdiffstats
path: root/src/byte_source.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/byte_source.c')
-rw-r--r--src/byte_source.c115
1 files changed, 45 insertions, 70 deletions
diff --git a/src/byte_source.c b/src/byte_source.c
index 7a839875..e4810b60 100644
--- a/src/byte_source.c
+++ b/src/byte_source.c
@@ -11,120 +11,95 @@
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
+#include <stdlib.h>
#include <string.h>
SerdStatus
serd_byte_source_page(SerdByteSource* const source)
{
uint8_t* const buf =
- (source->page_size > 1 ? source->file_buf : &source->read_byte);
+ (source->block_size > 1 ? source->block : &source->read_byte);
const size_t n_read =
- source->read_func(buf, 1, source->page_size, source->stream);
+ source->in->read(buf, 1, source->block_size, source->in->stream);
source->buf_size = n_read;
source->read_head = 0;
source->eof = false;
- if (n_read < source->page_size) {
+ if (n_read < source->block_size) {
buf[n_read] = '\0';
if (n_read == 0) {
source->eof = true;
- return (source->error_func(source->stream) ? SERD_BAD_STREAM
- : SERD_FAILURE);
+ return (source->in->error(source->in->stream) ? SERD_BAD_STREAM
+ : SERD_FAILURE);
}
}
return SERD_SUCCESS;
}
-SerdStatus
-serd_byte_source_open_source(SerdByteSource* const source,
- const SerdReadFunc read_func,
- const SerdStreamErrorFunc error_func,
- const SerdStreamCloseFunc close_func,
- void* const stream,
- const SerdNode* const name,
- const size_t page_size)
+static void
+serd_byte_source_init_buffer(SerdByteSource* const source)
{
- assert(read_func);
- assert(error_func);
- assert(page_size > 0);
-
- memset(source, '\0', sizeof(*source));
- source->read_func = read_func;
- source->error_func = error_func;
- source->close_func = close_func;
- source->stream = stream;
- source->page_size = page_size;
- source->buf_size = page_size;
- source->name = serd_node_copy(name);
- source->caret.document = source->name;
- source->caret.line = 1U;
- source->caret.col = 1U;
- source->from_stream = true;
-
- 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);
+ if (source->block_size > 1) {
+ source->block = (uint8_t*)serd_allocate_buffer(source->block_size);
+ source->read_buf = source->block;
+ memset(source->block, '\0', source->block_size);
} else {
source->read_buf = &source->read_byte;
}
-
- return SERD_SUCCESS;
}
-SerdStatus
-serd_byte_source_prepare(SerdByteSource* const source)
+SerdByteSource*
+serd_byte_source_new_input(SerdInputStream* const input,
+ const SerdNode* const name,
+ const size_t block_size)
{
- if (source->page_size == 0) {
- return SERD_FAILURE;
- }
+ assert(input);
- source->prepared = true;
-
- if (source->from_stream) {
- return (source->page_size > 1 ? serd_byte_source_page(source)
- : serd_byte_source_advance(source));
+ if (!block_size || !input->stream) {
+ return NULL;
}
- return SERD_SUCCESS;
-}
-
-SerdStatus
-serd_byte_source_open_string(SerdByteSource* const source,
- const char* const utf8,
- const SerdNode* const name)
-{
- memset(source, '\0', sizeof(*source));
+ SerdByteSource* source = (SerdByteSource*)calloc(1, sizeof(SerdByteSource));
- source->page_size = 1;
source->name =
- name ? serd_node_copy(name) : serd_new_string(serd_string("string"));
+ name ? serd_node_copy(name) : serd_new_string(serd_string("input"));
- source->page_size = 1U;
- source->read_buf = (const uint8_t*)utf8;
+ source->in = input;
+ source->block_size = block_size;
+ source->buf_size = block_size;
source->caret.document = source->name;
source->caret.line = 1U;
source->caret.col = 1U;
- return SERD_SUCCESS;
+ serd_byte_source_init_buffer(source);
+
+ return source;
}
-SerdStatus
-serd_byte_source_close(SerdByteSource* const source)
+void
+serd_byte_source_free(SerdByteSource* const source)
{
- SerdStatus st = SERD_SUCCESS;
- if (source->close_func) {
- st = source->close_func(source->stream) ? SERD_BAD_STREAM : SERD_SUCCESS;
+ if (source) {
+ if (source->block_size > 1) {
+ serd_free_aligned(source->block);
+ }
+
+ serd_node_free(source->name);
+ free(source);
}
+}
+
+SerdStatus
+serd_byte_source_prepare(SerdByteSource* const source)
+{
+ source->prepared = true;
- if (source->page_size > 1) {
- serd_free_aligned(source->file_buf);
+ if (source->block_size > 1) {
+ return serd_byte_source_page(source);
}
- serd_node_free(source->name);
- memset(source, '\0', sizeof(*source));
- return st;
+ return serd_byte_source_advance(source);
}