Skip to content

Instantly share code, notes, and snippets.

@njh
Created August 30, 2010 16:35
Show Gist options
  • Save njh/557652 to your computer and use it in GitHub Desktop.
Save njh/557652 to your computer and use it in GitHub Desktop.
diff --git a/src/rdf_init.c b/src/rdf_init.c
index 1509f1c..d2edac1 100644
--- a/src/rdf_init.c
+++ b/src/rdf_init.c
@@ -306,6 +306,45 @@ librdf_world_open(librdf_world *world)
/**
+ * librdf_world_set_rasqal:
+ * @world: librdf_world object
+ * @rasqal_world_ptr: rasqal_world object
+ *
+ * Set the #rasqal_world instance to be used with this #librdf_world.
+ *
+ * If no rasqal_world instance is set with this function,
+ * librdf_world_open() creates a new instance.
+ *
+ * Ownership of the rasqal_world is not taken. If the rasqal library
+ * instance is set with this function, librdf_free_world() will not
+ * free it.
+ *
+ **/
+void
+librdf_world_set_rasqal(librdf_world* world, rasqal_world* rasqal_world_ptr)
+{
+ LIBRDF_ASSERT_OBJECT_POINTER_RETURN(world, librdf_world);
+ world->rasqal_world_ptr = rasqal_world_ptr;
+}
+
+
+/**
+ * librdf_world_get_rasqal:
+ * @world: librdf_world object
+ *
+ * Get the #rasqal_world instance used by this #librdf_world.
+ *
+ * Return value: rasqal_world object or NULL on failure (e.g. not initialized)
+ **/
+rasqal_world*
+librdf_world_get_rasqal(librdf_world* world)
+{
+ LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(world, librdf_world, NULL);
+ return world->rasqal_world_ptr;
+}
+
+
+/**
* librdf_world_set_error:
* @world: redland world object
* @user_data: user data to pass to function
@@ -701,6 +740,7 @@ int
main(int argc, char *argv[])
{
librdf_world *world;
+ rasqal_world *rasqal_world;
unsigned char* id;
const char *program=librdf_basename((const char*)argv[0]);
@@ -714,6 +754,30 @@ main(int argc, char *argv[])
fprintf(stdout, "%s: Deleting world\n", program);
librdf_free_world(world);
+
+ /* Test setting and getting a the rasqal world */
+ fprintf(stdout, "%s: Setting and getting rasqal_world\n", program);
+ world = librdf_new_world();
+ if(!world) {
+ fprintf(stderr, "%s: librdf_new_world failed\n", program);
+ return 1;
+ }
+
+ rasqal_world = rasqal_new_world();
+ if(!rasqal_world) {
+ fprintf(stderr, "%s: rasqal_new_world failed\n", program);
+ return 1;
+ }
+
+ librdf_world_set_rasqal(world, rasqal_world);
+ if (librdf_world_get_rasqal(world) != rasqal_world) {
+ fprintf(stderr, "%s: librdf_world_set_rasqal/librdf_world_get_rasqal failed\n", program);
+ return 1;
+ }
+ rasqal_free_world(rasqal_world);
+ librdf_free_world(world);
+
+
/* Test id generation */
fprintf(stdout, "%s: Creating new world\n", program);
world = librdf_new_world();
diff --git a/src/rdf_init.h b/src/rdf_init.h
index 223ff39..e5a9265 100644
--- a/src/rdf_init.h
+++ b/src/rdf_init.h
@@ -43,7 +43,13 @@ void librdf_world_open(librdf_world *world);
REDLAND_API
void librdf_world_init_mutex(librdf_world *world);
-
+
+REDLAND_API
+void librdf_world_set_rasqal(librdf_world* world, rasqal_world* rasqal_world_ptr);
+
+REDLAND_API
+rasqal_world* librdf_world_get_rasqal(librdf_world* world);
+
REDLAND_API
void librdf_world_set_error(librdf_world* world, void *user_data, librdf_log_level_func error_handler);
REDLAND_API
diff --git a/src/rdf_init_internal.h b/src/rdf_init_internal.h
index 802ca20..154aa48 100644
--- a/src/rdf_init_internal.h
+++ b/src/rdf_init_internal.h
@@ -144,6 +144,7 @@ struct librdf_world_s
/* rasqal world object */
rasqal_world* rasqal_world_ptr;
+ int rasqal_world_allocated_here;
#ifdef RAPTOR_V2_AVAILABLE
/* raptor world object */
diff --git a/src/rdf_query_rasqal.c b/src/rdf_query_rasqal.c
index e07155a..47676b3 100644
--- a/src/rdf_query_rasqal.c
+++ b/src/rdf_query_rasqal.c
@@ -1371,28 +1371,33 @@ librdf_query_rasqal_constructor(librdf_world *world)
{
unsigned int i;
- world->rasqal_world_ptr=rasqal_new_world();
if(!world->rasqal_world_ptr) {
- LIBRDF_FATAL1(world, LIBRDF_FROM_QUERY, "failed to initialize rasqal");
- return;
- }
+ world->rasqal_world_ptr=rasqal_new_world();
+ world->rasqal_world_allocated_here = 1;
- /* Make sure rasqal works with the same raptor instance as everyone else.
- * In ifndef RAPTOR_V2_AVAILABLE case, raptor_world_ptr is NULL
- * -> ok to use with rasqal_world_set_raptor(),
- * rasqal should also have been built without RAPTOR_V2_AVAILABLE ->
- * rasqal_world_open() uses raptor_init() which just increments
- * the refcount on raptor instance already created in librdf_raptor_init().
- */
+ if(!world->rasqal_world_ptr) {
+ LIBRDF_FATAL1(world, LIBRDF_FROM_QUERY, "failed to initialize rasqal");
+ return;
+ }
+
+ /* Make sure rasqal works with the same raptor instance as everyone else.
+ * In ifndef RAPTOR_V2_AVAILABLE case, raptor_world_ptr is NULL
+ * -> ok to use with rasqal_world_set_raptor(),
+ * rasqal should also have been built without RAPTOR_V2_AVAILABLE ->
+ * rasqal_world_open() uses raptor_init() which just increments
+ * the refcount on raptor instance already created in librdf_raptor_init().
+ */
#ifdef RAPTOR_V2_AVAILABLE
- rasqal_world_set_raptor(world->rasqal_world_ptr, world->raptor_world_ptr);
-
- if(rasqal_world_open(world->rasqal_world_ptr)) {
- LIBRDF_FATAL1(world, LIBRDF_FROM_QUERY, "failed to initialize rasqal");
- return;
- }
+ rasqal_world_set_raptor(world->rasqal_world_ptr, world->raptor_world_ptr);
+
+ if(rasqal_world_open(world->rasqal_world_ptr)) {
+ LIBRDF_FATAL1(world, LIBRDF_FROM_QUERY, "failed to initialize rasqal");
+ return;
+ }
#endif
-
+ }
+
+
rasqal_set_triples_source_factory(world->rasqal_world_ptr, rasqal_redland_register_triples_source_factory, world);
/* enumerate from query language 1, so the default parser 0 is done last */
@@ -1448,7 +1453,7 @@ librdf_query_rasqal_constructor(librdf_world *world)
void
librdf_query_rasqal_destructor(librdf_world *world)
{
- if(world->rasqal_world_ptr) {
+ if(world->rasqal_world_ptr && world->rasqal_world_allocated_here) {
rasqal_free_world(world->rasqal_world_ptr);
world->rasqal_world_ptr=NULL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment