annotate osx/include/serd-0/serd/serd.h @ 88:fe7c3a0b0259

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