codec_base.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #ifndef _LSUP_CODEC_BASE_H
  2. #define _LSUP_CODEC_BASE_H
  3. #include "graph.h"
  4. /** @brief Codec iterator type.
  5. *
  6. * This structure holds state data including input and output for encoding and
  7. * decoding RDF. Normally it should not be inspected ormanipulated directly,
  8. * but rather passed to codec iteration functions for processing RDF.
  9. *
  10. * NOTE: This should be used as an opaque handle, however it is exposed here
  11. * for easier inclusion into each codec.
  12. */
  13. typedef struct codec_iter_t {
  14. char * rep; // String representation of a RDF fragment.
  15. LSUP_Triple * trp; // RDF fragment being encoded or decoded.
  16. LSUP_GraphIterator *gr_it; // Graph iterator.
  17. LSUP_NSMap * nsm; // Namespace map.
  18. size_t cur; // Internal cursor.
  19. LSUP_rc rc; // Internal return code.
  20. char * str_s; // Temporary string.
  21. char * str_p; // Temporary string.
  22. char * str_o; // Temporary string.
  23. } LSUP_CodecIterator;
  24. /** @brief Parse error information.
  25. *
  26. */
  27. /* TODO A plain string will suffice for now.
  28. typedef struct parse_error_t {
  29. unsigned int line; // Line number where the error occurred.
  30. unsigned int linec; // Position in line of the offending token.
  31. char * token; // String representation of the token.
  32. } LSUP_ParseError;
  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. * @param[in] gr The graph to be encoded. The graph's namespace map is used by the
  54. * codec for namespace prefixing. The graph may only be freed after the loop is
  55. * finalized.
  56. *
  57. * @return A codec iterator handle to be passed to a #gr_codec_iter_fn_t
  58. * function and, eventually, to a #gr_codec_done_fn_t function.
  59. */
  60. typedef LSUP_CodecIterator * (*gr_encode_init_fn_t)(const LSUP_Graph *gr);
  61. /** @brief Perform one encoding iteration.
  62. *
  63. * Implementations of this prototype should perform all the steps to encode one
  64. * or more triples into a complete RDF fragment representing a complete triple
  65. * or a set thereof. The input unit is up to the implementation.
  66. *
  67. * @param[in] it Iterator handle.
  68. *
  69. * @param[out] res Handle to be populated with a string obtained from encoding.
  70. * This pointer must be passed initialized (it may be NULL) and should be
  71. * eventually freed manually at the end of the loop (it is reallocated at each
  72. * iteration, so memory from a previous iteration may be overwritten with new
  73. * data).
  74. *
  75. * @return LSUP_OK if a new token was processed; LSUP_END if the end of the
  76. * loop was reached.
  77. */
  78. typedef LSUP_rc (*gr_codec_iter_fn_t)(LSUP_CodecIterator *it, void **res);
  79. typedef void (*gr_codec_done_fn_t)(LSUP_CodecIterator *it);
  80. /** @brief Prototype for decoding a string into a LSUP_Term.
  81. *
  82. * Implementations MAY ignore any other tokens after finding the first one.
  83. *
  84. * @param[in] rep NT representation of the term.
  85. *
  86. * @param[in] nsm Namespace map handle.
  87. *
  88. * @param[out] Pointer to the term handle to be created. Implementaions SHOULD
  89. * return NULL on a parse error.
  90. *
  91. * @return Implementations MUST return LSUP_OK on success and a negative value
  92. * on parsing error.
  93. */
  94. typedef LSUP_rc (*term_decode_fn_t)(
  95. const char *rep, const LSUP_NSMap *nsm, LSUP_Term **term);
  96. /** @brief Prototype for decoding a complete RDF document into a graph.
  97. *
  98. * Implementations SHOULD consume data from the file handle in chunks.
  99. *
  100. * @param[in] rep Open file handle pointing to the RDF data. Implementations
  101. * MUST NOT close the file handle.
  102. *
  103. * @param[out] gr Pointer to a graph handle to be generated from decoding.
  104. *
  105. * @param[out] ct If not NULL, it may be populated with the number of triples
  106. * parsed (which may be different from the resulting graph size).
  107. * Implementations may choose not not use this, and they must account for the
  108. * value to be NULL.
  109. *
  110. * @param[out] err Pointer to error info string. If no error occurs, it yields
  111. * NULL.
  112. *
  113. * @return Implementations MUST return LSUP_OK on success and a negative value
  114. * on parsing error.
  115. */
  116. typedef LSUP_rc (*gr_decode_fn_t)(
  117. FILE *rep, LSUP_Graph **gr, size_t *ct, char **err);
  118. /** @brief Codec structure.
  119. *
  120. * An instance of this structure is usually defined at compile time (see
  121. * examples in "include/codec_*.h" and "src/codec_*.c") and should have the
  122. * following defined:
  123. *
  124. * - name: A brief (16-char max), human-readable to identify the codec.
  125. * - mimetype: MIME type (32-char max) associated with the codec.
  126. * - extension: File extension associated with the serialized file.
  127. *
  128. * - term_encoder: Encode a single term.
  129. *
  130. * - gr_encode_init: Initialize a graph decoding loop.
  131. * - gr_encode_iter: Run one iteration of encoding on one or more triples.
  132. * - gr_encode_done: Finalize the encoding loop and free the support data.
  133. *
  134. * - term_decoder: Decode a single term.
  135. * - gr_decoder: Decode a RDF document into a graph.
  136. *
  137. * For documentation on the individual encoding and decoding callbacks, see the
  138. * related function prototypes.
  139. */
  140. typedef struct codec_t {
  141. char name[16]; // Name of the codec.
  142. char mimetype[32]; // MIME type associated with the codec.
  143. char extension[8]; // Serialized file extension.
  144. // Encoding.
  145. term_enc_fn_t term_encoder; // Term encoder function.
  146. gr_encode_init_fn_t gr_encode_init; // Graph encoder initialization.
  147. gr_codec_iter_fn_t gr_encode_iter; // Graph encoder iteration.
  148. gr_codec_done_fn_t gr_encode_done; // Graph encoder finalization.
  149. // Decoding.
  150. term_decode_fn_t term_decoder; // Term decoder function.
  151. gr_decode_fn_t gr_decoder; // Graph decoder function.
  152. } LSUP_Codec;
  153. #endif