annotate win32-mingw/include/serd/serd.h @ 30:553a5f65ef64

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