diff options
author | David Robillard <d@drobilla.net> | 2023-03-29 07:28:19 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2023-12-02 16:27:02 -0500 |
commit | fff826f406e0b9975fd8672041e50dd1a342339f (patch) | |
tree | 03a22b89b83a8b26fdf18dd8700e51833b488d81 /src/node.c | |
parent | 94d3433dcf0e77d2c867c9a2dd6928acfea4184c (diff) | |
download | serd-fff826f406e0b9975fd8672041e50dd1a342339f.tar.gz serd-fff826f406e0b9975fd8672041e50dd1a342339f.tar.bz2 serd-fff826f406e0b9975fd8672041e50dd1a342339f.zip |
Simplify URI API and implementation
Diffstat (limited to 'src/node.c')
-rw-r--r-- | src/node.c | 122 |
1 files changed, 48 insertions, 74 deletions
@@ -37,7 +37,7 @@ static const size_t serd_node_align = 2 * sizeof(uint64_t); static size_t serd_uri_string_length(const SerdURIView* const uri) { - size_t len = uri->path_base.length; + size_t len = uri->path_prefix.length; #define ADD_LEN(field, n_delims) \ if ((field).length) { \ @@ -152,29 +152,56 @@ serd_node_equals(const SerdNode* const a, const SerdNode* const b) } SerdNode* -serd_new_uri_from_node(const SerdNode* const uri_node, - const SerdURIView* const base, - SerdURIView* const out) +serd_new_uri(const char* const str) { - const char* uri_str = serd_node_string(uri_node); - return (uri_node && uri_node->type == SERD_URI && uri_str) - ? serd_new_uri_from_string(uri_str, base, out) - : NULL; + const size_t length = strlen(str); + SerdNode* node = serd_node_malloc(length, 0, SERD_URI); + memcpy(serd_node_buffer(node), str, length); + node->length = length; + return node; } SerdNode* -serd_new_uri_from_string(const char* const str, - const SerdURIView* const base, - SerdURIView* const out) +serd_new_parsed_uri(const SerdURIView uri) +{ + const size_t len = serd_uri_string_length(&uri); + SerdNode* const node = serd_node_malloc(len, 0, SERD_URI); + char* ptr = serd_node_buffer(node); + const size_t actual_len = serd_write_uri(uri, string_sink, &ptr); + + serd_node_buffer(node)[actual_len] = '\0'; + node->length = actual_len; + + return node; +} + +static SerdNode* +serd_new_from_uri(const SerdURIView uri, const SerdURIView base) { - if (!str || str[0] == '\0') { - // Empty URI => Base URI, or nothing if no base is given - return base ? serd_new_uri(base, NULL, out) : NULL; + const SerdURIView abs_uri = serd_resolve_uri(uri, base); + const size_t len = serd_uri_string_length(&abs_uri); + SerdNode* node = serd_node_malloc(len, 0, SERD_URI); + char* ptr = serd_node_buffer(node); + const size_t actual_len = serd_write_uri(abs_uri, string_sink, &ptr); + + serd_node_buffer(node)[actual_len] = '\0'; + node->length = actual_len; + + return node; +} + +SerdNode* +serd_new_resolved_uri(const SerdStringView string, const SerdURIView base) +{ + const SerdURIView uri = serd_parse_uri(string.data); + SerdNode* const result = serd_new_from_uri(uri, base); + + if (!serd_uri_string_has_scheme(serd_node_string(result))) { + serd_node_free(result); + return NULL; } - SerdURIView uri; - serd_uri_parse(str, &uri); - return serd_new_uri(&uri, base, out); // Resolve/Serialise + return result; } static bool @@ -270,61 +297,13 @@ serd_new_file_uri(const char* const path, const char* const string = serd_buffer_sink_finish(&buffer); SerdNode* const node = serd_new_substring(SERD_URI, string, length); if (out) { - serd_uri_parse(serd_node_buffer(node), out); + *out = serd_parse_uri(serd_node_buffer(node)); } free(buffer.buf); return node; } -SerdNode* -serd_new_uri(const SerdURIView* const uri, - const SerdURIView* const base, - SerdURIView* const out) -{ - SerdURIView abs_uri = *uri; - if (base) { - serd_uri_resolve(uri, base, &abs_uri); - } - - const size_t len = serd_uri_string_length(&abs_uri); - SerdNode* node = serd_node_malloc(len, 0, SERD_URI); - char* ptr = serd_node_buffer(node); - const size_t actual_len = serd_uri_serialise(&abs_uri, string_sink, &ptr); - - serd_node_buffer(node)[actual_len] = '\0'; - node->length = actual_len; - - if (out) { - serd_uri_parse(serd_node_buffer(node), out); // TODO: avoid double parse - } - - return node; -} - -SerdNode* -serd_new_relative_uri(const SerdURIView* const uri, - const SerdURIView* const base, - const SerdURIView* const root, - SerdURIView* const out) -{ - const size_t uri_len = serd_uri_string_length(uri); - const size_t base_len = serd_uri_string_length(base); - SerdNode* node = serd_node_malloc(uri_len + base_len, 0, SERD_URI); - char* ptr = serd_node_buffer(node); - const size_t actual_len = - serd_uri_serialise_relative(uri, base, root, string_sink, &ptr); - - serd_node_buffer(node)[actual_len] = '\0'; - node->length = actual_len; - - if (out) { - serd_uri_parse(serd_node_buffer(node), out); // TODO: avoid double parse - } - - return node; -} - static unsigned serd_digits(const double abs) { @@ -458,16 +437,11 @@ serd_node_string_view(const SerdNode* const node) return r; } -SerdURIView +SERD_PURE_FUNC SerdURIView serd_node_uri_view(const SerdNode* const node) { - SerdURIView result = SERD_URI_NULL; - - if (node->type == SERD_URI) { - serd_uri_parse(serd_node_string(node), &result); - } - - return result; + return (node->type == SERD_URI) ? serd_parse_uri(serd_node_string(node)) + : SERD_URI_NULL; } SerdNodeFlags |