annotate src/serd-0.18.2/serd/serd.h @ 12:b7bda433d832

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