annotate win32-mingw/include/serd/serd.h @ 124:e3d5853d5918

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