diff options
author | David Robillard <d@drobilla.net> | 2009-02-12 03:11:33 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2009-02-12 03:11:33 +0000 |
commit | f989767f728371ad85a49092ea2d04529ede670e (patch) | |
tree | 2d67240977013e33eaa9be5f4646e5c3751cfb02 /src/query.c | |
parent | d938b926f83537be7fe7260ea0537345e25be979 (diff) | |
download | lilv-f989767f728371ad85a49092ea2d04529ede670e.tar.gz lilv-f989767f728371ad85a49092ea2d04529ede670e.tar.bz2 lilv-f989767f728371ad85a49092ea2d04529ede670e.zip |
Factor out librdf_node -> SLV2Value marhshaling.
git-svn-id: http://svn.drobilla.net/lad/trunk/slv2@1926 a436a847-0d15-0410-975c-d299462d15a1
Diffstat (limited to 'src/query.c')
-rw-r--r-- | src/query.c | 89 |
1 files changed, 46 insertions, 43 deletions
diff --git a/src/query.c b/src/query.c index c9d0d14..edd28b4 100644 --- a/src/query.c +++ b/src/query.c @@ -38,6 +38,48 @@ static const char* slv2_query_prefixes = "PREFIX lv2ev: <http://lv2plug.in/ns/ext/event#>\n"; +/** Create a new SLV2Value from a librdf_node, or return NULL if impossible */ +SLV2Value +slv2_value_from_librdf_node(SLV2World world, librdf_node* node) +{ + SLV2Value result = NULL; + + librdf_uri* datatype_uri = NULL; + SLV2ValueType type = SLV2_VALUE_STRING; + + switch (librdf_node_get_type(node)) { + case LIBRDF_NODE_TYPE_RESOURCE: + type = SLV2_VALUE_URI; + result = slv2_value_new_librdf_uri(world, librdf_node_get_uri(node)); + break; + case LIBRDF_NODE_TYPE_LITERAL: + datatype_uri = librdf_node_get_literal_value_datatype_uri(node); + if (datatype_uri) { + if (!strcmp((const char*)librdf_uri_as_string(datatype_uri), + "http://www.w3.org/2001/XMLSchema#integer")) + type = SLV2_VALUE_INT; + else if (!strcmp((const char*)librdf_uri_as_string(datatype_uri), + "http://www.w3.org/2001/XMLSchema#decimal")) + type = SLV2_VALUE_FLOAT; + else + fprintf(stderr, "Unknown datatype %s\n", librdf_uri_as_string(datatype_uri)); + } + result = slv2_value_new(world, type, (const char*)librdf_node_get_literal_value(node)); + break; + case LIBRDF_NODE_TYPE_BLANK: + type = SLV2_VALUE_STRING; + result = slv2_value_new(world, type, (const char*)librdf_node_get_blank_identifier(node)); + break; + case LIBRDF_NODE_TYPE_UNKNOWN: + default: + fprintf(stderr, "Unknown RDF node type %d\n", librdf_node_get_type(node)); + break; + } + + return result; +} + + SLV2Values slv2_query_get_variable_bindings(SLV2World world, librdf_query_results* results, @@ -49,9 +91,7 @@ slv2_query_get_variable_bindings(SLV2World world, result = slv2_values_new(); while (!librdf_query_results_finished(results)) { - - librdf_node* node = - librdf_query_results_get_binding_value(results, variable); + librdf_node* node = librdf_query_results_get_binding_value(results, variable); if (node == NULL) { fprintf(stderr, "SLV2 ERROR: Variable %d bound to NULL.\n", variable); @@ -59,48 +99,11 @@ slv2_query_get_variable_bindings(SLV2World world, continue; } - librdf_uri* datatype_uri = NULL; - SLV2ValueType type = SLV2_VALUE_STRING; - - librdf_uri* uri_val = NULL; - const char* str_val = NULL; - - switch (librdf_node_get_type(node)) { - case LIBRDF_NODE_TYPE_RESOURCE: - type = SLV2_VALUE_URI; - uri_val = librdf_node_get_uri(node); - assert(uri_val); - break; - case LIBRDF_NODE_TYPE_LITERAL: - datatype_uri = librdf_node_get_literal_value_datatype_uri(node); - if (datatype_uri) { - if (!strcmp((const char*)librdf_uri_as_string(datatype_uri), - "http://www.w3.org/2001/XMLSchema#integer")) - type = SLV2_VALUE_INT; - else if (!strcmp((const char*)librdf_uri_as_string(datatype_uri), - "http://www.w3.org/2001/XMLSchema#decimal")) - type = SLV2_VALUE_FLOAT; - else - fprintf(stderr, "Unknown datatype %s\n", librdf_uri_as_string(datatype_uri)); - } - str_val = (const char*)librdf_node_get_literal_value(node); - break; - case LIBRDF_NODE_TYPE_BLANK: - str_val = (const char*)librdf_node_get_blank_identifier(node); - break; - case LIBRDF_NODE_TYPE_UNKNOWN: - default: - fprintf(stderr, "Unknown variable binding type %d\n", variable); - break; - } - - if (uri_val) - raptor_sequence_push(result, slv2_value_new_librdf_uri(world, uri_val)); - else if (str_val) - raptor_sequence_push(result, slv2_value_new(world, type, str_val)); + SLV2Value val = slv2_value_from_librdf_node(world, node); + if (val) + raptor_sequence_push(result, val); librdf_free_node(node); - librdf_query_results_next(results); } |