/* * rasqal_expression_evaluate_lang: * @e: The expression to evaluate. * @eval_context: Evaluation context * * INTERNAL - Evaluate RASQAL_EXPR_LANG (literal expr) expression. * * Return value: A #rasqal_literal value or NULL on failure. */ static rasqal_literal* rasqal_expression_evaluate_lang(rasqal_expression *e, rasqal_evaluation_context *eval_context, int *error_p) { rasqal_world* world = eval_context->world; rasqal_literal* l1; int free_literal = 1; rasqal_variable* v = NULL; unsigned char* new_s; l1 = rasqal_expression_evaluate2(e->arg1, eval_context, error_p); if(*error_p || !l1) goto failed; v = rasqal_literal_as_variable(l1); if(v) { rasqal_free_literal(l1); l1 = v->value; /* don't need v after this */ free_literal = 0; if(!l1) goto failed; } if(rasqal_literal_get_rdf_term_type(l1) != RASQAL_LITERAL_STRING) goto failed; if(l1->language) { size_t len = strlen(l1->language); new_s = (unsigned char*)RASQAL_MALLOC(cstring, len + 1); if(!new_s) goto failed; memcpy((char*)new_s, l1->language, len + 1); } else { new_s = (unsigned char*)RASQAL_MALLOC(cstring, 1); if(!new_s) goto failed; *new_s = '\0'; } if(free_literal) rasqal_free_literal(l1); /* after this new_s is owned by result */ return rasqal_new_string_literal(world, new_s, NULL, NULL, NULL); failed: if(error_p) *error_p = 1; if(free_literal) rasqal_free_literal(l1); return NULL; }
static rasqal_literal* rasqal_builtin_agg_expression_execute_result(void* user_data) { rasqal_builtin_agg_expression_execute* b; b = (rasqal_builtin_agg_expression_execute*)user_data; if(b->expr->op == RASQAL_EXPR_COUNT) { rasqal_literal* result; result = rasqal_new_integer_literal(b->world, RASQAL_LITERAL_INTEGER, b->count); return result; } if(b->expr->op == RASQAL_EXPR_GROUP_CONCAT) { size_t len; unsigned char* str; rasqal_literal* result; len = raptor_stringbuffer_length(b->sb); str = (unsigned char*)RASQAL_MALLOC(cstring, len + 1); if(!str) return NULL; if(raptor_stringbuffer_copy_to_string(b->sb, str, len)) { RASQAL_FREE(cstring, str); return NULL; } result = rasqal_new_string_literal(b->world, str, NULL, NULL, NULL); return result; } if(b->expr->op == RASQAL_EXPR_AVG) { rasqal_literal* count_l; rasqal_literal* result; count_l = rasqal_new_integer_literal(b->world, RASQAL_LITERAL_INTEGER, b->count); result = rasqal_literal_divide(b->l, count_l, &b->error); rasqal_free_literal(count_l); if(b->error) { /* result will be NULL and error will be non-0 on division by 0 * in which case the result is literal(integer 0) */ result = rasqal_new_integer_literal(b->world, RASQAL_LITERAL_INTEGER, 0); } return result; } return rasqal_new_literal_from_literal(b->l); }
rasqal_literal* redland_node_to_rasqal_literal(librdf_world* world, librdf_node *node) { rasqal_literal* l; if(librdf_node_is_resource(node)) { raptor_uri* uri=(raptor_uri*)librdf_new_uri_from_uri(librdf_node_get_uri(node)); l=rasqal_new_uri_literal(world->rasqal_world_ptr, uri); /* transfer uri ownership to literal */ } else if(librdf_node_is_literal(node)) { unsigned char *string; librdf_uri *uri; unsigned char *new_string; char *new_language=NULL; raptor_uri *new_datatype=NULL; size_t len; string=librdf_node_get_literal_value_as_counted_string(node, &len); new_string=(unsigned char*)rasqal_alloc_memory(len+1); if(!new_string) return NULL; strcpy((char*)new_string, (const char*)string); string=(unsigned char*)librdf_node_get_literal_value_language(node); if(string) { new_language=(char*)rasqal_alloc_memory(strlen((const char*)string)+1); if(!new_language) { rasqal_free_memory((void*)new_string); return NULL; } strcpy((char*)new_language, (const char*)string); } uri=librdf_node_get_literal_value_datatype_uri(node); if(uri) new_datatype=(raptor_uri*)librdf_new_uri_from_uri(uri); /* transfer new_string,new_language,new_datatype ownership to literal */ l = rasqal_new_string_literal(world->rasqal_world_ptr, (const unsigned char*)new_string, new_language, new_datatype, NULL); } else { unsigned char *blank=librdf_node_get_blank_identifier(node); unsigned char *new_blank; if(!blank) return NULL; new_blank=(unsigned char*)rasqal_alloc_memory(strlen((const char*)blank)+1); if(!new_blank) return NULL; strcpy((char*)new_blank, (const char*)blank); /* transfer new_blank ownership to literal */ l = rasqal_new_simple_literal(world->rasqal_world_ptr, RASQAL_LITERAL_BLANK, (const unsigned char*)new_blank); } return l; }
/* * rasqal_expression_evaluate_str: * @e: The expression to evaluate. * @eval_context: Evaluation context * * INTERNAL - Evaluate RASQAL_EXPR_STR (literal expr) expression. * * Return value: A #rasqal_literal value or NULL on failure. */ static rasqal_literal* rasqal_expression_evaluate_str(rasqal_expression *e, rasqal_evaluation_context *eval_context, int *error_p) { rasqal_world* world = eval_context->world; rasqal_literal* l1 = NULL; rasqal_literal* result = NULL; const unsigned char *s; size_t len; unsigned char *new_s; l1 = rasqal_expression_evaluate2(e->arg1, eval_context, error_p); if(*error_p || !l1) goto failed; /* Note: flags removes RASQAL_COMPARE_XQUERY as this is the * explicit stringify operation and we want URIs as strings. */ s = rasqal_literal_as_counted_string(l1, &len, (eval_context->flags & ~RASQAL_COMPARE_XQUERY), error_p); if(!s || *error_p) goto failed; new_s = (unsigned char *)RASQAL_MALLOC(cstring, len + 1); if(!new_s) goto failed; memcpy((char*)new_s, (const char*)s, len + 1); /* after this new_s is owned by result */ result = rasqal_new_string_literal(world, new_s, NULL, NULL, NULL); if(l1) rasqal_free_literal(l1); return result; failed: if(error_p) *error_p = 1; if(l1) rasqal_free_literal(l1); return NULL; }
int main(int argc, char *argv[]) { #if 0 raptor_uri *xsd_uri; raptor_uri *dateTime_uri; rasqal_literal *l1, *l2; int fn_i; raptor_uri* fn_uri; const unsigned char *fn_name; rasqal_extension_fn fn; raptor_sequence *fn_args; char *error; rasqal_literal *fn_result; rasqal_world *world; world = rasqal_new_world(); if(!world || rasqal_world_open(world)) { fprintf(stderr, "%s: rasqal_world init failed\n", program); return(1); } xsd_uri = raptor_new_uri(raptor_xmlschema_datatypes_namespace_uri); dateTime_uri = raptor_new_uri_from_uri_local_name(xsd_uri, (const unsigned char*)"dateTime"); rasqal_init_datatypes(); fn_args = raptor_new_sequence((raptor_sequence_free_handler*)rasqal_free_literal, (raptor_sequence_print_handler*)rasqal_literal_print); l1 = rasqal_new_string_literal((unsigned char*)strdup("2004-05-04"), NULL, raptor_uri_copy(dateTime_uri), NULL); raptor_sequence_push(fn_args, l1); l2 = rasqal_new_string_literal((unsigned char*)strdup("2003-01-02"), NULL, raptor_uri_copy(dateTime_uri), NULL); raptor_sequence_push(fn_args, l2); fn_i = 0; fn_name = rasqal_xsd_datatype_fns[fn_i].name; fn = rasqal_xsd_datatype_fns[fn_i].fn; fn_uri = rasqal_xsd_datatype_fns[fn_i].uri; error = NULL; fn_result = fn(fn_uri, fn_args, &error); raptor_free_sequence(fn_args); if(!fn_result) { if(error) fprintf(stderr, "function %s failed with error %s\n", fn_name, error); else fprintf(stderr, "function %s unknown error\n", fn_name); } else { fprintf(stderr, "function %s returned result: ", fn_name); rasqal_literal_print(fn_result, stderr); fputc('\n', stderr); } if(fn_result) rasqal_free_literal(fn_result); rasqal_finish_datatypes(); raptor_free_uri(xsd_uri); raptor_free_uri(dateTime_uri); rasqal_free_world(world); #endif return 0; }
/* * rasqal_expression_evaluate_strlang: * @e: The expression to evaluate. * @eval_context: Evaluation context * * INTERNAL - Evaluate RASQAL_EXPR_STRLANG(expr) expression. * * Return value: A #rasqal_literal string value or NULL on failure. */ static rasqal_literal* rasqal_expression_evaluate_strlang(rasqal_expression *e, rasqal_evaluation_context *eval_context, int *error_p) { rasqal_world* world = eval_context->world; rasqal_literal *l1 = NULL; rasqal_literal *l2 = NULL; const unsigned char* s = NULL; const unsigned char* lang = NULL; unsigned char* new_s = NULL; unsigned char* new_lang = NULL; size_t len; size_t lang_len; l1 = rasqal_expression_evaluate2(e->arg1, eval_context, error_p); if(*error_p || !l1) goto failed; if(l1->language || l1->datatype) { /* not a simple literal so return NULL success */ rasqal_free_literal(l1); return NULL; } s = rasqal_literal_as_counted_string(l1, &len, eval_context->flags, error_p); if(*error_p) goto failed; l2 = rasqal_expression_evaluate2(e->arg2, eval_context, error_p); if(*error_p || !l2) goto failed; lang = rasqal_literal_as_counted_string(l2, &lang_len, eval_context->flags, error_p); if(*error_p) goto failed; new_s = (unsigned char*)RASQAL_MALLOC(cstring, len + 1); if(!new_s) goto failed; memcpy(new_s, s, len + 1); new_lang = (unsigned char*)RASQAL_MALLOC(cstring, lang_len + 1); if(!new_lang) goto failed; memcpy(new_lang, lang, lang_len + 1); rasqal_free_literal(l1); rasqal_free_literal(l2); /* after this new_s and new_lang become owned by result */ return rasqal_new_string_literal(world, new_s, (const char*)new_lang, /*datatype */ NULL, /* qname */ NULL); failed: if(error_p) *error_p = 1; if(new_s) RASQAL_FREE(cstring, new_s); if(l1) rasqal_free_literal(l1); if(l2) rasqal_free_literal(l2); return NULL; }
/* * rasqal_expression_evaluate_strdt: * @e: The expression to evaluate. * @eval_context: Evaluation context * * INTERNAL - Evaluate RASQAL_EXPR_STRDT(expr) expression. * * Return value: A #rasqal_literal string value or NULL on failure. */ static rasqal_literal* rasqal_expression_evaluate_strdt(rasqal_expression *e, rasqal_evaluation_context *eval_context, int *error_p) { rasqal_world* world = eval_context->world; rasqal_literal *l1 = NULL; rasqal_literal *l2 = NULL; const unsigned char* s = NULL; unsigned char* new_s = NULL; size_t len; raptor_uri* dt_uri = NULL; l1 = rasqal_expression_evaluate2(e->arg1, eval_context, error_p); if(*error_p || !l1) goto failed; if(l1->language || l1->datatype) { /* not a simple literal so return NULL success */ rasqal_free_literal(l1); return NULL; } s = rasqal_literal_as_counted_string(l1, &len, eval_context->flags, error_p); if(*error_p) goto failed; l2 = rasqal_expression_evaluate2(e->arg2, eval_context, error_p); if(*error_p || !l2) goto failed; dt_uri = rasqal_literal_as_uri(l2); if(dt_uri) { dt_uri = raptor_uri_copy(dt_uri); } else { const unsigned char *uri_string; uri_string = rasqal_literal_as_string_flags(l2, eval_context->flags, error_p); if(*error_p) goto failed; dt_uri = raptor_new_uri(world->raptor_world_ptr, uri_string); if(!dt_uri) goto failed; } new_s =(unsigned char*)RASQAL_MALLOC(cstring, len + 1); if(!new_s) goto failed; memcpy(new_s, s, len + 1); rasqal_free_literal(l1); rasqal_free_literal(l2); /* after this new_s and dt_uri become owned by result */ return rasqal_new_string_literal(world, new_s, /* language */ NULL, dt_uri, /* qname */ NULL); failed: if(error_p) *error_p = 1; if(new_s) RASQAL_FREE(cstring, new_s); if(l1) rasqal_free_literal(l1); if(l2) rasqal_free_literal(l2); return NULL; }
/* * rasqal_expression_evaluate_uuid: * @e: The expression to evaluate. * @eval_context: Evaluation context * @want_uri: non-0 to return URI otherwise string * * INTERNAL - Evaluate SPARQL 1.1 RASQAL_EXPR_UUID, RASQAL_EXPR_STRUUID * * Return value: A #rasqal_literal URI / string value or NULL on failure. */ static rasqal_literal* rasqal_expression_evaluate_uuid(rasqal_expression *e, rasqal_evaluation_context *eval_context, int *error_p, int want_uri) { #ifdef RASQAL_UUID_NONE return NULL; #else rasqal_world* world = eval_context->world; #if defined(RASQAL_UUID_OSSP) uuid_t* data; #else uuid_t data; /* static */ int i; #endif size_t output_len = RASQAL_UUID_STRING_LEN; unsigned char* output; unsigned char* p; #if defined(RASQAL_UUID_LIBUUID) || defined(RASQAL_UUID_LIBC) uuid_generate(data); #endif #if defined(RASQAL_UUID_OSSP) uuid_create(&data); uuid_make(data, UUID_MAKE_V1); #endif #ifdef RASQAL_UUID_INTERNAL rasqal_uuid_generate(eval_context, data); #endif if(want_uri) output_len += RASQAL_UUID_URI_PREFIX_LEN; output = RASQAL_MALLOC(unsigned char*, output_len + 1); if(!output) { #if defined(RASQAL_UUID_OSSP) uuid_destroy(data); #endif return NULL; } p = output; if(want_uri) { memcpy(p, RASQAL_UUID_URI_PREFIX, RASQAL_UUID_URI_PREFIX_LEN); p += RASQAL_UUID_URI_PREFIX_LEN; } #if defined(RASQAL_UUID_OSSP) uuid_export(data, UUID_FMT_STR, p, /* data_len */ NULL); uuid_destroy(data); #else for(i = 0; i < RASQAL_UUID_LEN; i++) { unsigned short hex; unsigned char c = data[i]; hex = (c & 0xf0) >> 4; *p++ = (hex < 10) ? ('0' + hex) : ('a' + hex - 10); hex = (c & 0x0f); *p++ = (hex < 10) ? ('0' + hex) : ('a' + hex - 10); if(i == 3 || i == 5 || i == 7 || i == 9) *p++ = '-'; } *p = '\0'; #endif /* end if !RASQAL_UUID_OSSP */ /* after this output becomes owned by result */ if(want_uri) { raptor_uri* u; rasqal_literal* l = NULL; u = raptor_new_uri(world->raptor_world_ptr, output); if(u) l = rasqal_new_uri_literal(world, u); RASQAL_FREE(char*, output); return l; } else { return rasqal_new_string_literal(world, output, NULL, NULL, NULL); } #endif }
/* * rasqal_expression_evaluate_digest: * @e: The expression to evaluate. * @eval_context: Evaluation context * * INTERNAL - Evaluate SPARQL 1.1 RASQAL_EXPR_MD5, RASQAL_EXPR_SHA1, * RASQAL_EXPR_SHA224, RASQAL_EXPR_SHA256, RASQAL_EXPR_SHA384, * RASQAL_EXPR_SHA512 (string) expression. * * Return value: A #rasqal_literal xsd:string value or NULL on failure. */ rasqal_literal* rasqal_expression_evaluate_digest(rasqal_expression *e, rasqal_evaluation_context *eval_context, int *error_p) { rasqal_world* world = eval_context->world; rasqal_digest_type md_type = RASQAL_DIGEST_NONE; rasqal_literal* l1; const unsigned char *s; unsigned char *new_s; size_t len; int output_len; unsigned char *output = NULL; unsigned int i; unsigned char* p; /* Turn EXPR enum into DIGEST enum - we know they are ordered the same */ if(e->op >= RASQAL_EXPR_MD5 && e->op <= RASQAL_EXPR_SHA512) md_type = (rasqal_digest_type)(e->op - RASQAL_EXPR_MD5 + RASQAL_DIGEST_MD5); else return NULL; l1 = rasqal_expression_evaluate2(e->arg1, eval_context, error_p); if(*error_p || !l1) goto failed; s = rasqal_literal_as_counted_string(l1, &len, eval_context->flags, error_p); if(*error_p) goto failed; output_len = rasqal_digest_buffer(md_type, NULL, NULL, 0); if(output_len < 0) return NULL; output = RASQAL_MALLOC(unsigned char*, output_len); if(!output) return NULL; output_len = rasqal_digest_buffer(md_type, output, s, len); if(output_len < 0) goto failed; new_s = RASQAL_MALLOC(unsigned char*, (output_len * 2) + 1); if(!new_s) goto failed; p = new_s; for(i = 0; i < RASQAL_GOOD_CAST(unsigned int, output_len); i++) { unsigned short hex; char c = output[i]; hex = (c & 0xf0) >> 4; *p++ = (hex < 10) ? ('0' + hex) : ('a' + hex - 10); hex = (c & 0x0f); *p++ = (hex < 10) ? ('0' + hex) : ('a' + hex - 10); } *p = '\0'; RASQAL_FREE(char, output); rasqal_free_literal(l1); /* after this new_s becomes owned by result */ return rasqal_new_string_literal(world, new_s, NULL, NULL, NULL); failed: if(output) RASQAL_FREE(char, output); if(l1) rasqal_free_literal(l1); return NULL; }