123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- #ifndef _LSUP_CODEC_BASE_H
- #define _LSUP_CODEC_BASE_H
- #include "graph.h"
- /** @brief Codec iterator type.
- *
- * This structure holds state data including input and output for encoding and
- * decoding RDF. Normally it should not be inspected ormanipulated directly,
- * but rather passed to codec iteration functions for processing RDF.
- *
- * NOTE: This should be used as an opaque handle, however it is exposed here
- * for easier inclusion into each codec.
- */
- typedef struct codec_iter_t {
- char * rep; // String representation of a RDF fragment.
- LSUP_Triple * trp; // RDF fragment being encoded or decoded.
- LSUP_GraphIterator *gr_it; // Graph iterator.
- LSUP_NSMap * nsm; // Namespace map.
- size_t cur; // Internal cursor.
- LSUP_rc rc; // Internal return code.
- char * str_s; // Temporary string.
- char * str_p; // Temporary string.
- char * str_o; // Temporary string.
- } LSUP_CodecIterator;
- /** @brief Term encoder callback type.
- *
- * @param[in] term Single term handle.
- *
- * @param[in] nsm Namespace map. May be NULL for no prefix shortening.
- *
- * @param[out] rep Pointer to a string to be filled with the encoded term. The
- * caller is in charge of freeing the string after use. Returns undefined on
- * error.
- *
- * @return LSUP_OK on successful encoding; <0 for other errors.
- */
- typedef LSUP_rc (*term_enc_fn_t)(
- const LSUP_Term *term, const LSUP_NSMap *nsm, char **rep);
- /** TODO
- */
- typedef LSUP_rc (*term_dec_fn_t)(
- const char *rep, const LSUP_NSMap *nsm, LSUP_Term **term);
- /** @brief Initialize a graph encoding loop.
- *
- * This prototype is to be implemented by graph encoding loops. It should
- * create an iterator and perform all initial setup for finding triples.
- *
- * @param[in] gr The graph to be encoded. The graph's namespace map is used by the
- * codec for namespace prefixing. The graph may only be freed after the loop is
- * finalized.
- *
- * @return A codec iterator handle to be passed to a #gr_codec_iter_fn_t
- * function and, eventually, to a #gr_codec_done_fn_t function.
- */
- typedef LSUP_CodecIterator * (*gr_encode_init_fn_t)(const LSUP_Graph *gr);
- /** @brief Initialize a graph decoding loop.
- *
- * This prototype is to be implemented by graph decoding loops. It should
- * create an iterator and perform all initial setup for tokenizing text into
- * processing units.
- *
- * @param[in] rep The RDF string to be decoded. Any namespace prefixes defined in
- * this string will be used for decoding.
- *
- * @return A codec iterator handle to be passed to a #gr_codec_iter_fn_t
- * function and, eventually, to a #gr_codec_done_fn_t function.
- */
- typedef LSUP_CodecIterator * (*gr_decode_init_fn_t)(const char *rep);
- /** @brief Perform one encoding or decoding iteration.
- *
- * This prototype is used for both encoding and decoding function. It should
- * perform all the steps to either encode one or more triples into a complete
- * RDF fragment representing a complete triple or a set thereof, or to parse
- * a RDF string into one or more complete triples.
- *
- * @param[in] it Iterator handle.
- *
- * @param[out] res Handle to be populated with the data obtained from encoding
- * (a string) or decoding (a NULL-terminated array of triples). This pointer
- * must be passed initialized (it may be NULL) and should be eventually
- * freed manually at the end of the loop (it is reallocated at each iteration,
- * so memory from a previous iteration may be overwritten with new data).
- *
- * @return LSUP_OK if a new token was processed; LSUP_END if the end of the
- * loop was reached.
- */
- typedef LSUP_rc (*gr_codec_iter_fn_t)(LSUP_CodecIterator *it, void **res);
- typedef void (*gr_codec_done_fn_t)(LSUP_CodecIterator *it);
- /** TODO
- */
- typedef LSUP_rc (*gr_dec_fn_t)(const char *rep, LSUP_Graph **gr);
- /** @brief Codec structure.
- *
- * An instance of this structure is usually defined at compile time (see
- * examples in "include/codec_*.h" and "src/codec_*.c") and should have the
- * following defined:
- *
- * - name: A brief (16-char max), human-readable to identify the codec.
- * - mimetype: MIME type associated with the codec.
- * - extension: File extension associated with the serialized file.
- *
- * - term_encoder: Callback function for encoding a single term.
- * - term_decoder: Callback function for decoding a single term.
- *
- * - gr_encode_init: Initialize a graph decoding loop.
- * - gr_encode_iter: Run one iteration of encoding on one or more triples.
- * - gr_encode_done: Finalize the encoding loop and free the support data.
- *
- * - gr_decode_init: Initialize a graph decoding loop.
- * - gr_decode_iter: Run one iteration of decoding on one or more text lines.
- * - gr_decode_done: Finalize the decoding loop and free the support data.
- *
- * For documentation on the individual encoding and decoding callbaks, see the
- * related function prototypes.
- */
- typedef struct codec_t {
- char name[16]; // Name of the codec.
- char mimetype[32]; // MIME type associated with the codec.
- char extension[8]; // Serialized file extension.
- // Term encoding and decoding.
- term_enc_fn_t term_encoder; // Term encoder function.
- term_dec_fn_t term_decoder; // Term decoder function.
- // Graph encoding.
- gr_encode_init_fn_t gr_encode_init; // Graph encoder initialization.
- gr_codec_iter_fn_t gr_encode_iter; // Graph encoder initialization.
- gr_codec_done_fn_t gr_encode_done; // Graph encoder initialization.
- // Graph decoding.
- gr_decode_init_fn_t gr_decode_init; // Graph decoder initialization.
- gr_codec_iter_fn_t gr_decode_iter; // Graph decoder initialization.
- gr_codec_done_fn_t gr_decode_done; // Graph decoder initialization.
- } LSUP_Codec;
- #endif
|