aboutsummaryrefslogtreecommitdiffstats
path: root/src/byte_source.h
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-04-29 14:07:29 +0200
committerDavid Robillard <d@drobilla.net>2018-05-27 18:21:57 +0200
commitaa41376304f135bfc54d2b5c16fa8ecd7302ad24 (patch)
tree76f1eba1a5f7dd5ee1e56c74840b8babc4a0e474 /src/byte_source.h
parent9252a3f52012e6199b7a27b5c329c226614cc127 (diff)
downloadserd-aa41376304f135bfc54d2b5c16fa8ecd7302ad24.tar.gz
serd-aa41376304f135bfc54d2b5c16fa8ecd7302ad24.tar.bz2
serd-aa41376304f135bfc54d2b5c16fa8ecd7302ad24.zip
Clean up and separate internal headers
Diffstat (limited to 'src/byte_source.h')
-rw-r--r--src/byte_source.h79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/byte_source.h b/src/byte_source.h
new file mode 100644
index 00000000..c6c4702e
--- /dev/null
+++ b/src/byte_source.h
@@ -0,0 +1,79 @@
+/*
+ Copyright 2011-2018 David Robillard <http://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
+ copyright notice and this permission notice appear in all copies.
+
+ THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+
+#ifndef SERD_BYTE_SOURCE_H
+#define SERD_BYTE_SOURCE_H
+
+#include "serd_internal.h"
+
+#include <assert.h>
+#include <stddef.h>
+#include <stdio.h>
+
+typedef struct {
+ const char* filename;
+ unsigned line;
+ unsigned col;
+} Cursor;
+
+typedef struct {
+ SerdSource read_func; ///< Read function (e.g. fread)
+ SerdStreamErrorFunc error_func; ///< Error function (e.g. ferror)
+ void* stream; ///< Stream (e.g. FILE)
+ size_t page_size; ///< Number of bytes to read at a time
+ Cursor cur; ///< Cursor for error reporting
+ char* file_buf; ///< Buffer iff reading pages from a file
+ const char* read_buf; ///< Pointer to file_buf or read_byte
+ size_t read_head; ///< Offset into read_buf
+ char 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
+} SerdByteSource;
+
+SerdStatus
+serd_byte_source_open_file(SerdByteSource* source,
+ FILE* file,
+ bool bulk);
+
+SerdStatus
+serd_byte_source_open_string(SerdByteSource* source, const char* utf8);
+
+SerdStatus
+serd_byte_source_open_source(SerdByteSource* source,
+ SerdSource read_func,
+ SerdStreamErrorFunc error_func,
+ void* stream,
+ const char* name,
+ size_t page_size);
+
+SerdStatus
+serd_byte_source_close(SerdByteSource* source);
+
+SerdStatus
+serd_byte_source_prepare(SerdByteSource* source);
+
+static inline uint8_t
+serd_byte_source_peek(SerdByteSource* source)
+{
+ assert(source->prepared);
+ return source->read_buf[source->read_head];
+}
+
+SerdStatus
+serd_byte_source_advance(SerdByteSource* source);
+
+#endif // SERD_BYTE_SOURCE_H