/* * 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_data_graph* rasqal_new_data_graph_common(rasqal_world* world, raptor_uri* uri, raptor_iostream* iostr, raptor_uri* base_uri, raptor_uri* name_uri, int flags, const char* format_type, const char* format_name, raptor_uri* format_uri) { rasqal_data_graph* dg; dg = (rasqal_data_graph*)RASQAL_CALLOC(rasqal_data_graph, 1, sizeof(*dg)); if(dg) { dg->world = world; dg->usage = 1; if(iostr) dg->iostr = iostr; else if(uri) dg->uri = raptor_uri_copy(uri); if(name_uri) dg->name_uri = raptor_uri_copy(name_uri); dg->flags = flags; if(format_type) { size_t len = strlen(format_type); dg->format_type = RASQAL_MALLOC(string, len + 1); if(!dg->format_type) goto error; memcpy(dg->format_type, format_type, len + 1); } if(format_name) { size_t len = strlen(format_name); dg->format_name = RASQAL_MALLOC(string, len + 1); if(!dg->format_name) goto error; memcpy(dg->format_name, format_name, len + 1); } if(format_uri) dg->format_uri = raptor_uri_copy(format_uri); if(base_uri) dg->base_uri = raptor_uri_copy(base_uri); } return dg; error: rasqal_free_data_graph(dg); return NULL; }
static void rasqal_service_content_type_handler(raptor_www* www, void* userdata, const char* content_type) { rasqal_service* svc = (rasqal_service*)userdata; size_t len; if(svc->content_type) RASQAL_FREE(cstring, svc->content_type); len = strlen(content_type) + 1; svc->content_type = (char*)RASQAL_MALLOC(cstring, len); if(svc->content_type) { char* p; memcpy(svc->content_type, content_type, len); for(p = svc->content_type; *p; p++) { if(*p == ';' || *p == ' ') { *p = '\0'; break; } } } }
/** * rasqal_new_variable_from_variable: * @v: #rasqal_variable to copy * * Copy Constructor - Create a new Rasqal variable from an existing one * * This does a deep copy of all variable fields * * Return value: a new #rasqal_variable or NULL on failure. **/ rasqal_variable* rasqal_new_variable_from_variable(rasqal_variable* v) { rasqal_variable* new_v; size_t name_len; unsigned char *new_name; new_v = (rasqal_variable*)RASQAL_CALLOC(rasqal_variable, 1, sizeof(rasqal_variable)); if(!new_v) return NULL; name_len = strlen((const char*)v->name); new_name = (unsigned char*)RASQAL_MALLOC(cstring, name_len+1); if(!new_name) { RASQAL_FREE(rasqal_variable, new_v); return NULL; } memcpy(new_name, v->name, name_len+1); new_v->vars_table = v->vars_table; new_v->name= new_name; new_v->value= rasqal_new_literal_from_literal(v->value); new_v->offset= v->offset; new_v->type= v->type; new_v->expression= rasqal_new_expression_from_expression(v->expression); return new_v; }
/** * rasqal_world_set_default_generate_bnodeid_parameters: * @world: #rasqal_world object * @prefix: prefix string * @base: integer base identifier * * Set default bnodeid generation parameters * * Sets the parameters for the default algorithm used to generate * blank node IDs. The default algorithm uses both @prefix and @base * to generate a new identifier. The exact identifier generated is * not guaranteed to be a strict concatenation of @prefix and @base * but will use both parts. * * For finer control of the generated identifiers, use * rasqal_world_set_generate_bnodeid_handler() * * If prefix is NULL, the default prefix is used (currently "bnodeid") * If base is less than 1, it is initialised to 1. * * Return value: non-0 on failure **/ int rasqal_world_set_default_generate_bnodeid_parameters(rasqal_world* world, char *prefix, int base) { char *prefix_copy = NULL; size_t length = 0; RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(world, rasqal_world, 1); if(--base < 0) base = 0; if(prefix) { length = strlen(prefix); prefix_copy = (char*)RASQAL_MALLOC(cstring, length + 1); if(!prefix_copy) return 1; memcpy(prefix_copy, prefix, length + 1); } if(world->default_generate_bnodeid_handler_prefix) RASQAL_FREE(cstring, world->default_generate_bnodeid_handler_prefix); world->default_generate_bnodeid_handler_prefix = prefix_copy; world->default_generate_bnodeid_handler_prefix_length = length; world->default_generate_bnodeid_handler_base = base; return 0; }
/** * rasqal_xsd_decimal_set_string: * @dec: XSD Decimal * @string: lexical form * * Set an XSD Decimal value from a string lexical form * * Return value: non-0 on failure **/ int rasqal_xsd_decimal_set_string(rasqal_xsd_decimal* dec, const char* string) { int rc=0; size_t len; if(!string) return 1; rasqal_xsd_decimal_clear_string(dec); len = strlen(string); dec->string = (char*)RASQAL_MALLOC(cstring, len+1); if(!dec->string) return 1; memcpy(dec->string, string, len + 1); dec->string_len = len; #if defined(RASQAL_DECIMAL_C99) || defined(RASQAL_DECIMAL_NONE) dec->raw = strtod(string, NULL); #endif #ifdef RASQAL_DECIMAL_MPFR if(*string == '+') string++; rc = mpfr_set_str(dec->raw, string, 10, dec->rounding); #endif #ifdef RASQAL_DECIMAL_GMP if(*string == '+') string++; rc = mpf_set_str(dec->raw, string, 10); #endif return rc; }
static void rasqal_sparql_xml_sax2_characters_handler(void *user_data, raptor_xml_element* xml_element, const unsigned char *s, int len) { rasqal_rowsource_sparql_xml_context* con; con=(rasqal_rowsource_sparql_xml_context*)user_data; #ifdef TRACE_XML if(con->trace) { pad(stderr, con->depth); fputs("Text '", stderr); fwrite(s, sizeof(char), len, stderr); fprintf(stderr, "' (%d bytes)\n", len); } #endif if(con->state == STATE_literal || con->state == STATE_uri || con->state == STATE_bnode) { con->value_len=len; con->value=(char*)RASQAL_MALLOC(cstring, len+1); memcpy(con->value, s, len); con->value[len]='\0'; } }
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_new_service: * @world: rasqal_world object * @service_uri: sparql protocol service URI * @query_string: query string (or NULL) * @data_graphs: sequence of #rasqal_data_graph graphs for service * * Constructor - create a new rasqal protocol service object. * * Create a structure to execute a sparql protocol service at * @service_uri running the query @query_string and returning * a sparql result set. * * All arguments are copied by the service object * * Return value: a new #rasqal_query object or NULL on failure */ rasqal_service* rasqal_new_service(rasqal_world* world, raptor_uri* service_uri, const char* query_string, raptor_sequence* data_graphs) { rasqal_service* svc; size_t len = 0; RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(world, rasqal_world, NULL); RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(service_uri, raptor_uri, NULL); svc = (rasqal_service*)RASQAL_CALLOC(rasqal_service, 1, sizeof(*svc)); if(!svc) return NULL; svc->world = world; svc->service_uri = raptor_uri_copy(service_uri); if(query_string) { len = strlen(query_string); svc->query_string = RASQAL_MALLOC(cstring, len + 1); if(!svc->query_string) { rasqal_free_service(svc); return NULL; } memcpy(svc->query_string, query_string, len + 1); } svc->query_string_len = len; if(data_graphs) { int i; rasqal_data_graph* dg; #ifdef HAVE_RAPTOR2_API svc->data_graphs = raptor_new_sequence((raptor_data_free_handler)rasqal_free_data_graph, NULL); #else svc->data_graphs = raptor_new_sequence((raptor_sequence_free_handler*)rasqal_free_data_graph, NULL); #endif if(!svc->data_graphs) { rasqal_free_service(svc); return NULL; } for(i = 0; (dg = (rasqal_data_graph*)raptor_sequence_get_at(data_graphs, i)); i++) { raptor_sequence_push(svc->data_graphs, rasqal_new_data_graph_from_data_graph(dg)); } } return svc; }
/** * rasqal_new_xsd_decimal: * @world: rasqal world object * * Create a new XSD Decimal object. * * Return value: new xsd:decimal object or NULL on failure. **/ rasqal_xsd_decimal* rasqal_new_xsd_decimal(rasqal_world* world) { rasqal_xsd_decimal* dec; dec = (rasqal_xsd_decimal*)RASQAL_MALLOC(decimal, sizeof(rasqal_xsd_decimal)); if(dec) rasqal_xsd_decimal_init(dec); return dec; }
/* * 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; }
/* * rasqal_expression_evaluate_bnode_constructor: * @e: The expression to evaluate. * @eval_context: Evaluation context * * INTERNAL - Evaluate RASQAL_EXPR_BNODE (string) expression. * * Return value: A #rasqal_literal blank node value or NULL on failure. */ static rasqal_literal* rasqal_expression_evaluate_bnode_constructor(rasqal_expression *e, rasqal_evaluation_context *eval_context, int *error_p) { rasqal_world* world = eval_context->world; rasqal_literal *l1 = NULL; unsigned char *new_s = NULL; if(e->arg1) { const unsigned char *s; size_t len; 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; new_s = (unsigned char*)RASQAL_MALLOC(cstring, len + 1); if(!new_s) goto failed; memcpy((char*)new_s, s, len + 1); rasqal_free_literal(l1); } else { new_s = rasqal_world_generate_bnodeid(world, NULL); if(!new_s) goto failed; } /* after this new_s is owned by the result */ return rasqal_new_simple_literal(world, RASQAL_LITERAL_BLANK, new_s); failed: if(error_p) *error_p = 1; if(l1) rasqal_free_literal(l1); return NULL; }
/** * rasqal_xsd_format_float: * @i: float * @len_p: pointer to length of result or NULL * * INTERNAL - Format a new an xsd:float correctly * * Return value: new string or NULL on failure */ unsigned char* rasqal_xsd_format_float(float f, size_t *len_p) { unsigned char* string; /* FIXME: This is big enough for C float formatted in decimal as %1g */ #define FLOAT_BUFFER_SIZE 30 string = (unsigned char*)RASQAL_MALLOC(cstring, FLOAT_BUFFER_SIZE + 1); if(!string) return NULL; /* snprintf() takes as length the buffer size including NUL */ /* FIXME: %1g may not be the nearest to XSD xsd:float canonical format */ snprintf((char*)string, FLOAT_BUFFER_SIZE + 1, "%1g", (double)f); if(len_p) *len_p = strlen((const char*)string); return string; }
static unsigned char* rasqal_prefix_id(int prefix_id, unsigned char *string) { int tmpid = prefix_id; unsigned char* buffer; size_t length = strlen((const char*)string)+4; /* "r" +... + "_" +... \0 */ while(tmpid /= 10) length++; buffer = (unsigned char*)RASQAL_MALLOC(cstring, length); if(!buffer) return NULL; sprintf((char*)buffer, "r%d_%s", prefix_id, string); return buffer; }
/** * rasqal_engine_new_rowsort_map: * @flags: 1: do distinct * * INTERNAL - create a new map for sorting rows * */ rasqal_map* rasqal_engine_new_rowsort_map(int is_distinct, int compare_flags, raptor_sequence* order_conditions_sequence) { rowsort_compare_data* rcd; rcd = (rowsort_compare_data*)RASQAL_MALLOC(rowsort_compare_data, sizeof(rowsort_compare_data)); if(!rcd) return NULL; rcd->is_distinct = is_distinct; rcd->compare_flags = compare_flags; rcd->order_conditions_sequence = order_conditions_sequence; return rasqal_new_map(rasqal_engine_rowsort_row_compare, rcd, rasqal_engine_rowsort_free_compare_data, rasqal_engine_rowsort_map_free_row, rasqal_engine_rowsort_map_print_row, NULL, 0); }
/** * rasqal_service_set_format: * @svc: #rasqal_service service object * @format: service mime type (or NULL) * * Set the MIME Type to use in HTTP Accept when executing the service * * Return value: non 0 on failure **/ int rasqal_service_set_format(rasqal_service* svc, const char *format) { size_t len; if(svc->format) { RASQAL_FREE(cstring, svc->format); svc->format = NULL; } if(!format) return 0; len = strlen(format) + 1; svc->format = (char*)RASQAL_MALLOC(cstring, len); if(!svc->format) return 1; memcpy(svc->format, format, len); return 0; }
unsigned char* rasqal_world_default_generate_bnodeid_handler(void *user_data, unsigned char *user_bnodeid) { rasqal_world *world = (rasqal_world*)user_data; int id; unsigned char *buffer; int length; int tmpid; if(user_bnodeid) return user_bnodeid; id = ++world->default_generate_bnodeid_handler_base; tmpid = id; length = 2; /* min length 1 + \0 */ while(tmpid /= 10) length++; if(world->default_generate_bnodeid_handler_prefix) length += world->default_generate_bnodeid_handler_prefix_length; else length += 7; /* bnodeid */ buffer = (unsigned char*)RASQAL_MALLOC(cstring, length); if(!buffer) return NULL; if(world->default_generate_bnodeid_handler_prefix) { memcpy(buffer, world->default_generate_bnodeid_handler_prefix, world->default_generate_bnodeid_handler_prefix_length); sprintf((char*)buffer + world->default_generate_bnodeid_handler_prefix_length, "%d", id); } else sprintf((char*)buffer, "bnodeid%d", id); return buffer; }
/** * rasqal_xsd_format_integer: * @i: integer * @len_p: pointer to length of result or NULL * * INTERNAL - Format a C integer as a string in XSD decimal integer format. * * This is suitable for multiple XSD decimal integer types that are * xsd:integer or sub-types such as xsd:short, xsd:int, xsd:long, * * See http://www.w3.org/TR/xmlschema-2/#built-in-datatypes for the full list. * * Return value: new string or NULL on failure */ unsigned char* rasqal_xsd_format_integer(int i, size_t *len_p) { unsigned char* string; /* Buffer sizes need to format: * 4: 8 bit decimal integers (xsd:byte) "-128" to "127" * 6: 16 bit decimal integers (xsd:short) "-32768" to "32767" * 11: 32 bit decimal integers (xsd:int) "-2147483648" to "2147483647" * 20: 64 bit decimal integers (xsd:long) "-9223372036854775808" to "9223372036854775807" * (the lexical form may have leading 0s in non-canonical representations) */ #define INTEGER_BUFFER_SIZE 20 string = (unsigned char*)RASQAL_MALLOC(cstring, INTEGER_BUFFER_SIZE + 1); if(!string) return NULL; /* snprintf() takes as length the buffer size including NUL */ snprintf((char*)string, INTEGER_BUFFER_SIZE + 1, "%d", i); if(len_p) *len_p = strlen((const char*)string); return string; }
/** * rasqal_xsd_format_double: * @d: double * @len_p: pointer to length of result or NULL * * INTERNAL - Format a new an xsd:double correctly * * Return value: new string or NULL on failure */ unsigned char* rasqal_xsd_format_double(double d, size_t *len_p) { unsigned int e_index = 0; int trailing_zero_start = -1; unsigned int exponent_start; size_t len = 0; unsigned char* buf = NULL; if(d == 0.0f) { len = 5; buf = (unsigned char*)RASQAL_MALLOC(cstring, len + 1); if(!buf) return NULL; memcpy(buf, "0.0e0", len + 1); if(len_p) *len_p = len; return buf; } len = 20; buf = (unsigned char*)RASQAL_MALLOC(cstring, len + 1); if(!buf) return NULL; /* snprintf needs the length + 1 because it writes a \0 too */ snprintf((char*)buf, len + 1, "%1.14e", d); /* find the 'e' and start of mantissa trailing zeros */ for( ; buf[e_index]; ++e_index) { if(e_index > 0 && buf[e_index] == '0' && buf[e_index-1] != '0') trailing_zero_start = (int)e_index; else if(buf[e_index] == 'e') break; } if(trailing_zero_start >= 0) { if(buf[trailing_zero_start-1] == '.') ++trailing_zero_start; /* write an 'E' where the trailing zeros started */ buf[trailing_zero_start] = 'E'; if(buf[e_index + 1] == '-') { buf[trailing_zero_start + 1] = '-'; ++trailing_zero_start; } } else { buf[e_index] = 'E'; trailing_zero_start = (int)e_index + 1; } exponent_start = e_index+2; while(buf[exponent_start] == '0') ++exponent_start; if(trailing_zero_start >= 0) { len = strlen((const char*)buf); if(exponent_start == len) { len = trailing_zero_start + 2; buf[len-1] = '0'; buf[len] = '\0'; } else { /* copy the exponent (minus leading zeros) after the new E */ memmove(buf + trailing_zero_start + 1, buf + exponent_start, len - trailing_zero_start); len = strlen((const char*)buf); } } if(len_p) *len_p = len; return buf; }
int main(int argc, char **argv) { const char *program=rasqal_basename(argv[0]); rasqal_query *query = NULL; rasqal_query_results *results = NULL; raptor_uri *base_uri; unsigned char *data_string; unsigned char *uri_string; const char *query_language_name=QUERY_LANGUAGE; const char *query_format=QUERY_FORMAT; unsigned char *query_string; int count; rasqal_world *world; const char *data_file; world=rasqal_new_world(); if(!world || rasqal_world_open(world)) { fprintf(stderr, "%s: rasqal_world init failed\n", program); return(1); } if((data_file = getenv("RDF_DATA_FILE"))) { /* got data from environment */ } else { if(argc != 2) { fprintf(stderr, "USAGE: %s data-filename\n", program); return(1); } data_file = argv[1]; } data_string = raptor_uri_filename_to_uri_string(data_file); query_string=(unsigned char*)RASQAL_MALLOC(cstring, strlen((const char*)data_string)+strlen(query_format)+1); sprintf((char*)query_string, query_format, data_string); raptor_free_memory(data_string); uri_string=raptor_uri_filename_to_uri_string(""); base_uri = raptor_new_uri(world->raptor_world_ptr, uri_string); raptor_free_memory(uri_string); query=rasqal_new_query(world, query_language_name, NULL); if(!query) { fprintf(stderr, "%s: creating query in language %s FAILED\n", program, query_language_name); return(1); } printf("%s: preparing %s query\n", program, query_language_name); if(rasqal_query_prepare(query, query_string, base_uri)) { fprintf(stderr, "%s: %s query prepare FAILED\n", program, query_language_name); return(1); } RASQAL_FREE(cstring, query_string); printf("%s: executing query #1\n", program); results=rasqal_query_execute(query); if(!results) { fprintf(stderr, "%s: query execution 1 FAILED\n", program); return(1); } count=0; while(results && !rasqal_query_results_finished(results)) { int i; for(i=0; i<rasqal_query_results_get_bindings_count(results); i++) { const unsigned char *name=rasqal_query_results_get_binding_name(results, i); rasqal_literal *value=rasqal_query_results_get_binding_value(results, i); printf("result %d: variable %s=", count+1, (char*)name); rasqal_literal_print(value, stdout); putchar('\n'); } rasqal_query_results_next(results); count++; } if(results) rasqal_free_query_results(results); if(count != EXPECTED_RESULTS_COUNT) { fprintf(stderr, "%s: query execution 1 returned %d results, expected %d\n", program, count, EXPECTED_RESULTS_COUNT); return(1); } printf("%s: executing query #2\n", program); results = rasqal_query_execute(query); if(!results) { fprintf(stderr, "%s: query execution 2 FAILED\n", program); return(1); } count=0; while(results && !rasqal_query_results_finished(results)) { int i; for(i=0; i<rasqal_query_results_get_bindings_count(results); i++) { const unsigned char *name=rasqal_query_results_get_binding_name(results, i); rasqal_literal *value=rasqal_query_results_get_binding_value(results, i); printf("result %d: variable %s=", count+1, (char*)name); rasqal_literal_print(value, stdout); putchar('\n'); } rasqal_query_results_next(results); count++; } if(results) rasqal_free_query_results(results); if(count != EXPECTED_RESULTS_COUNT) { fprintf(stderr, "%s: query execution 2 returned %d results, expected %d\n", program, count, EXPECTED_RESULTS_COUNT); return(1); } printf("%s: executing query #3\n", program); results = rasqal_query_execute(query); if(!results) { fprintf(stderr, "%s: query execution 3 FAILED\n", program); return(1); } rasqal_free_query_results(results); printf("%s: executing query #4\n", program); results = rasqal_query_execute(query); if(!results) { fprintf(stderr, "%s: query execution 4 FAILED\n", program); return(1); } rasqal_free_query_results(results); rasqal_free_query(query); raptor_free_uri(base_uri); rasqal_free_world(world); return 0; }
int main(int argc, char *argv[]) { const char *program = rasqal_basename(argv[0]); rasqal_rowsource *rowsource = NULL; rasqal_world* world = NULL; rasqal_query* query = NULL; raptor_sequence* row_seq = NULL; raptor_sequence* expr_args_seq = NULL; int failures = 0; rasqal_variables_table* vt; rasqal_rowsource *input_rs = NULL; raptor_sequence* vars_seq = NULL; raptor_sequence* exprs_seq = NULL; int test_id; world = rasqal_new_world(); if(!world || rasqal_world_open(world)) { fprintf(stderr, "%s: rasqal_world init failed\n", program); return(1); } query = rasqal_new_query(world, "sparql", NULL); vt = query->vars_table; for(test_id = 0; test_id < AGGREGATION_TESTS_COUNT; test_id++) { int input_vars_count = test_data[test_id].input_vars; int output_rows_count = test_data[test_id].output_rows; int output_vars_count = test_data[test_id].output_vars; const int* input_group_ids = test_data[test_id].group_ids; const int* result_int_data = test_data[test_id].result_data; const char* const* result_string_data = test_data[test_id].result_string_data; rasqal_op op = test_data[test_id].op; raptor_sequence* seq = NULL; int count; int size; int i; char* output_var_name; rasqal_variable* output_var; rasqal_expression* expr; int output_row_size = (input_vars_count + output_vars_count); if(output_vars_count != 1) { fprintf(stderr, "%s: test %d expects %d variables which is not supported. Test skipped\n", program, test_id, output_vars_count); failures++; goto tidy; } row_seq = rasqal_new_row_sequence(world, vt, test_data[test_id].data, test_data[test_id].input_vars, &vars_seq); if(row_seq) { for(i = 0; i < test_data[test_id].input_rows; i++) { rasqal_row* row = (rasqal_row*)raptor_sequence_get_at(row_seq, i); row->group_id = input_group_ids[i]; } input_rs = rasqal_new_rowsequence_rowsource(world, query, vt, row_seq, vars_seq); /* vars_seq and row_seq are now owned by input_rs */ vars_seq = row_seq = NULL; } if(!input_rs) { fprintf(stderr, "%s: failed to create rowsequence rowsource\n", program); failures++; goto tidy; } expr_args_seq = raptor_new_sequence((raptor_data_free_handler)rasqal_free_expression, (raptor_data_print_handler)rasqal_expression_print); if(test_data[test_id].expr_agg_vars[0] != NULL) { int vindex; const unsigned char* var_name; for(vindex = 0; (var_name = (const unsigned char*)test_data[test_id].expr_agg_vars[vindex] ); vindex++) { rasqal_variable* v; rasqal_literal *l = NULL; rasqal_expression* e = NULL; v = rasqal_variables_table_get_by_name(vt, var_name); if(v) l = rasqal_new_variable_literal(world, v); if(l) e = rasqal_new_literal_expression(world, l); if(e) raptor_sequence_push(expr_args_seq, e); else { fprintf(stderr, "%s: failed to create variable %s\n", program, (const char*)var_name); failures++; goto tidy; } } } /* if vars */ output_var_name = (char*)RASQAL_MALLOC(cstring, 5); memcpy(output_var_name, "fake", 5); output_var = rasqal_variables_table_add(vt, RASQAL_VARIABLE_TYPE_ANONYMOUS, (const unsigned char*)output_var_name, NULL); expr = make_test_expr(world, expr_args_seq, op); /* expr_args_seq is now owned by expr */ expr_args_seq = NULL; exprs_seq = raptor_new_sequence((raptor_data_free_handler)rasqal_free_expression, (raptor_data_print_handler)rasqal_expression_print); raptor_sequence_push(exprs_seq, expr); /* expr is now owned by exprs_seq */ expr = NULL; vars_seq = raptor_new_sequence((raptor_data_free_handler)rasqal_free_variable, (raptor_data_print_handler)rasqal_variable_print); output_var = rasqal_new_variable_from_variable(output_var); raptor_sequence_push(vars_seq, output_var); rowsource = rasqal_new_aggregation_rowsource(world, query, input_rs, exprs_seq, vars_seq); /* exprs_seq, vars_seq and input_rs are now owned by rowsource */ exprs_seq = NULL; vars_seq = NULL; input_rs = NULL; if(!rowsource) { fprintf(stderr, "%s: failed to create aggregation rowsource\n", program); failures++; goto tidy; } /* Test the rowsource */ seq = rasqal_rowsource_read_all_rows(rowsource); if(!seq) { fprintf(stderr, "%s: test %d rasqal_rowsource_read_all_rows() returned a NULL seq for a aggregation rowsource\n", program, test_id); failures++; goto tidy; } count = raptor_sequence_size(seq); if(count != output_rows_count) { fprintf(stderr, "%s: test %d rasqal_rowsource_read_all_rows() returned %d rows for a aggregation rowsource, expected %d\n", program, test_id, count, output_rows_count); failures++; goto tidy; } size = rasqal_rowsource_get_size(rowsource); if(size != output_row_size) { fprintf(stderr, "%s: test %d rasqal_rowsource_get_size() returned %d columns (variables) for a aggregation rowsource, expected %d\n", program, test_id, size, output_row_size); failures++; goto tidy; } if(result_int_data) { for(i = 0; i < output_rows_count; i++) { rasqal_row* row = (rasqal_row*)raptor_sequence_get_at(seq, i); rasqal_literal* value; int integer; int expected_integer = result_int_data[i]; int vc; if(row->size != output_row_size) { fprintf(stderr, "%s: test %d row #%d is size %d expected %d\n", program, test_id, i, row->size, output_row_size); failures++; goto tidy; } /* Expected variable ordering in output row is: * {input vars} {output_vars} */ for(vc = 0; vc < output_vars_count; vc++) { rasqal_variable* row_var; int offset = input_vars_count + vc; row_var = rasqal_rowsource_get_variable_by_offset(rowsource, offset); value = row->values[offset]; if(!value) { fprintf(stderr, "%s: test %d row #%d %s value #%d result is NULL\n", program, test_id, i, row_var->name, vc); failures++; goto tidy; } if(value->type != RASQAL_LITERAL_INTEGER) { fprintf(stderr, "%s: test %d row #%d %s value #%d result is type %s expected integer\n", program, test_id, i, row_var->name, vc, rasqal_literal_type_label(value->type)); failures++; goto tidy; } integer = rasqal_literal_as_integer(value, NULL); if(integer != expected_integer) { fprintf(stderr, "%s: test %d row #%d %s value #%d result is %d expected %d\n", program, test_id, i, row_var->name, vc, integer, expected_integer); failures++; goto tidy; } } } } if(result_string_data) { for(i = 0; i < output_rows_count; i++) { rasqal_row* row = (rasqal_row*)raptor_sequence_get_at(seq, i); rasqal_literal* value; const unsigned char* str; const char* expected_string = result_string_data[i]; int vc; if(row->size != output_row_size) { fprintf(stderr, "%s: test %d row #%d is size %d expected %d\n", program, test_id, i, row->size, output_row_size); failures++; goto tidy; } /* Expected variable ordering in output row is: * {input vars} {output_vars} */ for(vc = 0; vc < output_vars_count; vc++) { rasqal_variable* row_var; int offset = input_vars_count + vc; row_var = rasqal_rowsource_get_variable_by_offset(rowsource, offset); value = row->values[offset]; if(!value) { fprintf(stderr, "%s: test %d row #%d %s value #%d result is NULL\n", program, test_id, i, row_var->name, vc); failures++; goto tidy; } if(value->type != RASQAL_LITERAL_STRING) { fprintf(stderr, "%s: test %d row #%d %s value #%d is type %s expected integer\n", program, test_id, i, row_var->name, vc, rasqal_literal_type_label(value->type)); failures++; goto tidy; } str = rasqal_literal_as_string(value); if(strcmp((const char*)str, expected_string)) { fprintf(stderr, "%s: test %d row #%d %s value #%d is %s expected %s\n", program, test_id, i, row_var->name, vc, str, expected_string); failures++; goto tidy; } } } } #ifdef RASQAL_DEBUG rasqal_rowsource_print_row_sequence(rowsource, seq, stderr); #endif raptor_free_sequence(seq); seq = NULL; rasqal_free_rowsource(rowsource); rowsource = NULL; if(expr_args_seq) raptor_free_sequence(expr_args_seq); expr_args_seq = NULL; } tidy: if(exprs_seq) raptor_free_sequence(exprs_seq); if(vars_seq) raptor_free_sequence(vars_seq); if(expr_args_seq) raptor_free_sequence(expr_args_seq); if(rowsource) rasqal_free_rowsource(rowsource); if(input_rs) rasqal_free_rowsource(input_rs); if(query) rasqal_free_query(query); if(world) rasqal_free_world(world); return failures; }
/* * 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_escaped_name_to_utf8_string: * @src: source name string * @len: length of source name string * @dest_lenp: pointer to store result string (or NULL) * @error_handler: error handling function * @error_data: data for error handle * * Get a UTF-8 and/or \u-escaped name as UTF-8. * * If dest_lenp is not NULL, the length of the resulting string is * stored at the pointed size_t. * * Return value: new UTF-8 string or NULL on failure. */ unsigned char* rasqal_escaped_name_to_utf8_string(const unsigned char *src, size_t len, size_t *dest_lenp, int (*error_handler)(rasqal_query *error_data, const char *message, ...), rasqal_query* error_data) { const unsigned char *p=src; size_t ulen=0; unsigned long unichar=0; unsigned char *result; unsigned char *dest; unsigned char *endp; int n; result=(unsigned char*)RASQAL_MALLOC(cstring, len+1); if(!result) return NULL; dest = result; endp = result + len; /* find end of string, fixing backslashed characters on the way */ while(len > 0) { unsigned char c=*p; if(c > 0x7f) { /* just copy the UTF-8 bytes through */ size_t unichar_len = raptor_unicode_utf8_string_get_char((const unsigned char*)p, len+1, NULL); if(unichar_len > len) { if(error_handler) error_handler(error_data, "UTF-8 encoding error at character %d (0x%02X) found.", c, c); /* UTF-8 encoding had an error or ended in the middle of a string */ RASQAL_FREE(cstring, result); return NULL; } memcpy(dest, p, unichar_len); dest+= unichar_len; p += unichar_len; len -= unichar_len; continue; } p++; len--; if(c != '\\') { /* not an escape - store and move on */ *dest++=c; continue; } if(!len) { RASQAL_FREE(cstring, result); return NULL; } c = *p++; len--; switch(c) { case '"': case '\\': *dest++=c; break; case 'u': case 'U': ulen=(c == 'u') ? 4 : 8; if(len < ulen) { if(error_handler) error_handler(error_data, "%c over end of line", c); RASQAL_FREE(cstring, result); return 0; } n=sscanf((const char*)p, ((ulen == 4) ? "%04lx" : "%08lx"), &unichar); if(n != 1) { if(error_handler) error_handler(error_data, "Bad %c escape", c); break; } p+=ulen; len-=ulen; if(unichar > 0x10ffff) { if(error_handler) error_handler(error_data, "Illegal Unicode character with code point #x%lX.", unichar); break; } dest += raptor_unicode_utf8_string_put_char(unichar, dest, endp - dest); break; default: if(error_handler) error_handler(error_data, "Illegal string escape \\%c in \"%s\"", c, src); RASQAL_FREE(cstring, result); return 0; } } /* end while */ /* terminate dest, can be shorter than source */ *dest='\0'; if(dest_lenp) *dest_lenp=dest-result; return result; }
/** * rasqal_new_row_sequence: * @world: world object ot use * @vt: variables table to use to declare variables * @row_data: row data * @vars_count: number of variables in row * @vars_seq_p: OUT parameter - pointer to place to store sequence of variables (or NULL) * * INTERNAL - Make a sequence of #rasqal_row* objects * with variables defined into the @vt table and values in the sequence * * The @row_data parameter is an array of strings forming a table of * width (vars_count * 2). * The first row is a list of variable names at offset 0. * The remaining rows are values where offset 0 is a literal and * offset 1 is a URI string. * * The last row is indicated by an entire row of NULLs. * * Return value: sequence of rows or NULL on failure */ raptor_sequence* rasqal_new_row_sequence(rasqal_world* world, rasqal_variables_table* vt, const char* const row_data[], int vars_count, raptor_sequence** vars_seq_p) { raptor_sequence *seq = NULL; raptor_sequence *vars_seq = NULL; int row_i; int column_i; int failed = 0; #define GET_CELL(row, column, offset) \ row_data[((((row)*vars_count)+(column))<<1)+(offset)] seq = raptor_new_sequence((raptor_data_free_handler)rasqal_free_row, (raptor_data_print_handler)rasqal_row_print); if(!seq) return NULL; if(vars_seq_p) { vars_seq = raptor_new_sequence((raptor_data_free_handler)rasqal_free_variable, (raptor_data_print_handler)rasqal_variable_print); if(!vars_seq) { raptor_free_sequence(seq); return NULL; } } /* row 0 is variables */ row_i = 0; for(column_i = 0; column_i < vars_count; column_i++) { const char * var_name = GET_CELL(row_i, column_i, 0); size_t var_name_len = strlen(var_name); const unsigned char* name; rasqal_variable* v; name = (unsigned char*)RASQAL_MALLOC(cstring, var_name_len+1); if(!name) { failed = 1; goto tidy; } memcpy((void*)name, var_name, var_name_len + 1); v = rasqal_variables_table_add(vt, RASQAL_VARIABLE_TYPE_NORMAL, name, NULL); if(!v) { failed = 1; goto tidy; } if(vars_seq) { v = rasqal_new_variable_from_variable(v); raptor_sequence_push(vars_seq, v); } } for(row_i = 1; 1; row_i++) { rasqal_row* row; int data_values_seen = 0; /* Terminate on an entire row of NULLs */ for(column_i = 0; column_i < vars_count; column_i++) { if(GET_CELL(row_i, column_i, 0) || GET_CELL(row_i, column_i, 1)) { data_values_seen++; break; } } if(!data_values_seen) break; row = rasqal_new_row_for_size(world, vars_count); if(!row) { raptor_free_sequence(seq); seq = NULL; goto tidy; } for(column_i = 0; column_i < vars_count; column_i++) { rasqal_literal* l = NULL; if(GET_CELL(row_i, column_i, 0)) { /* string literal */ const char* str = GET_CELL(row_i, column_i, 0); size_t str_len = strlen(str); char *eptr = NULL; int integer; integer = (int)strtol((const char*)str, &eptr, 10); if(!*eptr) { /* is an integer */ l = rasqal_new_integer_literal(world, RASQAL_LITERAL_INTEGER, integer); } else { unsigned char *val; val = (unsigned char*)RASQAL_MALLOC(cstring, str_len+1); if(val) { memcpy(val, str, str_len + 1); l = rasqal_new_string_literal_node(world, val, NULL, NULL); } else failed = 1; } } else if(GET_CELL(row_i, column_i, 1)) { /* URI */ const unsigned char* str; raptor_uri* u; str = (const unsigned char*)GET_CELL(row_i, column_i, 1); u = raptor_new_uri(world->raptor_world_ptr, str); if(u) l = rasqal_new_uri_literal(world, u); else failed = 1; } else { /* variable is not defined for this row */ continue; } if(!l) { rasqal_free_row(row); failed = 1; goto tidy; } rasqal_row_set_value_at(row, column_i, l); /* free our copy of literal, rasqal_row has a reference */ rasqal_free_literal(l); } raptor_sequence_push(seq, row); } tidy: if(failed) { if(seq) { raptor_free_sequence(seq); seq = NULL; } if(vars_seq) { raptor_free_sequence(vars_seq); vars_seq = NULL; } } else { if(vars_seq) { if(vars_seq_p) *vars_seq_p = vars_seq; else raptor_free_sequence(vars_seq); } } return seq; }
static void rasqal_sparql_xml_sax2_end_element_handler(void *user_data, raptor_xml_element* xml_element) { rasqal_rowsource_sparql_xml_context* con; raptor_qname* name; int i; rasqal_sparql_xml_read_state state=STATE_unknown; con=(rasqal_rowsource_sparql_xml_context*)user_data; name=raptor_xml_element_get_name(xml_element); for(i=STATE_first; i <= STATE_last; i++) { if(!strcmp((const char*)raptor_qname_get_local_name(name), sparql_xml_element_names[i])) { state=(rasqal_sparql_xml_read_state)i; con->state=state; } } if(state == STATE_unknown) { fprintf(stderr, "UNKNOWN element %s\n", raptor_qname_get_local_name(name)); con->failed++; } con->depth--; #ifdef TRACE_XML if(con->trace) { pad(stderr, con->depth); fprintf(stderr, "End Element %s (%d)\n", raptor_qname_get_local_name(name), con->state); } #endif switch(con->state) { case STATE_head: /* Only now is the full number of variables known */ con->variables_count = rasqal_variables_table_get_named_variables_count(con->vars_table); con->rowsource->size = con->variables_count; break; case STATE_literal: if(1) { rasqal_literal* l; unsigned char* lvalue; raptor_uri* datatype_uri=NULL; char* language_str=NULL; lvalue=(unsigned char*)RASQAL_MALLOC(cstring, con->value_len+1); memcpy(lvalue, con->value, con->value_len + 1); if(con->datatype) datatype_uri = raptor_new_uri(con->world->raptor_world_ptr, (const unsigned char*)con->datatype); if(con->language) { size_t language_len = strlen(con->language); language_str=(char*)RASQAL_MALLOC(cstring, language_len + 1); memcpy(language_str, con->language, language_len + 1); } l = rasqal_new_string_literal_node(con->world, lvalue, language_str, datatype_uri); rasqal_row_set_value_at(con->row, con->result_offset, l); rasqal_free_literal(l); RASQAL_DEBUG3("Saving row result %d string value at offset %d\n", con->offset, con->result_offset); } break; case STATE_bnode: if(1) { rasqal_literal* l; unsigned char* lvalue; lvalue=(unsigned char*)RASQAL_MALLOC(cstring, con->value_len+1); memcpy(lvalue, con->value, con->value_len + 1); l = rasqal_new_simple_literal(con->world, RASQAL_LITERAL_BLANK, lvalue); rasqal_row_set_value_at(con->row, con->result_offset, l); rasqal_free_literal(l); RASQAL_DEBUG3("Saving row result %d bnode value at offset %d\n", con->offset, con->result_offset); } break; case STATE_uri: if(1) { raptor_uri* uri; rasqal_literal* l; uri = raptor_new_uri(con->world->raptor_world_ptr, (const unsigned char*)con->value); l = rasqal_new_uri_literal(con->world, uri); rasqal_row_set_value_at(con->row, con->result_offset, l); rasqal_free_literal(l); RASQAL_DEBUG3("Saving row result %d uri value at offset %d\n", con->offset, con->result_offset); } break; case STATE_result: if(con->row) { RASQAL_DEBUG2("Saving row result %d\n", con->offset); raptor_sequence_push(con->results_sequence, con->row); } con->row=NULL; break; case STATE_unknown: case STATE_sparql: case STATE_variable: case STATE_results: case STATE_binding: default: break; } if(con->value) { RASQAL_FREE(cstring, con->value); con->value=NULL; } }
/** * rasqal_xsd_decimal_as_string: * @dec: XSD Decimal * * Get an XSD Decimal as a string lexical form. * * The returned string is shared and owned by the @dec object and * must be copied. * * Return value: lexical form string or NULL on failure. **/ char* rasqal_xsd_decimal_as_string(rasqal_xsd_decimal* dec) { char *s=NULL; size_t len=0; #ifdef RASQAL_DECIMAL_C99 #else char fmt[16]; size_t e_index = 0; size_t trailing_zero_start = 0; #endif if(dec->string) return dec->string; #ifdef RASQAL_DECIMAL_C99 len = dec->precision_digits; s = RASQAL_MALLOC(cstring, len + 1); if(!s) return NULL; /* NOTE: Never seen a sprintf that supports _Decimal yet */ snprintf(s, len, "%DDf", dec->raw); len = strlen(s); #else /* construct a format string */ snprintf(fmt, sizeof(fmt), DECIMAL_FMT, dec->precision_digits); /* decimal snprintf with no buffer to get buffer length */ len = DECIMAL_SNPRINTF(NULL, 0, fmt, dec->raw); s = (char*)RASQAL_MALLOC(cstring, len + 1); if(!s) return NULL; /* format into allocated buffer */ DECIMAL_SNPRINTF(s, len, fmt, dec->raw); len = strlen(s); /* find trailing zeros */ for(e_index = len - 1; e_index > 0 && s[e_index] == '0'; e_index--) { trailing_zero_start = e_index; } /* Stop string where the trailing zeros started */ if(trailing_zero_start) { s[trailing_zero_start] = '\0'; len = trailing_zero_start; } if(s[len - 1] == '.') { s[len-- - 1] = '\0'; } #endif dec->string = s; dec->string_len = len; return s; }
static void rasqal_sparql_xml_sax2_start_element_handler(void *user_data, raptor_xml_element *xml_element) { rasqal_rowsource_sparql_xml_context* con; int i; raptor_qname* name; rasqal_sparql_xml_read_state state=STATE_unknown; int attr_count; con=(rasqal_rowsource_sparql_xml_context*)user_data; name=raptor_xml_element_get_name(xml_element); for(i=STATE_first; i <= STATE_last; i++) { if(!strcmp((const char*)raptor_qname_get_local_name(name), sparql_xml_element_names[i])) { state=(rasqal_sparql_xml_read_state)i; con->state=state; } } if(state == STATE_unknown) { fprintf(stderr, "UNKNOWN element %s\n", raptor_qname_get_local_name(name)); con->failed++; } #ifdef TRACE_XML if(con->trace) { pad(stderr, con->depth); fprintf(stderr, "Element %s (%d)\n", raptor_qname_get_local_name(name), state); } #endif attr_count=raptor_xml_element_get_attributes_count(xml_element); con->name=NULL; con->datatype=NULL; con->language=NULL; if(attr_count > 0) { raptor_qname** attrs=raptor_xml_element_get_attributes(xml_element); for(i=0; i < attr_count; i++) { #ifdef TRACE_XML if(con->trace) { pad(stderr, con->depth+1); fprintf(stderr, "Attribute %s='%s'\n", raptor_qname_get_local_name(attrs[i]), raptor_qname_get_value(attrs[i])); } #endif if(!strcmp((const char*)raptor_qname_get_local_name(attrs[i]), "name")) con->name=(const char*)raptor_qname_get_counted_value(attrs[i], &con->name_length); else if(!strcmp((const char*)raptor_qname_get_local_name(attrs[i]), "datatype")) con->datatype=(const char*)raptor_qname_get_value(attrs[i]); } } if(raptor_xml_element_get_language(xml_element)) { con->language=(const char*)raptor_xml_element_get_language(xml_element); #ifdef TRACE_XML if(con->trace) { pad(stderr, con->depth+1); fprintf(stderr, "xml:lang '%s'\n", con->language); } #endif } switch(state) { case STATE_variable: if(con->name) { unsigned char* var_name; rasqal_variable *v; var_name = (unsigned char*)RASQAL_MALLOC(cstring, con->name_length+1); memcpy(var_name, con->name, con->name_length + 1); v = rasqal_variables_table_add(con->vars_table, RASQAL_VARIABLE_TYPE_NORMAL, var_name, NULL); if(v) rasqal_rowsource_add_variable(con->rowsource, v); } break; case STATE_result: if(1) { con->row = rasqal_new_row(con->rowsource); RASQAL_DEBUG2("Made new row %d\n", con->offset); con->offset++; } break; case STATE_binding: con->result_offset = rasqal_rowsource_get_variable_offset_by_name(con->rowsource, (const unsigned char*)con->name); break; case STATE_sparql: case STATE_head: case STATE_results: case STATE_literal: case STATE_bnode: case STATE_uri: case STATE_unknown: default: break; } con->depth++; }