aboutsummaryrefslogtreecommitdiffstats
path: root/src/node.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/node.c')
-rw-r--r--src/node.c137
1 files changed, 75 insertions, 62 deletions
diff --git a/src/node.c b/src/node.c
index 73cba587..4b8cdff4 100644
--- a/src/node.c
+++ b/src/node.c
@@ -54,7 +54,7 @@ DEFINE_XSD_NODE(integer)
DEFINE_XSD_NODE(base64Binary)
static SerdNode*
-serd_node_new_from_uri(const SerdURI* uri, const SerdURI* base);
+serd_new_from_uri(const SerdURI* uri, const SerdURI* base);
static size_t
serd_node_pad_size(const size_t n_bytes)
@@ -138,7 +138,7 @@ serd_node_set(SerdNode** dst, const SerdNode* src)
}
static SerdNode*
-serd_node_new_simple(SerdType type, const char* str)
+serd_new_simple(SerdType type, const char* str)
{
if (!str) {
return NULL;
@@ -153,7 +153,7 @@ serd_node_new_simple(SerdType type, const char* str)
}
SerdNode*
-serd_node_new_string(const char* str)
+serd_new_string(const char* str)
{
if (!str) {
return NULL;
@@ -169,15 +169,47 @@ serd_node_new_string(const char* str)
}
SerdNode*
-serd_node_new_literal(const char* str,
- const SerdNode* datatype,
- const char* lang)
+serd_new_plain_literal(const char* str, const char* lang)
+{
+ if (!str) {
+ return NULL;
+ } else if (!lang) {
+ return serd_new_string(str);
+ }
+
+ uint32_t flags = 0;
+ const size_t n_bytes = serd_strlen(str, &flags);
+ const size_t len = serd_node_pad_size(n_bytes);
+
+ SerdNode* node = NULL;
+ const size_t lang_len = strlen(lang);
+ const size_t total_len = len + sizeof(SerdNode) + lang_len;
+ flags |= SERD_HAS_LANGUAGE;
+ node = serd_node_malloc(total_len, flags, SERD_LITERAL);
+ memcpy(serd_node_buffer(node), str, n_bytes);
+ node->n_bytes = n_bytes;
+
+ SerdNode* lang_node = node + 1 + (len / serd_node_align);
+ lang_node->type = SERD_LITERAL;
+ lang_node->n_bytes = lang_len;
+ memcpy(serd_node_buffer(lang_node), lang, lang_len);
+ serd_node_check_padding(lang_node);
+
+ serd_node_check_padding(node);
+ return node;
+}
+
+SerdNode*
+serd_new_typed_literal(const char* str, const SerdNode* datatype)
{
serd_node_check_padding(datatype);
- if (!str || (lang && datatype &&
- strcmp(serd_node_buffer_c(datatype), NS_RDF "#langString")) ||
- (datatype && serd_node_get_type(datatype) != SERD_URI)) {
+ if (!str) {
+ return NULL;
+ } else if (!datatype) {
+ return serd_new_string(str);
+ } else if (!strcmp(serd_node_buffer_c(datatype), NS_RDF "#langString") ||
+ serd_node_get_type(datatype) != SERD_URI) {
return NULL;
}
@@ -186,56 +218,37 @@ serd_node_new_literal(const char* str,
const size_t len = serd_node_pad_size(n_bytes);
SerdNode* node = NULL;
- if (lang) {
- flags |= SERD_HAS_LANGUAGE;
- const size_t lang_len = strlen(lang);
- const size_t total_len = len + sizeof(SerdNode) + lang_len;
- node = serd_node_malloc(total_len, flags, SERD_LITERAL);
- memcpy(serd_node_buffer(node), str, n_bytes);
- node->n_bytes = n_bytes;
-
- SerdNode* lang_node = node + 1 + (len / serd_node_align);
- lang_node->type = SERD_LITERAL;
- lang_node->n_bytes = lang_len;
- memcpy(serd_node_buffer(lang_node), lang, lang_len);
- serd_node_check_padding(lang_node);
- } else if (datatype) {
- flags |= SERD_HAS_DATATYPE;
- const size_t datatype_len = strlen(serd_node_buffer_c(datatype));
- const size_t total_len = len + sizeof(SerdNode) + datatype_len;
- node = serd_node_malloc(total_len, flags, SERD_LITERAL);
- memcpy(serd_node_buffer(node), str, n_bytes);
- node->n_bytes = n_bytes;
-
- SerdNode* datatype_node = node + 1 + (len / serd_node_align);
- memcpy(datatype_node, datatype, sizeof(SerdNode) + datatype_len);
- serd_node_check_padding(datatype_node);
- } else {
- node = serd_node_malloc(n_bytes, flags, SERD_LITERAL);
- memcpy(serd_node_buffer(node), str, n_bytes);
- node->n_bytes = n_bytes;
- }
+ const size_t datatype_len = strlen(serd_node_buffer_c(datatype));
+ const size_t total_len = len + sizeof(SerdNode) + datatype_len;
+ flags |= SERD_HAS_DATATYPE;
+ node = serd_node_malloc(total_len, flags, SERD_LITERAL);
+ memcpy(serd_node_buffer(node), str, n_bytes);
+ node->n_bytes = n_bytes;
+
+ SerdNode* datatype_node = node + 1 + (len / serd_node_align);
+ memcpy(datatype_node, datatype, sizeof(SerdNode) + datatype_len);
+ serd_node_check_padding(datatype_node);
serd_node_check_padding(node);
return node;
}
SerdNode*
-serd_node_new_blank(const char* str)
+serd_new_blank(const char* str)
{
- return serd_node_new_simple(SERD_BLANK, str);
+ return serd_new_simple(SERD_BLANK, str);
}
SerdNode*
-serd_node_new_curie(const char* str)
+serd_new_curie(const char* str)
{
- return serd_node_new_simple(SERD_CURIE, str);
+ return serd_new_simple(SERD_CURIE, str);
}
SerdNode*
-serd_node_new_uri(const char* str)
+serd_new_uri(const char* str)
{
- return serd_node_new_simple(SERD_URI, str);
+ return serd_new_simple(SERD_URI, str);
}
/**
@@ -317,7 +330,7 @@ string_sink(const void* buf, size_t size, size_t nmemb, void* stream)
}
SerdNode*
-serd_node_new_resolved_uri(const char* str, const SerdNode* base)
+serd_new_resolved_uri(const char* str, const SerdNode* base)
{
if (!base || base->type != SERD_URI) {
return NULL;
@@ -325,7 +338,7 @@ serd_node_new_resolved_uri(const char* str, const SerdNode* base)
SerdURI base_uri;
serd_uri_parse(serd_node_get_string(base), &base_uri);
- return serd_node_new_resolved_uri_i(str, &base_uri);
+ return serd_new_resolved_uri_i(str, &base_uri);
}
SerdNode*
@@ -340,20 +353,20 @@ serd_node_resolve(const SerdNode* node, const SerdNode* base)
serd_uri_parse(serd_node_get_string(node), &uri);
serd_uri_parse(serd_node_get_string(base), &base_uri);
- return serd_node_new_from_uri(&uri, &base_uri);
+ return serd_new_from_uri(&uri, &base_uri);
}
SerdNode*
-serd_node_new_resolved_uri_i(const char* str, const SerdURI* base)
+serd_new_resolved_uri_i(const char* str, const SerdURI* base)
{
if (!str || str[0] == '\0') {
// Empty URI => Base URI, or nothing if no base is given
- return base ? serd_node_new_from_uri(base, NULL) : NULL;
+ return base ? serd_new_from_uri(base, NULL) : NULL;
}
SerdURI uri;
serd_uri_parse(str, &uri);
- return serd_node_new_from_uri(&uri, base);
+ return serd_new_from_uri(&uri, base);
}
static inline bool
@@ -376,7 +389,7 @@ is_uri_path_char(const char c)
}
SerdNode*
-serd_node_new_file_uri(const char* path, const char* hostname, bool escape)
+serd_new_file_uri(const char* path, const char* hostname, bool escape)
{
const size_t path_len = strlen(path);
const size_t hostname_len = hostname ? strlen(hostname) : 0;
@@ -407,14 +420,14 @@ serd_node_new_file_uri(const char* path, const char* hostname, bool escape)
}
serd_buffer_sink_finish(&buffer);
- SerdNode* node = serd_node_new_uri((const char*)buffer.buf);
+ SerdNode* node = serd_new_uri((const char*)buffer.buf);
free(buffer.buf);
serd_node_check_padding(node);
return node;
}
static SerdNode*
-serd_node_new_from_uri(const SerdURI* uri, const SerdURI* base)
+serd_new_from_uri(const SerdURI* uri, const SerdURI* base)
{
SerdURI abs_uri = *uri;
if (base) {
@@ -434,9 +447,9 @@ serd_node_new_from_uri(const SerdURI* uri, const SerdURI* base)
}
SerdNode*
-serd_node_new_relative_uri(const char* str,
- const SerdNode* base,
- const SerdNode* root)
+serd_new_relative_uri(const char* str,
+ const SerdNode* base,
+ const SerdNode* root)
{
SerdURI uri = SERD_URI_NULL;
SerdURI base_uri = SERD_URI_NULL;
@@ -472,7 +485,7 @@ serd_digits(double abs)
}
SerdNode*
-serd_node_new_decimal(double d, unsigned frac_digits, const SerdNode* datatype)
+serd_new_decimal(double d, unsigned frac_digits, const SerdNode* datatype)
{
if (isnan(d) || isinf(d)) {
return NULL;
@@ -535,7 +548,7 @@ serd_node_new_decimal(double d, unsigned frac_digits, const SerdNode* datatype)
}
SerdNode*
-serd_node_new_integer(int64_t i, const SerdNode* datatype)
+serd_new_integer(int64_t i, const SerdNode* datatype)
{
const SerdNode* type = datatype ? datatype : &serd_xsd_integer.node;
int64_t abs_i = (i < 0) ? -i : i;
@@ -567,10 +580,10 @@ serd_node_new_integer(int64_t i, const SerdNode* datatype)
}
SerdNode*
-serd_node_new_blob(const void* buf,
- size_t size,
- bool wrap_lines,
- const SerdNode* datatype)
+serd_new_blob(const void* buf,
+ size_t size,
+ bool wrap_lines,
+ const SerdNode* datatype)
{
if (!buf || !size) {
return NULL;