From c5fd0ae6a7f9a471b208d35d8567e7c7e4f67597 Mon Sep 17 00:00:00 2001 From: Dave Beckett Date: Sat, 9 Oct 2010 18:21:30 -0700 Subject: Add example for guessing parser from content in files --- examples/Makefile.am | 6 ++++- examples/rdfguess.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 examples/rdfguess.c (limited to 'examples') diff --git a/examples/Makefile.am b/examples/Makefile.am index c3a60375..91c15244 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -21,7 +21,7 @@ # # -EXTRA_PROGRAMS = raptor_abort grapper rdfcat rdfprint rdfserialize +EXTRA_PROGRAMS = raptor_abort grapper rdfcat rdfprint rdfserialize rdfguess examples: $(EXTRA_PROGRAMS) @@ -53,6 +53,10 @@ rdfserialize_SOURCES = rdfserialize.c rdfserialize_LDADD=$(top_builddir)/src/libraptor2.la rdfserialize_DEPENDENCIES = $(top_builddir)/src/libraptor2.la +rdfguess_SOURCES = rdfguess.c +rdfguess_LDADD=$(top_builddir)/src/libraptor2.la +rdfguess_DEPENDENCIES = $(top_builddir)/src/libraptor2.la + $(top_builddir)/src/libraptor2.la: cd $(top_builddir)/src && $(MAKE) libraptor2.la diff --git a/examples/rdfguess.c b/examples/rdfguess.c new file mode 100644 index 00000000..7875cb8f --- /dev/null +++ b/examples/rdfguess.c @@ -0,0 +1,76 @@ +#include +#include +#include + +#include + + +/* rdfguess.c: guess parser name from filename and its content */ + +#define READ_BUFFER_SIZE 256 + +int +main(int argc, char *argv[]) +{ + raptor_world *world = NULL; + char *buffer[READ_BUFFER_SIZE]; + const char *filename; + int rc = 1; + int i; + + world = raptor_new_world(); + + if(argc < 2) { + fprintf(stderr, "USAGE rdfguess: FILENAMES...\n"); + goto tidy; + } + + for(i = 1; (filename = (const char*)argv[1]); i++) { + raptor_iostream* iostr = NULL; + const char* name; + size_t read_len; + size_t count; + + if(access(filename, R_OK)) { + fprintf(stderr, "rdfguess: %s not found\n", filename); + goto tidy; + } + + iostr = raptor_new_iostream_from_filename(world, filename); + if(!iostr) { + fprintf(stderr, "rdfguess: Could not create iostream for %s\n", filename); + goto tidy; + } + + read_len = READ_BUFFER_SIZE; + count = raptor_iostream_read_bytes(buffer, 1, read_len, iostr); + if(count < 1) { + fprintf(stderr, "rdfguess: Failed to read any data from file %s\n", + filename); + goto tidy; + } + + + name = raptor_world_guess_parser_name(world, + /* uri*/ NULL, + /* mime_type */ NULL, + (const unsigned char*)buffer, + read_len, + /* identifier */ (const unsigned char *)filename); + + if(name) + fprintf(stdout, "rdfguess: %s guessed to be %s\n", filename, name); + else + fprintf(stdout, "rdfguess: failed to guess parser\n"); + } + + rc = 0; + + tidy: + if(iostr) + raptor_free_iostream(iostr); + + raptor_free_world(world); + + return rc; +} -- cgit v1.2.1