cannam@130: /* cannam@130: Copyright 2011-2016 David Robillard cannam@130: cannam@130: Permission to use, copy, modify, and/or distribute this software for any cannam@130: purpose with or without fee is hereby granted, provided that the above cannam@130: copyright notice and this permission notice appear in all copies. cannam@130: cannam@130: THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES cannam@130: WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF cannam@130: MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR cannam@130: ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES cannam@130: WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN cannam@130: ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF cannam@130: OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. cannam@130: */ cannam@130: cannam@130: /** cannam@130: @file serd.h API for Serd, a lightweight RDF syntax library. cannam@130: */ cannam@130: cannam@130: #ifndef SERD_SERD_H cannam@130: #define SERD_SERD_H cannam@130: cannam@130: #include cannam@130: #include cannam@130: #include cannam@130: #include cannam@130: cannam@130: #ifdef SERD_SHARED cannam@130: # ifdef _WIN32 cannam@130: # define SERD_LIB_IMPORT __declspec(dllimport) cannam@130: # define SERD_LIB_EXPORT __declspec(dllexport) cannam@130: # else cannam@130: # define SERD_LIB_IMPORT __attribute__((visibility("default"))) cannam@130: # define SERD_LIB_EXPORT __attribute__((visibility("default"))) cannam@130: # endif cannam@130: # ifdef SERD_INTERNAL cannam@130: # define SERD_API SERD_LIB_EXPORT cannam@130: # else cannam@130: # define SERD_API SERD_LIB_IMPORT cannam@130: # endif cannam@130: #else cannam@130: # define SERD_API cannam@130: #endif cannam@130: cannam@130: #ifdef __cplusplus cannam@130: extern "C" { cannam@130: #else cannam@130: # include cannam@130: #endif cannam@130: cannam@130: /** cannam@130: @defgroup serd Serd cannam@130: A lightweight RDF syntax library. cannam@130: @{ cannam@130: */ cannam@130: cannam@130: /** cannam@130: Environment. cannam@130: cannam@130: Represents the state required to resolve a CURIE or relative URI, e.g. the cannam@130: base URI and set of namespace prefixes at a particular point. cannam@130: */ cannam@130: typedef struct SerdEnvImpl SerdEnv; cannam@130: cannam@130: /** cannam@130: RDF reader. cannam@130: cannam@130: Parses RDF by calling user-provided sink functions as input is consumed cannam@130: (much like an XML SAX parser). cannam@130: */ cannam@130: typedef struct SerdReaderImpl SerdReader; cannam@130: cannam@130: /** cannam@130: RDF writer. cannam@130: cannam@130: Provides a number of functions to allow writing RDF syntax out to some cannam@130: stream. These functions are deliberately compatible with the sink functions cannam@130: used by SerdReader, so a reader can be directly connected to a writer to cannam@130: re-serialise a document with minimal overhead. cannam@130: */ cannam@130: typedef struct SerdWriterImpl SerdWriter; cannam@130: cannam@130: /** cannam@130: Return status code. cannam@130: */ cannam@130: typedef enum { cannam@130: SERD_SUCCESS, /**< No error */ cannam@130: SERD_FAILURE, /**< Non-fatal failure */ cannam@130: SERD_ERR_UNKNOWN, /**< Unknown error */ cannam@130: SERD_ERR_BAD_SYNTAX, /**< Invalid syntax */ cannam@130: SERD_ERR_BAD_ARG, /**< Invalid argument */ cannam@130: SERD_ERR_NOT_FOUND, /**< Not found */ cannam@130: SERD_ERR_ID_CLASH, /**< Encountered clashing blank node IDs */ cannam@130: SERD_ERR_BAD_CURIE, /**< Invalid CURIE (e.g. prefix does not exist) */ cannam@130: SERD_ERR_INTERNAL /**< Unexpected internal error (should not happen) */ cannam@130: } SerdStatus; cannam@130: cannam@130: /** cannam@130: RDF syntax type. cannam@130: */ cannam@130: typedef enum { cannam@130: /** cannam@130: Turtle - Terse RDF Triple Language (UTF-8). cannam@130: @see Turtle cannam@130: */ cannam@130: SERD_TURTLE = 1, cannam@130: cannam@130: /** cannam@130: NTriples - Line-based RDF triples (ASCII). cannam@130: @see NTriples cannam@130: */ cannam@130: SERD_NTRIPLES = 2 cannam@130: } SerdSyntax; cannam@130: cannam@130: /** cannam@130: Flags indication inline abbreviation information for a statement. cannam@130: */ cannam@130: typedef enum { cannam@130: SERD_EMPTY_S = 1 << 1, /**< Empty blank node subject */ cannam@130: SERD_EMPTY_O = 1 << 2, /**< Empty blank node object */ cannam@130: SERD_ANON_S_BEGIN = 1 << 3, /**< Start of anonymous subject */ cannam@130: SERD_ANON_O_BEGIN = 1 << 4, /**< Start of anonymous object */ cannam@130: SERD_ANON_CONT = 1 << 5, /**< Continuation of anonymous node */ cannam@130: SERD_LIST_S_BEGIN = 1 << 6, /**< Start of list subject */ cannam@130: SERD_LIST_O_BEGIN = 1 << 7, /**< Start of list object */ cannam@130: SERD_LIST_CONT = 1 << 8 /**< Continuation of list */ cannam@130: } SerdStatementFlag; cannam@130: cannam@130: /** cannam@130: Bitwise OR of SerdNodeFlag values. cannam@130: */ cannam@130: typedef uint32_t SerdStatementFlags; cannam@130: cannam@130: /** cannam@130: Type of a syntactic RDF node. cannam@130: cannam@130: This is more precise than the type of an abstract RDF node. An abstract cannam@130: node is either a resource, literal, or blank. In syntax there are two ways cannam@130: to refer to a resource (by URI or CURIE) and two ways to refer to a blank cannam@130: (by ID or anonymously). Anonymous (inline) blank nodes are expressed using cannam@130: SerdStatementFlags rather than this type. cannam@130: */ cannam@130: typedef enum { cannam@130: /** cannam@130: The type of a nonexistent node. cannam@130: cannam@130: This type is useful as a sentinel, but is never emitted by the reader. cannam@130: */ cannam@130: SERD_NOTHING = 0, cannam@130: cannam@130: /** cannam@130: Literal value. cannam@130: cannam@130: A literal optionally has either a language, or a datatype (not both). cannam@130: */ cannam@130: SERD_LITERAL = 1, cannam@130: cannam@130: /** cannam@130: URI (absolute or relative). cannam@130: cannam@130: Value is an unquoted URI string, which is either a relative reference cannam@130: with respect to the current base URI (e.g. "foo/bar"), or an absolute cannam@130: URI (e.g. "http://example.org/foo"). cannam@130: @see RFC3986. cannam@130: */ cannam@130: SERD_URI = 2, cannam@130: cannam@130: /** cannam@130: CURIE, a shortened URI. cannam@130: cannam@130: Value is an unquoted CURIE string relative to the current environment, cannam@130: e.g. "rdf:type". cannam@130: @see CURIE Syntax 1.0 cannam@130: */ cannam@130: SERD_CURIE = 3, cannam@130: cannam@130: /** cannam@130: A blank node. cannam@130: cannam@130: Value is a blank node ID, e.g. "id3", which is meaningful only within cannam@130: this serialisation. cannam@130: @see Turtle cannam@130: nodeID cannam@130: */ cannam@130: SERD_BLANK = 4 cannam@130: cannam@130: } SerdType; cannam@130: cannam@130: /** cannam@130: Flags indicating certain string properties relevant to serialisation. cannam@130: */ cannam@130: typedef enum { cannam@130: SERD_HAS_NEWLINE = 1, /**< Contains line breaks ('\\n' or '\\r') */ cannam@130: SERD_HAS_QUOTE = 1 << 1 /**< Contains quotes ('"') */ cannam@130: } SerdNodeFlag; cannam@130: cannam@130: /** cannam@130: Bitwise OR of SerdNodeFlag values. cannam@130: */ cannam@130: typedef uint32_t SerdNodeFlags; cannam@130: cannam@130: /** cannam@130: A syntactic RDF node. cannam@130: */ cannam@130: typedef struct { cannam@130: const uint8_t* buf; /**< Value string */ cannam@130: size_t n_bytes; /**< Size in bytes (not including null) */ cannam@130: size_t n_chars; /**< Length in characters (not including null)*/ cannam@130: SerdNodeFlags flags; /**< Node flags (e.g. string properties) */ cannam@130: SerdType type; /**< Node type */ cannam@130: } SerdNode; cannam@130: cannam@130: /** cannam@130: An unterminated string fragment. cannam@130: */ cannam@130: typedef struct { cannam@130: const uint8_t* buf; /**< Start of chunk */ cannam@130: size_t len; /**< Length of chunk in bytes */ cannam@130: } SerdChunk; cannam@130: cannam@130: /** cannam@130: An error description. cannam@130: */ cannam@130: typedef struct { cannam@130: SerdStatus status; /**< Error code */ cannam@130: const uint8_t* filename; /**< File where error was encountered, or NULL */ cannam@130: unsigned line; /**< Line where error was encountered, or 0 */ cannam@130: unsigned col; /**< Column where error was encountered */ cannam@130: const char* fmt; /**< Message format string (printf style) */ cannam@130: va_list* args; /**< Arguments for fmt */ cannam@130: } SerdError; cannam@130: cannam@130: /** cannam@130: A parsed URI. cannam@130: cannam@130: This struct directly refers to chunks in other strings, it does not own any cannam@130: memory itself. Thus, URIs can be parsed and/or resolved against a base URI cannam@130: in-place without allocating memory. cannam@130: */ cannam@130: typedef struct { cannam@130: SerdChunk scheme; /**< Scheme */ cannam@130: SerdChunk authority; /**< Authority */ cannam@130: SerdChunk path_base; /**< Path prefix if relative */ cannam@130: SerdChunk path; /**< Path suffix */ cannam@130: SerdChunk query; /**< Query */ cannam@130: SerdChunk fragment; /**< Fragment */ cannam@130: } SerdURI; cannam@130: cannam@130: /** cannam@130: Syntax style options. cannam@130: cannam@130: The style of the writer output can be controlled by ORing together cannam@130: values from this enumeration. Note that some options are only supported cannam@130: for some syntaxes (e.g. NTriples does not support abbreviation and is cannam@130: always ASCII). cannam@130: */ cannam@130: typedef enum { cannam@130: SERD_STYLE_ABBREVIATED = 1, /**< Abbreviate triples when possible. */ cannam@130: SERD_STYLE_ASCII = 1 << 1, /**< Escape all non-ASCII characters. */ cannam@130: SERD_STYLE_RESOLVED = 1 << 2, /**< Resolve URIs against base URI. */ cannam@130: SERD_STYLE_CURIED = 1 << 3, /**< Shorten URIs into CURIEs. */ cannam@130: SERD_STYLE_BULK = 1 << 4 /**< Write output in pages. */ cannam@130: } SerdStyle; cannam@130: cannam@130: /** cannam@130: @name String Utilities cannam@130: @{ cannam@130: */ cannam@130: cannam@130: /** cannam@130: Return a string describing a status code. cannam@130: */ cannam@130: SERD_API cannam@130: const uint8_t* cannam@130: serd_strerror(SerdStatus status); cannam@130: cannam@130: /** cannam@130: Measure a UTF-8 string. cannam@130: @return Length of `str` in characters (except NULL). cannam@130: @param str A null-terminated UTF-8 string. cannam@130: @param n_bytes (Output) Set to the size of `str` in bytes (except NULL). cannam@130: @param flags (Output) Set to the applicable flags. cannam@130: */ cannam@130: SERD_API cannam@130: size_t cannam@130: serd_strlen(const uint8_t* str, size_t* n_bytes, SerdNodeFlags* flags); cannam@130: cannam@130: /** cannam@130: Parse a string to a double. cannam@130: cannam@130: The API of this function is identical to the standard C strtod function, cannam@130: except this function is locale-independent and always matches the lexical cannam@130: format used in the Turtle grammar (the decimal point is always "."). cannam@130: */ cannam@130: SERD_API cannam@130: double cannam@130: serd_strtod(const char* str, char** endptr); cannam@130: cannam@130: /** cannam@130: Decode a base64 string. cannam@130: This function can be used to deserialise a blob node created with cannam@130: serd_node_new_blob(). cannam@130: cannam@130: @param str Base64 string to decode. cannam@130: @param len The length of `str`. cannam@130: @param size Set to the size of the returned blob in bytes. cannam@130: @return A newly allocated blob which must be freed with free(). cannam@130: */ cannam@130: SERD_API cannam@130: void* cannam@130: serd_base64_decode(const uint8_t* str, size_t len, size_t* size); cannam@130: cannam@130: /** cannam@130: @} cannam@130: @name URI cannam@130: @{ cannam@130: */ cannam@130: cannam@130: static const SerdURI SERD_URI_NULL = { cannam@130: {NULL, 0}, {NULL, 0}, {NULL, 0}, {NULL, 0}, {NULL, 0}, {NULL, 0} cannam@130: }; cannam@130: cannam@130: /** cannam@130: Return the local path for `uri`, or NULL if `uri` is not a file URI. cannam@130: Note this (inappropriately named) function only removes the file scheme if cannam@130: necessary, and returns `uri` unmodified if it is an absolute path. Percent cannam@130: encoding and other issues are not handled, to properly convert a file URI to cannam@130: a path, use serd_file_uri_parse(). cannam@130: */ cannam@130: SERD_API cannam@130: const uint8_t* cannam@130: serd_uri_to_path(const uint8_t* uri); cannam@130: cannam@130: /** cannam@130: Get the unescaped path and hostname from a file URI. cannam@130: @param uri A file URI. cannam@130: @param hostname If non-NULL, set to the hostname, if present. cannam@130: @return The path component of the URI. cannam@130: cannam@130: The returned path and `*hostname` must be freed with free(). cannam@130: */ cannam@130: SERD_API cannam@130: uint8_t* cannam@130: serd_file_uri_parse(const uint8_t* uri, uint8_t** hostname); cannam@130: cannam@130: /** cannam@130: Return true iff `utf8` starts with a valid URI scheme. cannam@130: */ cannam@130: SERD_API cannam@130: bool cannam@130: serd_uri_string_has_scheme(const uint8_t* utf8); cannam@130: cannam@130: /** cannam@130: Parse `utf8`, writing result to `out`. cannam@130: */ cannam@130: SERD_API cannam@130: SerdStatus cannam@130: serd_uri_parse(const uint8_t* utf8, SerdURI* out); cannam@130: cannam@130: /** cannam@130: Set `out` to `uri` resolved against `base`. cannam@130: */ cannam@130: SERD_API cannam@130: void cannam@130: serd_uri_resolve(const SerdURI* uri, const SerdURI* base, SerdURI* out); cannam@130: cannam@130: /** cannam@130: Sink function for raw string output. cannam@130: */ cannam@130: typedef size_t (*SerdSink)(const void* buf, size_t len, void* stream); cannam@130: cannam@130: /** cannam@130: Serialise `uri` with a series of calls to `sink`. cannam@130: */ cannam@130: SERD_API cannam@130: size_t cannam@130: serd_uri_serialise(const SerdURI* uri, SerdSink sink, void* stream); cannam@130: cannam@130: /** cannam@130: Serialise `uri` relative to `base` with a series of calls to `sink`. cannam@130: cannam@130: The `uri` is written as a relative URI iff if it a child of `base` and @c cannam@130: root. The optional `root` parameter must be a prefix of `base` and can be cannam@130: used keep up-references ("../") within a certain namespace. cannam@130: */ cannam@130: SERD_API cannam@130: size_t cannam@130: serd_uri_serialise_relative(const SerdURI* uri, cannam@130: const SerdURI* base, cannam@130: const SerdURI* root, cannam@130: SerdSink sink, cannam@130: void* stream); cannam@130: cannam@130: /** cannam@130: @} cannam@130: @name Node cannam@130: @{ cannam@130: */ cannam@130: cannam@130: static const SerdNode SERD_NODE_NULL = { NULL, 0, 0, 0, SERD_NOTHING }; cannam@130: cannam@130: /** cannam@130: Make a (shallow) node from `str`. cannam@130: cannam@130: This measures, but does not copy, `str`. No memory is allocated. cannam@130: */ cannam@130: SERD_API cannam@130: SerdNode cannam@130: serd_node_from_string(SerdType type, const uint8_t* str); cannam@130: cannam@130: /** cannam@130: Make a deep copy of `node`. cannam@130: cannam@130: @return a node that the caller must free with serd_node_free(). cannam@130: */ cannam@130: SERD_API cannam@130: SerdNode cannam@130: serd_node_copy(const SerdNode* node); cannam@130: cannam@130: /** cannam@130: Return true iff `a` is equal to `b`. cannam@130: */ cannam@130: SERD_API cannam@130: bool cannam@130: serd_node_equals(const SerdNode* a, const SerdNode* b); cannam@130: cannam@130: /** cannam@130: Simple wrapper for serd_node_new_uri() to resolve a URI node. cannam@130: */ cannam@130: SERD_API cannam@130: SerdNode cannam@130: serd_node_new_uri_from_node(const SerdNode* uri_node, cannam@130: const SerdURI* base, cannam@130: SerdURI* out); cannam@130: cannam@130: /** cannam@130: Simple wrapper for serd_node_new_uri() to resolve a URI string. cannam@130: */ cannam@130: SERD_API cannam@130: SerdNode cannam@130: serd_node_new_uri_from_string(const uint8_t* str, cannam@130: const SerdURI* base, cannam@130: SerdURI* out); cannam@130: cannam@130: /** cannam@130: Create a new file URI node from a file system path and optional hostname. cannam@130: cannam@130: Backslashes in Windows paths will be converted and '%' will always be cannam@130: percent encoded. If `escape` is true, all other invalid characters will be cannam@130: percent encoded as well. cannam@130: cannam@130: If `path` is relative, `hostname` is ignored. cannam@130: If `out` is not NULL, it will be set to the parsed URI. cannam@130: */ cannam@130: SERD_API cannam@130: SerdNode cannam@130: serd_node_new_file_uri(const uint8_t* path, cannam@130: const uint8_t* hostname, cannam@130: SerdURI* out, cannam@130: bool escape); cannam@130: cannam@130: /** cannam@130: Create a new node by serialising `uri` into a new string. cannam@130: cannam@130: @param uri The URI to serialise. cannam@130: cannam@130: @param base Base URI to resolve `uri` against (or NULL for no resolution). cannam@130: cannam@130: @param out Set to the parsing of the new URI (i.e. points only to cannam@130: memory owned by the new returned node). cannam@130: */ cannam@130: SERD_API cannam@130: SerdNode cannam@130: serd_node_new_uri(const SerdURI* uri, const SerdURI* base, SerdURI* out); cannam@130: cannam@130: /** cannam@130: Create a new node by serialising `uri` into a new relative URI. cannam@130: cannam@130: @param uri The URI to serialise. cannam@130: cannam@130: @param base Base URI to make `uri` relative to, if possible. cannam@130: cannam@130: @param root Root URI for resolution (see serd_uri_serialise_relative()). cannam@130: cannam@130: @param out Set to the parsing of the new URI (i.e. points only to cannam@130: memory owned by the new returned node). cannam@130: */ cannam@130: SERD_API cannam@130: SerdNode cannam@130: serd_node_new_relative_uri(const SerdURI* uri, cannam@130: const SerdURI* base, cannam@130: const SerdURI* root, cannam@130: SerdURI* out); cannam@130: cannam@130: /** cannam@130: Create a new node by serialising `d` into an xsd:decimal string. cannam@130: cannam@130: The resulting node will always contain a `.', start with a digit, and end cannam@130: with a digit (i.e. will have a leading and/or trailing `0' if necessary). cannam@130: It will never be in scientific notation. A maximum of `frac_digits` digits cannam@130: will be written after the decimal point, but trailing zeros will cannam@130: automatically be omitted (except one if `d` is a round integer). cannam@130: cannam@130: Note that about 16 and 8 fractional digits are required to precisely cannam@130: represent a double and float, respectively. cannam@130: cannam@130: @param d The value for the new node. cannam@130: @param frac_digits The maximum number of digits after the decimal place. cannam@130: */ cannam@130: SERD_API cannam@130: SerdNode cannam@130: serd_node_new_decimal(double d, unsigned frac_digits); cannam@130: cannam@130: /** cannam@130: Create a new node by serialising `i` into an xsd:integer string. cannam@130: */ cannam@130: SERD_API cannam@130: SerdNode cannam@130: serd_node_new_integer(int64_t i); cannam@130: cannam@130: /** cannam@130: Create a node by serialising `buf` into an xsd:base64Binary string. cannam@130: This function can be used to make a serialisable node out of arbitrary cannam@130: binary data, which can be decoded using serd_base64_decode(). cannam@130: cannam@130: @param buf Raw binary input data. cannam@130: @param size Size of `buf`. cannam@130: @param wrap_lines Wrap lines at 76 characters to conform to RFC 2045. cannam@130: */ cannam@130: SERD_API cannam@130: SerdNode cannam@130: serd_node_new_blob(const void* buf, size_t size, bool wrap_lines); cannam@130: cannam@130: /** cannam@130: Free any data owned by `node`. cannam@130: cannam@130: Note that if `node` is itself dynamically allocated (which is not the case cannam@130: for nodes created internally by serd), it will not be freed. cannam@130: */ cannam@130: SERD_API cannam@130: void cannam@130: serd_node_free(SerdNode* node); cannam@130: cannam@130: /** cannam@130: @} cannam@130: @name Event Handlers cannam@130: @{ cannam@130: */ cannam@130: cannam@130: /** cannam@130: Sink (callback) for errors. cannam@130: cannam@130: @param handle Handle for user data. cannam@130: @param error Error description. cannam@130: */ cannam@130: typedef SerdStatus (*SerdErrorSink)(void* handle, cannam@130: const SerdError* error); cannam@130: cannam@130: /** cannam@130: Sink (callback) for base URI changes. cannam@130: cannam@130: Called whenever the base URI of the serialisation changes. cannam@130: */ cannam@130: typedef SerdStatus (*SerdBaseSink)(void* handle, cannam@130: const SerdNode* uri); cannam@130: cannam@130: /** cannam@130: Sink (callback) for namespace definitions. cannam@130: cannam@130: Called whenever a prefix is defined in the serialisation. cannam@130: */ cannam@130: typedef SerdStatus (*SerdPrefixSink)(void* handle, cannam@130: const SerdNode* name, cannam@130: const SerdNode* uri); cannam@130: cannam@130: /** cannam@130: Sink (callback) for statements. cannam@130: cannam@130: Called for every RDF statement in the serialisation. cannam@130: */ cannam@130: typedef SerdStatus (*SerdStatementSink)(void* handle, cannam@130: SerdStatementFlags flags, cannam@130: const SerdNode* graph, cannam@130: const SerdNode* subject, cannam@130: const SerdNode* predicate, cannam@130: const SerdNode* object, cannam@130: const SerdNode* object_datatype, cannam@130: const SerdNode* object_lang); cannam@130: cannam@130: /** cannam@130: Sink (callback) for anonymous node end markers. cannam@130: cannam@130: This is called to indicate that the anonymous node with the given cannam@130: `value` will no longer be referred to by any future statements cannam@130: (i.e. the anonymous serialisation of the node is finished). cannam@130: */ cannam@130: typedef SerdStatus (*SerdEndSink)(void* handle, cannam@130: const SerdNode* node); cannam@130: cannam@130: /** cannam@130: @} cannam@130: @name Environment cannam@130: @{ cannam@130: */ cannam@130: cannam@130: /** cannam@130: Create a new environment. cannam@130: */ cannam@130: SERD_API cannam@130: SerdEnv* cannam@130: serd_env_new(const SerdNode* base_uri); cannam@130: cannam@130: /** cannam@130: Free `ns`. cannam@130: */ cannam@130: SERD_API cannam@130: void cannam@130: serd_env_free(SerdEnv* env); cannam@130: cannam@130: /** cannam@130: Get the current base URI. cannam@130: */ cannam@130: SERD_API cannam@130: const SerdNode* cannam@130: serd_env_get_base_uri(const SerdEnv* env, cannam@130: SerdURI* out); cannam@130: cannam@130: /** cannam@130: Set the current base URI. cannam@130: */ cannam@130: SERD_API cannam@130: SerdStatus cannam@130: serd_env_set_base_uri(SerdEnv* env, cannam@130: const SerdNode* uri); cannam@130: cannam@130: /** cannam@130: Set a namespace prefix. cannam@130: */ cannam@130: SERD_API cannam@130: SerdStatus cannam@130: serd_env_set_prefix(SerdEnv* env, cannam@130: const SerdNode* name, cannam@130: const SerdNode* uri); cannam@130: cannam@130: /** cannam@130: Set a namespace prefix. cannam@130: */ cannam@130: SERD_API cannam@130: SerdStatus cannam@130: serd_env_set_prefix_from_strings(SerdEnv* env, cannam@130: const uint8_t* name, cannam@130: const uint8_t* uri); cannam@130: cannam@130: /** cannam@130: Qualify `uri` into a CURIE if possible. cannam@130: */ cannam@130: SERD_API cannam@130: bool cannam@130: serd_env_qualify(const SerdEnv* env, cannam@130: const SerdNode* uri, cannam@130: SerdNode* prefix, cannam@130: SerdChunk* suffix); cannam@130: cannam@130: /** cannam@130: Expand `curie`. cannam@130: */ cannam@130: SERD_API cannam@130: SerdStatus cannam@130: serd_env_expand(const SerdEnv* env, cannam@130: const SerdNode* curie, cannam@130: SerdChunk* uri_prefix, cannam@130: SerdChunk* uri_suffix); cannam@130: cannam@130: /** cannam@130: Expand `node`, which must be a CURIE or URI, to a full URI. cannam@130: */ cannam@130: SERD_API cannam@130: SerdNode cannam@130: serd_env_expand_node(const SerdEnv* env, cannam@130: const SerdNode* node); cannam@130: cannam@130: /** cannam@130: Call `func` for each prefix defined in `env`. cannam@130: */ cannam@130: SERD_API cannam@130: void cannam@130: serd_env_foreach(const SerdEnv* env, cannam@130: SerdPrefixSink func, cannam@130: void* handle); cannam@130: cannam@130: /** cannam@130: @} cannam@130: @name Reader cannam@130: @{ cannam@130: */ cannam@130: cannam@130: /** cannam@130: Create a new RDF reader. cannam@130: */ cannam@130: SERD_API cannam@130: SerdReader* cannam@130: serd_reader_new(SerdSyntax syntax, cannam@130: void* handle, cannam@130: void (*free_handle)(void*), cannam@130: SerdBaseSink base_sink, cannam@130: SerdPrefixSink prefix_sink, cannam@130: SerdStatementSink statement_sink, cannam@130: SerdEndSink end_sink); cannam@130: cannam@130: /** cannam@130: Enable or disable strict parsing. cannam@130: cannam@130: The reader is non-strict (lax) by default, which will tolerate URIs with cannam@130: invalid characters. Setting strict will fail when parsing such files. An cannam@130: error is printed for invalid input in either case. cannam@130: */ cannam@130: SERD_API cannam@130: void cannam@130: serd_reader_set_strict(SerdReader* reader, bool strict); cannam@130: cannam@130: /** cannam@130: Set a function to be called when errors occur during reading. cannam@130: cannam@130: The `error_sink` will be called with `handle` as its first argument. If cannam@130: no error function is set, errors are printed to stderr in GCC style. cannam@130: */ cannam@130: SERD_API cannam@130: void cannam@130: serd_reader_set_error_sink(SerdReader* reader, cannam@130: SerdErrorSink error_sink, cannam@130: void* handle); cannam@130: cannam@130: /** cannam@130: Return the `handle` passed to serd_reader_new(). cannam@130: */ cannam@130: SERD_API cannam@130: void* cannam@130: serd_reader_get_handle(const SerdReader* reader); cannam@130: cannam@130: /** cannam@130: Set a prefix to be added to all blank node identifiers. cannam@130: cannam@130: This is useful when multiple files are to be parsed into the same output cannam@130: (e.g. a store, or other files). Since Serd preserves blank node IDs, this cannam@130: could cause conflicts where two non-equivalent blank nodes are merged, cannam@130: resulting in corrupt data. By setting a unique blank node prefix for each cannam@130: parsed file, this can be avoided, while preserving blank node names. cannam@130: */ cannam@130: SERD_API cannam@130: void cannam@130: serd_reader_add_blank_prefix(SerdReader* reader, cannam@130: const uint8_t* prefix); cannam@130: cannam@130: /** cannam@130: Set the URI of the default graph. cannam@130: cannam@130: If this is set, the reader will emit quads with the graph set to the given cannam@130: node for any statements that are not in a named graph (which is currently cannam@130: all of them since Serd currently does not support any graph syntaxes). cannam@130: */ cannam@130: SERD_API cannam@130: void cannam@130: serd_reader_set_default_graph(SerdReader* reader, cannam@130: const SerdNode* graph); cannam@130: cannam@130: /** cannam@130: Read a file at a given `uri`. cannam@130: */ cannam@130: SERD_API cannam@130: SerdStatus cannam@130: serd_reader_read_file(SerdReader* reader, cannam@130: const uint8_t* uri); cannam@130: cannam@130: /** cannam@130: Start an incremental read from a file handle. cannam@130: cannam@130: Iff `bulk` is true, `file` will be read a page at a time. This is more cannam@130: efficient, but uses a page of memory and means that an entire page of input cannam@130: must be ready before any callbacks will fire. To react as soon as input cannam@130: arrives, set `bulk` to false. cannam@130: */ cannam@130: SERD_API cannam@130: SerdStatus cannam@130: serd_reader_start_stream(SerdReader* me, cannam@130: FILE* file, cannam@130: const uint8_t* name, cannam@130: bool bulk); cannam@130: cannam@130: /** cannam@130: Read a single "chunk" of data during an incremental read. cannam@130: cannam@130: This function will read a single top level description, and return. This cannam@130: may be a directive, statement, or several statements; essentially it reads cannam@130: until a '.' is encountered. This is particularly useful for reading cannam@130: directly from a pipe or socket. cannam@130: */ cannam@130: SERD_API cannam@130: SerdStatus cannam@130: serd_reader_read_chunk(SerdReader* me); cannam@130: cannam@130: /** cannam@130: Finish an incremental read from a file handle. cannam@130: */ cannam@130: SERD_API cannam@130: SerdStatus cannam@130: serd_reader_end_stream(SerdReader* me); cannam@130: cannam@130: /** cannam@130: Read `file`. cannam@130: */ cannam@130: SERD_API cannam@130: SerdStatus cannam@130: serd_reader_read_file_handle(SerdReader* reader, cannam@130: FILE* file, cannam@130: const uint8_t* name); cannam@130: cannam@130: /** cannam@130: Read `utf8`. cannam@130: */ cannam@130: SERD_API cannam@130: SerdStatus cannam@130: serd_reader_read_string(SerdReader* me, const uint8_t* utf8); cannam@130: cannam@130: /** cannam@130: Free `reader`. cannam@130: */ cannam@130: SERD_API cannam@130: void cannam@130: serd_reader_free(SerdReader* reader); cannam@130: cannam@130: /** cannam@130: @} cannam@130: @name Writer cannam@130: @{ cannam@130: */ cannam@130: cannam@130: /** cannam@130: Create a new RDF writer. cannam@130: */ cannam@130: SERD_API cannam@130: SerdWriter* cannam@130: serd_writer_new(SerdSyntax syntax, cannam@130: SerdStyle style, cannam@130: SerdEnv* env, cannam@130: const SerdURI* base_uri, cannam@130: SerdSink sink, cannam@130: void* stream); cannam@130: cannam@130: /** cannam@130: Free `writer`. cannam@130: */ cannam@130: SERD_API cannam@130: void cannam@130: serd_writer_free(SerdWriter* writer); cannam@130: cannam@130: /** cannam@130: Return the env used by `writer`. cannam@130: */ cannam@130: SERD_API cannam@130: SerdEnv* cannam@130: serd_writer_get_env(SerdWriter* writer); cannam@130: cannam@130: /** cannam@130: A convenience sink function for writing to a FILE*. cannam@130: cannam@130: This function can be used as a SerdSink when writing to a FILE*. The cannam@130: `stream` parameter must be a FILE* opened for writing. cannam@130: */ cannam@130: SERD_API cannam@130: size_t cannam@130: serd_file_sink(const void* buf, size_t len, void* stream); cannam@130: cannam@130: /** cannam@130: A convenience sink function for writing to a string. cannam@130: cannam@130: This function can be used as a SerdSink to write to a SerdChunk which is cannam@130: resized as necessary with realloc(). The `stream` parameter must point to cannam@130: an initialized SerdChunk. When the write is finished, the string should be cannam@130: retrieved with serd_chunk_sink_finish(). cannam@130: */ cannam@130: SERD_API cannam@130: size_t cannam@130: serd_chunk_sink(const void* buf, size_t len, void* stream); cannam@130: cannam@130: /** cannam@130: Finish a serialisation to a chunk with serd_chunk_sink(). cannam@130: cannam@130: The returned string is the result of the serialisation, which is NULL cannam@130: terminated (by this function) and owned by the caller. cannam@130: */ cannam@130: SERD_API cannam@130: uint8_t* cannam@130: serd_chunk_sink_finish(SerdChunk* stream); cannam@130: cannam@130: /** cannam@130: Set a function to be called when errors occur during writing. cannam@130: cannam@130: The `error_sink` will be called with `handle` as its first argument. If cannam@130: no error function is set, errors are printed to stderr. cannam@130: */ cannam@130: SERD_API cannam@130: void cannam@130: serd_writer_set_error_sink(SerdWriter* writer, cannam@130: SerdErrorSink error_sink, cannam@130: void* handle); cannam@130: cannam@130: /** cannam@130: Set a prefix to be removed from matching blank node identifiers. cannam@130: */ cannam@130: SERD_API cannam@130: void cannam@130: serd_writer_chop_blank_prefix(SerdWriter* writer, cannam@130: const uint8_t* prefix); cannam@130: cannam@130: /** cannam@130: Set the current output base URI (and emit directive if applicable). cannam@130: cannam@130: Note this function can be safely casted to SerdBaseSink. cannam@130: */ cannam@130: SERD_API cannam@130: SerdStatus cannam@130: serd_writer_set_base_uri(SerdWriter* writer, cannam@130: const SerdNode* uri); cannam@130: cannam@130: /** cannam@130: Set the current root URI. cannam@130: cannam@130: The root URI should be a prefix of the base URI. The path of the root URI cannam@130: is the highest path any relative up-reference can refer to. For example, cannam@130: with root and base , cannam@130: will be written as <../>, but will be cannam@130: written non-relatively as . If the root is not explicitly set, cannam@130: it defaults to the base URI, so no up-references will be created at all. cannam@130: */ cannam@130: SERD_API cannam@130: SerdStatus cannam@130: serd_writer_set_root_uri(SerdWriter* writer, cannam@130: const SerdNode* uri); cannam@130: cannam@130: /** cannam@130: Set a namespace prefix (and emit directive if applicable). cannam@130: cannam@130: Note this function can be safely casted to SerdPrefixSink. cannam@130: */ cannam@130: SERD_API cannam@130: SerdStatus cannam@130: serd_writer_set_prefix(SerdWriter* writer, cannam@130: const SerdNode* name, cannam@130: const SerdNode* uri); cannam@130: cannam@130: /** cannam@130: Write a statement. cannam@130: cannam@130: Note this function can be safely casted to SerdStatementSink. cannam@130: */ cannam@130: SERD_API cannam@130: SerdStatus cannam@130: serd_writer_write_statement(SerdWriter* writer, cannam@130: SerdStatementFlags flags, cannam@130: const SerdNode* graph, cannam@130: const SerdNode* subject, cannam@130: const SerdNode* predicate, cannam@130: const SerdNode* object, cannam@130: const SerdNode* object_datatype, cannam@130: const SerdNode* object_lang); cannam@130: cannam@130: /** cannam@130: Mark the end of an anonymous node's description. cannam@130: cannam@130: Note this function can be safely casted to SerdEndSink. cannam@130: */ cannam@130: SERD_API cannam@130: SerdStatus cannam@130: serd_writer_end_anon(SerdWriter* writer, cannam@130: const SerdNode* node); cannam@130: cannam@130: /** cannam@130: Finish a write. cannam@130: */ cannam@130: SERD_API cannam@130: SerdStatus cannam@130: serd_writer_finish(SerdWriter* writer); cannam@130: cannam@130: /** cannam@130: @} cannam@130: @} cannam@130: */ cannam@130: cannam@130: #ifdef __cplusplus cannam@130: } /* extern "C" */ cannam@130: #endif cannam@130: cannam@130: #endif /* SERD_SERD_H */