Browse Source

WIP Fixing encoder content.

Stefano Cossu 1 year ago
parent
commit
64fc2265a0
5 changed files with 44 additions and 27 deletions
  1. 6 6
      include/namespace.h
  2. 19 9
      src/codec/codec_ttl.c
  3. 1 1
      src/environment.c
  4. 16 7
      src/namespace.c
  5. 2 4
      src/term.c

+ 6 - 6
include/namespace.h

@@ -101,10 +101,10 @@ LSUP_nsmap_get_pfx (const LSUP_NSMap *map, const char *ns);
  * @param[in] uri URI string to denormalize.
  *
  * @param[out] fq_uri String pointer to be filled with the FQ URI. If the
- *  namespace is not in the map or an error occurred, this will be NULL.
- *  The caller is in charge of freeing the memory.
+ *  namespace is not in the map or an error occurred, this will be NULL. The
+ *  caller is in charge of freeing the memory.
  *
- * @return LSUP_OK on success, LSUP_NORESULT if no entry was found in the map,
+ * @return LSUP_OK on success, LSUP_VALUE_ERR if no entry was found in the map,
  *  LSUP_MEM_ERR if a memory allocation error ocurred.
  */
 LSUP_rc
@@ -124,9 +124,9 @@ LSUP_nsmap_normalize_uri (
  *
  * @param[in] uri URI string to normalize.
  *
- * @param[out] String pointer to be filled with the prefixed URI. If the
- *  namespace is not in the map or an error occurred, this will be NULL.
- *  The caller is in charge of freeing the memory.
+ * @param[out] pfx_uri String pointer to be filled with the prefixed URI. If
+ *  the namespace is not in the map, this will be a duplicate of the original
+ *  FQ URI. The caller is in charge of freeing the memory.
  *
  * @return LSUP_OK on success, LSUP_NORESULT if no entry was found in the map,
  *  LSUP_MEM_ERR if a memory allocation error ocurred.

+ 19 - 9
src/codec/codec_ttl.c

@@ -24,16 +24,21 @@ term_to_ttl (const LSUP_Term *term, const LSUP_NSMap *nsm, char **out_p)
 {
     LSUP_rc rc;
     char *out = NULL, *escaped;
-    const char *metadata = NULL;
+    char *metadata = NULL;
     size_t buf_len;
 
-    // Free previous content if not NULL.
-    if (*out_p != NULL) out = realloc (*out_p, 0);
-
+    LSUP_rc md_rc;
     switch (term->type) {
         case LSUP_TERM_IRIREF:
-            LSUP_nsmap_denormalize_uri (nsm, term->data, &out);
-            if (UNLIKELY (!out)) return LSUP_MEM_ERR;
+            md_rc = LSUP_nsmap_denormalize_uri (nsm, term->data, &out);
+            PRCCK (md_rc);
+            if (md_rc == LSUP_NORESULT) {
+                // 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), ">");
+            }
             rc = LSUP_OK;
             break;
 
@@ -53,8 +58,12 @@ term_to_ttl (const LSUP_Term *term, const LSUP_NSMap *nsm, char **out_p)
                 term->datatype != 0
                 && term->datatype != LSUP_default_datatype
             ) {
-                metadata = term->datatype->data;
-                buf_len += strlen (metadata) + 4; // Room for ^^<>
+                md_rc = LSUP_nsmap_denormalize_uri (
+                        nsm, term->datatype->data, &metadata);
+                PRCCK (md_rc);
+                // Room for `^^<>` for FQURI, `^^` for NS URI
+                unsigned padding = md_rc == LSUP_NORESULT ? 4 : 2;
+                buf_len += strlen (metadata) + padding;
             }
 
             out = realloc (out, buf_len);
@@ -78,7 +87,7 @@ term_to_ttl (const LSUP_Term *term, const LSUP_NSMap *nsm, char **out_p)
             buf_len = strlen (escaped) + 3; // Room for "" and terminator
 
             if (term->lang[0] != '\0') {
-                metadata = term->lang;
+                metadata = strndup (term->lang, sizeof (LSUP_LangTag));
                 buf_len += strlen (metadata) + 1; // Room for @
             }
 
@@ -108,6 +117,7 @@ term_to_ttl (const LSUP_Term *term, const LSUP_NSMap *nsm, char **out_p)
             out = NULL;
             rc = LSUP_PARSE_ERR;
     }
+    free (metadata);
 
     *out_p = out;
     return rc;

+ 1 - 1
src/environment.c

@@ -69,7 +69,7 @@ LSUP_init (void)
     // Default context URI.
     char *default_ctx_str = getenv ("LSUP_DEFAULT_CTX");
     if (!default_ctx_str ) default_ctx_str = DEFAULT_CTX_LABEL;
-    LSUP_default_ctx = LSUP_iriref_new (default_ctx_str, LSUP_default_nsm);
+    LSUP_default_ctx = LSUP_iriref_new (default_ctx_str, NULL);
     if (UNLIKELY (!LSUP_default_ctx)) return LSUP_ERROR;
     LSUP_default_ctx_buf = LSUP_term_serialize (LSUP_default_ctx);
     if (UNLIKELY (!LSUP_default_ctx_buf)) return LSUP_ERROR;

+ 16 - 7
src/namespace.c

@@ -145,6 +145,7 @@ LSUP_rc
 LSUP_nsmap_normalize_uri (
         const NSMap *map, const char *pfx_uri, char **fq_uri_p)
 {
+    LSUP_rc rc;
     char *fq_uri = NULL;
 
     size_t pfx_len = strcspn (pfx_uri, ":");
@@ -169,13 +170,16 @@ LSUP_nsmap_normalize_uri (
 
         strcpy (fq_uri, ns);
         strcat (fq_uri, pfx_uri + pfx_len + 1);
-    }
 
-    else fq_uri = strdup (pfx_uri);
+        rc = LSUP_OK;
+    } else {
+        log_error ("No NS prefix found in map to normalize %s", pfx_uri);
+        rc = LSUP_VALUE_ERR;
+    }
 
     *fq_uri_p = fq_uri;
 
-    return LSUP_OK;
+    return rc;
 }
 
 
@@ -189,11 +193,13 @@ LSUP_nsmap_denormalize_uri (
      * This function has to count the characters left over from the match in
      * order to add the URI suffix.
      */
+    LSUP_rc rc;
     const NSEntry *entry;
     const char *pfx = NULL;
     size_t i = 0, offset;
+
     while (hashmap_iter ((NSMap *)map, &i, (void **) &entry)) {
-        offset = strlen(entry->ns);
+        offset = strlen (entry->ns);
         if (strncmp (entry->ns, fq_uri, offset) == 0) {
             pfx = entry->pfx;
             break;
@@ -208,13 +214,16 @@ LSUP_nsmap_denormalize_uri (
 
         sprintf (pfx_uri, "%s:%s", pfx, fq_uri + offset);
 
-    }
+        rc = LSUP_OK;
 
-    else pfx_uri = strdup (fq_uri);
+    } else {
+        pfx_uri = strdup (fq_uri);
+        rc = LSUP_NORESULT;
+    }
 
     *pfx_uri_p = pfx_uri;
 
-    return LSUP_OK;
+    return rc;
 }
 
 

+ 2 - 4
src/term.c

@@ -757,13 +757,11 @@ term_init (
 
             // Find fully qualified IRI to parse.
             if (term->type == LSUP_TERM_NS_IRIREF) {
-                if (LSUP_nsmap_normalize_uri (
-                    metadata, data, &fquri) != LSUP_OK
-                ) {
+                if (LSUP_nsmap_normalize_uri (metadata, data, &fquri) < 0) {
                     log_error ("Error normalizing IRI data.");
-
                     return LSUP_VALUE_ERR;
                 }
+
                 log_debug ("Fully qualified IRI: %s", fquri);
             } else fquri = (char *) data;