summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorDave Beckett <dave@dajobe.org>2010-10-09 18:21:30 -0700
committerDave Beckett <dave@dajobe.org>2010-10-09 18:21:30 -0700
commitc5fd0ae6a7f9a471b208d35d8567e7c7e4f67597 (patch)
tree8bfa51f2736bdeee54f285e9358b019cbb144ec6 /examples
parent89e925e41be63aa4b5bf82dd34b9a31f18087a2b (diff)
downloadraptor-c5fd0ae6a7f9a471b208d35d8567e7c7e4f67597.tar.gz
Add example for guessing parser from content in files
Diffstat (limited to 'examples')
-rw-r--r--examples/Makefile.am6
-rw-r--r--examples/rdfguess.c76
2 files changed, 81 insertions, 1 deletions
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 <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <raptor.h>
+
+
+/* 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;
+}