|
@@ -23,36 +23,38 @@ static LSUP_rc
|
|
term_to_ttl (const LSUP_Term *term, const LSUP_NSMap *nsm, char **out_p)
|
|
term_to_ttl (const LSUP_Term *term, const LSUP_NSMap *nsm, char **out_p)
|
|
{
|
|
{
|
|
LSUP_rc rc;
|
|
LSUP_rc rc;
|
|
- char *out = NULL, *escaped;
|
|
|
|
|
|
+ char *tmp = NULL, *out;
|
|
char *metadata = NULL;
|
|
char *metadata = NULL;
|
|
size_t buf_len;
|
|
size_t buf_len;
|
|
|
|
|
|
- LSUP_rc md_rc;
|
|
|
|
|
|
+ LSUP_rc md_rc = LSUP_NORESULT;
|
|
switch (term->type) {
|
|
switch (term->type) {
|
|
case LSUP_TERM_IRIREF:
|
|
case LSUP_TERM_IRIREF:
|
|
- md_rc = LSUP_nsmap_denormalize_uri (nsm, term->data, &out);
|
|
|
|
|
|
+ md_rc = LSUP_nsmap_denormalize_uri (nsm, term->data, &tmp);
|
|
PRCCK (md_rc);
|
|
PRCCK (md_rc);
|
|
if (md_rc == LSUP_NORESULT) {
|
|
if (md_rc == LSUP_NORESULT) {
|
|
// If URI counld not be shortened, add `<>`
|
|
// If URI counld not be shortened, add `<>`
|
|
- char *tmp = realloc (out, strlen (out) + 2);
|
|
|
|
- if (UNLIKELY (!tmp)) return LSUP_MEM_ERR;
|
|
|
|
- out = tmp;
|
|
|
|
- out = strcat (strcat (strcat (tmp, "<"), tmp), ">");
|
|
|
|
- }
|
|
|
|
|
|
+ out = realloc (*out_p, strlen (tmp) + 3);
|
|
|
|
+ if (UNLIKELY (!out)) return LSUP_MEM_ERR;
|
|
|
|
+ sprintf (out, "<%s>", tmp);
|
|
|
|
+ free (tmp);
|
|
|
|
+
|
|
|
|
+ } else out = tmp;
|
|
rc = LSUP_OK;
|
|
rc = LSUP_OK;
|
|
break;
|
|
break;
|
|
|
|
|
|
case LSUP_TERM_NS_IRIREF:
|
|
case LSUP_TERM_NS_IRIREF:
|
|
- out = strdup (term->data);
|
|
|
|
|
|
+ out = realloc (*out_p, strlen (term->data) + 1);
|
|
if (UNLIKELY (!out)) return LSUP_MEM_ERR;
|
|
if (UNLIKELY (!out)) return LSUP_MEM_ERR;
|
|
|
|
+ strcpy (out, term->data);
|
|
rc = LSUP_OK;
|
|
rc = LSUP_OK;
|
|
break;
|
|
break;
|
|
|
|
|
|
case LSUP_TERM_LITERAL:
|
|
case LSUP_TERM_LITERAL:
|
|
// Calculate string length.
|
|
// Calculate string length.
|
|
- if (escape_lit (term->data, &escaped) != LSUP_OK)
|
|
|
|
|
|
+ if (escape_lit (term->data, &tmp) != LSUP_OK)
|
|
return LSUP_ERROR;
|
|
return LSUP_ERROR;
|
|
- buf_len = strlen (escaped) + 3; // Room for "" and terminator
|
|
|
|
|
|
+ buf_len = strlen (tmp) + 3; // Room for "" and terminator
|
|
|
|
|
|
if (
|
|
if (
|
|
term->datatype != 0
|
|
term->datatype != 0
|
|
@@ -66,15 +68,17 @@ term_to_ttl (const LSUP_Term *term, const LSUP_NSMap *nsm, char **out_p)
|
|
buf_len += strlen (metadata) + padding;
|
|
buf_len += strlen (metadata) + padding;
|
|
}
|
|
}
|
|
|
|
|
|
- out = realloc (out, buf_len);
|
|
|
|
|
|
+ out = realloc (*out_p, buf_len);
|
|
if (UNLIKELY (!out)) return LSUP_MEM_ERR;
|
|
if (UNLIKELY (!out)) return LSUP_MEM_ERR;
|
|
|
|
|
|
- sprintf (out, "\"%s\"", escaped);
|
|
|
|
- free (escaped);
|
|
|
|
-
|
|
|
|
- // Add datatype.
|
|
|
|
- if (metadata)
|
|
|
|
- out = strcat (strcat (strcat (out, "^^<"), metadata), ">");
|
|
|
|
|
|
+ if (metadata) {
|
|
|
|
+ char *fmt = (
|
|
|
|
+ md_rc == LSUP_NORESULT ? "\"%s\"^^<%s>"
|
|
|
|
+ : "\"%s\"^^%s");
|
|
|
|
+ sprintf (out, fmt, tmp, metadata);
|
|
|
|
+ }
|
|
|
|
+ else sprintf (out, "\"%s\"", tmp);
|
|
|
|
+ free (tmp);
|
|
|
|
|
|
rc = LSUP_OK;
|
|
rc = LSUP_OK;
|
|
|
|
|
|
@@ -82,20 +86,20 @@ term_to_ttl (const LSUP_Term *term, const LSUP_NSMap *nsm, char **out_p)
|
|
|
|
|
|
case LSUP_TERM_LT_LITERAL:
|
|
case LSUP_TERM_LT_LITERAL:
|
|
// Calculate string length.
|
|
// Calculate string length.
|
|
- if (escape_lit (term->data, &escaped) != LSUP_OK)
|
|
|
|
|
|
+ if (escape_lit (term->data, &tmp) != LSUP_OK)
|
|
return LSUP_ERROR;
|
|
return LSUP_ERROR;
|
|
- buf_len = strlen (escaped) + 3; // Room for "" and terminator
|
|
|
|
|
|
+ buf_len = strlen (tmp) + 3; // Room for "" and terminator
|
|
|
|
|
|
if (term->lang[0] != '\0') {
|
|
if (term->lang[0] != '\0') {
|
|
metadata = strndup (term->lang, sizeof (LSUP_LangTag));
|
|
metadata = strndup (term->lang, sizeof (LSUP_LangTag));
|
|
buf_len += strlen (metadata) + 1; // Room for @
|
|
buf_len += strlen (metadata) + 1; // Room for @
|
|
}
|
|
}
|
|
|
|
|
|
- out = realloc (out, buf_len);
|
|
|
|
|
|
+ out = realloc (*out_p, buf_len);
|
|
if (UNLIKELY (!out)) return LSUP_MEM_ERR;
|
|
if (UNLIKELY (!out)) return LSUP_MEM_ERR;
|
|
|
|
|
|
- sprintf (out, "\"%s\"", escaped);
|
|
|
|
- free (escaped);
|
|
|
|
|
|
+ sprintf (out, "\"%s\"", tmp);
|
|
|
|
+ free (tmp);
|
|
|
|
|
|
// Add lang.
|
|
// Add lang.
|
|
if (metadata) out = strcat (strcat (out, "@"), metadata);
|
|
if (metadata) out = strcat (strcat (out, "@"), metadata);
|
|
@@ -105,7 +109,7 @@ term_to_ttl (const LSUP_Term *term, const LSUP_NSMap *nsm, char **out_p)
|
|
break;
|
|
break;
|
|
|
|
|
|
case LSUP_TERM_BNODE:
|
|
case LSUP_TERM_BNODE:
|
|
- out = realloc (out, strlen (term->data) + 3);
|
|
|
|
|
|
+ out = realloc (*out_p, strlen (term->data) + 3);
|
|
if (UNLIKELY (!out)) return LSUP_MEM_ERR;
|
|
if (UNLIKELY (!out)) return LSUP_MEM_ERR;
|
|
|
|
|
|
sprintf (out, "_:%s", term->data);
|
|
sprintf (out, "_:%s", term->data);
|
|
@@ -114,7 +118,7 @@ term_to_ttl (const LSUP_Term *term, const LSUP_NSMap *nsm, char **out_p)
|
|
break;
|
|
break;
|
|
|
|
|
|
default:
|
|
default:
|
|
- out = NULL;
|
|
|
|
|
|
+ out = *out_p; // This is considered garbage.
|
|
rc = LSUP_PARSE_ERR;
|
|
rc = LSUP_PARSE_ERR;
|
|
}
|
|
}
|
|
free (metadata);
|
|
free (metadata);
|
|
@@ -148,22 +152,24 @@ build_prolog (LSUP_TTLCodecIterator *it, char **res_p)
|
|
char *res = fmt_header ("# ");
|
|
char *res = fmt_header ("# ");
|
|
|
|
|
|
const char ***nsm = LSUP_nsmap_dump (LSUP_graph_namespace (it->gr));
|
|
const char ***nsm = LSUP_nsmap_dump (LSUP_graph_namespace (it->gr));
|
|
- const char *ns_tpl = "@prefix %s: <%s> .\n";
|
|
|
|
|
|
+ char *ns_tpl = "@prefix %s: <%s> .\n";
|
|
|
|
|
|
// Prefix map.
|
|
// Prefix map.
|
|
for (size_t i = 0; nsm[i]; i++) {
|
|
for (size_t i = 0; nsm[i]; i++) {
|
|
const char **ns = nsm[i];
|
|
const char **ns = nsm[i];
|
|
size_t old_len = strlen (res);
|
|
size_t old_len = strlen (res);
|
|
size_t ns_len = strlen (ns[0]) + strlen (ns[1]) + strlen (ns_tpl);
|
|
size_t ns_len = strlen (ns[0]) + strlen (ns[1]) + strlen (ns_tpl);
|
|
- char *tmp = realloc (res, old_len + ns_len);
|
|
|
|
|
|
+ char *tmp = realloc (res, old_len + ns_len + 1);
|
|
if (UNLIKELY (!tmp)) return LSUP_MEM_ERR;
|
|
if (UNLIKELY (!tmp)) return LSUP_MEM_ERR;
|
|
res = tmp;
|
|
res = tmp;
|
|
|
|
|
|
sprintf (res + old_len, ns_tpl, ns[0], ns[1]);
|
|
sprintf (res + old_len, ns_tpl, ns[0], ns[1]);
|
|
|
|
+ free (ns);
|
|
}
|
|
}
|
|
|
|
+ free (nsm);
|
|
|
|
|
|
// Base.
|
|
// Base.
|
|
- char *base_uri_str;
|
|
|
|
|
|
+ char *base_uri_str = NULL;
|
|
LSUP_rc rc = LSUP_nsmap_denormalize_uri (
|
|
LSUP_rc rc = LSUP_nsmap_denormalize_uri (
|
|
LSUP_graph_namespace (it->gr), LSUP_graph_uri (it->gr)->data,
|
|
LSUP_graph_namespace (it->gr), LSUP_graph_uri (it->gr)->data,
|
|
&base_uri_str);
|
|
&base_uri_str);
|
|
@@ -172,9 +178,11 @@ build_prolog (LSUP_TTLCodecIterator *it, char **res_p)
|
|
char *base_stmt = malloc (strlen (base_stmt_tpl) + strlen (base_uri_str));
|
|
char *base_stmt = malloc (strlen (base_stmt_tpl) + strlen (base_uri_str));
|
|
if (!UNLIKELY (base_stmt)) return LSUP_MEM_ERR;
|
|
if (!UNLIKELY (base_stmt)) return LSUP_MEM_ERR;
|
|
sprintf (base_stmt, base_stmt_tpl, base_uri_str);
|
|
sprintf (base_stmt, base_stmt_tpl, base_uri_str);
|
|
- res = realloc (res, strlen (res) + strlen (base_stmt));
|
|
|
|
|
|
+ free (base_uri_str);
|
|
|
|
+ res = realloc (res, strlen (res) + strlen (base_stmt) + 1);
|
|
if (!UNLIKELY (res)) return LSUP_MEM_ERR;
|
|
if (!UNLIKELY (res)) return LSUP_MEM_ERR;
|
|
res = strcat (res, base_stmt);
|
|
res = strcat (res, base_stmt);
|
|
|
|
+ free (base_stmt);
|
|
|
|
|
|
*res_p = res;
|
|
*res_p = res;
|
|
it->rc = LSUP_OK;
|
|
it->rc = LSUP_OK;
|
|
@@ -191,7 +199,7 @@ gr_to_ttl_iter (void *h, char **res_p) {
|
|
if (it->rc == LSUP_NORESULT) return build_prolog (it, res_p);
|
|
if (it->rc == LSUP_NORESULT) return build_prolog (it, res_p);
|
|
|
|
|
|
LSUP_Term *s = NULL;
|
|
LSUP_Term *s = NULL;
|
|
- char *res = NULL; // Result string.
|
|
|
|
|
|
+ char *res = *res_p; // Result string will be reallocated.
|
|
RCCK (LSUP_term_set_next (it->subjects, &it->s_cur, &s));
|
|
RCCK (LSUP_term_set_next (it->subjects, &it->s_cur, &s));
|
|
|
|
|
|
term_to_ttl (s, LSUP_graph_namespace (it->gr), &res);
|
|
term_to_ttl (s, LSUP_graph_namespace (it->gr), &res);
|
|
@@ -210,10 +218,7 @@ gr_to_ttl_iter (void *h, char **res_p) {
|
|
RCCK (term_to_ttl (p, LSUP_graph_namespace (it->gr), &p_str));
|
|
RCCK (term_to_ttl (p, LSUP_graph_namespace (it->gr), &p_str));
|
|
char *tmp = realloc (
|
|
char *tmp = realloc (
|
|
res, strlen (res) + strlen (p_str) + strlen (p_join) + 1);
|
|
res, strlen (res) + strlen (p_str) + strlen (p_join) + 1);
|
|
- if (UNLIKELY (!tmp)) {
|
|
|
|
- it->rc = LSUP_MEM_ERR;
|
|
|
|
- goto finally;
|
|
|
|
- }
|
|
|
|
|
|
+ if (UNLIKELY (!tmp)) goto memfail;
|
|
res = strcat (strcat (tmp, p_join), p_str);
|
|
res = strcat (strcat (tmp, p_join), p_str);
|
|
|
|
|
|
free (p_str);
|
|
free (p_str);
|
|
@@ -229,10 +234,7 @@ gr_to_ttl_iter (void *h, char **res_p) {
|
|
RCCK (it->rc);
|
|
RCCK (it->rc);
|
|
char *tmp = realloc (
|
|
char *tmp = realloc (
|
|
res, strlen (res) + strlen (o_str) + strlen (o_join) + 1);
|
|
res, strlen (res) + strlen (o_str) + strlen (o_join) + 1);
|
|
- if (UNLIKELY (!tmp)) {
|
|
|
|
- it->rc = LSUP_MEM_ERR;
|
|
|
|
- goto finally;
|
|
|
|
- }
|
|
|
|
|
|
+ if (UNLIKELY (!tmp)) goto memfail;
|
|
res = strcat (strcat (tmp, o_join), o_str);
|
|
res = strcat (strcat (tmp, o_join), o_str);
|
|
o_join = ", ";
|
|
o_join = ", ";
|
|
}
|
|
}
|
|
@@ -241,18 +243,19 @@ gr_to_ttl_iter (void *h, char **res_p) {
|
|
|
|
|
|
char *s_sep = ".\n";
|
|
char *s_sep = ".\n";
|
|
char *tmp = realloc (res, strlen (res) + strlen (s_sep) + 1);
|
|
char *tmp = realloc (res, strlen (res) + strlen (s_sep) + 1);
|
|
- if (UNLIKELY (!tmp)) {
|
|
|
|
- res = NULL;
|
|
|
|
- it->rc = LSUP_MEM_ERR;
|
|
|
|
- goto finally;
|
|
|
|
- }
|
|
|
|
|
|
+ if (UNLIKELY (!tmp)) goto memfail;
|
|
|
|
|
|
- *res_p = strcat (res, s_sep);
|
|
|
|
|
|
+ *res_p = strcat (tmp, s_sep);
|
|
|
|
|
|
-finally:
|
|
|
|
LSUP_link_map_iter_free (lmit);
|
|
LSUP_link_map_iter_free (lmit);
|
|
|
|
+ LSUP_link_map_free (lmap);
|
|
|
|
|
|
return it->rc;
|
|
return it->rc;
|
|
|
|
+
|
|
|
|
+memfail:
|
|
|
|
+ free (res);
|
|
|
|
+ *res_p = NULL;
|
|
|
|
+ return LSUP_MEM_ERR;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|