/* functions implementing storage api */ static int librdf_storage_tstore_init(librdf_storage* storage, const char *name, librdf_hash* options) { librdf_storage_tstore_instance* context=(librdf_storage_tstore_instance*)LIBRDF_CALLOC( librdf_storage_tstore_instance, 1, sizeof(librdf_storage_tstore_instance)); if(!context) { if(options) librdf_free_hash(options); return 1; } librdf_storage_set_instance(storage, context); context->host=librdf_hash_get_del(options, "host"); context->db=librdf_hash_get_del(options, "database"); context->user=librdf_hash_get_del(options, "user"); context->password=librdf_hash_get_del(options, "password"); context->model=librdf_hash_get_del(options, "model"); /* no more options, might as well free them now */ if(options) librdf_free_hash(options); return 0; }
/* functions implementing storage api */ static int librdf_storage_file_init(librdf_storage* storage, const char *name, librdf_hash* options) { char *name_copy; char *contexts; int rc = 1; int is_uri = !strcmp(storage->factory->name, "uri"); const char *format_name = (is_uri ? "guess" : "rdfxml"); librdf_storage_file_instance* context; context = LIBRDF_CALLOC(librdf_storage_file_instance*, 1, sizeof(*context)); if(!context) goto done; librdf_storage_set_instance(storage, context); /* Cannot save contexts in a file; pass everything else on */ contexts = librdf_hash_get_del(options, "contexts"); if(contexts) LIBRDF_FREE(char*, contexts); context->format_name = librdf_hash_get_del(options, "format"); if(context->format_name) { /* for 'file' and 'uri' storage, check this is a valid parser * for 'file' storage, also check this is a valid serializer */ if(!librdf_parser_check_name(storage->world, context->format_name) || (!is_uri && !librdf_serializer_check_name(storage->world, context->format_name))) { librdf_log(storage->world, 0, LIBRDF_LOG_WARN, LIBRDF_FROM_STORAGE, NULL, "Ignoring storage %s format option '%s' - using default format '%s'", storage->factory->name, context->format_name, format_name); LIBRDF_FREE(char*, context->format_name); context->format_name = NULL; } if(context->format_name) format_name = context->format_name; }
/* functions implementing storage api */ static int librdf_storage_file_init(librdf_storage* storage, const char *name, librdf_hash* options) { char *name_copy; char *contexts; int rc = 1; int is_uri = !strcmp(storage->factory->name, "uri"); const char *format_name = (is_uri ? "guess" : "rdfxml"); librdf_storage_file_instance* context; context = (librdf_storage_file_instance*)LIBRDF_CALLOC(librdf_storage_file_instance, 1, sizeof(librdf_storage_file_instance)); if(!context) goto done; librdf_storage_set_instance(storage, context); /* Cannot save contexts in a file; pass everything else on */ contexts = librdf_hash_get_del(options, "contexts"); if(contexts) LIBRDF_FREE(cstring, contexts); context->format_name = librdf_hash_get_del(options, "format"); if(context->format_name) { /* for 'file' and 'uri' storage, check this is a valid parser * for 'file' storage, also check this is a valid serializer */ if(!librdf_parser_check_name(storage->world, context->format_name) || (!is_uri && !librdf_serializer_check_name(storage->world, context->format_name))) { librdf_log(storage->world, 0, LIBRDF_LOG_WARN, LIBRDF_FROM_STORAGE, NULL, "Ignoring storage %s format option '%s' - using default format '%s'", storage->factory->name, context->format_name, format_name); LIBRDF_FREE(cstring, context->format_name); context->format_name = NULL; } if(context->format_name) format_name = context->format_name; } if(is_uri) context->uri = librdf_new_uri(storage->world, (const unsigned char*)name); else { context->name_len = strlen(name); name_copy = (char*)LIBRDF_MALLOC(cstring, context->name_len+1); if(!name_copy) goto done; strcpy(name_copy,name); context->name = name_copy; context->uri = librdf_new_uri_from_filename(storage->world, context->name); } context->storage = librdf_new_storage_with_options(storage->world, NULL, NULL, options); if(!context->storage) goto done; context->model = librdf_new_model(storage->world, context->storage, NULL); if(!context->model) goto done; if(is_uri || !access((const char*)context->name, F_OK)) { librdf_parser *parser; parser = librdf_new_parser(storage->world, format_name, NULL, NULL); if(!parser) { rc = 1; goto done; } librdf_parser_parse_into_model(parser, context->uri, NULL, context->model); librdf_free_parser(parser); } context->changed = 0; rc = 0; done: /* no more options, might as well free them now */ if(options) librdf_free_hash(options); return rc; }