Mercurial > hg > sv-dependency-builds
comparison win32-mingw/include/serd/serd.h @ 16:7f77decdb26d
Move serd/sord includes
author | Chris Cannam <chris.cannam@eecs.qmul.ac.uk> |
---|---|
date | Mon, 25 Mar 2013 12:24:18 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
14:48209aa7aa51 | 16:7f77decdb26d |
---|---|
1 /* | |
2 Copyright 2011-2012 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 @c str in characters (except NULL). | |
281 @param str A null-terminated UTF-8 string. | |
282 @param n_bytes (Output) Set to the size of @c 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 @c 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 = {{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}}; | |
321 | |
322 /** | |
323 Return the local path for @c uri, or NULL if @c uri is not a file URI. | |
324 Note this (inappropriately named) function only removes the file scheme if | |
325 necessary, and returns @c uri unmodified if it is an absolute path. Percent | |
326 encoding and other issues are not handled, to properly convert a file URI to | |
327 a path, use serd_file_uri_parse(). | |
328 */ | |
329 SERD_API | |
330 const uint8_t* | |
331 serd_uri_to_path(const uint8_t* uri); | |
332 | |
333 /** | |
334 Get the unescaped path and hostname from a file URI. | |
335 @param uri A file URI. | |
336 @param hostname If non-NULL, set to the hostname, if present. | |
337 @return The path component of the URI. | |
338 | |
339 Both the returned path and @c hostname (if applicable) are owned by the | |
340 caller and must be freed with free(). | |
341 */ | |
342 SERD_API | |
343 uint8_t* | |
344 serd_file_uri_parse(const uint8_t* uri, uint8_t** hostname); | |
345 | |
346 /** | |
347 Return true iff @c utf8 starts with a valid URI scheme. | |
348 */ | |
349 SERD_API | |
350 bool | |
351 serd_uri_string_has_scheme(const uint8_t* utf8); | |
352 | |
353 /** | |
354 Parse @c utf8, writing result to @c out. | |
355 */ | |
356 SERD_API | |
357 SerdStatus | |
358 serd_uri_parse(const uint8_t* utf8, SerdURI* out); | |
359 | |
360 /** | |
361 Set @c out to @c uri resolved against @c base. | |
362 */ | |
363 SERD_API | |
364 void | |
365 serd_uri_resolve(const SerdURI* uri, const SerdURI* base, SerdURI* out); | |
366 | |
367 /** | |
368 Sink function for raw string output. | |
369 */ | |
370 typedef size_t (*SerdSink)(const void* buf, size_t len, void* stream); | |
371 | |
372 /** | |
373 Serialise @c uri with a series of calls to @c sink. | |
374 */ | |
375 SERD_API | |
376 size_t | |
377 serd_uri_serialise(const SerdURI* uri, SerdSink sink, void* stream); | |
378 | |
379 /** | |
380 Serialise @c uri relative to @c base with a series of calls to @c sink. | |
381 | |
382 The @c uri is written as a relative URI iff if it a child of @c base and @c | |
383 root. The optional @c root parameter must be a prefix of @c base and can be | |
384 used keep up-references ("../") within a certain namespace. | |
385 */ | |
386 SERD_API | |
387 size_t | |
388 serd_uri_serialise_relative(const SerdURI* uri, | |
389 const SerdURI* base, | |
390 const SerdURI* root, | |
391 SerdSink sink, | |
392 void* stream); | |
393 | |
394 /** | |
395 @} | |
396 @name Node | |
397 @{ | |
398 */ | |
399 | |
400 static const SerdNode SERD_NODE_NULL = { 0, 0, 0, 0, SERD_NOTHING }; | |
401 | |
402 /** | |
403 Make a (shallow) node from @c str. | |
404 | |
405 This measures, but does not copy, @c str. No memory is allocated. | |
406 */ | |
407 SERD_API | |
408 SerdNode | |
409 serd_node_from_string(SerdType type, const uint8_t* str); | |
410 | |
411 /** | |
412 Make a deep copy of @c node. | |
413 | |
414 @return a node that the caller must free with @ref serd_node_free. | |
415 */ | |
416 SERD_API | |
417 SerdNode | |
418 serd_node_copy(const SerdNode* node); | |
419 | |
420 /** | |
421 Return true iff @c a is equal to @c b. | |
422 */ | |
423 SERD_API | |
424 bool | |
425 serd_node_equals(const SerdNode* a, const SerdNode* b); | |
426 | |
427 /** | |
428 Simple wrapper for serd_node_new_uri to resolve a URI node. | |
429 */ | |
430 SERD_API | |
431 SerdNode | |
432 serd_node_new_uri_from_node(const SerdNode* uri_node, | |
433 const SerdURI* base, | |
434 SerdURI* out); | |
435 | |
436 /** | |
437 Simple wrapper for serd_node_new_uri to resolve a URI string. | |
438 */ | |
439 SERD_API | |
440 SerdNode | |
441 serd_node_new_uri_from_string(const uint8_t* str, | |
442 const SerdURI* base, | |
443 SerdURI* out); | |
444 | |
445 /** | |
446 Create a new file URI node from a file system path and optional hostname. | |
447 | |
448 Backslashes in Windows paths will be converted and '%' will always be | |
449 percent encoded. If @c escape is true, all other invalid characters will be | |
450 percent encoded as well. | |
451 | |
452 If @c path is relative, @c hostname is ignored. | |
453 If @c out is not NULL, it will be set to the parsed URI. | |
454 */ | |
455 SERD_API | |
456 SerdNode | |
457 serd_node_new_file_uri(const uint8_t* path, | |
458 const uint8_t* hostname, | |
459 SerdURI* out, | |
460 bool escape); | |
461 | |
462 /** | |
463 Create a new node by serialising @c uri into a new string. | |
464 | |
465 @param uri The URI to parse and serialise. | |
466 | |
467 @param base Base URI to resolve @c uri against (or NULL for no resolution). | |
468 | |
469 @param out Set to the parsing of the new URI (i.e. points only to | |
470 memory owned by the new returned node). | |
471 */ | |
472 SERD_API | |
473 SerdNode | |
474 serd_node_new_uri(const SerdURI* uri, const SerdURI* base, SerdURI* out); | |
475 | |
476 /** | |
477 Create a new node by serialising @c d into an xsd:decimal string. | |
478 | |
479 The resulting node will always contain a `.', start with a digit, and end | |
480 with a digit (i.e. will have a leading and/or trailing `0' if necessary). | |
481 It will never be in scientific notation. A maximum of @c frac_digits digits | |
482 will be written after the decimal point, but trailing zeros will | |
483 automatically be omitted (except one if @c d is a round integer). | |
484 | |
485 Note that about 16 and 8 fractional digits are required to precisely | |
486 represent a double and float, respectively. | |
487 | |
488 @param d The value for the new node. | |
489 @param frac_digits The maximum number of digits after the decimal place. | |
490 */ | |
491 SERD_API | |
492 SerdNode | |
493 serd_node_new_decimal(double d, unsigned frac_digits); | |
494 | |
495 /** | |
496 Create a new node by serialising @c i into an xsd:integer string. | |
497 */ | |
498 SERD_API | |
499 SerdNode | |
500 serd_node_new_integer(int64_t i); | |
501 | |
502 /** | |
503 Create a node by serialising @c buf into an xsd:base64Binary string. | |
504 This function can be used to make a serialisable node out of arbitrary | |
505 binary data, which can be decoded using serd_base64_decode(). | |
506 | |
507 @param buf Raw binary input data. | |
508 @param size Size of @c buf. | |
509 @param wrap_lines Wrap lines at 76 characters to conform to RFC 2045. | |
510 */ | |
511 SERD_API | |
512 SerdNode | |
513 serd_node_new_blob(const void* buf, size_t size, bool wrap_lines); | |
514 | |
515 /** | |
516 Free any data owned by @c node. | |
517 | |
518 Note that if @c node is itself dynamically allocated (which is not the case | |
519 for nodes created internally by serd), it will not be freed. | |
520 */ | |
521 SERD_API | |
522 void | |
523 serd_node_free(SerdNode* node); | |
524 | |
525 /** | |
526 @} | |
527 @name Event Handlers | |
528 @{ | |
529 */ | |
530 | |
531 /** | |
532 Sink (callback) for errors. | |
533 | |
534 @param handle Handle for user data. | |
535 @param error Error description. | |
536 */ | |
537 typedef SerdStatus (*SerdErrorSink)(void* handle, | |
538 const SerdError* error); | |
539 | |
540 /** | |
541 Sink (callback) for base URI changes. | |
542 | |
543 Called whenever the base URI of the serialisation changes. | |
544 */ | |
545 typedef SerdStatus (*SerdBaseSink)(void* handle, | |
546 const SerdNode* uri); | |
547 | |
548 /** | |
549 Sink (callback) for namespace definitions. | |
550 | |
551 Called whenever a prefix is defined in the serialisation. | |
552 */ | |
553 typedef SerdStatus (*SerdPrefixSink)(void* handle, | |
554 const SerdNode* name, | |
555 const SerdNode* uri); | |
556 | |
557 /** | |
558 Sink (callback) for statements. | |
559 | |
560 Called for every RDF statement in the serialisation. | |
561 */ | |
562 typedef SerdStatus (*SerdStatementSink)(void* handle, | |
563 SerdStatementFlags flags, | |
564 const SerdNode* graph, | |
565 const SerdNode* subject, | |
566 const SerdNode* predicate, | |
567 const SerdNode* object, | |
568 const SerdNode* object_datatype, | |
569 const SerdNode* object_lang); | |
570 | |
571 /** | |
572 Sink (callback) for anonymous node end markers. | |
573 | |
574 This is called to indicate that the anonymous node with the given | |
575 @c value will no longer be referred to by any future statements | |
576 (i.e. the anonymous serialisation of the node is finished). | |
577 */ | |
578 typedef SerdStatus (*SerdEndSink)(void* handle, | |
579 const SerdNode* node); | |
580 | |
581 /** | |
582 @} | |
583 @name Environment | |
584 @{ | |
585 */ | |
586 | |
587 /** | |
588 Create a new environment. | |
589 */ | |
590 SERD_API | |
591 SerdEnv* | |
592 serd_env_new(const SerdNode* base_uri); | |
593 | |
594 /** | |
595 Free @c ns. | |
596 */ | |
597 SERD_API | |
598 void | |
599 serd_env_free(SerdEnv* env); | |
600 | |
601 /** | |
602 Get the current base URI. | |
603 */ | |
604 SERD_API | |
605 const SerdNode* | |
606 serd_env_get_base_uri(const SerdEnv* env, | |
607 SerdURI* out); | |
608 | |
609 /** | |
610 Set the current base URI. | |
611 */ | |
612 SERD_API | |
613 SerdStatus | |
614 serd_env_set_base_uri(SerdEnv* env, | |
615 const SerdNode* uri); | |
616 | |
617 /** | |
618 Set a namespace prefix. | |
619 */ | |
620 SERD_API | |
621 SerdStatus | |
622 serd_env_set_prefix(SerdEnv* env, | |
623 const SerdNode* name, | |
624 const SerdNode* uri); | |
625 | |
626 /** | |
627 Set a namespace prefix. | |
628 */ | |
629 SERD_API | |
630 SerdStatus | |
631 serd_env_set_prefix_from_strings(SerdEnv* env, | |
632 const uint8_t* name, | |
633 const uint8_t* uri); | |
634 | |
635 /** | |
636 Qualify @c uri into a CURIE if possible. | |
637 */ | |
638 SERD_API | |
639 bool | |
640 serd_env_qualify(const SerdEnv* env, | |
641 const SerdNode* uri, | |
642 SerdNode* prefix, | |
643 SerdChunk* suffix); | |
644 | |
645 /** | |
646 Expand @c curie. | |
647 */ | |
648 SERD_API | |
649 SerdStatus | |
650 serd_env_expand(const SerdEnv* env, | |
651 const SerdNode* curie, | |
652 SerdChunk* uri_prefix, | |
653 SerdChunk* uri_suffix); | |
654 | |
655 /** | |
656 Expand @c node, which must be a CURIE or URI, to a full URI. | |
657 */ | |
658 SERD_API | |
659 SerdNode | |
660 serd_env_expand_node(const SerdEnv* env, | |
661 const SerdNode* node); | |
662 | |
663 /** | |
664 Call @c func for each prefix defined in @c env. | |
665 */ | |
666 SERD_API | |
667 void | |
668 serd_env_foreach(const SerdEnv* env, | |
669 SerdPrefixSink func, | |
670 void* handle); | |
671 | |
672 /** | |
673 @} | |
674 @name Reader | |
675 @{ | |
676 */ | |
677 | |
678 /** | |
679 Create a new RDF reader. | |
680 */ | |
681 SERD_API | |
682 SerdReader* | |
683 serd_reader_new(SerdSyntax syntax, | |
684 void* handle, | |
685 void (*free_handle)(void*), | |
686 SerdBaseSink base_sink, | |
687 SerdPrefixSink prefix_sink, | |
688 SerdStatementSink statement_sink, | |
689 SerdEndSink end_sink); | |
690 | |
691 /** | |
692 Set a function to be called when errors occur during reading. | |
693 | |
694 The @p error_sink will be called with @p handle as its first argument. If | |
695 no error function is set, errors are printed to stderr in GCC style. | |
696 */ | |
697 SERD_API | |
698 void | |
699 serd_reader_set_error_sink(SerdReader* reader, | |
700 SerdErrorSink error_sink, | |
701 void* handle); | |
702 | |
703 /** | |
704 Return the @c handle passed to @ref serd_reader_new. | |
705 */ | |
706 SERD_API | |
707 void* | |
708 serd_reader_get_handle(const SerdReader* reader); | |
709 | |
710 /** | |
711 Set a prefix to be added to all blank node identifiers. | |
712 | |
713 This is useful when multiple files are to be parsed into the same output | |
714 (e.g. a store, or other files). Since Serd preserves blank node IDs, this | |
715 could cause conflicts where two non-equivalent blank nodes are merged, | |
716 resulting in corrupt data. By setting a unique blank node prefix for each | |
717 parsed file, this can be avoided, while preserving blank node names. | |
718 */ | |
719 SERD_API | |
720 void | |
721 serd_reader_add_blank_prefix(SerdReader* reader, | |
722 const uint8_t* prefix); | |
723 | |
724 /** | |
725 Set the URI of the default graph. | |
726 | |
727 If this is set, the reader will emit quads with the graph set to the given | |
728 node for any statements that are not in a named graph (which is currently | |
729 all of them since Serd currently does not support any graph syntaxes). | |
730 */ | |
731 SERD_API | |
732 void | |
733 serd_reader_set_default_graph(SerdReader* reader, | |
734 const SerdNode* graph); | |
735 | |
736 /** | |
737 Read a file at a given @c uri. | |
738 */ | |
739 SERD_API | |
740 SerdStatus | |
741 serd_reader_read_file(SerdReader* reader, | |
742 const uint8_t* uri); | |
743 | |
744 /** | |
745 Start an incremental read from a file handle. | |
746 | |
747 Iff @p bulk is true, @p file will be read a page at a time. This is more | |
748 efficient, but uses a page of memory and means that an entire page of input | |
749 must be ready before any callbacks will fire. To react as soon as input | |
750 arrives, set @p bulk to false. | |
751 */ | |
752 SERD_API | |
753 SerdStatus | |
754 serd_reader_start_stream(SerdReader* me, | |
755 FILE* file, | |
756 const uint8_t* name, | |
757 bool bulk); | |
758 | |
759 /** | |
760 Read a single "chunk" of data during an incremental read. | |
761 | |
762 This function will read a single top level description, and return. This | |
763 may be a directive, statement, or several statements; essentially it reads | |
764 until a '.' is encountered. This is particularly useful for reading | |
765 directly from a pipe or socket. | |
766 */ | |
767 SERD_API | |
768 SerdStatus | |
769 serd_reader_read_chunk(SerdReader* me); | |
770 | |
771 /** | |
772 Finish an incremental read from a file handle. | |
773 */ | |
774 SERD_API | |
775 SerdStatus | |
776 serd_reader_end_stream(SerdReader* me); | |
777 | |
778 /** | |
779 Read @c file. | |
780 */ | |
781 SERD_API | |
782 SerdStatus | |
783 serd_reader_read_file_handle(SerdReader* reader, | |
784 FILE* file, | |
785 const uint8_t* name); | |
786 | |
787 /** | |
788 Read @c utf8. | |
789 */ | |
790 SERD_API | |
791 SerdStatus | |
792 serd_reader_read_string(SerdReader* me, const uint8_t* utf8); | |
793 | |
794 /** | |
795 Free @c reader. | |
796 */ | |
797 SERD_API | |
798 void | |
799 serd_reader_free(SerdReader* reader); | |
800 | |
801 /** | |
802 @} | |
803 @name Writer | |
804 @{ | |
805 */ | |
806 | |
807 /** | |
808 Create a new RDF writer. | |
809 */ | |
810 SERD_API | |
811 SerdWriter* | |
812 serd_writer_new(SerdSyntax syntax, | |
813 SerdStyle style, | |
814 SerdEnv* env, | |
815 const SerdURI* base_uri, | |
816 SerdSink sink, | |
817 void* stream); | |
818 | |
819 /** | |
820 Free @c writer. | |
821 */ | |
822 SERD_API | |
823 void | |
824 serd_writer_free(SerdWriter* writer); | |
825 | |
826 /** | |
827 Return the env used by @c writer. | |
828 */ | |
829 SERD_API | |
830 SerdEnv* | |
831 serd_writer_get_env(SerdWriter* writer); | |
832 | |
833 /** | |
834 A convenience sink function for writing to a FILE*. | |
835 | |
836 This function can be used as a SerdSink when writing to a FILE*. The | |
837 @c stream parameter must be a FILE* opened for writing. | |
838 */ | |
839 SERD_API | |
840 size_t | |
841 serd_file_sink(const void* buf, size_t len, void* stream); | |
842 | |
843 /** | |
844 A convenience sink function for writing to a string. | |
845 | |
846 This function can be used as a SerdSink to write to a SerdChunk which is | |
847 resized as necessary with realloc(). The @c stream parameter must point to | |
848 an initialized SerdChunk. When the write is finished, the string should be | |
849 retrieved with serd_chunk_sink_finish(). | |
850 */ | |
851 SERD_API | |
852 size_t | |
853 serd_chunk_sink(const void* buf, size_t len, void* stream); | |
854 | |
855 /** | |
856 Finish a serialisation to a chunk with serd_chunk_sink(). | |
857 | |
858 The returned string is the result of the serialisation, which is NULL | |
859 terminated (by this function) and owned by the caller. | |
860 */ | |
861 SERD_API | |
862 uint8_t* | |
863 serd_chunk_sink_finish(SerdChunk* stream); | |
864 | |
865 /** | |
866 Set a function to be called when errors occur during writing. | |
867 | |
868 The @p error_sink will be called with @p handle as its first argument. If | |
869 no error function is set, errors are printed to stderr. | |
870 */ | |
871 SERD_API | |
872 void | |
873 serd_writer_set_error_sink(SerdWriter* writer, | |
874 SerdErrorSink error_sink, | |
875 void* handle); | |
876 | |
877 /** | |
878 Set a prefix to be removed from matching blank node identifiers. | |
879 */ | |
880 SERD_API | |
881 void | |
882 serd_writer_chop_blank_prefix(SerdWriter* writer, | |
883 const uint8_t* prefix); | |
884 | |
885 /** | |
886 Set the current output base URI (and emit directive if applicable). | |
887 | |
888 Note this function can be safely casted to SerdBaseSink. | |
889 */ | |
890 SERD_API | |
891 SerdStatus | |
892 serd_writer_set_base_uri(SerdWriter* writer, | |
893 const SerdNode* uri); | |
894 | |
895 /** | |
896 Set the current root URI. | |
897 | |
898 The root URI should be a prefix of the base URI. The path of the root URI | |
899 is the highest path any relative up-reference can refer to. For example, | |
900 with root <file:///foo/root> and base <file:///foo/root/base>, | |
901 <file:///foo/root> will be written as <../>, but <file:///foo> will be | |
902 written non-relatively as <file:///foo>. If the root is not explicitly set, | |
903 it defaults to the base URI, so no up-references will be created at all. | |
904 */ | |
905 SERD_API | |
906 SerdStatus | |
907 serd_writer_set_root_uri(SerdWriter* writer, | |
908 const SerdNode* uri); | |
909 | |
910 /** | |
911 Set a namespace prefix (and emit directive if applicable). | |
912 | |
913 Note this function can be safely casted to SerdPrefixSink. | |
914 */ | |
915 SERD_API | |
916 SerdStatus | |
917 serd_writer_set_prefix(SerdWriter* writer, | |
918 const SerdNode* name, | |
919 const SerdNode* uri); | |
920 | |
921 /** | |
922 Write a statement. | |
923 | |
924 Note this function can be safely casted to SerdStatementSink. | |
925 */ | |
926 SERD_API | |
927 SerdStatus | |
928 serd_writer_write_statement(SerdWriter* writer, | |
929 SerdStatementFlags flags, | |
930 const SerdNode* graph, | |
931 const SerdNode* subject, | |
932 const SerdNode* predicate, | |
933 const SerdNode* object, | |
934 const SerdNode* object_datatype, | |
935 const SerdNode* object_lang); | |
936 | |
937 /** | |
938 Mark the end of an anonymous node's description. | |
939 | |
940 Note this function can be safely casted to SerdEndSink. | |
941 */ | |
942 SERD_API | |
943 SerdStatus | |
944 serd_writer_end_anon(SerdWriter* writer, | |
945 const SerdNode* node); | |
946 | |
947 /** | |
948 Finish a write. | |
949 */ | |
950 SERD_API | |
951 SerdStatus | |
952 serd_writer_finish(SerdWriter* writer); | |
953 | |
954 /** | |
955 @} | |
956 @} | |
957 */ | |
958 | |
959 #ifdef __cplusplus | |
960 } /* extern "C" */ | |
961 #endif | |
962 | |
963 #endif /* SERD_SERD_H */ |