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.

serd.h 26KB

12 years ago
9 years ago
12 years ago
9 years ago
12 years ago
12 years ago
12 years ago
12 years ago
9 years ago
12 years ago
12 years ago
12 years ago
9 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
9 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
9 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  1. /*
  2. Copyright 2011-2015 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 = {{0,0},{0,0},{0,0},{0,0},{0,0},{0,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. The returned path and `*hostname` must be freed with free().
  288. */
  289. SERD_API
  290. uint8_t*
  291. serd_file_uri_parse(const uint8_t* uri, uint8_t** hostname);
  292. /**
  293. Return true iff `utf8` starts with a valid URI scheme.
  294. */
  295. SERD_API
  296. bool
  297. serd_uri_string_has_scheme(const uint8_t* utf8);
  298. /**
  299. Parse `utf8`, writing result to `out`.
  300. */
  301. SERD_API
  302. SerdStatus
  303. serd_uri_parse(const uint8_t* utf8, SerdURI* out);
  304. /**
  305. Set `out` to `uri` resolved against `base`.
  306. */
  307. SERD_API
  308. void
  309. serd_uri_resolve(const SerdURI* uri, const SerdURI* base, SerdURI* out);
  310. /**
  311. Sink function for raw string output.
  312. */
  313. typedef size_t (*SerdSink)(const void* buf, size_t len, void* stream);
  314. /**
  315. Serialise `uri` with a series of calls to `sink`.
  316. */
  317. SERD_API
  318. size_t
  319. serd_uri_serialise(const SerdURI* uri, SerdSink sink, void* stream);
  320. /**
  321. Serialise `uri` relative to `base` with a series of calls to `sink`.
  322. The `uri` is written as a relative URI iff if it a child of `base` and @c
  323. root. The optional `root` parameter must be a prefix of `base` and can be
  324. used keep up-references ("../") within a certain namespace.
  325. */
  326. SERD_API
  327. size_t
  328. serd_uri_serialise_relative(const SerdURI* uri,
  329. const SerdURI* base,
  330. const SerdURI* root,
  331. SerdSink sink,
  332. void* stream);
  333. /**
  334. @}
  335. @name Node
  336. @{
  337. */
  338. static const SerdNode SERD_NODE_NULL = { 0, 0, 0, 0, SERD_NOTHING };
  339. /**
  340. Make a (shallow) node from `str`.
  341. This measures, but does not copy, `str`. No memory is allocated.
  342. */
  343. SERD_API
  344. SerdNode
  345. serd_node_from_string(SerdType type, const uint8_t* str);
  346. /**
  347. Make a deep copy of `node`.
  348. @return a node that the caller must free with serd_node_free().
  349. */
  350. SERD_API
  351. SerdNode
  352. serd_node_copy(const SerdNode* node);
  353. /**
  354. Return true iff `a` is equal to `b`.
  355. */
  356. SERD_API
  357. bool
  358. serd_node_equals(const SerdNode* a, const SerdNode* b);
  359. /**
  360. Simple wrapper for serd_node_new_uri to resolve a URI node.
  361. */
  362. SERD_API
  363. SerdNode
  364. serd_node_new_uri_from_node(const SerdNode* uri_node,
  365. const SerdURI* base,
  366. SerdURI* out);
  367. /**
  368. Simple wrapper for serd_node_new_uri to resolve a URI string.
  369. */
  370. SERD_API
  371. SerdNode
  372. serd_node_new_uri_from_string(const uint8_t* str,
  373. const SerdURI* base,
  374. SerdURI* out);
  375. /**
  376. Create a new file URI node from a file system path and optional hostname.
  377. Backslashes in Windows paths will be converted and '%' will always be
  378. percent encoded. If `escape` is true, all other invalid characters will be
  379. percent encoded as well.
  380. If `path` is relative, `hostname` is ignored.
  381. If `out` is not NULL, it will be set to the parsed URI.
  382. */
  383. SERD_API
  384. SerdNode
  385. serd_node_new_file_uri(const uint8_t* path,
  386. const uint8_t* hostname,
  387. SerdURI* out,
  388. bool escape);
  389. /**
  390. Create a new node by serialising `uri` into a new string.
  391. @param uri The URI to parse and serialise.
  392. @param base Base URI to resolve `uri` against (or NULL for no resolution).
  393. @param out Set to the parsing of the new URI (i.e. points only to
  394. memory owned by the new returned node).
  395. */
  396. SERD_API
  397. SerdNode
  398. serd_node_new_uri(const SerdURI* uri, const SerdURI* base, SerdURI* out);
  399. /**
  400. Create a new node by serialising `d` into an xsd:decimal string.
  401. The resulting node will always contain a `.', start with a digit, and end
  402. with a digit (i.e. will have a leading and/or trailing `0' if necessary).
  403. It will never be in scientific notation. A maximum of `frac_digits` digits
  404. will be written after the decimal point, but trailing zeros will
  405. automatically be omitted (except one if `d` is a round integer).
  406. Note that about 16 and 8 fractional digits are required to precisely
  407. represent a double and float, respectively.
  408. @param d The value for the new node.
  409. @param frac_digits The maximum number of digits after the decimal place.
  410. */
  411. SERD_API
  412. SerdNode
  413. serd_node_new_decimal(double d, unsigned frac_digits);
  414. /**
  415. Create a new node by serialising `i` into an xsd:integer string.
  416. */
  417. SERD_API
  418. SerdNode
  419. serd_node_new_integer(int64_t i);
  420. /**
  421. Create a node by serialising `buf` into an xsd:base64Binary string.
  422. This function can be used to make a serialisable node out of arbitrary
  423. binary data, which can be decoded using serd_base64_decode().
  424. @param buf Raw binary input data.
  425. @param size Size of `buf`.
  426. @param wrap_lines Wrap lines at 76 characters to conform to RFC 2045.
  427. */
  428. SERD_API
  429. SerdNode
  430. serd_node_new_blob(const void* buf, size_t size, bool wrap_lines);
  431. /**
  432. Free any data owned by `node`.
  433. Note that if `node` is itself dynamically allocated (which is not the case
  434. for nodes created internally by serd), it will not be freed.
  435. */
  436. SERD_API
  437. void
  438. serd_node_free(SerdNode* node);
  439. /**
  440. @}
  441. @name Event Handlers
  442. @{
  443. */
  444. /**
  445. Sink (callback) for errors.
  446. @param handle Handle for user data.
  447. @param error Error description.
  448. */
  449. typedef SerdStatus (*SerdErrorSink)(void* handle,
  450. const SerdError* error);
  451. /**
  452. Sink (callback) for base URI changes.
  453. Called whenever the base URI of the serialisation changes.
  454. */
  455. typedef SerdStatus (*SerdBaseSink)(void* handle,
  456. const SerdNode* uri);
  457. /**
  458. Sink (callback) for namespace definitions.
  459. Called whenever a prefix is defined in the serialisation.
  460. */
  461. typedef SerdStatus (*SerdPrefixSink)(void* handle,
  462. const SerdNode* name,
  463. const SerdNode* uri);
  464. /**
  465. Sink (callback) for statements.
  466. Called for every RDF statement in the serialisation.
  467. */
  468. typedef SerdStatus (*SerdStatementSink)(void* handle,
  469. SerdStatementFlags flags,
  470. const SerdNode* graph,
  471. const SerdNode* subject,
  472. const SerdNode* predicate,
  473. const SerdNode* object,
  474. const SerdNode* object_datatype,
  475. const SerdNode* object_lang);
  476. /**
  477. Sink (callback) for anonymous node end markers.
  478. This is called to indicate that the anonymous node with the given
  479. `value` will no longer be referred to by any future statements
  480. (i.e. the anonymous serialisation of the node is finished).
  481. */
  482. typedef SerdStatus (*SerdEndSink)(void* handle,
  483. const SerdNode* node);
  484. /**
  485. @}
  486. @name Environment
  487. @{
  488. */
  489. /**
  490. Create a new environment.
  491. */
  492. SERD_API
  493. SerdEnv*
  494. serd_env_new(const SerdNode* base_uri);
  495. /**
  496. Free `ns`.
  497. */
  498. SERD_API
  499. void
  500. serd_env_free(SerdEnv* env);
  501. /**
  502. Get the current base URI.
  503. */
  504. SERD_API
  505. const SerdNode*
  506. serd_env_get_base_uri(const SerdEnv* env,
  507. SerdURI* out);
  508. /**
  509. Set the current base URI.
  510. */
  511. SERD_API
  512. SerdStatus
  513. serd_env_set_base_uri(SerdEnv* env,
  514. const SerdNode* uri);
  515. /**
  516. Set a namespace prefix.
  517. */
  518. SERD_API
  519. SerdStatus
  520. serd_env_set_prefix(SerdEnv* env,
  521. const SerdNode* name,
  522. const SerdNode* uri);
  523. /**
  524. Set a namespace prefix.
  525. */
  526. SERD_API
  527. SerdStatus
  528. serd_env_set_prefix_from_strings(SerdEnv* env,
  529. const uint8_t* name,
  530. const uint8_t* uri);
  531. /**
  532. Qualify `uri` into a CURIE if possible.
  533. */
  534. SERD_API
  535. bool
  536. serd_env_qualify(const SerdEnv* env,
  537. const SerdNode* uri,
  538. SerdNode* prefix,
  539. SerdChunk* suffix);
  540. /**
  541. Expand `curie`.
  542. */
  543. SERD_API
  544. SerdStatus
  545. serd_env_expand(const SerdEnv* env,
  546. const SerdNode* curie,
  547. SerdChunk* uri_prefix,
  548. SerdChunk* uri_suffix);
  549. /**
  550. Expand `node`, which must be a CURIE or URI, to a full URI.
  551. */
  552. SERD_API
  553. SerdNode
  554. serd_env_expand_node(const SerdEnv* env,
  555. const SerdNode* node);
  556. /**
  557. Call `func` for each prefix defined in `env`.
  558. */
  559. SERD_API
  560. void
  561. serd_env_foreach(const SerdEnv* env,
  562. SerdPrefixSink func,
  563. void* handle);
  564. /**
  565. @}
  566. @name Reader
  567. @{
  568. */
  569. /**
  570. Create a new RDF reader.
  571. */
  572. SERD_API
  573. SerdReader*
  574. serd_reader_new(SerdSyntax syntax,
  575. void* handle,
  576. void (*free_handle)(void*),
  577. SerdBaseSink base_sink,
  578. SerdPrefixSink prefix_sink,
  579. SerdStatementSink statement_sink,
  580. SerdEndSink end_sink);
  581. /**
  582. Enable or disable strict parsing.
  583. The reader is non-strict (lax) by default, which will tolerate URIs with
  584. invalid characters. Setting strict will fail when parsing such files. An
  585. error is printed for invalid input in either case.
  586. */
  587. SERD_API
  588. void
  589. serd_reader_set_strict(SerdReader* reader, bool strict);
  590. /**
  591. Set a function to be called when errors occur during reading.
  592. The `error_sink` will be called with `handle` as its first argument. If
  593. no error function is set, errors are printed to stderr in GCC style.
  594. */
  595. SERD_API
  596. void
  597. serd_reader_set_error_sink(SerdReader* reader,
  598. SerdErrorSink error_sink,
  599. void* handle);
  600. /**
  601. Return the `handle` passed to serd_reader_new().
  602. */
  603. SERD_API
  604. void*
  605. serd_reader_get_handle(const SerdReader* reader);
  606. /**
  607. Set a prefix to be added to all blank node identifiers.
  608. This is useful when multiple files are to be parsed into the same output
  609. (e.g. a store, or other files). Since Serd preserves blank node IDs, this
  610. could cause conflicts where two non-equivalent blank nodes are merged,
  611. resulting in corrupt data. By setting a unique blank node prefix for each
  612. parsed file, this can be avoided, while preserving blank node names.
  613. */
  614. SERD_API
  615. void
  616. serd_reader_add_blank_prefix(SerdReader* reader,
  617. const uint8_t* prefix);
  618. /**
  619. Set the URI of the default graph.
  620. If this is set, the reader will emit quads with the graph set to the given
  621. node for any statements that are not in a named graph (which is currently
  622. all of them since Serd currently does not support any graph syntaxes).
  623. */
  624. SERD_API
  625. void
  626. serd_reader_set_default_graph(SerdReader* reader,
  627. const SerdNode* graph);
  628. /**
  629. Read a file at a given `uri`.
  630. */
  631. SERD_API
  632. SerdStatus
  633. serd_reader_read_file(SerdReader* reader,
  634. const uint8_t* uri);
  635. /**
  636. Start an incremental read from a file handle.
  637. Iff `bulk` is true, `file` will be read a page at a time. This is more
  638. efficient, but uses a page of memory and means that an entire page of input
  639. must be ready before any callbacks will fire. To react as soon as input
  640. arrives, set `bulk` to false.
  641. */
  642. SERD_API
  643. SerdStatus
  644. serd_reader_start_stream(SerdReader* me,
  645. FILE* file,
  646. const uint8_t* name,
  647. bool bulk);
  648. /**
  649. Read a single "chunk" of data during an incremental read.
  650. This function will read a single top level description, and return. This
  651. may be a directive, statement, or several statements; essentially it reads
  652. until a '.' is encountered. This is particularly useful for reading
  653. directly from a pipe or socket.
  654. */
  655. SERD_API
  656. SerdStatus
  657. serd_reader_read_chunk(SerdReader* me);
  658. /**
  659. Finish an incremental read from a file handle.
  660. */
  661. SERD_API
  662. SerdStatus
  663. serd_reader_end_stream(SerdReader* me);
  664. /**
  665. Read `file`.
  666. */
  667. SERD_API
  668. SerdStatus
  669. serd_reader_read_file_handle(SerdReader* reader,
  670. FILE* file,
  671. const uint8_t* name);
  672. /**
  673. Read `utf8`.
  674. */
  675. SERD_API
  676. SerdStatus
  677. serd_reader_read_string(SerdReader* me, const uint8_t* utf8);
  678. /**
  679. Free `reader`.
  680. */
  681. SERD_API
  682. void
  683. serd_reader_free(SerdReader* reader);
  684. /**
  685. @}
  686. @name Writer
  687. @{
  688. */
  689. /**
  690. Create a new RDF writer.
  691. */
  692. SERD_API
  693. SerdWriter*
  694. serd_writer_new(SerdSyntax syntax,
  695. SerdStyle style,
  696. SerdEnv* env,
  697. const SerdURI* base_uri,
  698. SerdSink sink,
  699. void* stream);
  700. /**
  701. Free `writer`.
  702. */
  703. SERD_API
  704. void
  705. serd_writer_free(SerdWriter* writer);
  706. /**
  707. Return the env used by `writer`.
  708. */
  709. SERD_API
  710. SerdEnv*
  711. serd_writer_get_env(SerdWriter* writer);
  712. /**
  713. A convenience sink function for writing to a FILE*.
  714. This function can be used as a SerdSink when writing to a FILE*. The
  715. `stream` parameter must be a FILE* opened for writing.
  716. */
  717. SERD_API
  718. size_t
  719. serd_file_sink(const void* buf, size_t len, void* stream);
  720. /**
  721. A convenience sink function for writing to a string.
  722. This function can be used as a SerdSink to write to a SerdChunk which is
  723. resized as necessary with realloc(). The `stream` parameter must point to
  724. an initialized SerdChunk. When the write is finished, the string should be
  725. retrieved with serd_chunk_sink_finish().
  726. */
  727. SERD_API
  728. size_t
  729. serd_chunk_sink(const void* buf, size_t len, void* stream);
  730. /**
  731. Finish a serialisation to a chunk with serd_chunk_sink().
  732. The returned string is the result of the serialisation, which is NULL
  733. terminated (by this function) and owned by the caller.
  734. */
  735. SERD_API
  736. uint8_t*
  737. serd_chunk_sink_finish(SerdChunk* stream);
  738. /**
  739. Set a function to be called when errors occur during writing.
  740. The `error_sink` will be called with `handle` as its first argument. If
  741. no error function is set, errors are printed to stderr.
  742. */
  743. SERD_API
  744. void
  745. serd_writer_set_error_sink(SerdWriter* writer,
  746. SerdErrorSink error_sink,
  747. void* handle);
  748. /**
  749. Set a prefix to be removed from matching blank node identifiers.
  750. */
  751. SERD_API
  752. void
  753. serd_writer_chop_blank_prefix(SerdWriter* writer,
  754. const uint8_t* prefix);
  755. /**
  756. Set the current output base URI (and emit directive if applicable).
  757. Note this function can be safely casted to SerdBaseSink.
  758. */
  759. SERD_API
  760. SerdStatus
  761. serd_writer_set_base_uri(SerdWriter* writer,
  762. const SerdNode* uri);
  763. /**
  764. Set the current root URI.
  765. The root URI should be a prefix of the base URI. The path of the root URI
  766. is the highest path any relative up-reference can refer to. For example,
  767. with root <file:///foo/root> and base <file:///foo/root/base>,
  768. <file:///foo/root> will be written as <../>, but <file:///foo> will be
  769. written non-relatively as <file:///foo>. If the root is not explicitly set,
  770. it defaults to the base URI, so no up-references will be created at all.
  771. */
  772. SERD_API
  773. SerdStatus
  774. serd_writer_set_root_uri(SerdWriter* writer,
  775. const SerdNode* uri);
  776. /**
  777. Set a namespace prefix (and emit directive if applicable).
  778. Note this function can be safely casted to SerdPrefixSink.
  779. */
  780. SERD_API
  781. SerdStatus
  782. serd_writer_set_prefix(SerdWriter* writer,
  783. const SerdNode* name,
  784. const SerdNode* uri);
  785. /**
  786. Write a statement.
  787. Note this function can be safely casted to SerdStatementSink.
  788. */
  789. SERD_API
  790. SerdStatus
  791. serd_writer_write_statement(SerdWriter* writer,
  792. SerdStatementFlags flags,
  793. const SerdNode* graph,
  794. const SerdNode* subject,
  795. const SerdNode* predicate,
  796. const SerdNode* object,
  797. const SerdNode* object_datatype,
  798. const SerdNode* object_lang);
  799. /**
  800. Mark the end of an anonymous node's description.
  801. Note this function can be safely casted to SerdEndSink.
  802. */
  803. SERD_API
  804. SerdStatus
  805. serd_writer_end_anon(SerdWriter* writer,
  806. const SerdNode* node);
  807. /**
  808. Finish a write.
  809. */
  810. SERD_API
  811. SerdStatus
  812. serd_writer_finish(SerdWriter* writer);
  813. /**
  814. @}
  815. @}
  816. */
  817. #ifdef __cplusplus
  818. } /* extern "C" */
  819. #endif
  820. #endif /* SERD_SERD_H */