|
@@ -6,11 +6,11 @@
|
|
|
* This iterator yields one or more triples at a time, one group per subject,
|
|
|
* with the most compact form allowed by Turtle, e.g.
|
|
|
*
|
|
|
- * :s :p1 :o1, :o2, o3; p2 o4, o5, <http://example.com/ext1> .
|
|
|
+ * ```:s :p1 :o1, :o2, o3; :p2 :o4, :o5, <http://example.com/ext1> .```
|
|
|
*/
|
|
|
typedef struct {
|
|
|
const VOLK_Codec * codec; ///< Codec that generated this iterator.
|
|
|
- const VOLK_Graph * gr; ///< Graph being encoded.
|
|
|
+ const VOLK_Graph * gr; ///< Graph being encoded.
|
|
|
VOLK_TermSet * subjects; ///< All subjects in the graph.
|
|
|
size_t s_cur; ///< Term set cursor.
|
|
|
VOLK_rc rc; ///< Internal return code.
|
|
@@ -26,8 +26,10 @@ static VOLK_rc
|
|
|
term_to_ttl (const VOLK_Term *term, char **out_p)
|
|
|
{
|
|
|
VOLK_rc rc;
|
|
|
- char *tmp = NULL, *out = NULL;
|
|
|
- char *metadata = NULL;
|
|
|
+ char
|
|
|
+ *tmp = NULL,
|
|
|
+ *out = NULL,
|
|
|
+ *metadata = NULL;
|
|
|
size_t buf_len;
|
|
|
|
|
|
VOLK_rc md_rc = VOLK_NORESULT;
|
|
@@ -36,18 +38,23 @@ term_to_ttl (const VOLK_Term *term, char **out_p)
|
|
|
if (strcmp (term->data, VOLK_RDF_TYPE) == 0) {
|
|
|
// Shorten RDF type
|
|
|
buf_len = 2;
|
|
|
- tmp = "a";
|
|
|
+ out = realloc (*out_p, 2);
|
|
|
+ if (UNLIKELY (!out)) return VOLK_MEM_ERR;
|
|
|
+ out[0] = 'a';
|
|
|
+ out[1] = '\0';
|
|
|
} else {
|
|
|
md_rc = VOLK_nsmap_denormalize_uri (term->data, &tmp);
|
|
|
PRCCK (md_rc);
|
|
|
if (md_rc == VOLK_NORESULT) {
|
|
|
// If URI counld not be shortened, add `<>`
|
|
|
- out = realloc (*out_p, strlen (tmp) + 3);
|
|
|
+ // and copy term from the original.
|
|
|
+ out = realloc (*out_p, strlen (term->data) + 3);
|
|
|
if (UNLIKELY (!out)) return VOLK_MEM_ERR;
|
|
|
- sprintf (out, "<%s>", tmp);
|
|
|
- free (tmp);
|
|
|
-
|
|
|
+ sprintf (out, "<%s>", term->data);
|
|
|
} else {
|
|
|
+ // If URI was shortened, write it out without `<>` and
|
|
|
+ // use previously allocated data from denormalization.
|
|
|
+ // Free previous output pointer
|
|
|
free (*out_p);
|
|
|
out = tmp;
|
|
|
}
|
|
@@ -147,6 +154,7 @@ term_to_ttl (const VOLK_Term *term, char **out_p)
|
|
|
|
|
|
default:
|
|
|
out = *out_p; // This is considered garbage.
|
|
|
+ log_error ("Invalid term type: %d", term->type);
|
|
|
rc = VOLK_PARSE_ERR;
|
|
|
}
|
|
|
free (metadata);
|