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.c34
1 files changed, 22 insertions, 12 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;