annotate osx/include/serd/serd.h @ 83:ae30d91d2ffe

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