triple.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #ifndef _LSUP_TRIPLE_H
  2. #define _LSUP_TRIPLE_H
  3. #include "term.h"
  4. typedef struct LSUP_Triple {
  5. LSUP_Term *s;
  6. LSUP_Term *p;
  7. LSUP_Term *o;
  8. } LSUP_Triple;
  9. typedef struct LSUP_SerTriple {
  10. LSUP_Buffer *s;
  11. LSUP_Buffer *p;
  12. LSUP_Buffer *o;
  13. } LSUP_SerTriple;
  14. typedef enum {
  15. TRP_POS_S = 0,
  16. TRP_POS_P = 1,
  17. TRP_POS_O = 2,
  18. } LSUP_TriplePos;
  19. LSUP_Triple *
  20. LSUP_triple_new();
  21. LSUP_SerTriple *
  22. LSUP_striple_new();
  23. LSUP_Triple *
  24. LSUP_triple_new_from_striple(const LSUP_SerTriple *sspo);
  25. LSUP_SerTriple *
  26. LSUP_striple_new_from_triple(const LSUP_Triple *spo);
  27. /** @brief Serialize a RDF triple into a buffer triple.
  28. *
  29. * The internal structure must be freed with #LSUP_striple_done after use.
  30. *
  31. * @param spo[in] Triple to be serialized.
  32. * @param sspo[out] Buffer triple handle. It must point to an already allocated
  33. * structure.
  34. *
  35. * @return LSUP_OK or an error code resulting from #LSUP_term_serialize.
  36. */
  37. LSUP_rc
  38. LSUP_triple_serialize(const LSUP_Triple *spo, LSUP_SerTriple *sspo);
  39. /** @brief Deserialize a buffer triple into a RDF triple.
  40. *
  41. * The internal structure must be freed with #LSUP_triple_done after use.
  42. *
  43. * @param sspo[in] Buffer triple to be serialized.
  44. * @param spo[out] RDF triple handle. It must point to an already allocated
  45. * structure.
  46. *
  47. * @return LSUP_OK or an error code resulting from #LSUP_term_deserialize.
  48. */
  49. LSUP_rc
  50. LSUP_triple_deserialize(const LSUP_SerTriple *sspo, LSUP_Triple *spo);
  51. /** @brief Free the internal pointers of a triple.
  52. *
  53. * @param spo[in] Triple to be freed.
  54. */
  55. void
  56. LSUP_triple_done (LSUP_Triple *spo);
  57. /** @brief Free the internal pointers of a buffer triple.
  58. *
  59. * @param sspo[in] Buffer triple to be freed.
  60. */
  61. void
  62. LSUP_striple_done (LSUP_SerTriple *sspo);
  63. /** @brief Free a triple and all its internal pointers.
  64. *
  65. * @param spo[in] Triple to be freed.
  66. */
  67. void
  68. LSUP_triple_free (LSUP_Triple *spo);
  69. /** @brief Free a buffer triple and all its internal pointers.
  70. *
  71. * @param sspo[in] Buffer triple to be freed.
  72. */
  73. void
  74. LSUP_striple_free (LSUP_SerTriple *sspo);
  75. #define _FN_BODY \
  76. if (n == TRP_POS_S) return trp->s; \
  77. if (n == TRP_POS_P) return trp->p; \
  78. if (n == TRP_POS_O) return trp->o; \
  79. return NULL;
  80. /** @brief Get triple by term position.
  81. *
  82. * Useful for looping over all terms.
  83. *
  84. * @param trp[in] Triple pointer.
  85. *
  86. * @param n[in] A number between 0÷2.
  87. *
  88. * @return Corresponding triple term or NULL if n is out of range.
  89. */
  90. inline LSUP_Term *
  91. LSUP_triple_pos(const LSUP_Triple *trp, LSUP_TriplePos n)
  92. { _FN_BODY }
  93. /** @brief Get serialized triple by term position.
  94. *
  95. * Useful for looping over all terms.
  96. *
  97. * @param trp[in] Serialized triple pointer.
  98. *
  99. * @param n[in] A number between 0÷2.
  100. *
  101. * @return Corresponding serialized term or NULL if n is out of range.
  102. */
  103. inline LSUP_Buffer *
  104. LSUP_striple_pos(const LSUP_SerTriple *trp, LSUP_TriplePos n)
  105. { _FN_BODY }
  106. #undef _FN_BODY
  107. #endif