codec_nt.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include "codec/codec_nt.h"
  2. /// NT codec iterator type.
  3. typedef struct {
  4. const LSUP_Codec * codec; // Codec that generated this iterator.
  5. LSUP_GraphIterator *gr_it; // Graph iterator.
  6. const LSUP_NSMap * nsm; // Namespace map.
  7. size_t cur; // Internal cursor.
  8. LSUP_rc rc; // Internal return code.
  9. char * str_s, // Temporary string.
  10. * str_p, // Temporary string.
  11. * str_o; // Temporary string.
  12. } LSUP_NTCodecIterator;
  13. /* * * Codec functions. * * */
  14. static LSUP_rc
  15. term_to_nt (const LSUP_Term *term, const LSUP_NSMap *nsm, char **out_p)
  16. {
  17. LSUP_rc rc;
  18. char *out = NULL, *escaped;
  19. const char *metadata = NULL;
  20. size_t buf_len;
  21. char *data = term->data;
  22. switch (term->type) {
  23. case LSUP_TERM_NS_IRIREF:
  24. LSUP_nsmap_normalize_uri (nsm, term->data, &data);
  25. case LSUP_TERM_IRIREF:
  26. out = realloc (*out_p, strlen (data) + 3);
  27. if (UNLIKELY (!out)) return LSUP_MEM_ERR;
  28. sprintf (out, "<%s>", data);
  29. if (data != term->data) free (data);
  30. rc = LSUP_OK;
  31. break;
  32. case LSUP_TERM_LITERAL:
  33. // Calculate string length.
  34. if (escape_lit (term->data, &escaped) != LSUP_OK)
  35. return LSUP_ERROR;
  36. buf_len = strlen (escaped) + 3; // Room for "" and terminator
  37. if (
  38. term->datatype != 0
  39. && term->datatype != LSUP_default_datatype
  40. ) {
  41. metadata = term->datatype->data;
  42. buf_len += strlen (metadata) + 4; // Room for ^^<>
  43. }
  44. out = realloc (*out_p, buf_len);
  45. if (UNLIKELY (!out)) return LSUP_MEM_ERR;
  46. sprintf (out, "\"%s\"", escaped);
  47. free (escaped);
  48. // Add datatype.
  49. if (metadata)
  50. out = strcat (strcat (strcat (out, "^^<"), metadata), ">");
  51. rc = LSUP_OK;
  52. break;
  53. case LSUP_TERM_LT_LITERAL:
  54. // Calculate string length.
  55. if (escape_lit (term->data, &escaped) != LSUP_OK)
  56. return LSUP_ERROR;
  57. buf_len = strlen (escaped) + 3; // Room for "" and terminator
  58. if (term->lang[0] != '\0') {
  59. metadata = term->lang;
  60. buf_len += strlen (metadata) + 1; // Room for @
  61. }
  62. out = realloc (*out_p, buf_len);
  63. if (UNLIKELY (!out)) return LSUP_MEM_ERR;
  64. sprintf (out, "\"%s\"", escaped);
  65. free (escaped);
  66. // Add lang.
  67. if (metadata) out = strcat (strcat (out, "@"), metadata);
  68. rc = LSUP_OK;
  69. break;
  70. case LSUP_TERM_BNODE:
  71. out = realloc (*out_p, strlen (term->data) + 3);
  72. if (UNLIKELY (!out)) return LSUP_MEM_ERR;
  73. sprintf (out, "_:%s", term->data);
  74. rc = LSUP_OK;
  75. break;
  76. default:
  77. out = *out_p; // This is considered garbage.
  78. rc = LSUP_PARSE_ERR;
  79. }
  80. *out_p = out;
  81. return rc;
  82. }
  83. static void *
  84. gr_to_nt_init (const LSUP_Graph *gr)
  85. {
  86. LSUP_NTCodecIterator *it;
  87. CALLOC_GUARD (it, NULL);
  88. it->codec = &nt_codec;
  89. it->gr_it = LSUP_graph_lookup(gr, NULL, NULL, NULL, &it->cur);
  90. it->nsm = LSUP_graph_namespace (gr);
  91. return it;
  92. }
  93. static LSUP_rc
  94. gr_to_nt_iter (void *h, char **res) {
  95. LSUP_NTCodecIterator *it = h;
  96. LSUP_Triple *spo = NULL;
  97. LSUP_rc rc = LSUP_graph_iter_next (it->gr_it, &spo);
  98. if (rc != LSUP_OK) goto finally;
  99. term_to_nt (spo->s, it->nsm, &it->str_s);
  100. term_to_nt (spo->p, it->nsm, &it->str_p);
  101. term_to_nt (spo->o, it->nsm, &it->str_o);
  102. LSUP_triple_free (spo);
  103. // 3 term separators + dot + newline + terminal = 6
  104. char *tmp = realloc (
  105. *res, strlen (it->str_s) + strlen (it->str_p)
  106. + strlen (it->str_o) + 6);
  107. if (UNLIKELY (!tmp)) {
  108. *res = NULL;
  109. rc = LSUP_MEM_ERR;
  110. goto finally;
  111. }
  112. sprintf (tmp, "%s %s %s .\n", it->str_s, it->str_p, it->str_o);
  113. *res = tmp;
  114. it->cur++;
  115. finally:
  116. return rc;
  117. }
  118. static void
  119. gr_to_nt_done (void *h)
  120. {
  121. LSUP_NTCodecIterator *it = h;
  122. LSUP_graph_iter_free (it->gr_it);
  123. free (it->str_s);
  124. free (it->str_p);
  125. free (it->str_o);
  126. free (it);
  127. }
  128. const LSUP_Codec nt_codec = {
  129. .name = "N-Triples",
  130. .mimetype = "application/n-triples",
  131. .extension = "nt",
  132. .encode_term = term_to_nt,
  133. .encode_graph_init = gr_to_nt_init,
  134. .encode_graph_iter = gr_to_nt_iter,
  135. .encode_graph_done = gr_to_nt_done,
  136. .decode_term = LSUP_nt_parse_term,
  137. .decode_graph = LSUP_nt_parse_doc,
  138. };