diff options
-rw-r--r-- | serd/serd.h | 24 | ||||
-rw-r--r-- | src/env.c | 10 | ||||
-rw-r--r-- | src/node.c | 90 | ||||
-rw-r--r-- | src/serd_internal.h | 6 | ||||
-rw-r--r-- | src/serdi.c | 58 | ||||
-rw-r--r-- | wscript | 1 |
6 files changed, 115 insertions, 74 deletions
diff --git a/serd/serd.h b/serd/serd.h index b63f442f..182f263a 100644 --- a/serd/serd.h +++ b/serd/serd.h @@ -178,6 +178,30 @@ typedef struct { static const SerdNode SERD_NODE_NULL = { 0, 0, 0, 0 }; +/** Make a deep copy of @a node. + * @return a node that the caller must free with @ref serd_node_free. + */ +SERD_API +SerdNode +serd_node_copy(const SerdNode* node); + +/** Create a new node by serialising @a uri into a new string. + * @param uri The URI to parse and serialise. + * @param out (Output) set to the parsing of the new URI (i.e. points only to + * memory owned by the new returned node). + */ +SERD_API +SerdNode +serd_node_new_uri(const SerdURI* uri, SerdURI* out); + +/** Free any data owned by @a node. + * Note that if @a node is itself dynamically allocated (which is not the case + * for nodes created internally by serd), it will not be freed. + */ +SERD_API +void +serd_node_free(SerdNode* node); + /** @} */ /** @name SerdEnv @@ -32,16 +32,6 @@ struct SerdEnvImpl { size_t n_prefixes; }; -static SerdNode -serd_node_copy(const SerdNode* node) -{ - SerdNode copy = *node; - uint8_t* buf = malloc(copy.n_bytes); - memcpy(buf, node->buf, copy.n_bytes); - copy.buf = buf; - return copy; -} - SERD_API SerdEnv serd_env_new() diff --git a/src/node.c b/src/node.c new file mode 100644 index 00000000..01904375 --- /dev/null +++ b/src/node.c @@ -0,0 +1,90 @@ +/* Serd, a lightweight RDF syntax library. + * Copyright 2011 David Robillard <d@drobilla.net> + * + * Serd is free software: you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Serd is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <stdlib.h> +#include <string.h> + +#include "serd_internal.h" + +SERD_API +SerdNode +serd_node_copy(const SerdNode* node) +{ + SerdNode copy = *node; + uint8_t* buf = malloc(copy.n_bytes); + memcpy(buf, node->buf, copy.n_bytes); + copy.buf = buf; + return copy; +} + +static size_t +serd_uri_string_length(const SerdURI* uri) +{ + size_t len = uri->path_base.len; + +#define ADD_LEN(field, n_delims) \ + if ((field).len) { len += (field).len + (n_delims); } + + ADD_LEN(uri->path, 1); // + possible leading `/' + ADD_LEN(uri->scheme, 1); // + trailing `:' + ADD_LEN(uri->authority, 2); // + leading `//' + ADD_LEN(uri->query, 1); // + leading `?' + ADD_LEN(uri->fragment, 1); // + leading `#' + + return len; +} + +static size_t +string_sink(const void* buf, size_t len, void* stream) +{ + uint8_t** ptr = (uint8_t**)stream; + memcpy(*ptr, buf, len); + *ptr += len; + return len; +} + +SERD_API +SerdNode +serd_node_new_uri(const SerdURI* uri, SerdURI* out) +{ + const size_t len = serd_uri_string_length(uri); + uint8_t* buf = malloc(len + 1); + + SerdNode node = { SERD_URI, len + 1, len, buf }; // FIXME: UTF-8 + + uint8_t* ptr = buf; + const size_t actual_len = serd_uri_serialise(uri, string_sink, &ptr); + + buf[actual_len] = '\0'; + node.n_bytes = actual_len + 1; + node.n_chars = actual_len; + + // FIXME: double parse + if (!serd_uri_parse(buf, out)) { + fprintf(stderr, "error parsing URI\n"); + return SERD_NODE_NULL; + } + + return node; +} + +SERD_API +void +serd_node_free(SerdNode* node) +{ + free((uint8_t*)node->buf); // FIXME: ick, const cast +} diff --git a/src/serd_internal.h b/src/serd_internal.h index 5b2dac3e..a82565c6 100644 --- a/src/serd_internal.h +++ b/src/serd_internal.h @@ -121,10 +121,4 @@ serd_strlen(const uint8_t* utf8, size_t* out_n_bytes) return n_chars; } -static inline void -serd_node_free(SerdNode* node) -{ - free((uint8_t*)node->buf); // FIXME: ick, const cast -} - #endif // SERD_INTERNAL_H diff --git a/src/serdi.c b/src/serdi.c index 27a4d9ed..496531b5 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -29,64 +29,6 @@ typedef struct { SerdURI base_uri; } State; -static size_t -serd_uri_string_length(const SerdURI* uri) -{ - size_t len = uri->path_base.len; - -#define ADD_LEN(field, n_delims) \ - if ((field).len) { len += (field).len + (n_delims); } - - ADD_LEN(uri->path, 1); // + possible leading `/' - ADD_LEN(uri->scheme, 1); // + trailing `:' - ADD_LEN(uri->authority, 2); // + leading `//' - ADD_LEN(uri->query, 1); // + leading `?' - ADD_LEN(uri->fragment, 1); // + leading `#' - - return len; -} - -static size_t -string_sink(const void* buf, size_t len, void* stream) -{ - uint8_t** ptr = (uint8_t**)stream; - memcpy(*ptr, buf, len); - *ptr += len; - return len; -} - -// FIXME: doesn't belong here -static SerdNode -serd_node_new_uri(const SerdURI* uri, SerdURI* out) -{ - const size_t len = serd_uri_string_length(uri); - uint8_t* buf = malloc(len + 1); - - SerdNode node = { SERD_URI, len + 1, len, buf }; // FIXME: UTF-8 - - uint8_t* ptr = buf; - const size_t actual_len = serd_uri_serialise(uri, string_sink, &ptr); - - buf[actual_len] = '\0'; - node.n_bytes = actual_len + 1; - node.n_chars = actual_len; - - // FIXME: double parse - if (!serd_uri_parse(buf, out)) { - fprintf(stderr, "error parsing URI\n"); - return SERD_NODE_NULL; - } - - return node; -} - -// FIXME: doesn't belong here -static void -serd_node_free(SerdNode* node) -{ - free((uint8_t*)node->buf); // FIXME: ick, const cast -} - static uint8_t* copy_string(const uint8_t* str, size_t* n_bytes) { @@ -56,6 +56,7 @@ def build(bld): lib_source = ''' src/env.c + src/node.c src/reader.c src/uri.c src/writer.c |