annotate win64-msvc/include/serd/serd.h @ 130:1c067f014d80

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