annotate win32-mingw/include/serd-0/serd/serd.h @ 93:5fcdb63f4cc6

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