codec.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #ifndef _LSUP_CODEC_BASE_H
  2. #define _LSUP_CODEC_BASE_H
  3. #include "graph.h"
  4. /**
  5. * Max data size passed to the scanner and parser at each iteration.
  6. */
  7. #ifdef LSUP_RDF_STREAM_CHUNK_SIZE
  8. #define CHUNK_SIZE LSUP_RDF_STREAM_CHUNK_SIZE
  9. #else
  10. #define CHUNK_SIZE 8192
  11. #endif
  12. typedef struct codec_t LSUP_Codec;
  13. /// Parser state.
  14. typedef struct {
  15. LSUP_GraphIterator * it; ///< Iterator used to build the graph.
  16. LSUP_NSMap * nsm; ///< NS map used in the document.
  17. LSUP_Term * base; ///< Base IRI used in the document.
  18. size_t ct; ///< Statements parsed.
  19. LSUP_rc rc; ///< Internal return code.
  20. } LSUP_TTLParserState;
  21. /** @brief Parse error information.
  22. *
  23. */
  24. /* TODO A plain string will suffice for now.
  25. typedef struct parse_error_t {
  26. unsigned int line; // Line number where the error occurred.
  27. unsigned int linec; // Position in line of the offending token.
  28. char * token; // String representation of the token.
  29. } LSUP_ParseError;
  30. */
  31. /*
  32. * Interface prototypes.
  33. */
  34. /** @brief Term encoder callback type.
  35. *
  36. * @param[in] term Single term handle.
  37. *
  38. * @param[in] nsm Namespace map. May be NULL for no prefix shortening.
  39. *
  40. * @param[out] rep Pointer to a string to be filled with the encoded term. The
  41. * caller is in charge of freeing the string after use. Returns undefined on
  42. * error.
  43. *
  44. * @return LSUP_OK on successful encoding; <0 for other errors.
  45. */
  46. typedef LSUP_rc (*term_enc_fn_t)(
  47. const LSUP_Term *term, const LSUP_NSMap *nsm, char **rep);
  48. /** @brief Initialize a graph encoding loop.
  49. *
  50. * This prototype is to be implemented by graph encoding loops. It should
  51. * create an iterator and perform all initial setup for finding triples.
  52. *
  53. * Implementations MUST set the "codec" member of the iterator to the address
  54. * of the codec that generated it.
  55. *
  56. * @param[in] gr The graph to be encoded. The graph's namespace map is used by
  57. * the codec for namespace prefixing. The graph may only be freed after the
  58. * loop is finalized.
  59. *
  60. * @return A codec iterator handle to be passed to a #gr_codec_iter_fn_t
  61. * function and, eventually, to a #gr_codec_done_fn_t function. This
  62. * structure is opaque and defined by each codec according to its own needs.
  63. */
  64. typedef void * (*gr_encode_init_fn_t)(const LSUP_Graph *gr);
  65. /** @brief Perform one encoding iteration.
  66. *
  67. * Implementations of this prototype MUST perform all the steps to encode one
  68. * or more complete triples into an RDF fragment representing those triples.
  69. * The input and output units are up to the implementation and a caller SHOULD
  70. * assume that multiple lines may be yielded at each iteration.
  71. *
  72. * @param[in] it Iterator handle.
  73. *
  74. * @param[out] res Handle to be populated with a string obtained from encoding.
  75. * The output data should be UTF-8 encoded. This pointer must be initialized
  76. * (even to NULL) and should be eventually freed manually at the end of the
  77. * loop. It is reallocated at each iteration, so memory from a previous
  78. * iteration may be overwritten with new data.
  79. *
  80. * @return LSUP_OK if a new token was processed; LSUP_END if the end of the
  81. * loop was reached.
  82. */
  83. typedef LSUP_rc (*gr_encode_iter_fn_t)(void *it, char **res);
  84. /** @brief Finalize an encoding operation.
  85. *
  86. * Implementations SHOULD use this function to perform all necessary steps to
  87. * clean up memory and free the iterator handle after a graph has been
  88. * completely encoded.
  89. *
  90. * @param[in] it Iterator handle.
  91. */
  92. typedef void (*gr_encode_done_fn_t)(void *it);
  93. /** @brief Prototype for decoding a string into a LSUP_Term.
  94. *
  95. * Implementations MAY ignore any other tokens after finding the first one.
  96. *
  97. * @param[in] rep NT representation of the term.
  98. *
  99. * @param[in] nsm Namespace map handle.
  100. *
  101. * @param[out] Pointer to the term handle to be created. Implementaions SHOULD
  102. * return NULL on a parse error.
  103. *
  104. * @return Implementations MUST return LSUP_OK on success and a negative value
  105. * on parsing error.
  106. */
  107. typedef LSUP_rc (*term_decode_fn_t)(
  108. const char *rep, const LSUP_NSMap *nsm, LSUP_Term **term);
  109. /** @brief Prototype for decoding a complete RDF document into a graph.
  110. *
  111. * Implementations SHOULD consume data from the file handle in chunks.
  112. *
  113. * @param[in] rep Open file handle pointing to the RDF data. Implementations
  114. * MUST NOT close the file handle.
  115. *
  116. * @param[out] gr Pointer to a graph handle to be generated from decoding.
  117. *
  118. * @param[out] ct If not NULL, it may be populated with the number of triples
  119. * parsed (which may be different from the resulting graph size).
  120. * Implementations may choose not not use this, and they must account for the
  121. * value to be NULL.
  122. *
  123. * @param[out] err Pointer to error info string. If no error occurs, it yields
  124. * NULL.
  125. *
  126. * @return Implementations MUST return LSUP_OK on success and a negative value
  127. * on parsing error.
  128. */
  129. typedef LSUP_rc (*gr_decode_fn_t)(
  130. FILE *rep, LSUP_Graph **gr, size_t *ct, char **err);
  131. /** @brief Codec structure.
  132. *
  133. * An instance of this structure is usually defined at compile time (see
  134. * examples in "include/codec_*.h" and "src/codec_*.c") and should have the
  135. * following defined:
  136. *
  137. * - name: A brief (16-char max), human-readable to identify the codec.
  138. * - mimetype: MIME type (32-char max) associated with the codec.
  139. * - extension: File extension associated with the serialized file.
  140. *
  141. * - encode_term: Encode a single term.
  142. *
  143. * - encode_graph_init: Initialize a graph decoding loop.
  144. * - encode_graph_iter: Run one iteration of encoding on one or more triples.
  145. * - encode_graph_done: Finalize the encoding loop and free the support data.
  146. *
  147. * - decode_term: Decode a single term.
  148. * - decode_graph: Decode a RDF document into a graph.
  149. *
  150. * For documentation on the individual encoding and decoding callbacks, see the
  151. * related function prototypes.
  152. */
  153. struct codec_t {
  154. char name[16]; // Name of the codec.
  155. char mimetype[32]; // MIME type associated with the codec.
  156. char extension[8]; // Serialized file extension.
  157. // Encoding.
  158. term_enc_fn_t encode_term; // Term encoder function.
  159. gr_encode_init_fn_t encode_graph_init; // Graph encoder initialization.
  160. gr_encode_iter_fn_t encode_graph_iter; // Graph encoder iteration.
  161. gr_encode_done_fn_t encode_graph_done; // Graph encoder finalization.
  162. // Decoding.
  163. term_decode_fn_t decode_term; // Term decoder function.
  164. gr_decode_fn_t decode_graph; // Graph decoder function.
  165. };
  166. /*
  167. * Common utility functions.
  168. */
  169. /** @brief strdup() for unsigned char.
  170. *
  171. * This is to be used with uint8_t sequences considered to be UTF-8 sequences,
  172. * requird by re2c (it won't work with byte sequences containing `NUL`).
  173. */
  174. inline uint8_t
  175. *uint8_dup (const uint8_t *str)
  176. { return (uint8_t *) strdup ((char *) str); }
  177. /** @brief strndup() for unsigned char.
  178. *
  179. * This is to be used with uint8_t sequences considered to be UTF-8 sequences,
  180. * requird by re2c (it won't work with byte sequences containing `NUL`).
  181. */
  182. inline uint8_t
  183. *uint8_ndup (const uint8_t *str, size_t size)
  184. { return (uint8_t *) strndup ((char *) str, size); }
  185. /** @brief Add escape character (backslash) to illegal literal characters.
  186. *
  187. * @param[in] in Input string.
  188. *
  189. * @param out[out] Output string.
  190. *
  191. * @return LSUP_OK on success; LSUP_MEM_ERR on memory error.
  192. */
  193. LSUP_rc
  194. escape_lit (const char *in, char **out);
  195. /** @brief Replace non-printable characters with their literal byte.
  196. *
  197. * Escape backslash is to be added separately.
  198. */
  199. static inline char
  200. escape_char (const char c) {
  201. switch (c) {
  202. case '\t': return 't';
  203. case '\b': return 'b';
  204. case '\n': return 'n';
  205. case '\r': return 'r';
  206. case '\f': return 'f';
  207. default: return c;
  208. }
  209. }
  210. /** @brief Unescape a single character.
  211. *
  212. * Convert escaped special characters such as `\t`, `\n`, etc. into their
  213. * corresponding code points.
  214. *
  215. * Non-special characters are returned unchanged.
  216. *
  217. * @param[in] c Character to unescape. Note that this is the single character
  218. * after `\`.
  219. *
  220. * @return Code point corresponding to the escaped character.
  221. */
  222. inline char
  223. unescape_char (const char c)
  224. {
  225. switch (c) {
  226. case 't': return '\t';
  227. case 'b': return '\b';
  228. case 'n': return '\n';
  229. case 'r': return '\r';
  230. case 'f': return '\f';
  231. default: return c;
  232. }
  233. }
  234. /** @brief Replace \uxxxx and \Uxxxxxxxx with Unicode bytes.
  235. *
  236. * @param[in] esc_str Escaped string.
  237. *
  238. * @param[in] size Maximum number of characters to scan, à la strncpy().
  239. *
  240. * @return String with escape sequences replaced by Unicode bytes.
  241. */
  242. uint8_t *unescape_unicode (const uint8_t *esc_str, size_t size);
  243. /** @brief Format an informational header.
  244. *
  245. * The information includes software version and current date. It is terminated
  246. * by a newline + NUL and prefixed with the string specified in `pfx`. It is
  247. * NOT prefixed by any comment characters.
  248. *
  249. * @param[in] pfx Prefix to add to the string. It may be a comment starter,
  250. * such as `# `.
  251. */
  252. char *fmt_header (char *pfx);
  253. #endif