Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

964 lines
25KB

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