codec_base.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 Term encoder callback type.
  25. *
  26. * @param[in] term Single term handle.
  27. *
  28. * @param[in] nsm Namespace map. May be NULL for no prefix shortening.
  29. *
  30. * @param[out] rep Pointer to a string to be filled with the encoded term. The
  31. * caller is in charge of freeing the string after use. Returns undefined on
  32. * error.
  33. *
  34. * @return LSUP_OK on successful encoding; <0 for other errors.
  35. */
  36. typedef LSUP_rc (*term_enc_fn_t)(
  37. const LSUP_Term *term, const LSUP_NSMap *nsm, char **rep);
  38. /** TODO
  39. */
  40. typedef LSUP_rc (*term_dec_fn_t)(
  41. const char *rep, const LSUP_NSMap *nsm, LSUP_Term **term);
  42. /** @brief Initialize a graph encoding loop.
  43. *
  44. * This prototype is to be implemented by graph encoding loops. It should
  45. * create an iterator and perform all initial setup for finding triples.
  46. *
  47. * @param[in] gr The graph to be encoded. The graph's namespace map is used by the
  48. * codec for namespace prefixing. The graph may only be freed after the loop is
  49. * finalized.
  50. *
  51. * @return A codec iterator handle to be passed to a #gr_codec_iter_fn_t
  52. * function and, eventually, to a #gr_codec_done_fn_t function.
  53. */
  54. typedef LSUP_CodecIterator * (*gr_encode_init_fn_t)(const LSUP_Graph *gr);
  55. /** @brief Initialize a graph decoding loop.
  56. *
  57. * This prototype is to be implemented by graph decoding loops. It should
  58. * create an iterator and perform all initial setup for tokenizing text into
  59. * processing units.
  60. *
  61. * @param[in] rep The RDF string to be decoded. Any namespace prefixes defined in
  62. * this string will be used for decoding.
  63. *
  64. * @return A codec iterator handle to be passed to a #gr_codec_iter_fn_t
  65. * function and, eventually, to a #gr_codec_done_fn_t function.
  66. */
  67. typedef LSUP_CodecIterator * (*gr_decode_init_fn_t)(const char *rep);
  68. /** @brief Perform one encoding or decoding iteration.
  69. *
  70. * This prototype is used for both encoding and decoding function. It should
  71. * perform all the steps to either encode one or more triples into a complete
  72. * RDF fragment representing a complete triple or a set thereof, or to parse
  73. * a RDF string into one or more complete triples.
  74. *
  75. * @param[in] it Iterator handle.
  76. *
  77. * @param[out] res Handle to be populated with the data obtained from encoding
  78. * (a string) or decoding (a NULL-terminated array of triples). This pointer
  79. * must be passed initialized (it may be NULL) and should be eventually
  80. * freed manually at the end of the loop (it is reallocated at each iteration,
  81. * so memory from a previous iteration may be overwritten with new data).
  82. *
  83. * @return LSUP_OK if a new token was processed; LSUP_END if the end of the
  84. * loop was reached.
  85. */
  86. typedef LSUP_rc (*gr_codec_iter_fn_t)(LSUP_CodecIterator *it, void **res);
  87. typedef void (*gr_codec_done_fn_t)(LSUP_CodecIterator *it);
  88. /** TODO
  89. */
  90. typedef LSUP_rc (*gr_dec_fn_t)(const char *rep, LSUP_Graph **gr);
  91. /** @brief Codec structure.
  92. *
  93. * An instance of this structure is usually defined at compile time (see
  94. * examples in "include/codec_*.h" and "src/codec_*.c") and should have the
  95. * following defined:
  96. *
  97. * - name: A brief (16-char max), human-readable to identify the codec.
  98. * - mimetype: MIME type associated with the codec.
  99. * - extension: File extension associated with the serialized file.
  100. *
  101. * - term_encoder: Callback function for encoding a single term.
  102. * - term_decoder: Callback function for decoding a single term.
  103. *
  104. * - gr_encode_init: Initialize a graph decoding loop.
  105. * - gr_encode_iter: Run one iteration of encoding on one or more triples.
  106. * - gr_encode_done: Finalize the encoding loop and free the support data.
  107. *
  108. * - gr_decode_init: Initialize a graph decoding loop.
  109. * - gr_decode_iter: Run one iteration of decoding on one or more text lines.
  110. * - gr_decode_done: Finalize the decoding loop and free the support data.
  111. *
  112. * For documentation on the individual encoding and decoding callbaks, see the
  113. * related function prototypes.
  114. */
  115. typedef struct codec_t {
  116. char name[16]; // Name of the codec.
  117. char mimetype[32]; // MIME type associated with the codec.
  118. char extension[8]; // Serialized file extension.
  119. // Term encoding and decoding.
  120. term_enc_fn_t term_encoder; // Term encoder function.
  121. term_dec_fn_t term_decoder; // Term decoder function.
  122. // Graph encoding.
  123. gr_encode_init_fn_t gr_encode_init; // Graph encoder initialization.
  124. gr_codec_iter_fn_t gr_encode_iter; // Graph encoder initialization.
  125. gr_codec_done_fn_t gr_encode_done; // Graph encoder initialization.
  126. // Graph decoding.
  127. gr_decode_init_fn_t gr_decode_init; // Graph decoder initialization.
  128. gr_codec_iter_fn_t gr_decode_iter; // Graph decoder initialization.
  129. gr_codec_done_fn_t gr_decode_done; // Graph decoder initialization.
  130. } LSUP_Codec;
  131. #endif