Skip to content

Commit

Permalink
Add rdfcount
Browse files Browse the repository at this point in the history
  • Loading branch information
dajobe committed Sep 27, 2013
1 parent 81f945e commit 56b294c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
1 change: 1 addition & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ rdfcat
rdfguess
rdfprint
rdfserialize
rdfcount
8 changes: 7 additions & 1 deletion examples/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
#
#

EXTRA_PROGRAMS = raptor_abort grapper rdfcat rdfprint rdfserialize rdfguess
EXTRA_PROGRAMS = \
raptor_abort grapper \
rdfcat rdfprint rdfserialize rdfguess rdfcount

examples: $(EXTRA_PROGRAMS)

Expand Down Expand Up @@ -57,6 +59,10 @@ rdfguess_SOURCES = rdfguess.c
rdfguess_LDADD=$(top_builddir)/src/libraptor2.la
rdfguess_DEPENDENCIES = $(top_builddir)/src/libraptor2.la

rdfcount_SOURCES = rdfcount.c
rdfcount_LDADD=$(top_builddir)/src/libraptor2.la
rdfcount_DEPENDENCIES = $(top_builddir)/src/libraptor2.la


$(top_builddir)/src/libraptor2.la:
cd $(top_builddir)/src && $(MAKE) libraptor2.la
Expand Down
62 changes: 62 additions & 0 deletions examples/rdfcount.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <stdio.h>
#include <raptor2.h>

/* rdfcount.c: parse any number of RDF/XML files and count the triples */

static void
count_triples(void* user_data, raptor_statement* triple)
{
unsigned int* count_p = (unsigned int*)user_data;
(*count_p)++;
}

int
main(int argc, char *argv[])
{
raptor_world *world = NULL;
const char* program = "rdfcount";
raptor_parser* rdf_parser = NULL;
unsigned int i;
unsigned int count;
unsigned int files_count = 0;
unsigned int total_count = 0;

world = raptor_new_world();

/* just one parser is used and reused here */
rdf_parser = raptor_new_parser(world, "rdfxml");

raptor_parser_set_statement_handler(rdf_parser, &count, count_triples);

for(i = 1; argv[i]; i++) {
const char* filename = argv[i];
unsigned char *uri_string;
raptor_uri *uri, *base_uri;

uri_string = raptor_uri_filename_to_uri_string(filename);
uri = raptor_new_uri(world, uri_string);
base_uri = raptor_uri_copy(uri);

count = 0;
if(!raptor_parser_parse_file(rdf_parser, uri, base_uri)) {
fprintf(stderr, "%s: %s : %d triples\n", program, filename, count);
total_count += count;
files_count++;
} else {
fprintf(stderr, "%s: %s : failed to parse\n", program, filename);
}

raptor_free_uri(base_uri);
raptor_free_uri(uri);
raptor_free_memory(uri_string);
}

raptor_free_parser(rdf_parser);

raptor_free_world(world);

fprintf(stderr, "%s: Total count: %d files %d triples\n",
program, files_count, total_count);

return 0;
}

0 comments on commit 56b294c

Please sign in to comment.