comparison win64-msvc/include/serd/serd.h @ 130:1c067f014d80

64-bit MSVC builds
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 18 Oct 2016 15:59:23 +0100
parents
children
comparison
equal deleted inserted replaced
129:90a976269628 130:1c067f014d80
1 /*
2 Copyright 2011-2016 David Robillard <http://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 /**
18 @file serd.h API for Serd, a lightweight RDF syntax library.
19 */
20
21 #ifndef SERD_SERD_H
22 #define SERD_SERD_H
23
24 #include <stdarg.h>
25 #include <stddef.h>
26 #include <stdint.h>
27 #include <stdio.h>
28
29 #ifdef SERD_SHARED
30 # ifdef _WIN32
31 # define SERD_LIB_IMPORT __declspec(dllimport)
32 # define SERD_LIB_EXPORT __declspec(dllexport)
33 # else
34 # define SERD_LIB_IMPORT __attribute__((visibility("default")))
35 # define SERD_LIB_EXPORT __attribute__((visibility("default")))
36 # endif
37 # ifdef SERD_INTERNAL
38 # define SERD_API SERD_LIB_EXPORT
39 # else
40 # define SERD_API SERD_LIB_IMPORT
41 # endif
42 #else
43 # define SERD_API
44 #endif
45
46 #ifdef __cplusplus
47 extern "C" {
48 #else
49 # include <stdbool.h>
50 #endif
51
52 /**
53 @defgroup serd Serd
54 A lightweight RDF syntax library.
55 @{
56 */
57
58 /**
59 Environment.
60
61 Represents the state required to resolve a CURIE or relative URI, e.g. the
62 base URI and set of namespace prefixes at a particular point.
63 */
64 typedef struct SerdEnvImpl SerdEnv;
65
66 /**
67 RDF reader.
68
69 Parses RDF by calling user-provided sink functions as input is consumed
70 (much like an XML SAX parser).
71 */
72 typedef struct SerdReaderImpl SerdReader;
73
74 /**
75 RDF writer.
76
77 Provides a number of functions to allow writing RDF syntax out to some
78 stream. These functions are deliberately compatible with the sink functions
79 used by SerdReader, so a reader can be directly connected to a writer to
80 re-serialise a document with minimal overhead.
81 */
82 typedef struct SerdWriterImpl SerdWriter;
83
84 /**
85 Return status code.
86 */
87 typedef enum {
88 SERD_SUCCESS, /**< No error */
89 SERD_FAILURE, /**< Non-fatal failure */
90 SERD_ERR_UNKNOWN, /**< Unknown error */
91 SERD_ERR_BAD_SYNTAX, /**< Invalid syntax */
92 SERD_ERR_BAD_ARG, /**< Invalid argument */
93 SERD_ERR_NOT_FOUND, /**< Not found */
94 SERD_ERR_ID_CLASH, /**< Encountered clashing blank node IDs */
95 SERD_ERR_BAD_CURIE, /**< Invalid CURIE (e.g. prefix does not exist) */
96 SERD_ERR_INTERNAL /**< Unexpected internal error (should not happen) */
97 } SerdStatus;
98
99 /**
100 RDF syntax type.
101 */
102 typedef enum {
103 /**
104 Turtle - Terse RDF Triple Language (UTF-8).
105 @see <a href="http://www.w3.org/TeamSubmission/turtle/">Turtle</a>
106 */
107 SERD_TURTLE = 1,
108
109 /**
110 NTriples - Line-based RDF triples (ASCII).
111 @see <a href="http://www.w3.org/TR/rdf-testcases#ntriples">NTriples</a>
112 */
113 SERD_NTRIPLES = 2
114 } SerdSyntax;
115
116 /**
117 Flags indication inline abbreviation information for a statement.
118 */
119 typedef enum {
120 SERD_EMPTY_S = 1 << 1, /**< Empty blank node subject */
121 SERD_EMPTY_O = 1 << 2, /**< Empty blank node object */
122 SERD_ANON_S_BEGIN = 1 << 3, /**< Start of anonymous subject */
123 SERD_ANON_O_BEGIN = 1 << 4, /**< Start of anonymous object */
124 SERD_ANON_CONT = 1 << 5, /**< Continuation of anonymous node */
125 SERD_LIST_S_BEGIN = 1 << 6, /**< Start of list subject */
126 SERD_LIST_O_BEGIN = 1 << 7, /**< Start of list object */
127 SERD_LIST_CONT = 1 << 8 /**< Continuation of list */
128 } SerdStatementFlag;
129
130 /**
131 Bitwise OR of SerdNodeFlag values.
132 */
133 typedef uint32_t SerdStatementFlags;
134
135 /**
136 Type of a syntactic RDF node.
137
138 This is more precise than the type of an abstract RDF node. An abstract
139 node is either a resource, literal, or blank. In syntax there are two ways
140 to refer to a resource (by URI or CURIE) and two ways to refer to a blank
141 (by ID or anonymously). Anonymous (inline) blank nodes are expressed using
142 SerdStatementFlags rather than this type.
143 */
144 typedef enum {
145 /**
146 The type of a nonexistent node.
147
148 This type is useful as a sentinel, but is never emitted by the reader.
149 */
150 SERD_NOTHING = 0,
151
152 /**
153 Literal value.
154
155 A literal optionally has either a language, or a datatype (not both).
156 */
157 SERD_LITERAL = 1,
158
159 /**
160 URI (absolute or relative).
161
162 Value is an unquoted URI string, which is either a relative reference
163 with respect to the current base URI (e.g. "foo/bar"), or an absolute
164 URI (e.g. "http://example.org/foo").
165 @see <a href="http://tools.ietf.org/html/rfc3986">RFC3986</a>.
166 */
167 SERD_URI = 2,
168
169 /**
170 CURIE, a shortened URI.
171
172 Value is an unquoted CURIE string relative to the current environment,
173 e.g. "rdf:type".
174 @see <a href="http://www.w3.org/TR/curie">CURIE Syntax 1.0</a>
175 */
176 SERD_CURIE = 3,
177
178 /**
179 A blank node.
180
181 Value is a blank node ID, e.g. "id3", which is meaningful only within
182 this serialisation.
183 @see <a href="http://www.w3.org/TeamSubmission/turtle#nodeID">Turtle
184 <tt>nodeID</tt></a>
185 */
186 SERD_BLANK = 4
187
188 } SerdType;
189
190 /**
191 Flags indicating certain string properties relevant to serialisation.
192 */
193 typedef enum {
194 SERD_HAS_NEWLINE = 1, /**< Contains line breaks ('\\n' or '\\r') */
195 SERD_HAS_QUOTE = 1 << 1 /**< Contains quotes ('"') */
196 } SerdNodeFlag;
197
198 /**
199 Bitwise OR of SerdNodeFlag values.
200 */
201 typedef uint32_t SerdNodeFlags;
202
203 /**
204 A syntactic RDF node.
205 */
206 typedef struct {
207 const uint8_t* buf; /**< Value string */
208 size_t n_bytes; /**< Size in bytes (not including null) */
209 size_t n_chars; /**< Length in characters (not including null)*/
210 SerdNodeFlags flags; /**< Node flags (e.g. string properties) */
211 SerdType type; /**< Node type */
212 } SerdNode;
213
214 /**
215 An unterminated string fragment.
216 */
217 typedef struct {
218 const uint8_t* buf; /**< Start of chunk */
219 size_t len; /**< Length of chunk in bytes */
220 } SerdChunk;
221
222 /**
223 An error description.
224 */
225 typedef struct {
226 SerdStatus status; /**< Error code */
227 const uint8_t* filename; /**< File where error was encountered, or NULL */
228 unsigned line; /**< Line where error was encountered, or 0 */
229 unsigned col; /**< Column where error was encountered */
230 const char* fmt; /**< Message format string (printf style) */
231 va_list* args; /**< Arguments for fmt */
232 } SerdError;
233
234 /**
235 A parsed URI.
236
237 This struct directly refers to chunks in other strings, it does not own any
238 memory itself. Thus, URIs can be parsed and/or resolved against a base URI
239 in-place without allocating memory.
240 */
241 typedef struct {
242 SerdChunk scheme; /**< Scheme */
243 SerdChunk authority; /**< Authority */
244 SerdChunk path_base; /**< Path prefix if relative */
245 SerdChunk path; /**< Path suffix */
246 SerdChunk query; /**< Query */
247 SerdChunk fragment; /**< Fragment */
248 } SerdURI;
249
250 /**
251 Syntax style options.
252
253 The style of the writer output can be controlled by ORing together
254 values from this enumeration. Note that some options are only supported
255 for some syntaxes (e.g. NTriples does not support abbreviation and is
256 always ASCII).
257 */
258 typedef enum {
259 SERD_STYLE_ABBREVIATED = 1, /**< Abbreviate triples when possible. */
260 SERD_STYLE_ASCII = 1 << 1, /**< Escape all non-ASCII characters. */
261 SERD_STYLE_RESOLVED = 1 << 2, /**< Resolve URIs against base URI. */
262 SERD_STYLE_CURIED = 1 << 3, /**< Shorten URIs into CURIEs. */
263 SERD_STYLE_BULK = 1 << 4 /**< Write output in pages. */
264 } SerdStyle;
265
266 /**
267 @name String Utilities
268 @{
269 */
270
271 /**
272 Return a string describing a status code.
273 */
274 SERD_API
275 const uint8_t*
276 serd_strerror(SerdStatus status);
277
278 /**
279 Measure a UTF-8 string.
280 @return Length of `str` in characters (except NULL).
281 @param str A null-terminated UTF-8 string.
282 @param n_bytes (Output) Set to the size of `str` in bytes (except NULL).
283 @param flags (Output) Set to the applicable flags.
284 */
285 SERD_API
286 size_t
287 serd_strlen(const uint8_t* str, size_t* n_bytes, SerdNodeFlags* flags);
288
289 /**
290 Parse a string to a double.
291
292 The API of this function is identical to the standard C strtod function,
293 except this function is locale-independent and always matches the lexical
294 format used in the Turtle grammar (the decimal point is always ".").
295 */
296 SERD_API
297 double
298 serd_strtod(const char* str, char** endptr);
299
300 /**
301 Decode a base64 string.
302 This function can be used to deserialise a blob node created with
303 serd_node_new_blob().
304
305 @param str Base64 string to decode.
306 @param len The length of `str`.
307 @param size Set to the size of the returned blob in bytes.
308 @return A newly allocated blob which must be freed with free().
309 */
310 SERD_API
311 void*
312 serd_base64_decode(const uint8_t* str, size_t len, size_t* size);
313
314 /**
315 @}
316 @name URI
317 @{
318 */
319
320 static const SerdURI SERD_URI_NULL = {
321 {NULL, 0}, {NULL, 0}, {NULL, 0}, {NULL, 0}, {NULL, 0}, {NULL, 0}
322 };
323
324 /**
325 Return the local path for `uri`, or NULL if `uri` is not a file URI.
326 Note this (inappropriately named) function only removes the file scheme if
327 necessary, and returns `uri` unmodified if it is an absolute path. Percent
328 encoding and other issues are not handled, to properly convert a file URI to
329 a path, use serd_file_uri_parse().
330 */
331 SERD_API
332 const uint8_t*
333 serd_uri_to_path(const uint8_t* uri);
334
335 /**
336 Get the unescaped path and hostname from a file URI.
337 @param uri A file URI.
338 @param hostname If non-NULL, set to the hostname, if present.
339 @return The path component of the URI.
340
341 The returned path and `*hostname` must be freed with free().
342 */
343 SERD_API
344 uint8_t*
345 serd_file_uri_parse(const uint8_t* uri, uint8_t** hostname);
346
347 /**
348 Return true iff `utf8` starts with a valid URI scheme.
349 */
350 SERD_API
351 bool
352 serd_uri_string_has_scheme(const uint8_t* utf8);
353
354 /**
355 Parse `utf8`, writing result to `out`.
356 */
357 SERD_API
358 SerdStatus
359 serd_uri_parse(const uint8_t* utf8, SerdURI* out);
360
361 /**
362 Set `out` to `uri` resolved against `base`.
363 */
364 SERD_API
365 void
366 serd_uri_resolve(const SerdURI* uri, const SerdURI* base, SerdURI* out);
367
368 /**
369 Sink function for raw string output.
370 */
371 typedef size_t (*SerdSink)(const void* buf, size_t len, void* stream);
372
373 /**
374 Serialise `uri` with a series of calls to `sink`.
375 */
376 SERD_API
377 size_t
378 serd_uri_serialise(const SerdURI* uri, SerdSink sink, void* stream);
379
380 /**
381 Serialise `uri` relative to `base` with a series of calls to `sink`.
382
383 The `uri` is written as a relative URI iff if it a child of `base` and @c
384 root. The optional `root` parameter must be a prefix of `base` and can be
385 used keep up-references ("../") within a certain namespace.
386 */
387 SERD_API
388 size_t
389 serd_uri_serialise_relative(const SerdURI* uri,
390 const SerdURI* base,
391 const SerdURI* root,
392 SerdSink sink,
393 void* stream);
394
395 /**
396 @}
397 @name Node
398 @{
399 */
400
401 static const SerdNode SERD_NODE_NULL = { NULL, 0, 0, 0, SERD_NOTHING };
402
403 /**
404 Make a (shallow) node from `str`.
405
406 This measures, but does not copy, `str`. No memory is allocated.
407 */
408 SERD_API
409 SerdNode
410 serd_node_from_string(SerdType type, const uint8_t* str);
411
412 /**
413 Make a deep copy of `node`.
414
415 @return a node that the caller must free with serd_node_free().
416 */
417 SERD_API
418 SerdNode
419 serd_node_copy(const SerdNode* node);
420
421 /**
422 Return true iff `a` is equal to `b`.
423 */
424 SERD_API
425 bool
426 serd_node_equals(const SerdNode* a, const SerdNode* b);
427
428 /**
429 Simple wrapper for serd_node_new_uri() to resolve a URI node.
430 */
431 SERD_API
432 SerdNode
433 serd_node_new_uri_from_node(const SerdNode* uri_node,
434 const SerdURI* base,
435 SerdURI* out);
436
437 /**
438 Simple wrapper for serd_node_new_uri() to resolve a URI string.
439 */
440 SERD_API
441 SerdNode
442 serd_node_new_uri_from_string(const uint8_t* str,
443 const SerdURI* base,
444 SerdURI* out);
445
446 /**
447 Create a new file URI node from a file system path and optional hostname.
448
449 Backslashes in Windows paths will be converted and '%' will always be
450 percent encoded. If `escape` is true, all other invalid characters will be
451 percent encoded as well.
452
453 If `path` is relative, `hostname` is ignored.
454 If `out` is not NULL, it will be set to the parsed URI.
455 */
456 SERD_API
457 SerdNode
458 serd_node_new_file_uri(const uint8_t* path,
459 const uint8_t* hostname,
460 SerdURI* out,
461 bool escape);
462
463 /**
464 Create a new node by serialising `uri` into a new string.
465
466 @param uri The URI to serialise.
467
468 @param base Base URI to resolve `uri` against (or NULL for no resolution).
469
470 @param out Set to the parsing of the new URI (i.e. points only to
471 memory owned by the new returned node).
472 */
473 SERD_API
474 SerdNode
475 serd_node_new_uri(const SerdURI* uri, const SerdURI* base, SerdURI* out);
476
477 /**
478 Create a new node by serialising `uri` into a new relative URI.
479
480 @param uri The URI to serialise.
481
482 @param base Base URI to make `uri` relative to, if possible.
483
484 @param root Root URI for resolution (see serd_uri_serialise_relative()).
485
486 @param out Set to the parsing of the new URI (i.e. points only to
487 memory owned by the new returned node).
488 */
489 SERD_API
490 SerdNode
491 serd_node_new_relative_uri(const SerdURI* uri,
492 const SerdURI* base,
493 const SerdURI* root,
494 SerdURI* out);
495
496 /**
497 Create a new node by serialising `d` into an xsd:decimal string.
498
499 The resulting node will always contain a `.', start with a digit, and end
500 with a digit (i.e. will have a leading and/or trailing `0' if necessary).
501 It will never be in scientific notation. A maximum of `frac_digits` digits
502 will be written after the decimal point, but trailing zeros will
503 automatically be omitted (except one if `d` is a round integer).
504
505 Note that about 16 and 8 fractional digits are required to precisely
506 represent a double and float, respectively.
507
508 @param d The value for the new node.
509 @param frac_digits The maximum number of digits after the decimal place.
510 */
511 SERD_API
512 SerdNode
513 serd_node_new_decimal(double d, unsigned frac_digits);
514
515 /**
516 Create a new node by serialising `i` into an xsd:integer string.
517 */
518 SERD_API
519 SerdNode
520 serd_node_new_integer(int64_t i);
521
522 /**
523 Create a node by serialising `buf` into an xsd:base64Binary string.
524 This function can be used to make a serialisable node out of arbitrary
525 binary data, which can be decoded using serd_base64_decode().
526
527 @param buf Raw binary input data.
528 @param size Size of `buf`.
529 @param wrap_lines Wrap lines at 76 characters to conform to RFC 2045.
530 */
531 SERD_API
532 SerdNode
533 serd_node_new_blob(const void* buf, size_t size, bool wrap_lines);
534
535 /**
536 Free any data owned by `node`.
537
538 Note that if `node` is itself dynamically allocated (which is not the case
539 for nodes created internally by serd), it will not be freed.
540 */
541 SERD_API
542 void
543 serd_node_free(SerdNode* node);
544
545 /**
546 @}
547 @name Event Handlers
548 @{
549 */
550
551 /**
552 Sink (callback) for errors.
553
554 @param handle Handle for user data.
555 @param error Error description.
556 */
557 typedef SerdStatus (*SerdErrorSink)(void* handle,
558 const SerdError* error);
559
560 /**
561 Sink (callback) for base URI changes.
562
563 Called whenever the base URI of the serialisation changes.
564 */
565 typedef SerdStatus (*SerdBaseSink)(void* handle,
566 const SerdNode* uri);
567
568 /**
569 Sink (callback) for namespace definitions.
570
571 Called whenever a prefix is defined in the serialisation.
572 */
573 typedef SerdStatus (*SerdPrefixSink)(void* handle,
574 const SerdNode* name,
575 const SerdNode* uri);
576
577 /**
578 Sink (callback) for statements.
579
580 Called for every RDF statement in the serialisation.
581 */
582 typedef SerdStatus (*SerdStatementSink)(void* handle,
583 SerdStatementFlags flags,
584 const SerdNode* graph,
585 const SerdNode* subject,
586 const SerdNode* predicate,
587 const SerdNode* object,
588 const SerdNode* object_datatype,
589 const SerdNode* object_lang);
590
591 /**
592 Sink (callback) for anonymous node end markers.
593
594 This is called to indicate that the anonymous node with the given
595 `value` will no longer be referred to by any future statements
596 (i.e. the anonymous serialisation of the node is finished).
597 */
598 typedef SerdStatus (*SerdEndSink)(void* handle,
599 const SerdNode* node);
600
601 /**
602 @}
603 @name Environment
604 @{
605 */
606
607 /**
608 Create a new environment.
609 */
610 SERD_API
611 SerdEnv*
612 serd_env_new(const SerdNode* base_uri);
613
614 /**
615 Free `ns`.
616 */
617 SERD_API
618 void
619 serd_env_free(SerdEnv* env);
620
621 /**
622 Get the current base URI.
623 */
624 SERD_API
625 const SerdNode*
626 serd_env_get_base_uri(const SerdEnv* env,
627 SerdURI* out);
628
629 /**
630 Set the current base URI.
631 */
632 SERD_API
633 SerdStatus
634 serd_env_set_base_uri(SerdEnv* env,
635 const SerdNode* uri);
636
637 /**
638 Set a namespace prefix.
639 */
640 SERD_API
641 SerdStatus
642 serd_env_set_prefix(SerdEnv* env,
643 const SerdNode* name,
644 const SerdNode* uri);
645
646 /**
647 Set a namespace prefix.
648 */
649 SERD_API
650 SerdStatus
651 serd_env_set_prefix_from_strings(SerdEnv* env,
652 const uint8_t* name,
653 const uint8_t* uri);
654
655 /**
656 Qualify `uri` into a CURIE if possible.
657 */
658 SERD_API
659 bool
660 serd_env_qualify(const SerdEnv* env,
661 const SerdNode* uri,
662 SerdNode* prefix,
663 SerdChunk* suffix);
664
665 /**
666 Expand `curie`.
667 */
668 SERD_API
669 SerdStatus
670 serd_env_expand(const SerdEnv* env,
671 const SerdNode* curie,
672 SerdChunk* uri_prefix,
673 SerdChunk* uri_suffix);
674
675 /**
676 Expand `node`, which must be a CURIE or URI, to a full URI.
677 */
678 SERD_API
679 SerdNode
680 serd_env_expand_node(const SerdEnv* env,
681 const SerdNode* node);
682
683 /**
684 Call `func` for each prefix defined in `env`.
685 */
686 SERD_API
687 void
688 serd_env_foreach(const SerdEnv* env,
689 SerdPrefixSink func,
690 void* handle);
691
692 /**
693 @}
694 @name Reader
695 @{
696 */
697
698 /**
699 Create a new RDF reader.
700 */
701 SERD_API
702 SerdReader*
703 serd_reader_new(SerdSyntax syntax,
704 void* handle,
705 void (*free_handle)(void*),
706 SerdBaseSink base_sink,
707 SerdPrefixSink prefix_sink,
708 SerdStatementSink statement_sink,
709 SerdEndSink end_sink);
710
711 /**
712 Enable or disable strict parsing.
713
714 The reader is non-strict (lax) by default, which will tolerate URIs with
715 invalid characters. Setting strict will fail when parsing such files. An
716 error is printed for invalid input in either case.
717 */
718 SERD_API
719 void
720 serd_reader_set_strict(SerdReader* reader, bool strict);
721
722 /**
723 Set a function to be called when errors occur during reading.
724
725 The `error_sink` will be called with `handle` as its first argument. If
726 no error function is set, errors are printed to stderr in GCC style.
727 */
728 SERD_API
729 void
730 serd_reader_set_error_sink(SerdReader* reader,
731 SerdErrorSink error_sink,
732 void* handle);
733
734 /**
735 Return the `handle` passed to serd_reader_new().
736 */
737 SERD_API
738 void*
739 serd_reader_get_handle(const SerdReader* reader);
740
741 /**
742 Set a prefix to be added to all blank node identifiers.
743
744 This is useful when multiple files are to be parsed into the same output
745 (e.g. a store, or other files). Since Serd preserves blank node IDs, this
746 could cause conflicts where two non-equivalent blank nodes are merged,
747 resulting in corrupt data. By setting a unique blank node prefix for each
748 parsed file, this can be avoided, while preserving blank node names.
749 */
750 SERD_API
751 void
752 serd_reader_add_blank_prefix(SerdReader* reader,
753 const uint8_t* prefix);
754
755 /**
756 Set the URI of the default graph.
757
758 If this is set, the reader will emit quads with the graph set to the given
759 node for any statements that are not in a named graph (which is currently
760 all of them since Serd currently does not support any graph syntaxes).
761 */
762 SERD_API
763 void
764 serd_reader_set_default_graph(SerdReader* reader,
765 const SerdNode* graph);
766
767 /**
768 Read a file at a given `uri`.
769 */
770 SERD_API
771 SerdStatus
772 serd_reader_read_file(SerdReader* reader,
773 const uint8_t* uri);
774
775 /**
776 Start an incremental read from a file handle.
777
778 Iff `bulk` is true, `file` will be read a page at a time. This is more
779 efficient, but uses a page of memory and means that an entire page of input
780 must be ready before any callbacks will fire. To react as soon as input
781 arrives, set `bulk` to false.
782 */
783 SERD_API
784 SerdStatus
785 serd_reader_start_stream(SerdReader* me,
786 FILE* file,
787 const uint8_t* name,
788 bool bulk);
789
790 /**
791 Read a single "chunk" of data during an incremental read.
792
793 This function will read a single top level description, and return. This
794 may be a directive, statement, or several statements; essentially it reads
795 until a '.' is encountered. This is particularly useful for reading
796 directly from a pipe or socket.
797 */
798 SERD_API
799 SerdStatus
800 serd_reader_read_chunk(SerdReader* me);
801
802 /**
803 Finish an incremental read from a file handle.
804 */
805 SERD_API
806 SerdStatus
807 serd_reader_end_stream(SerdReader* me);
808
809 /**
810 Read `file`.
811 */
812 SERD_API
813 SerdStatus
814 serd_reader_read_file_handle(SerdReader* reader,
815 FILE* file,
816 const uint8_t* name);
817
818 /**
819 Read `utf8`.
820 */
821 SERD_API
822 SerdStatus
823 serd_reader_read_string(SerdReader* me, const uint8_t* utf8);
824
825 /**
826 Free `reader`.
827 */
828 SERD_API
829 void
830 serd_reader_free(SerdReader* reader);
831
832 /**
833 @}
834 @name Writer
835 @{
836 */
837
838 /**
839 Create a new RDF writer.
840 */
841 SERD_API
842 SerdWriter*
843 serd_writer_new(SerdSyntax syntax,
844 SerdStyle style,
845 SerdEnv* env,
846 const SerdURI* base_uri,
847 SerdSink sink,
848 void* stream);
849
850 /**
851 Free `writer`.
852 */
853 SERD_API
854 void
855 serd_writer_free(SerdWriter* writer);
856
857 /**
858 Return the env used by `writer`.
859 */
860 SERD_API
861 SerdEnv*
862 serd_writer_get_env(SerdWriter* writer);
863
864 /**
865 A convenience sink function for writing to a FILE*.
866
867 This function can be used as a SerdSink when writing to a FILE*. The
868 `stream` parameter must be a FILE* opened for writing.
869 */
870 SERD_API
871 size_t
872 serd_file_sink(const void* buf, size_t len, void* stream);
873
874 /**
875 A convenience sink function for writing to a string.
876
877 This function can be used as a SerdSink to write to a SerdChunk which is
878 resized as necessary with realloc(). The `stream` parameter must point to
879 an initialized SerdChunk. When the write is finished, the string should be
880 retrieved with serd_chunk_sink_finish().
881 */
882 SERD_API
883 size_t
884 serd_chunk_sink(const void* buf, size_t len, void* stream);
885
886 /**
887 Finish a serialisation to a chunk with serd_chunk_sink().
888
889 The returned string is the result of the serialisation, which is NULL
890 terminated (by this function) and owned by the caller.
891 */
892 SERD_API
893 uint8_t*
894 serd_chunk_sink_finish(SerdChunk* stream);
895
896 /**
897 Set a function to be called when errors occur during writing.
898
899 The `error_sink` will be called with `handle` as its first argument. If
900 no error function is set, errors are printed to stderr.
901 */
902 SERD_API
903 void
904 serd_writer_set_error_sink(SerdWriter* writer,
905 SerdErrorSink error_sink,
906 void* handle);
907
908 /**
909 Set a prefix to be removed from matching blank node identifiers.
910 */
911 SERD_API
912 void
913 serd_writer_chop_blank_prefix(SerdWriter* writer,
914 const uint8_t* prefix);
915
916 /**
917 Set the current output base URI (and emit directive if applicable).
918
919 Note this function can be safely casted to SerdBaseSink.
920 */
921 SERD_API
922 SerdStatus
923 serd_writer_set_base_uri(SerdWriter* writer,
924 const SerdNode* uri);
925
926 /**
927 Set the current root URI.
928
929 The root URI should be a prefix of the base URI. The path of the root URI
930 is the highest path any relative up-reference can refer to. For example,
931 with root <file:///foo/root> and base <file:///foo/root/base>,
932 <file:///foo/root> will be written as <../>, but <file:///foo> will be
933 written non-relatively as <file:///foo>. If the root is not explicitly set,
934 it defaults to the base URI, so no up-references will be created at all.
935 */
936 SERD_API
937 SerdStatus
938 serd_writer_set_root_uri(SerdWriter* writer,
939 const SerdNode* uri);
940
941 /**
942 Set a namespace prefix (and emit directive if applicable).
943
944 Note this function can be safely casted to SerdPrefixSink.
945 */
946 SERD_API
947 SerdStatus
948 serd_writer_set_prefix(SerdWriter* writer,
949 const SerdNode* name,
950 const SerdNode* uri);
951
952 /**
953 Write a statement.
954
955 Note this function can be safely casted to SerdStatementSink.
956 */
957 SERD_API
958 SerdStatus
959 serd_writer_write_statement(SerdWriter* writer,
960 SerdStatementFlags flags,
961 const SerdNode* graph,
962 const SerdNode* subject,
963 const SerdNode* predicate,
964 const SerdNode* object,
965 const SerdNode* object_datatype,
966 const SerdNode* object_lang);
967
968 /**
969 Mark the end of an anonymous node's description.
970
971 Note this function can be safely casted to SerdEndSink.
972 */
973 SERD_API
974 SerdStatus
975 serd_writer_end_anon(SerdWriter* writer,
976 const SerdNode* node);
977
978 /**
979 Finish a write.
980 */
981 SERD_API
982 SerdStatus
983 serd_writer_finish(SerdWriter* writer);
984
985 /**
986 @}
987 @}
988 */
989
990 #ifdef __cplusplus
991 } /* extern "C" */
992 #endif
993
994 #endif /* SERD_SERD_H */