aboutsummaryrefslogtreecommitdiffstats
path: root/src/node.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-05-12 18:03:13 +0200
committerDavid Robillard <d@drobilla.net>2018-12-30 17:58:17 -0500
commit2b0e3de607f2b0eca00b3986ecbcd9fe26ecf95d (patch)
treee2574e8e816bcef8fae80538123f3a3b386a30eb /src/node.c
parentf539637ea4dc40a8a2552f15092ea3abd69d5343 (diff)
downloadserd-2b0e3de607f2b0eca00b3986ecbcd9fe26ecf95d.tar.gz
serd-2b0e3de607f2b0eca00b3986ecbcd9fe26ecf95d.tar.bz2
serd-2b0e3de607f2b0eca00b3986ecbcd9fe26ecf95d.zip
Set datatypes on integer, decimal, and base64 nodes
Diffstat (limited to 'src/node.c')
-rw-r--r--src/node.c91
1 files changed, 65 insertions, 26 deletions
diff --git a/src/node.c b/src/node.c
index f8f3dfba..81d492ea 100644
--- a/src/node.c
+++ b/src/node.c
@@ -40,6 +40,19 @@
static const size_t serd_node_align = sizeof(SerdNode);
+typedef struct StaticNode {
+ SerdNode node;
+ char buf[sizeof(NS_XSD) + sizeof("base64Binary") + 1];
+} StaticNode;
+
+#define DEFINE_XSD_NODE(name) \
+ static const StaticNode serd_xsd_ ## name = { \
+ { sizeof(NS_XSD #name) - 1, 0, SERD_URI }, NS_XSD #name };
+
+DEFINE_XSD_NODE(decimal)
+DEFINE_XSD_NODE(integer)
+DEFINE_XSD_NODE(base64Binary)
+
static SerdNode*
serd_node_new_from_uri(const SerdURI* uri, const SerdURI* base);
@@ -47,7 +60,21 @@ static size_t
serd_node_pad_size(const size_t n_bytes)
{
const size_t pad = serd_node_align - (n_bytes + 2) % serd_node_align;
- return n_bytes + 2 + pad;
+ const size_t size = n_bytes + 2 + pad;
+ assert(size % serd_node_align == 0);
+ return size;
+}
+
+static const SerdNode*
+serd_node_get_meta_c(const SerdNode* node)
+{
+ return node + 1 + (serd_node_pad_size(node->n_bytes) / serd_node_align);
+}
+
+static SerdNode*
+serd_node_get_meta(SerdNode* node)
+{
+ return node + 1 + (serd_node_pad_size(node->n_bytes) / serd_node_align);
}
static size_t
@@ -391,21 +418,26 @@ serd_digits(double abs)
}
SerdNode*
-serd_node_new_decimal(double d, unsigned frac_digits)
+serd_node_new_decimal(double d, unsigned frac_digits, const SerdNode* datatype)
{
if (isnan(d) || isinf(d)) {
return NULL;
}
+ const SerdNode* type = datatype ? datatype : &serd_xsd_decimal.node;
const double abs_d = fabs(d);
const unsigned int_digits = serd_digits(abs_d);
const size_t len = int_digits + frac_digits + 3;
- SerdNode* const node = serd_node_malloc(len, 0, SERD_LITERAL);
- char* const buf = serd_node_buffer(node);
- const double int_part = floor(abs_d);
+ const size_t type_len = serd_node_total_size(type);
+ const size_t total_len = len + type_len;
+
+ SerdNode* const node =
+ serd_node_malloc(total_len, SERD_HAS_DATATYPE, SERD_LITERAL);
// Point s to decimal point location
- char* s = buf + int_digits;
+ char* const buf = serd_node_buffer(node);
+ const double int_part = floor(abs_d);
+ char* s = buf + int_digits;
if (d < 0.0) {
*buf = '-';
++s;
@@ -443,19 +475,25 @@ serd_node_new_decimal(double d, unsigned frac_digits)
}
}
+ memcpy(serd_node_get_meta(node), type, type_len);
return node;
}
SerdNode*
-serd_node_new_integer(int64_t i)
+serd_node_new_integer(int64_t i, const SerdNode* datatype)
{
- int64_t abs_i = (i < 0) ? -i : i;
- const unsigned digits = serd_digits(abs_i);
- SerdNode* node = serd_node_malloc(digits + 2, 0, SERD_LITERAL);
- char* buf = serd_node_buffer(node);
+ const SerdNode* type = datatype ? datatype : &serd_xsd_integer.node;
+ int64_t abs_i = (i < 0) ? -i : i;
+ const unsigned digits = serd_digits(abs_i);
+ const size_t type_len = serd_node_total_size(type);
+ const size_t total_len = digits + 2 + type_len;
+
+ SerdNode* node =
+ serd_node_malloc(total_len, SERD_HAS_DATATYPE, SERD_LITERAL);
// Point s to the end
- char* s = buf + digits - 1;
+ char* buf = serd_node_buffer(node);
+ char* s = buf + digits - 1;
if (i < 0) {
*buf = '-';
++s;
@@ -468,24 +506,34 @@ serd_node_new_integer(int64_t i)
*s-- = '0' + (abs_i % 10);
} while ((abs_i /= 10) > 0);
+ memcpy(serd_node_get_meta(node), type, type_len);
return node;
}
SerdNode*
-serd_node_new_blob(const void* buf, size_t size, bool wrap_lines)
+serd_node_new_blob(const void* buf,
+ size_t size,
+ bool wrap_lines,
+ const SerdNode* datatype)
{
if (!buf || !size) {
return NULL;
}
- const size_t len = serd_base64_get_length(size, wrap_lines);
- SerdNode* const node = serd_node_malloc(len + 1, 0, SERD_LITERAL);
+ const SerdNode* type = datatype ? datatype : &serd_xsd_base64Binary.node;
+ const size_t len = serd_base64_get_length(size, wrap_lines);
+ const size_t type_len = serd_node_total_size(type);
+ const size_t total_len = len + 1 + type_len;
+
+ SerdNode* const node =
+ serd_node_malloc(total_len, SERD_HAS_DATATYPE, SERD_LITERAL);
if (serd_base64_encode(serd_node_buffer(node), buf, size, wrap_lines)) {
node->flags |= SERD_HAS_NEWLINE;
}
node->n_bytes = len;
+ memcpy(serd_node_get_meta(node), type, type_len);
return node;
}
@@ -507,15 +555,6 @@ serd_node_get_length(const SerdNode* node)
return node ? node->n_bytes : 0;
}
-static const SerdNode*
-serd_node_get_meta_node(const SerdNode* node)
-{
- const size_t len = serd_node_pad_size(node->n_bytes);
- assert((intptr_t)node % serd_node_align == 0);
- assert(len % serd_node_align == 0);
- return node + 1 + (len / serd_node_align);
-}
-
const SerdNode*
serd_node_get_datatype(const SerdNode* node)
{
@@ -523,7 +562,7 @@ serd_node_get_datatype(const SerdNode* node)
return NULL;
}
- const SerdNode* const datatype = serd_node_get_meta_node(node);
+ const SerdNode* const datatype = serd_node_get_meta_c(node);
assert(datatype->type == SERD_URI || datatype->type == SERD_CURIE);
return datatype;
}
@@ -535,7 +574,7 @@ serd_node_get_language(const SerdNode* node)
return NULL;
}
- const SerdNode* const lang = serd_node_get_meta_node(node);
+ const SerdNode* const lang = serd_node_get_meta_c(node);
assert(lang->type == SERD_LITERAL);
return lang;
}