Skip to content

Instantly share code, notes, and snippets.

@abargnesi
Created October 26, 2015 02:02
Show Gist options
  • Save abargnesi/8402baf1a019a2179e70 to your computer and use it in GitHub Desktop.
Save abargnesi/8402baf1a019a2179e70 to your computer and use it in GitHub Desktop.
Load an RDF file into a SQLite database without journaling.
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <librdf.h>
#include <raptor2.h>
/* function prototypes */
int main(int argc, char *argv[]);
int main(int argc, char *argv[]) {
librdf_uri* uri;
librdf_world* world;
librdf_storage* storage;
librdf_parser* parser;
librdf_stream *stream;
librdf_model* model;
char *extfind;
char *parser_name = NULL;
char *storage_name = NULL;
if (argc != 3) {
fprintf(stderr, "usage: %s <rdf content URI> <name>\n", argv[0]);
return(1);
}
// open world
world = librdf_new_world();
librdf_world_open(world);
// assign uri for content
uri = librdf_new_uri(world, (const unsigned char*) argv[1]);
// assign storage name
storage_name = argv[2];
if (!storage_name) {
fprintf(stderr, "storage name was not set\n");
return 1;
}
extfind = strstr(argv[1], ".ttl");
if (extfind != NULL) {
parser_name = "turtle";
} else {
extfind = strstr(argv[1], ".nt");
if (extfind != NULL) {
parser_name = "ntriples";
}
}
if (!parser_name) {
fprintf(stderr, "uri was not turtle or ntriples\n");
return 1;
}
fprintf(stdout, "%s\n", extfind);
// create storage
storage = librdf_new_storage(world, "sqlite", storage_name, "new='yes',synchronous='off'");
if (!storage) {
fprintf(stderr, "could not create sqlite storage");
return(1);
}
// create model
model = librdf_new_model(world, storage, NULL);
// create parser
parser = librdf_new_parser(world, parser_name, NULL, NULL);
stream = librdf_parser_parse_as_stream(parser, uri, NULL);
librdf_model_add_statements(model, stream);
//librdf_parser_parse_into_model(parser, uri, NULL, model);
// free up
librdf_free_parser(parser);
librdf_free_model(model);
librdf_free_uri(uri);
librdf_free_world(world);
return 0;
}
@mro
Copy link

mro commented Oct 26, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment