diff options
Diffstat (limited to 'src/byte_source.h')
-rw-r--r-- | src/byte_source.h | 56 |
1 files changed, 26 insertions, 30 deletions
diff --git a/src/byte_source.h b/src/byte_source.h index d054e156..2bd06bdf 100644 --- a/src/byte_source.h +++ b/src/byte_source.h @@ -1,5 +1,5 @@ /* - Copyright 2011-2020 David Robillard <d@drobilla.net> + Copyright 2011-2021 David Robillard <d@drobilla.net> Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above @@ -26,29 +26,27 @@ #include <stddef.h> #include <stdint.h> -typedef enum { - FROM_STRING, ///< Reading from a user-provided buffer - FROM_FILENAME, ///< Reading from a file we opened - FROM_FUNCTION, ///< Reading from a user-provided function -} SerdByteSourceType; - -struct SerdByteSourceImpl { - 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; ///< File position 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 - SerdByteSourceType type; ///< Type of input - uint8_t read_byte; ///< 1-byte 'buffer' used when not paging - bool prepared; ///< True iff prepared for reading - bool eof; ///< True iff end of file reached -}; +typedef struct { + 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; + +SerdByteSource* +serd_byte_source_new_input(SerdInputStream* input, + const SerdNode* name, + size_t block_size); + +void +serd_byte_source_free(SerdByteSource* source); SerdStatus serd_byte_source_prepare(SerdByteSource* source); @@ -71,6 +69,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; @@ -79,12 +79,8 @@ serd_byte_source_advance(SerdByteSource* source) ++source->caret.col; } - if (source->type != FROM_STRING) { - 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; |