aboutsummaryrefslogtreecommitdiffstats
path: root/src/byte_source.h
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2020-06-28 23:26:48 +0200
committerDavid Robillard <d@drobilla.net>2023-12-02 18:49:08 -0500
commita083c64f506175029280ff76defa0ad7d7ae2ea0 (patch)
tree5e666749e352659d225d9c45c60bee06bd2bfe5c /src/byte_source.h
parent20eb7727954f9d8b7164146895904bbe595f5932 (diff)
downloadserd-a083c64f506175029280ff76defa0ad7d7ae2ea0.tar.gz
serd-a083c64f506175029280ff76defa0ad7d7ae2ea0.tar.bz2
serd-a083c64f506175029280ff76defa0ad7d7ae2ea0.zip
Simplify input stream API
Diffstat (limited to 'src/byte_source.h')
-rw-r--r--src/byte_source.h61
1 files changed, 22 insertions, 39 deletions
diff --git a/src/byte_source.h b/src/byte_source.h
index d40012ea..3a16a7c6 100644
--- a/src/byte_source.h
+++ b/src/byte_source.h
@@ -7,9 +7,9 @@
#include "caret.h" // IWYU pragma: keep
#include "serd/caret.h"
+#include "serd/input_stream.h"
#include "serd/node.h"
#include "serd/status.h"
-#include "serd/stream.h"
#include "zix/attributes.h"
#include <assert.h>
@@ -17,42 +17,27 @@
#include <stddef.h>
#include <stdint.h>
-typedef int (*SerdStreamCloseFunc)(void*);
-
typedef struct {
- SerdReadFunc read_func; ///< Read function (e.g. fread)
- SerdStreamErrorFunc error_func; ///< Error function (e.g. ferror)
- SerdStreamCloseFunc close_func; ///< Function for closing stream
- void* stream; ///< Stream (e.g. FILE)
- size_t page_size; ///< Number of bytes to read at a time
- size_t buf_size; ///< Number of bytes in file_buf
- SerdNode* name; ///< Name of stream (referenced by cur)
- SerdCaret caret; ///< Caret for error reporting
- uint8_t* file_buf; ///< Buffer iff reading pages from a file
- const uint8_t* read_buf; ///< Pointer to file_buf or read_byte
- size_t read_head; ///< Offset into read_buf
- uint8_t read_byte; ///< 1-byte 'buffer' used when not paging
- bool from_stream; ///< True iff reading from `stream`
- bool prepared; ///< True iff prepared for reading
- bool eof; ///< True iff end of file reached
+ SerdInputStream* in; ///< Input stream to read from
+ size_t block_size; ///< Number of bytes to read at a time
+ size_t buf_size; ///< Number of bytes in block
+ SerdNode* name; ///< Name of stream (for caret)
+ SerdCaret caret; ///< File position for error reporting
+ uint8_t* block; ///< Buffer if reading blocks
+ const uint8_t* read_buf; ///< Pointer to block or read_byte
+ size_t read_head; ///< Offset into read_buf
+ uint8_t read_byte; ///< 1-byte 'buffer' if reading bytes
+ bool prepared; ///< True iff prepared for reading
+ bool eof; ///< True iff end of file reached
} SerdByteSource;
-SerdStatus
-serd_byte_source_open_string(SerdByteSource* source,
- const char* utf8,
- const SerdNode* name);
-
-SerdStatus
-serd_byte_source_open_source(SerdByteSource* source,
- SerdReadFunc read_func,
- SerdStreamErrorFunc error_func,
- SerdStreamCloseFunc close_func,
- void* stream,
- const SerdNode* name,
- size_t page_size);
+SerdByteSource*
+serd_byte_source_new_input(SerdInputStream* input,
+ const SerdNode* name,
+ size_t block_size);
-SerdStatus
-serd_byte_source_close(SerdByteSource* source);
+void
+serd_byte_source_free(SerdByteSource* source);
SerdStatus
serd_byte_source_prepare(SerdByteSource* source);
@@ -74,6 +59,8 @@ serd_byte_source_advance(SerdByteSource* source)
const bool was_eof = source->eof;
switch (serd_byte_source_peek(source)) {
+ case '\0':
+ break;
case '\n':
++source->caret.line;
source->caret.col = 0;
@@ -82,12 +69,8 @@ serd_byte_source_advance(SerdByteSource* source)
++source->caret.col;
}
- if (source->from_stream) {
- if (++source->read_head >= source->buf_size) {
- st = serd_byte_source_page(source);
- }
- } else if (!source->eof) {
- source->eof = source->read_buf[++source->read_head] == '\0';
+ if (++source->read_head >= source->buf_size) {
+ st = serd_byte_source_page(source);
}
return (was_eof && source->eof) ? SERD_FAILURE : st;