annotate win64-msvc/include/serd/serd.h @ 75:8f7fd4420df7

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