/** * librdf_free_uri: * @uri: #librdf_uri object * * Destructor - destroy a #librdf_uri object. * **/ void librdf_free_uri(librdf_uri* uri) { #ifdef LIBRDF_USE_RAPTOR_URI if(!uri) return; raptor_free_uri(uri); #else librdf_hash_datum key; /* on stack */ #ifdef WITH_THREADS librdf_world *world; #endif if(!uri) return; #ifdef WITH_THREADS world = uri->world; pthread_mutex_lock(world->mutex); #endif uri->usage--; #if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1 LIBRDF_DEBUG3("URI %s usage count now %d\n", uri->string, uri->usage); #endif /* decrement usage, don't free if not 0 yet*/ if(uri->usage) { #ifdef WITH_THREADS pthread_mutex_unlock(world->mutex); #endif return; } #if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1 LIBRDF_DEBUG3("Deleting URI %s from hash, max usage was %d\n", uri->string, uri->max_usage); #endif key.data=uri->string; key.size=uri->string_length; /* Hash deletion fails only if the key is not found. This is not a fatal error so do not check for return value. */ librdf_hash_delete_all(uri->world->uris_hash, &key); if(uri->string) LIBRDF_FREE(cstring, uri->string); LIBRDF_FREE(librdf_uri, uri); #ifdef WITH_THREADS pthread_mutex_unlock(world->mutex); #endif #endif /* !LIBRDF_USE_RAPTOR_URI */ }
/** * librdf_storage_register_factory - Register a storage factory * @name: the storage factory name * @label: the storage factory label * @factory: pointer to function to call to register the factory * **/ void librdf_storage_register_factory(const char *name, const char *label, void (*factory) (librdf_storage_factory*)) { librdf_storage_factory *storage, *h; char *name_copy; char *label_copy; #if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1 LIBRDF_DEBUG2("Received registration for storage %s\n", name); #endif storage=(librdf_storage_factory*)LIBRDF_CALLOC(librdf_storage_factory, 1, sizeof(librdf_storage_factory)); if(!storage) LIBRDF_FATAL1(world, "Out of memory"); name_copy=(char*)LIBRDF_CALLOC(cstring, strlen(name)+1, 1); if(!name_copy) { LIBRDF_FREE(librdf_storage, storage); LIBRDF_FATAL1(world, "Out of memory"); } strcpy(name_copy, name); storage->name=name_copy; for(h = storages; h; h = h->next ) { if(!strcmp(h->name, name_copy)) { LIBRDF_FREE(cstring, name_copy); LIBRDF_FREE(librdf_storage, storage); LIBRDF_ERROR2(NULL, "storage %s already registered\n", h->name); return; } } label_copy=(char*)LIBRDF_CALLOC(cstring, strlen(label)+1, 1); if(!label_copy) { LIBRDF_FREE(librdf_storage, storage); LIBRDF_FATAL1(world, "Out of memory"); } strcpy(label_copy, label); storage->label=label_copy; /* Call the storage registration function on the new object */ (*factory)(storage); #if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1 LIBRDF_DEBUG3("%s has context size %d\n", name, storage->context_length); #endif storage->next = storages; storages = storage; }
static void librdf_sql_config_store_triple(void *user_data, const raptor_statement *triple) { librdf_sql_config* config=(librdf_sql_config*)user_data; int i; for(i=0; i < config->predicates_count; i++) { if(triple->predicate_type != RAPTOR_IDENTIFIER_TYPE_RESOURCE || triple->object_type != RAPTOR_IDENTIFIER_TYPE_LITERAL) continue; if(!strcmp((const char*)librdf_uri_as_string((librdf_uri*)triple->predicate), config->predicate_uri_strings[i])) { config->values[i]=strdup((char*)triple->object); #if LIBRDF_DEBUG > 1 LIBRDF_DEBUG3("Set config value %d to '%s'\n", i, config->values[i]); #endif } } return; }
/** * librdf_statement_decode2: * @world: redland world * @statement: the statement to deserialise into * @context_node: pointer to #librdf_node context_node to deserialise into * @buffer: the buffer to use * @length: buffer size * * Decodes a statement + context node from a buffer. * * Decodes the serialised statement (as created by librdf_statement_encode() ) * from the given buffer. If a context node is found and context_node is * not NULL, a pointer to the new #librdf_node is stored in *context_node. * * Return value: number of bytes used or 0 on failure (bad encoding, allocation failure) **/ size_t librdf_statement_decode2(librdf_world* world, librdf_statement* statement, librdf_node** context_node, unsigned char *buffer, size_t length) { unsigned char *p; librdf_node* node; unsigned char type; size_t total_length=0; LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(statement, librdf_statement, 0); #if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1 LIBRDF_DEBUG2("Decoding buffer of %d bytes\n", length); #endif /* absolute minimum - first byte is type */ if(length < 1) return 0; p=buffer; if(*p++ != 'x') return 0; length--; total_length++; while(length>0) { size_t node_len; type=*p++; length--; total_length++; if(!length) return 0; if(!(node=librdf_node_decode(world, &node_len, p, length))) return 0; p += node_len; length -= node_len; total_length += node_len; #if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1 LIBRDF_DEBUG3("Found type %c (%d bytes)\n", type, node_len); #endif switch(type) { case 's': /* subject */ statement->subject=node; break; case 'p': /* predicate */ statement->predicate=node; break; case 'o': /* object */ statement->object=node; break; case 'c': /* context */ if(context_node) *context_node=node; else librdf_free_node(node); break; default: librdf_log(world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STATEMENT, NULL, "Illegal statement encoding '%c' seen", p[-1]); return 0; } } return total_length; }
/** * librdf_new_uri2: * @world: redland world object * @uri_string: URI in string form * @length: length of string * * Constructor - create a new #librdf_uri object from a counted URI string. * * A new URI is constructed from a copy of the string. If the string * is a NULL pointer or 0 length or empty (first byte is 0) then the * result is NULL. * * Return value: a new #librdf_uri object or NULL on failure **/ librdf_uri* librdf_new_uri2(librdf_world *world, const unsigned char *uri_string, size_t length) { #ifdef LIBRDF_USE_RAPTOR_URI return raptor_new_uri_from_counted_string(world->raptor_world_ptr, uri_string, length); #else librdf_uri* new_uri; unsigned char *new_string; librdf_hash_datum key, value; /* on stack - not allocated */ librdf_hash_datum *old_value; /* just to be safe */ memset(&key, 0, sizeof(key)); memset(&value, 0, sizeof(value)); librdf_world_open(world); if(!uri_string || !length || !*uri_string) return NULL; #ifdef WITH_THREADS pthread_mutex_lock(world->mutex); #endif key.data = (char*)uri_string; key.size = length; /* if existing URI found in hash, return it */ if((old_value = librdf_hash_get_one(world->uris_hash, &key))) { new_uri = *(librdf_uri**)old_value->data; #if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1 LIBRDF_DEBUG3("Found existing URI %s in hash with current usage %d\n", uri_string, new_uri->usage); #endif librdf_free_hash_datum(old_value); new_uri->usage++; #if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1 if(new_uri->usage > new_uri->max_usage) new_uri->max_usage = new_uri->usage; #endif goto unlock; } /* otherwise create a new one */ #if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1 LIBRDF_DEBUG2("Creating new URI %s in hash\n", uri_string); #endif new_uri = (librdf_uri*)LIBRDF_CALLOC(librdf_uri, 1, sizeof(librdf_uri)); if(!new_uri) goto unlock; new_uri->world = world; new_uri->string_length = length; new_string = (unsigned char*)LIBRDF_MALLOC(cstring, length+1); if(!new_string) { LIBRDF_FREE(librdf_uri, new_uri); new_uri = NULL; goto unlock; } strcpy((char*)new_string, (const char*)uri_string); new_uri->string = new_string; new_uri->usage = 1; #if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1 new_uri->max_usage = 1; #endif value.data = &new_uri; value.size = sizeof(librdf_uri*); /* store in hash: URI-string => (librdf_uri*) */ if(librdf_hash_put(world->uris_hash, &key, &value)) { LIBRDF_FREE(cstring, new_string); LIBRDF_FREE(librdf_uri, new_uri); new_uri = NULL; } unlock: #ifdef WITH_THREADS pthread_mutex_unlock(world->mutex); #endif return new_uri; #endif /* !LIBRDF_USE_RAPTOR_URI */ }
/** * librdf_serializer_register_factory: * @world: redland world object * @name: the name of the serializer * @label: the label of the serializer (optional) * @mime_type: MIME type of the syntax (optional) * @uri_string: URI of the syntax (optional) * @factory: function to be called to register the factor parameters * * Register a serializer factory . * **/ REDLAND_EXTERN_C void librdf_serializer_register_factory(librdf_world *world, const char *name, const char *label, const char *mime_type, const unsigned char *uri_string, void (*factory) (librdf_serializer_factory*)) { librdf_serializer_factory *serializer; librdf_world_open(world); #if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1 LIBRDF_DEBUG2("Received registration for serializer %s\n", name); #endif if(!world->serializers) { world->serializers = raptor_new_sequence((raptor_data_free_handler)librdf_free_serializer_factory, NULL); if(!world->serializers) goto oom; } serializer=(librdf_serializer_factory*)LIBRDF_CALLOC(librdf_serializer_factory, 1, sizeof(librdf_serializer_factory)); if(!serializer) goto oom; serializer->name=(char*)LIBRDF_MALLOC(cstring, strlen(name)+1); if(!serializer->name) goto oom_tidy; strcpy(serializer->name, name); if(label) { serializer->label=(char*)LIBRDF_MALLOC(cstring, strlen(label)+1); if(!serializer->label) goto oom_tidy; strcpy(serializer->label, label); } /* register mime type if any */ if(mime_type) { serializer->mime_type=(char*)LIBRDF_MALLOC(cstring, strlen(mime_type)+1); if(!serializer->mime_type) goto oom_tidy; strcpy(serializer->mime_type, mime_type); } /* register URI if any */ if(uri_string) { serializer->type_uri=librdf_new_uri(world, uri_string); if(!serializer->type_uri) goto oom_tidy; } if(raptor_sequence_push(world->serializers, serializer)) goto oom; /* Call the serializer registration function on the new object */ (*factory)(serializer); #if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1 LIBRDF_DEBUG3("%s has context size %d\n", name, serializer->context_length); #endif return; oom_tidy: librdf_free_serializer_factory(serializer); oom: LIBRDF_FATAL1(world, LIBRDF_FROM_SERIALIZER, "Out of memory"); }