annotate src/serd-0.18.2/serd/serd.h @ 169:223a55898ab9 tip default

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