1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033 |
- #include "tpl.h"
- #include "term.h"
- /** @brief tpl packing format for a term.
- *
- * The pack elements are: 1. term type (char); 2. data (string); 3. void* type
- * metadata, cast to 8-byte unsigned.
- */
- #define TERM_PACK_FMT "csU"
- #define MAX_VALID_TERM_TYPE LSUP_TERM_BNODE /* For type validation. */
- /*
- * Data structures.
- */
- /// Sub-match coordinates in IRI parsing results.
- typedef struct match_coord_t {
- size_t offset; ///< Offset of match from start of string.
- size_t size; ///< Length of match.
- } MatchCoord;
- /// Matching sub-patterns for IRI parts.
- struct iri_info_t {
- LSUP_NSMap * nsm; ///< NSM handle for prefixed IRI.
- MatchCoord prefix; ///< URI prefix (scheme + authority).
- MatchCoord path; ///< URI path (including fragment).
- MatchCoord frag; ///< URI fragment.
- };
- /// Key-term pair in term set.
- typedef struct keyed_term {
- LSUP_Key key; ///< Key (hash) of the term.
- LSUP_Term * term; ///< Term handle.
- } KeyedTerm;
- /** @brief Single link between a term and a term set.
- *
- * This link is not qualified and may not be used by itself. It belongs
- * in a #LSUP_LinkMap which qualifies all links of the same type.
- */
- typedef struct link {
- KeyedTerm * term; ///< Linked term.
- LSUP_TermSet * tset; ///< Term set linked to the term.
- } Link;
- /// Opaque link map iterator.
- struct link_map_iter {
- const LSUP_LinkMap *map; ///< Link map to iterate.
- size_t i; ///< Linking term loop cursor.
- size_t j; ///< Term set loop cursor.
- LSUP_Term * ext; ///< External link to look for connections.
- Link * link; ///< Current link being retrieved.
- };
- /*
- * A link map is thus nested:
- *
- * - A link map contains a hash map of Link instances (link).
- * - Each Link contains a KeyedTerm (term) and a TermSet (tset).
- * - Each term set is a hash map of KeyedTerm instances.
- * - Each KeyedTerm contains a Term and its hash.
- */
- typedef struct link_map {
- LSUP_LinkType type; ///< Link type.
- struct hashmap * links; ///< Map of #Link instances.
- } LSUP_LinkMap;
- /*
- * External variables.
- */
- uint32_t LSUP_default_dtype_key = 0;
- LSUP_Term *LSUP_default_datatype = NULL;
- LSUP_TermSet *LSUP_term_cache = NULL;
- /*
- * Static variables.
- */
- // Characters not allowed in a URI string.
- static const char *invalid_uri_chars = "<>\" {}|\\^`";
- /// Minimum valid type code.
- static const LSUP_TermType MIN_VALID_TYPE = LSUP_TERM_IRIREF;
- /// Maximum valid type code. Change this if adding to enum LSUP_TermType.
- static const LSUP_TermType MAX_VALID_TYPE = LSUP_TERM_BNODE;
- /*
- * Static prototypes.
- */
- static LSUP_rc
- term_init (
- LSUP_Term *term, LSUP_TermType type, const char *data, void *metadata);
- /*
- * Term set callbacks.
- */
- static uint64_t
- tset_hash_fn (
- const void *item, uint64_t seed0, uint64_t seed1)
- { return ((const KeyedTerm *) item)->key; }
- static int
- tset_cmp_fn (const void *a, const void *b, void *udata)
- {
- return
- ((const KeyedTerm *) a)->key -
- ((const KeyedTerm *) b)->key;
- }
- static void
- tset_free_fn (void *item)
- { LSUP_term_free (((KeyedTerm *) item)->term); }
- /*
- * Link map callbacks.
- */
- static uint64_t
- link_map_hash_fn (
- const void *item, uint64_t seed0, uint64_t seed1)
- { return ((const Link *)item)->term->key; }
- static int
- link_map_cmp_fn (const void *a, const void *b, void *udata)
- {
- return
- ((const Link *)a)->term->key -
- ((const Link *)b)->term->key;
- }
- static void
- link_map_free_fn (void *item)
- {
- Link *link = item;
- LSUP_term_free (link->term->term);
- free (link->term);
- LSUP_term_set_free (link->tset);
- }
- static LSUP_rc parse_iri (char *iri, MatchCoord coords[]);
- /*
- * Term API.
- */
- LSUP_Term *
- LSUP_term_new (
- LSUP_TermType type, const char *data, void *metadata)
- {
- LSUP_Term *term;
- CALLOC_GUARD (term, NULL);
- // If undefined, just set the type.
- if (type == LSUP_TERM_UNDEFINED) term->type = type;
- else if (UNLIKELY (term_init (
- term, type, data, metadata) != LSUP_OK)) {
- free (term);
- return NULL;
- }
- return term;
- }
- LSUP_Term *
- LSUP_term_copy (const LSUP_Term *src)
- {
- void *metadata = NULL;
- if (LSUP_IS_IRI (src))
- metadata = (void *) LSUP_iriref_nsm (src);
- else if (src->type == LSUP_TERM_LITERAL)
- metadata = (void *) src->datatype;
- else if (src->type == LSUP_TERM_LT_LITERAL) {
- metadata = (void *) src->lang;
- }
- return LSUP_term_new (src->type, src->data, metadata);
- }
- LSUP_Term *
- LSUP_term_new_from_buffer (const LSUP_Buffer *sterm)
- {
- if (UNLIKELY (!sterm)) return NULL;
- LSUP_Term *term = NULL;
- LSUP_TermType type = LSUP_TERM_UNDEFINED;
- char *data = NULL;
- void *metadata;
- tpl_node *tn;
- tn = tpl_map (TERM_PACK_FMT, &type, &data, &metadata);
- if (UNLIKELY (!tn)) goto finally;
- if (UNLIKELY (tpl_load (tn, TPL_MEM, sterm->addr, sterm->size) < 0)) {
- log_error ("Error loading serialized term.");
- goto finally;
- }
- if (UNLIKELY (tpl_unpack (tn, 0) < 0)) {
- log_error ("Error unpacking serialized term.");
- goto finally;
- }
- if (type == LSUP_TERM_LT_LITERAL)
- term = LSUP_lt_literal_new (data, (char *)&metadata);
- else term = LSUP_term_new (type, data, metadata);
- finally:
- tpl_free (tn);
- free (data);
- return term;
- }
- LSUP_Term *
- LSUP_iriref_absolute (const LSUP_Term *root, const LSUP_Term *iri)
- {
- if (! LSUP_IS_IRI (iri)) {
- log_error ("Provided path is not an IRI.");
- return NULL;
- }
- if (! LSUP_IS_IRI (root)) {
- log_error ("Provided root is not an IRI.");
- return NULL;
- }
- char
- *data,
- *pfx = LSUP_iriref_prefix (iri);
- if (strlen (pfx) > 0) data = iri->data;
- else if (iri->data[0] == '/') {
- free (pfx);
- pfx = LSUP_iriref_prefix (root);
- data = malloc (strlen (iri->data) + strlen (pfx) + 1);
- if (!data) return NULL;
- sprintf (data, "%s%s", pfx, iri->data);
- } else {
- data = malloc (strlen (iri->data) + strlen (root->data) + 1);
- if (!data) return NULL;
- sprintf (data, "%s%s", root->data, iri->data);
- }
- free (pfx);
- LSUP_Term *ret = LSUP_iriref_new (data, NULL);
- if (data != iri->data) free (data);
- return ret;
- }
- LSUP_Term *
- LSUP_iriref_relative (const LSUP_Term *root, const LSUP_Term *iri)
- {
- if (! LSUP_IS_IRI (iri)) {
- log_error ("Provided path is not an IRI.");
- return NULL;
- }
- if (! LSUP_IS_IRI (root)) {
- log_error ("Provided root is not an IRI.");
- return NULL;
- }
- size_t offset = (
- strstr (iri->data, root->data) == iri->data ?
- strlen (root->data) : 0);
- return LSUP_iriref_new (iri->data + offset, LSUP_iriref_nsm (iri));
- }
- LSUP_Buffer *
- LSUP_term_serialize (const LSUP_Term *term)
- {
- /*
- * In serializing a term, the fact that two terms of different types may
- * be semantically identical must be taken into account. Specifically, a
- * namespace-prefixed IRI ref is identical to its fully qualified version,
- * and a LSUP_TERM_LT_LITERAL with no language tag is identical to a
- * LSUP_TERM_LITERAL of xsd:string type, made up of the same string. Such
- * terms must have identical serializations.
- */
- if (UNLIKELY (!term)) return NULL;
- LSUP_Term *tmp_term;
- void *metadata = NULL;
- if (term->type == LSUP_TERM_NS_IRIREF) {
- // For IRI refs, simply serialize the FQ version of the term.
- char *fq_uri;
- if (LSUP_nsmap_normalize_uri (
- term->iri_info->nsm, term->data, &fq_uri
- ) != LSUP_OK) return NULL;
- tmp_term = LSUP_iriref_new (fq_uri, NULL);
- free (fq_uri);
- } else if (term->type == LSUP_TERM_LT_LITERAL) {
- // For LT literals with empty lang tag, convert to a normal xsd:string.
- if (strlen (term->lang) == 0)
- tmp_term = LSUP_literal_new (term->data, NULL);
- else tmp_term = LSUP_lt_literal_new (term->data, (char *) term->lang);
- } else tmp_term = LSUP_term_new (
- term->type, term->data, (void *) term->datatype);
- // "datatype" can be anything here since it's cast to void *.
- // metadata field is ignored for IRI ref.
- if (tmp_term->type == LSUP_TERM_LITERAL)
- metadata = tmp_term->datatype;
- else if (tmp_term->type == LSUP_TERM_LT_LITERAL)
- memcpy (&metadata, tmp_term->lang, sizeof (metadata));
- LSUP_Buffer *sterm;
- MALLOC_GUARD (sterm, NULL);
- //log_trace ("Effective term being serialized: %s", tmp_term->data);
- int rc = tpl_jot (
- TPL_MEM, &sterm->addr, &sterm->size, TERM_PACK_FMT,
- &tmp_term->type, &tmp_term->data, &metadata);
- LSUP_term_free (tmp_term);
- if (rc != 0) {
- LSUP_buffer_free (sterm);
- return NULL;
- }
- return sterm;
- }
- LSUP_Key
- LSUP_term_hash (const LSUP_Term *term)
- {
- LSUP_Buffer *buf;
- if (UNLIKELY (!term)) buf = BUF_DUMMY;
- else buf = LSUP_term_serialize (term);
- LSUP_Key key = LSUP_buffer_hash (buf);
- LSUP_buffer_free (buf);
- return key;
- }
- void
- LSUP_term_free (LSUP_Term *term)
- {
- if (UNLIKELY (!term)) return;
- if (LSUP_IS_IRI (term)) free (term->iri_info);
- free (term->data);
- free (term);
- }
- LSUP_NSMap *
- LSUP_iriref_nsm (const LSUP_Term *iri)
- {
- if (iri->type != LSUP_TERM_IRIREF && iri->type != LSUP_TERM_NS_IRIREF) {
- log_error ("Term is not a IRI ref type.");
- return NULL;
- }
- return iri->iri_info->nsm;
- }
- char *
- LSUP_iriref_prefix (const LSUP_Term *iri)
- {
- if (iri->type != LSUP_TERM_IRIREF && iri->type != LSUP_TERM_NS_IRIREF) {
- log_error ("Term is not a IRI ref type.");
- return NULL;
- }
- // if (iri->iri_info->prefix.size == 0) return NULL;
- return strndup (
- iri->data + iri->iri_info->prefix.offset,
- iri->iri_info->prefix.size);
- }
- char *
- LSUP_iriref_path (const LSUP_Term *iri)
- {
- if (iri->type != LSUP_TERM_IRIREF && iri->type != LSUP_TERM_NS_IRIREF) {
- log_error ("Term is not a IRI ref type.");
- return NULL;
- }
- // if (iri->iri_info->path.size == 0) return NULL;
- return strndup (
- iri->data + iri->iri_info->path.offset,
- iri->iri_info->path.size);
- }
- char *
- LSUP_iriref_frag (const LSUP_Term *iri)
- {
- if (iri->type != LSUP_TERM_IRIREF && iri->type != LSUP_TERM_NS_IRIREF) {
- log_error ("Term is not a IRI ref type.");
- return NULL;
- }
- // if (iri->iri_info->frag.size == 0) return NULL;
- return strndup (
- iri->data + iri->iri_info->frag.offset,
- iri->iri_info->frag.size);
- }
- /*
- * Triple API.
- */
- LSUP_Triple *
- LSUP_triple_new(LSUP_Term *s, LSUP_Term *p, LSUP_Term *o)
- {
- LSUP_Triple *spo = malloc (sizeof (*spo));
- if (!spo) return NULL;
- if (UNLIKELY (LSUP_triple_init (spo, s, p, o))) {
- free (spo);
- return NULL;
- }
- return spo;
- }
- LSUP_Triple *
- LSUP_triple_new_from_btriple (const LSUP_BufferTriple *sspo)
- {
- LSUP_Triple *spo = malloc (sizeof (*spo));
- if (!spo) return NULL;
- spo->s = LSUP_term_new_from_buffer (sspo->s);
- spo->p = LSUP_term_new_from_buffer (sspo->p);
- spo->o = LSUP_term_new_from_buffer (sspo->o);
- return spo;
- }
- LSUP_BufferTriple *
- LSUP_triple_serialize (const LSUP_Triple *spo)
- {
- LSUP_BufferTriple *sspo = malloc (sizeof (*sspo));
- if (!sspo) return NULL;
- sspo->s = LSUP_term_serialize (spo->s);
- sspo->p = LSUP_term_serialize (spo->p);
- sspo->o = LSUP_term_serialize (spo->o);
- return sspo;
- }
- LSUP_rc
- LSUP_triple_init (LSUP_Triple *spo, LSUP_Term *s, LSUP_Term *p, LSUP_Term *o)
- {
- /* FIXME TRP_DUMMY is a problem here.
- if (! LSUP_IS_IRI (s) && s->type != LSUP_TERM_BNODE) {
- log_error ("Subject is not of a valid term type: %d", s->type);
- return LSUP_VALUE_ERR;
- }
- if (! LSUP_IS_IRI (p)) {
- log_error ("Predicate is not of a valid term type: %d", p->type);
- return LSUP_VALUE_ERR;
- }
- */
- spo->s = s;
- spo->p = p;
- spo->o = o;
- return LSUP_OK;
- }
- void
- LSUP_triple_done (LSUP_Triple *spo)
- {
- if (UNLIKELY (!spo)) return;
- LSUP_term_free (spo->s);
- LSUP_term_free (spo->p);
- LSUP_term_free (spo->o);
- }
- void
- LSUP_triple_free (LSUP_Triple *spo)
- {
- if (UNLIKELY (!spo)) return;
- LSUP_term_free (spo->s);
- LSUP_term_free (spo->p);
- LSUP_term_free (spo->o);
- free (spo);
- }
- /*
- * Multi-add functions.
- */
- LSUP_TermSet *
- LSUP_term_set_new ()
- {
- // Capacity of 4 is an arbitrary guess.
- LSUP_TermSet *ts = hashmap_new (
- sizeof (KeyedTerm), 4, LSUP_HASH_SEED, 0,
- tset_hash_fn, tset_cmp_fn, tset_free_fn, NULL);
- if (UNLIKELY (hashmap_oom (ts))) return NULL;
- return ts;
- }
- LSUP_rc
- LSUP_term_set_add (LSUP_TermSet *ts, LSUP_Term *term, LSUP_Term **existing)
- {
- LSUP_Hash key = LSUP_term_hash (term);
- KeyedTerm entry_s = {.key=key, .term=term};
- KeyedTerm *ex = hashmap_get (ts, &entry_s);
- if (ex) {
- if (existing) *existing = ex->term;
- return LSUP_NOACTION;
- }
- hashmap_set (ts, &entry_s);
- if (hashmap_oom (ts)) return LSUP_MEM_ERR;
- return LSUP_OK;
- }
- const LSUP_Term *
- LSUP_term_set_get (LSUP_TermSet *ts, LSUP_Key key)
- {
- KeyedTerm *entry = hashmap_get (ts, &(KeyedTerm){.key=key});
- if (entry) log_trace ("ID found for key %lx: %s", key, entry->term->data);
- else log_trace ("No ID found for key %lx.", key);
- return (entry) ? entry->term : NULL;
- }
- LSUP_rc
- LSUP_term_set_next (LSUP_TermSet *ts, size_t *i, LSUP_Term **term)
- {
- KeyedTerm *kt = NULL;
- if (!hashmap_iter (ts, i, (void **)&kt)) return LSUP_END;
- if (term) *term = kt->term;
- return LSUP_OK;
- }
- void
- LSUP_term_set_free (LSUP_TermSet *ts)
- { hashmap_free (ts); }
- LSUP_LinkMap *
- LSUP_link_map_new (LSUP_LinkType type)
- {
- LSUP_LinkMap *cm;
- MALLOC_GUARD (cm, NULL);
- cm->type = type;
- cm->links = hashmap_new (
- sizeof (Link), 0, LSUP_HASH_SEED, 0,
- link_map_hash_fn, link_map_cmp_fn, link_map_free_fn, NULL);
- return cm;
- }
- void
- LSUP_link_map_free (LSUP_LinkMap *cm)
- {
- hashmap_free (cm->links);
- free (cm);
- }
- LSUP_LinkType
- LSUP_link_map_type (const LSUP_LinkMap *map)
- { return map->type; }
- // TODO Memory error handling.
- LSUP_rc
- LSUP_link_map_add (
- LSUP_LinkMap *cmap, LSUP_Term *term, LSUP_TermSet *tset)
- {
- // Keyed term to look up the link term and insert it, if necessary.
- KeyedTerm entry_s = {.key=LSUP_term_hash (term), .term=term};
- Link *ex = hashmap_get (cmap->links, &(Link){.term=&entry_s});
- if (ex) {
- // Add terms one by one to the existing term set.
- log_trace (
- "Linking term %s exists. Adding individual terms.",
- ex->term->term->data);
- size_t i = 0;
- KeyedTerm *kt;
- while (hashmap_iter (tset, &i, (void **)&kt)) {
- log_trace (
- "Adding term %s to link %s",
- kt->term->data, ex->term->term->data);
- if (hashmap_get (ex->tset, kt))
- // Term already exist, free the new one and move on.
- LSUP_term_free (kt->term);
- else
- // Insert KeyedTerm, the term set now owns the underlying term.
- hashmap_set (ex->tset, kt);
- }
- // Free link term that hasn't been used.
- LSUP_term_free (term);
- } else {
- // Add the new term and the termset wholesale.
- log_trace ("Adding new linking term %s.", term->data);
- // Allocate inserted member on heap, it will be owned by the map.
- KeyedTerm *ins;
- MALLOC_GUARD (ins, LSUP_MEM_ERR);
- memcpy (ins, &entry_s, sizeof (entry_s));
- Link link = {.term=ins, .tset=tset};
- hashmap_set (cmap->links, &link);
- }
- return LSUP_OK;
- }
- LSUP_LinkMapIterator *
- LSUP_link_map_iter_new (const LSUP_LinkMap *lmap, LSUP_Term *ext)
- {
- LSUP_LinkMapIterator *it;
- CALLOC_GUARD (it, NULL);
- it->map = lmap;
- it->ext = ext;
- return it;
- }
- void
- LSUP_link_map_iter_free (LSUP_LinkMapIterator *it)
- { free (it); }
- LSUP_rc
- LSUP_link_map_next (
- LSUP_LinkMapIterator *it, LSUP_Term **lt, LSUP_TermSet **ts)
- {
- if (!hashmap_iter (it->map->links, &it->i, (void **)&it->link))
- return LSUP_END;
- *lt = it->link->term->term;
- *ts = it->link->tset;
- return LSUP_OK;
- }
- // TODO dismantle if the only triple generator is for the graph.
- LSUP_rc
- LSUP_link_map_triples (
- LSUP_LinkMapIterator *it, LSUP_Triple *spo)
- {
- // Assign external (related) term.
- if (it->map->type == LSUP_LINK_INBOUND)
- spo->o = it->ext;
- else if (it->map->type == LSUP_LINK_OUTBOUND)
- spo->s = it->ext;
- else spo->p = it->ext;
- KeyedTerm *kt;
- // If we are already handling a link, continue the internal loop.
- if (it->link) goto int_loop;
- ext_loop:
- // Advance external counter and start new internal loop.
- it->j = 0;
- if (!hashmap_iter (it->map->links, &it->i, (void **)&it->link))
- return LSUP_END;
- int_loop:
- // If end of the term set is reached, start with a new linking term.
- if (!hashmap_iter (it->link->tset, &it->j, (void **)&kt)) goto ext_loop;
- // Continue pulling from term set.
- // Assign linking term.
- if (it->map->type == LSUP_LINK_EDGE) spo->s = it->link->term->term;
- else spo->p = it->link->term->term;
- // Assign term in term set.
- if (it->map->type == LSUP_LINK_INBOUND) spo->s = kt->term;
- else spo->o = kt->term;
- return LSUP_OK;
- }
- /*
- * Static functions.
- */
- static LSUP_rc
- term_init (
- LSUP_Term *term, LSUP_TermType type,
- const char *data, void *metadata)
- {
- // This can never be LSUP_TERM_UNDEFINED.
- if (type < MIN_VALID_TYPE || type > MAX_VALID_TYPE) {
- log_error ("%d is not a valid term type.", type);
- return LSUP_VALUE_ERR;
- }
- term->type = type;
- if (data) {
- // Validate IRI.
- if (LSUP_IS_IRI (term)) {
- char *fquri;
- // Find fully qualified IRI to parse.
- if (term->type == LSUP_TERM_NS_IRIREF) {
- 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;
- if (strpbrk (fquri, invalid_uri_chars) != NULL) {
- log_warn (
- "Characters %s are not valid in a URI. Got: %s\n",
- invalid_uri_chars, fquri);
- #if 0
- // TODO This causes W3C TTL test #29 to fail. Remove?
- return LSUP_VALUE_ERR;
- #endif
- }
- // Capture interesting IRI parts.
- MatchCoord matches[7] = {}; // Initialize all to 0.
- if (UNLIKELY (parse_iri (fquri, matches) != LSUP_OK)) {
- log_error ("Error matching URI pattern.");
- return LSUP_VALUE_ERR;
- }
- if (term->type == LSUP_TERM_NS_IRIREF) free (fquri);
- MALLOC_GUARD (term->iri_info, LSUP_MEM_ERR);
- term->iri_info->prefix = matches[1];
- term->iri_info->path = matches[4];
- term->iri_info->frag = matches[6];
- term->iri_info->nsm = metadata;
- }
- term->data = strdup (data);
- } else {
- // No data. Make up a random UUID or URI if allowed.
- if (type == LSUP_TERM_IRIREF || type == LSUP_TERM_BNODE) {
- uuid_t uuid;
- uuid_generate_random (uuid);
- uuid_str_t uuid_str;
- uuid_unparse_lower (uuid, uuid_str);
- if (type == LSUP_TERM_IRIREF) {
- term->data = malloc (UUID4_URN_SIZE);
- snprintf (
- term->data, UUID4_URN_SIZE, "urn:uuid4:%s", uuid_str);
- MALLOC_GUARD (term->iri_info, LSUP_MEM_ERR);
- // Allocate IRI match patterns manually.
- term->iri_info->prefix.offset = 0;
- term->iri_info->prefix.size = 4;
- term->iri_info->path.offset = 4;
- term->iri_info->path.size = UUIDSTR_SIZE + 6;
- term->iri_info->frag.offset = 0;
- term->iri_info->frag.size = 0;
- term->iri_info->nsm = NULL;
- } else term->data = strdup (uuid_str);
- } else {
- log_error ("No data provided for term.");
- return LSUP_VALUE_ERR;
- }
- }
- if (term->type == LSUP_TERM_LT_LITERAL) {
- if (!metadata) {
- log_warn ("Lang tag is NULL. Creating a non-tagged literal.");
- term->type = LSUP_TERM_LITERAL;
- } else {
- char *lang_str = (char *) metadata;
- log_trace("Lang string: '%s'", lang_str);
- // Lang tags longer than 7 characters will be truncated.
- strncpy(term->lang, lang_str, sizeof (term->lang) - 1);
- if (strlen (term->lang) < 1) {
- log_error ("Lang tag cannot be an empty string.");
- return LSUP_VALUE_ERR;
- }
- term->lang[7] = '\0';
- }
- }
- if (term->type == LSUP_TERM_LITERAL) {
- term->datatype = metadata;
- if (! term->datatype) term->datatype = LSUP_default_datatype;
- log_trace ("Storing data type: %s", term->datatype->data);
- if (! LSUP_IS_IRI (term->datatype)) {
- log_error (
- "Literal data type is not an IRI: %s",
- term->datatype->data);
- return LSUP_VALUE_ERR;
- }
- LSUP_Term *ex = NULL;
- LSUP_term_set_add (LSUP_term_cache, term->datatype, &ex);
- if (ex && ex != term->datatype) {
- // Replace datatype handle with the one in term cache, and free
- // the new one.
- if (term->datatype != LSUP_default_datatype)
- LSUP_term_free (term->datatype);
- term->datatype = ex;
- }
- //log_trace ("Datatype address: %p", term->datatype);
- log_trace ("Datatype hash: %lx", LSUP_term_hash (term->datatype));
- } else if (term->type == LSUP_TERM_BNODE) {
- // TODO This is not usable for global skolemization.
- term->bnode_id = LSUP_HASH (
- term->data, strlen (term->data) + 1, LSUP_HASH_SEED);
- }
- return LSUP_OK;
- }
- /**
- * @brief scan an IRI string and parse IRI parts.
- *
- * Experimental replacement of a regex engine for better performance.
- *
- * Slightly adapted from regex on
- * https://datatracker.ietf.org/doc/html/rfc3986#appendix-B to capture relevant
- * parts of the IRI.
- *
- * Reference regex and group numbering:
- * ^((?([^:/?#]+):)?(?//([^/?#]*))?)((?[^?#]*)(?\?([^#]*))?(?#(.*))?)
- * 1 2 3 4 5 6
- *
- * Capturing groups:
- *
- * #0: Full parsed URI (http://example.org/123/456/?query=blah#frag)
- * #1: Prefix (http://example.org)
- * #2: Scheme (http)
- * #3: Authority (example.org)
- * #4: Path, including query and fragment (/123/456/?query=blah#frag)
- * #5: Query (query=blah)
- * #6: Fragment (frag)
- *
- *
- * @param iri_str[in] IRI string to parse.
- *
- * @param match_coord_t[out] coord Coordinates to be stored. This must be a
- * pre-allocated array of at least 7 elements.
- *
- * The first size_t of each element stores the relative position of a match,
- * and the second one stores the length of the match. A length of 0 indicates
- * no match.
- */
- static LSUP_rc
- parse_iri (char *iri_str, MatchCoord coord[]) {
- char *cur = iri_str;
- size_t iri_len = strlen (iri_str);
- MatchCoord tmp = {}; // Temporary storage for capture groups
- // Redundant if only called by term_init.
- // memset (coord, 0, sizeof(*coord));
- log_debug ("Parsing IRI: %s", iri_str);
- // #2: ([^:/?#]+)
- while (
- *cur != ':' && *cur != '/' && *cur != '?'
- && *cur != '#' && *cur != '\0') {
- tmp.size++;
- cur++;
- }
- // Non-capturing: (?([^:/?#]+):)?
- if (tmp.size > 0 && *cur == ':') {
- // Got capture groups #2 and #3. Store them.
- coord[2].offset = 0;
- coord[2].size = tmp.size;
- cur++;
- log_debug ("Group #2: %lu, %lu", coord[2].offset, coord[2].size);
- } else cur = iri_str; // Backtrack if no match.
- // Non-capturing: (?//([^/?#]*))?
- if (*cur == '/' && *(cur + 1) == '/') {
- cur += 2;
- tmp.offset = cur - iri_str;
- tmp.size = 0;
- // #3: ([^/?#]*)
- while (*cur != '/' && *cur != '?' && *cur != '#' && *cur != '\0') {
- tmp.size++;
- cur++;
- }
- coord[3].offset = tmp.offset;
- coord[3].size = tmp.size;
- log_debug ("Group #3: %lu, %lu", coord[3].offset, coord[3].size);
- }
- // Capture group 1.
- coord[1].offset = 0;
- coord[1].size = cur - iri_str;
- log_debug ("Group #1: %lu, %lu", coord[1].offset, coord[1].size);
- tmp.offset = cur - iri_str;
- tmp.size = 0;
- coord[4].offset = tmp.offset;
- coord[4].size = iri_len - tmp.offset;
- log_debug ("Group #4: %lu, %lu", coord[4].offset, coord[4].size);
- // Non-capturing: (?[^?#]*)
- while (*cur != '?' && *cur != '#' && *cur != '\0') {
- tmp.size++;
- cur++;
- }
- // Non-capturing: (?\?([^#]*))
- if (*cur == '?') {
- // 5: ([^#]*)
- tmp.offset = ++cur - iri_str;
- tmp.size = 0;
- while (*cur != '#' && *cur != '\0') {
- tmp.size++;
- cur++;
- }
- if (tmp.size > 0) {
- // Got capture group #5.
- coord[5].offset = tmp.offset;
- coord[5].size = tmp.size;
- log_debug ("Group #5: %lu, %lu", coord[5].offset, coord[5].size);
- }
- }
- // Non-capturing: (?#(.*))?
- if (*cur == '#') {
- // #6: (.*)
- coord[6].offset = ++cur - iri_str;
- coord[6].size = iri_str + iri_len - cur;
- log_debug ("Group #6: %lu, %lu", coord[6].offset, coord[6].size);
- }
- coord[0].offset = 0;
- coord[0].size = iri_len;
- log_debug ("Full match: %lu, %lu", coord[0].offset, coord[0].size);
- return LSUP_OK;
- }
- /*
- * Extern inline functions.
- */
- LSUP_Key LSUP_term_hash (const LSUP_Term *term);
- LSUP_Term *LSUP_iriref_new (const char *data, LSUP_NSMap *nsm);
- LSUP_Term *LSUP_literal_new (const char *data, LSUP_Term *datatype);
- LSUP_Term *LSUP_lt_literal_new (const char *data, char *lang);
- LSUP_Term *LSUP_bnode_new (const char *data);
- bool LSUP_term_equals (const LSUP_Term *term1, const LSUP_Term *term2);
- LSUP_Term *LSUP_triple_pos (const LSUP_Triple *trp, LSUP_TriplePos n);
- LSUP_Key LSUP_triple_hash (const LSUP_Triple *trp);
|