From 77eebec72c0507309ce89f8cdaceff4adfd147cf Mon Sep 17 00:00:00 2001 From: David Robillard Date: Wed, 12 Jan 2022 16:18:17 -0500 Subject: Add serd_nodes_file_uri() --- include/serd/serd.h | 12 ++++++++++++ src/nodes.c | 26 ++++++++++++++++++++++++++ test/test_uri.c | 10 ++++++---- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/include/serd/serd.h b/include/serd/serd.h index 7a280b64..52245077 100644 --- a/include/serd/serd.h +++ b/include/serd/serd.h @@ -1459,6 +1459,18 @@ SERD_API const SerdNode* SERD_ALLOCATED serd_nodes_parsed_uri(SerdNodes* SERD_NONNULL nodes, SerdURIView uri); +/** + Make a file URI node from a path and optional hostname. + + A new node will be constructed with serd_node_construct_file_uri() if an + equivalent one is not already in the set. +*/ +SERD_API +const SerdNode* SERD_ALLOCATED +serd_nodes_file_uri(SerdNodes* SERD_NONNULL nodes, + SerdStringView path, + SerdStringView hostname); + /** Make a literal node with optional datatype or language. diff --git a/src/nodes.c b/src/nodes.c index d488fc71..b989259d 100644 --- a/src/nodes.c +++ b/src/nodes.c @@ -503,6 +503,32 @@ serd_nodes_parsed_uri(SerdNodes* const nodes, const SerdURIView uri) return serd_nodes_manage_entry(nodes, entry); } +const SerdNode* +serd_nodes_file_uri(SerdNodes* const nodes, + const SerdStringView path, + const SerdStringView hostname) +{ + assert(nodes); + + /* Computing a hash for the serialised URI here would be quite complex, so, + since this isn't expected to be a particularly hot case, we just allocate + a new entry and try to do a normal insertion. */ + + // Determine how much space the node needs + SerdWriteResult r = serd_node_construct_file_uri(0u, NULL, path, hostname); + assert(r.status == SERD_OVERFLOW); // Currently no other errors + + // Allocate a new entry to write the URI node into + NodesEntry* const entry = new_entry(nodes->allocator, r.count); + if (entry) { + r = serd_node_construct_file_uri(r.count, &entry->node, path, hostname); + assert(!r.status); + (void)r; + } + + return serd_nodes_manage_entry(nodes, entry); +} + const SerdNode* serd_nodes_blank(SerdNodes* const nodes, const SerdStringView string) { diff --git a/test/test_uri.c b/test/test_uri.c index 2b0c9b16..ec102d6f 100644 --- a/test/test_uri.c +++ b/test/test_uri.c @@ -85,12 +85,14 @@ test_file_uri(const char* const hostname, expected_path = path; } - SerdNode* node = - serd_new_file_uri(NULL, SERD_STRING(path), SERD_OPTIONAL_STRING(hostname)); + SerdNodes* const nodes = serd_nodes_new(NULL); + + const SerdNode* node = serd_nodes_file_uri( + nodes, SERD_STRING(path), SERD_OPTIONAL_STRING(hostname)); const char* node_str = serd_node_string(node); char* out_hostname = NULL; - char* out_path = serd_parse_file_uri(NULL, node_str, &out_hostname); + char* const out_path = serd_parse_file_uri(NULL, node_str, &out_hostname); assert(!strcmp(node_str, expected_uri)); assert((hostname && out_hostname) || (!hostname && !out_hostname)); @@ -99,7 +101,7 @@ test_file_uri(const char* const hostname, serd_free(NULL, out_path); serd_free(NULL, out_hostname); - serd_node_free(NULL, node); + serd_nodes_free(nodes); } static void -- cgit v1.2.1