annotate win32-mingw/include/serd-0/serd/serd.h @ 8:c29fa680fb5a

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