|
@@ -9,8 +9,6 @@ typedef struct ns_entry_t {
|
|
|
char * ns; // Fully qualified NS.
|
|
|
} NSEntry;
|
|
|
|
|
|
-typedef struct hashmap NSMap;
|
|
|
-
|
|
|
/** @brief Iterator for dumping NS map.
|
|
|
*/
|
|
|
struct dump_iter_t {
|
|
@@ -58,7 +56,7 @@ static bool nsmap_dump_ns_iter_fn (const void *item, void *udata)
|
|
|
* API.
|
|
|
*/
|
|
|
|
|
|
-NSMap *
|
|
|
+LSUP_NSMap *
|
|
|
LSUP_nsmap_new (void)
|
|
|
{
|
|
|
return hashmap_new (
|
|
@@ -68,12 +66,12 @@ LSUP_nsmap_new (void)
|
|
|
|
|
|
|
|
|
void
|
|
|
-LSUP_nsmap_free (NSMap *map)
|
|
|
+LSUP_nsmap_free (LSUP_NSMap *map)
|
|
|
{ hashmap_free (map); }
|
|
|
|
|
|
|
|
|
LSUP_rc
|
|
|
-LSUP_nsmap_add (NSMap *map, const char *pfx, const char *nsstr)
|
|
|
+LSUP_nsmap_add (LSUP_NSMap *map, const char *pfx, const char *nsstr)
|
|
|
{
|
|
|
NSEntry entry_s = {};
|
|
|
|
|
@@ -82,33 +80,30 @@ LSUP_nsmap_add (NSMap *map, const char *pfx, const char *nsstr)
|
|
|
"Prefix `%s` is longer than the maximum allowed size "
|
|
|
"(%d characters). Truncating.", pfx, PFX_LEN - 1);
|
|
|
strncpy (entry_s.pfx, pfx, PFX_LEN -1);
|
|
|
- char *ns = strdup (nsstr);
|
|
|
-
|
|
|
- NSEntry *ret = hashmap_get (map, &entry_s);
|
|
|
- if (!ret) {
|
|
|
- entry_s.ns = ns;
|
|
|
- hashmap_set (map, &entry_s);
|
|
|
- if (hashmap_oom (map)) return LSUP_MEM_ERR;
|
|
|
- LOG_DEBUG("Added prefix '%s' to NS map @%p.", entry_s.pfx, map);
|
|
|
- } else {
|
|
|
+ entry_s.ns = strdup (nsstr);
|
|
|
+
|
|
|
+ const NSEntry *ret = hashmap_delete (map, &entry_s);
|
|
|
+ if (!ret) LOG_DEBUG("Adding prefix '%s' to NS map @%p.", entry_s.pfx, map);
|
|
|
+ else {
|
|
|
LOG_DEBUG(
|
|
|
"Replacing NS '%s' with '%s' for prefix '%s'.",
|
|
|
- ret->ns, ns, entry_s.pfx);
|
|
|
+ ret->ns, entry_s.ns, entry_s.pfx);
|
|
|
+ // Free replaced NS string.
|
|
|
free (ret->ns);
|
|
|
- ret->ns = ns;
|
|
|
}
|
|
|
- // Free replaced NS string.
|
|
|
+ hashmap_set (map, &entry_s);
|
|
|
+ if (hashmap_oom (map)) return LSUP_MEM_ERR;
|
|
|
|
|
|
return LSUP_OK;
|
|
|
}
|
|
|
|
|
|
|
|
|
LSUP_rc
|
|
|
-LSUP_nsmap_remove (NSMap *map, const char *pfx)
|
|
|
+LSUP_nsmap_remove (LSUP_NSMap *map, const char *pfx)
|
|
|
{
|
|
|
NSEntry entry_s = {};
|
|
|
strncpy (entry_s.pfx, pfx, PFX_LEN - 1);
|
|
|
- NSEntry *entry = hashmap_delete (map, &entry_s);
|
|
|
+ const NSEntry *entry = hashmap_delete (map, &entry_s);
|
|
|
|
|
|
if (!entry) return LSUP_NOACTION;
|
|
|
|
|
@@ -119,22 +114,22 @@ LSUP_nsmap_remove (NSMap *map, const char *pfx)
|
|
|
|
|
|
|
|
|
const char *
|
|
|
-LSUP_nsmap_get_ns (const NSMap *map, const char *pfx)
|
|
|
+LSUP_nsmap_get_ns (const LSUP_NSMap *map, const char *pfx)
|
|
|
{
|
|
|
NSEntry entry_s = {};
|
|
|
strncpy (entry_s.pfx, pfx, PFX_LEN - 1);
|
|
|
- NSEntry *entry = hashmap_get ((NSMap *)map, &entry_s);
|
|
|
+ const NSEntry *entry = hashmap_get ((LSUP_NSMap *)map, &entry_s);
|
|
|
|
|
|
return (entry) ? entry->ns : NULL;
|
|
|
}
|
|
|
|
|
|
|
|
|
const char *
|
|
|
-LSUP_nsmap_get_pfx (const NSMap *map, const char *ns)
|
|
|
+LSUP_nsmap_get_pfx (const LSUP_NSMap *map, const char *ns)
|
|
|
{
|
|
|
const NSEntry *entry;
|
|
|
size_t i = 0;
|
|
|
- while (hashmap_iter ((NSMap *)map, &i, (void **) &entry)) {
|
|
|
+ while (hashmap_iter ((LSUP_NSMap *)map, &i, (void **) &entry)) {
|
|
|
if (strncmp (entry->ns, ns, strlen (ns)) == 0)
|
|
|
return entry->pfx;
|
|
|
}
|
|
@@ -145,7 +140,7 @@ LSUP_nsmap_get_pfx (const NSMap *map, const char *ns)
|
|
|
|
|
|
LSUP_rc
|
|
|
LSUP_nsmap_normalize_uri (
|
|
|
- const NSMap *map, const char *pfx_uri, char **fq_uri_p)
|
|
|
+ const LSUP_NSMap *map, const char *pfx_uri, char **fq_uri_p)
|
|
|
{
|
|
|
LSUP_rc rc;
|
|
|
char *fq_uri = NULL;
|
|
@@ -162,7 +157,7 @@ LSUP_nsmap_normalize_uri (
|
|
|
strncpy (pfx, pfx_uri, pfx_len);
|
|
|
pfx[pfx_len] = '\0';
|
|
|
|
|
|
- const char *ns = LSUP_nsmap_get_ns ((NSMap *)map, pfx);
|
|
|
+ const char *ns = LSUP_nsmap_get_ns ((LSUP_NSMap *)map, pfx);
|
|
|
|
|
|
if (ns) {
|
|
|
// -1 for :, +1 for terminator.
|
|
@@ -192,7 +187,7 @@ no_prefix:
|
|
|
|
|
|
LSUP_rc
|
|
|
LSUP_nsmap_denormalize_uri (
|
|
|
- const NSMap *map, const char *fq_uri, char **pfx_uri_p)
|
|
|
+ const LSUP_NSMap *map, const char *fq_uri, char **pfx_uri_p)
|
|
|
{
|
|
|
/*
|
|
|
* This is different from LSUP_nsmap_get_ns, in that the URI being looked
|
|
@@ -205,7 +200,7 @@ LSUP_nsmap_denormalize_uri (
|
|
|
const char *pfx = NULL;
|
|
|
size_t i = 0, offset;
|
|
|
|
|
|
- while (hashmap_iter ((NSMap *)map, &i, (void **) &entry)) {
|
|
|
+ while (hashmap_iter ((LSUP_NSMap *)map, &i, (void **) &entry)) {
|
|
|
offset = strlen (entry->ns);
|
|
|
if (strncmp (entry->ns, fq_uri, offset) == 0) {
|
|
|
pfx = entry->pfx;
|
|
@@ -235,9 +230,9 @@ LSUP_nsmap_denormalize_uri (
|
|
|
|
|
|
|
|
|
const char ***
|
|
|
-LSUP_nsmap_dump (const NSMap *map)
|
|
|
+LSUP_nsmap_dump (const LSUP_NSMap *map)
|
|
|
{
|
|
|
- size_t i = hashmap_count ((NSMap *) map);
|
|
|
+ size_t i = hashmap_count ((LSUP_NSMap *) map);
|
|
|
|
|
|
const char ***data = malloc (2 * (i + 1) * sizeof (char *));
|
|
|
if (UNLIKELY (!data)) return NULL;
|
|
@@ -248,7 +243,7 @@ LSUP_nsmap_dump (const NSMap *map)
|
|
|
}
|
|
|
|
|
|
struct dump_iter_t cur = {.i = 0, .data = data};
|
|
|
- hashmap_scan ((NSMap *) map, nsmap_dump_ns_iter_fn, &cur);
|
|
|
+ hashmap_scan ((LSUP_NSMap *) map, nsmap_dump_ns_iter_fn, &cur);
|
|
|
data[i] = NULL; // Sentinel
|
|
|
|
|
|
return data;
|