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