LMMS
Loading...
Searching...
No Matches
reader.h
Go to the documentation of this file.
1/*
2 Copyright 2011-2020 David Robillard <d@drobilla.net>
3
4 Permission to use, copy, modify, and/or distribute this software for any
5 purpose with or without fee is hereby granted, provided that the above
6 copyright notice and this permission notice appear in all copies.
7
8 THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15*/
16
17#ifndef SERD_READER_H
18#define SERD_READER_H
19
20#include "byte_source.h"
21#include "stack.h"
22
23#include "serd/serd.h"
24
25#include <assert.h>
26#include <stdbool.h>
27#include <stdint.h>
28#include <stdio.h>
29
30#if defined(__GNUC__)
31# define SERD_LOG_FUNC(fmt, arg1) __attribute__((format(printf, fmt, arg1)))
32#else
33# define SERD_LOG_FUNC(fmt, arg1)
34#endif
35
36#ifdef SERD_STACK_CHECK
37# define SERD_STACK_ASSERT_TOP(reader, ref) \
38 assert(ref == reader->allocs[reader->n_allocs - 1]);
39#else
40# define SERD_STACK_ASSERT_TOP(reader, ref)
41#endif
42
43/* Reference to a node in the stack (we can not use pointers since the
44 stack may be reallocated, invalidating any pointers to elements).
45*/
46typedef size_t Ref;
47
57
85
86SERD_LOG_FUNC(3, 4)
88r_err(SerdReader* reader, SerdStatus st, const char* fmt, ...);
89
90Ref
92 size_t maxlen,
94 const char* str,
95 size_t n_bytes);
96
97Ref
98push_node(SerdReader* reader, SerdType type, const char* str, size_t n_bytes);
99
100SERD_PURE_FUNC size_t
101genid_size(SerdReader* reader);
102
103Ref
104blank_id(SerdReader* reader);
105
106void
107set_blank_id(SerdReader* reader, Ref ref, size_t buf_size);
108
110deref(SerdReader* reader, Ref ref);
111
112Ref
113pop_node(SerdReader* reader, Ref ref);
114
116emit_statement(SerdReader* reader, ReadContext ctx, Ref o, Ref d, Ref l);
117
120
123
126
127static inline int
129{
130 SerdByteSource* source = &reader->source;
131
132 return source->eof ? EOF : (int)source->read_buf[source->read_head];
133}
134
135static inline int
136eat_byte_safe(SerdReader* reader, const int byte)
137{
138 (void)byte;
139
140 const int c = peek_byte(reader);
141 assert(c == byte);
142
144 return c;
145}
146
147static inline int
148eat_byte_check(SerdReader* reader, const int byte)
149{
150 const int c = peek_byte(reader);
151 if (c != byte) {
152 r_err(reader, SERD_ERR_BAD_SYNTAX, "expected `%c', not `%c'\n", byte, c);
153 return 0;
154 }
155 return eat_byte_safe(reader, byte);
156}
157
158static inline SerdStatus
159eat_string(SerdReader* reader, const char* str, unsigned n)
160{
161 for (unsigned i = 0; i < n; ++i) {
162 if (!eat_byte_check(reader, ((const uint8_t*)str)[i])) {
163 return SERD_ERR_BAD_SYNTAX;
164 }
165 }
166 return SERD_SUCCESS;
167}
168
169static inline SerdStatus
170push_byte(SerdReader* reader, Ref ref, const int c)
171{
172 assert(c != EOF);
173 SERD_STACK_ASSERT_TOP(reader, ref);
174
175 uint8_t* const s = (uint8_t*)serd_stack_push(&reader->stack, 1);
176 SerdNode* const node = (SerdNode*)(reader->stack.buf + ref);
177
178 ++node->n_bytes;
179 if (!(c & 0x80)) { // Starts with 0 bit, start of new character
180 ++node->n_chars;
181 }
182
183 *(s - 1) = (uint8_t)c;
184 *s = '\0';
185 return SERD_SUCCESS;
186}
187
188static inline void
189push_bytes(SerdReader* reader, Ref ref, const uint8_t* bytes, unsigned len)
190{
191 for (unsigned i = 0; i < len; ++i) {
192 push_byte(reader, ref, bytes[i]);
193 }
194}
195
196#endif // SERD_READER_H
assert(0)
CAdPlugDatabase::CRecord::RecordType type
Definition adplugdb.cpp:93
static SerdStatus serd_byte_source_advance(SerdByteSource *source)
Definition byte_source.h:81
int * l
Definition inflate.c:1579
unsigned d
Definition inflate.c:940
register unsigned i
Definition inflate.c:1575
unsigned s
Definition inflate.c:1555
#define buf_size
SerdStatus(* SerdStatementSink)(void *SERD_NULLABLE handle, SerdStatementFlags flags, const SerdNode *SERD_NULLABLE graph, const SerdNode *SERD_NONNULL subject, const SerdNode *SERD_NONNULL predicate, const SerdNode *SERD_NONNULL object, const SerdNode *SERD_NULLABLE object_datatype, const SerdNode *SERD_NULLABLE object_lang)
Definition serd.h:632
SerdStatus(* SerdEndSink)(void *SERD_NULLABLE handle, const SerdNode *SERD_NONNULL node)
Definition serd.h:649
SerdStatus(* SerdPrefixSink)(void *SERD_NULLABLE handle, const SerdNode *SERD_NONNULL name, const SerdNode *SERD_NONNULL uri)
Definition serd.h:623
SerdStatus(* SerdErrorSink)(void *SERD_NULLABLE handle, const SerdError *SERD_NONNULL error)
Definition serd.h:607
SerdStatus(* SerdBaseSink)(void *SERD_NULLABLE handle, const SerdNode *SERD_NONNULL uri)
Definition serd.h:615
SerdStatus
Return status code.
Definition serd.h:100
SerdSyntax
RDF syntax type.
Definition serd.h:113
struct SerdReaderImpl SerdReader
Streaming parser that reads a text stream and writes to a statement sink.
Definition serd.h:94
uint32_t SerdStatementFlags
Bitwise OR of SerdStatementFlag values.
Definition serd.h:133
SerdType
Definition serd.h:146
@ SERD_ERR_BAD_SYNTAX
Invalid syntax.
Definition serd.h:104
@ SERD_SUCCESS
No error.
Definition serd.h:101
#define SERD_PURE_FUNC
Definition serd.h:42
SerdStatus r_err(SerdReader *reader, SerdStatus st, const char *fmt,...)
Definition reader.c:32
SerdNode * deref(SerdReader *reader, const Ref ref)
Definition reader.c:116
SerdStatus emit_statement(SerdReader *reader, ReadContext ctx, Ref o, Ref d, Ref l)
Definition reader.c:143
void set_blank_id(SerdReader *reader, Ref ref, size_t buf_size)
Definition reader.c:44
Ref push_node(SerdReader *reader, SerdType type, const char *str, size_t n_bytes)
Definition reader.c:110
Ref pop_node(SerdReader *reader, Ref ref)
Definition reader.c:127
Ref blank_id(SerdReader *reader)
Definition reader.c:59
Ref push_node_padded(SerdReader *reader, size_t maxlen, SerdType type, const char *str, size_t n_bytes)
Definition reader.c:83
size_t genid_size(SerdReader *reader)
Definition reader.c:53
unsigned char uint8_t
Definition mid.cpp:98
SerdStatus read_turtleTrigDoc(SerdReader *reader)
Definition n3.c:1651
SerdStatus read_nquadsDoc(SerdReader *reader)
Definition n3.c:1667
SerdStatus read_n3_statement(SerdReader *reader)
Definition n3.c:1563
SerdStatus r_err(SerdReader *reader, SerdStatus st, const char *fmt,...)
Definition reader.c:32
static int peek_byte(SerdReader *reader)
Definition reader.h:128
#define SERD_LOG_FUNC(fmt, arg1)
Definition reader.h:33
static int eat_byte_safe(SerdReader *reader, const int byte)
Definition reader.h:136
static int eat_byte_check(SerdReader *reader, const int byte)
Definition reader.h:148
#define SERD_STACK_ASSERT_TOP(reader, ref)
Definition reader.h:40
static SerdStatus push_byte(SerdReader *reader, Ref ref, const int c)
Definition reader.h:170
static SerdStatus eat_string(SerdReader *reader, const char *str, unsigned n)
Definition reader.h:159
static void push_bytes(SerdReader *reader, Ref ref, const uint8_t *bytes, unsigned len)
Definition reader.h:189
size_t Ref
Definition reader.h:46
static void * serd_stack_push(SerdStack *stack, size_t n_bytes)
Definition stack.h:65
Definition reader.h:48
Ref object
Definition reader.h:52
SerdStatementFlags * flags
Definition reader.h:55
Ref graph
Definition reader.h:49
Ref datatype
Definition reader.h:53
Ref lang
Definition reader.h:54
Ref subject
Definition reader.h:50
Ref predicate
Definition reader.h:51
Definition byte_source.h:34
size_t read_head
Offset into read_buf.
Definition byte_source.h:43
const uint8_t * read_buf
Pointer to file_buf or read_byte.
Definition byte_source.h:42
bool eof
True iff end of file reached.
Definition byte_source.h:47
A syntactic RDF node.
Definition serd.h:199
size_t n_bytes
Size in bytes (excluding null).
Definition serd.h:201
size_t n_chars
String length (excluding null).
Definition serd.h:202
Definition reader.h:58
Ref rdf_first
Definition reader.h:67
void(* free_handle)(void *ptr)
Definition reader.h:60
SerdPrefixSink prefix_sink
Definition reader.h:62
SerdErrorSink error_sink
Definition reader.h:65
Ref rdf_rest
Definition reader.h:68
uint8_t * bprefix
Definition reader.h:76
size_t bprefix_len
Definition reader.h:77
SerdByteSource source
Definition reader.h:71
void * error_handle
Definition reader.h:66
bool seen_genid
Definition reader.h:79
SerdStack stack
Definition reader.h:72
Ref rdf_nil
Definition reader.h:69
SerdNode default_graph
Definition reader.h:70
SerdStatementSink statement_sink
Definition reader.h:63
bool strict
True iff strict parsing.
Definition reader.h:78
uint8_t * buf
Definition reader.h:75
SerdEndSink end_sink
Definition reader.h:64
unsigned next_id
Definition reader.h:74
SerdBaseSink base_sink
Definition reader.h:61
void * handle
Definition reader.h:59
SerdSyntax syntax
Definition reader.h:73
Definition stack.h:30
uint8_t * buf
Stack memory.
Definition stack.h:31
int n
Definition crypt.c:458
return c
Definition crypt.c:175
fmt[0]
Definition fileio.c:2503
typedef int(UZ_EXP MsgFn)()
#define void
Definition unzip.h:396
#define const
Definition zconf.h:137