aboutsummaryrefslogtreecommitdiffstats
path: root/src/serdi.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2011-05-18 02:00:03 +0000
committerDavid Robillard <d@drobilla.net>2011-05-18 02:00:03 +0000
commitb92d598a22fdad8c96a1167362d4bb79015af006 (patch)
treebf4768c8b4357aafbee9218806f1fe80156dc356 /src/serdi.c
parent88e6e9a333e99c304451b7755117928a213913ef (diff)
downloadserd-b92d598a22fdad8c96a1167362d4bb79015af006.tar.gz
serd-b92d598a22fdad8c96a1167362d4bb79015af006.tar.bz2
serd-b92d598a22fdad8c96a1167362d4bb79015af006.zip
Add test to ensure blank node IDs don't clash with generated IDs.
Add handle destructor parameter to serd_reader_new. Add serd_reader_get_handle. Rename serd_reader_set_blank_prefix to serd_reader_add_blank_prefix. Rename serd_reader_read_file to serd_reader_read_file_handle. Add new serd_reader_read_file that takes a path/URI parameter. Add serdi -i option to select input syntax. Add serdi -p and -c options to add/chop a prefix to/from blank IDs. Add optional base_uri parameter to serd_env_new. Add serd_writer_chop_blank_prefix. Bump version to 0.3.0. git-svn-id: http://svn.drobilla.net/serd/trunk@183 490d8e77-9747-427b-9fa3-0b8f29cee8a0
Diffstat (limited to 'src/serdi.c')
-rw-r--r--src/serdi.c94
1 files changed, 66 insertions, 28 deletions
diff --git a/src/serdi.c b/src/serdi.c
index bc7c9d18..833e92d2 100644
--- a/src/serdi.c
+++ b/src/serdi.c
@@ -45,8 +45,11 @@ print_usage(const char* name, bool error)
fprintf(os, "Read and write RDF syntax.\n");
fprintf(os, "Use - for INPUT to read from standard input.\n\n");
fprintf(os, " -h Display this help and exit\n");
+ fprintf(os, " -i SYNTAX Input syntax (`turtle' or `ntriples')\n");
fprintf(os, " -o SYNTAX Output syntax (`turtle' or `ntriples')\n");
fprintf(os, " -s INPUT Parse INPUT as string (terminates options)\n");
+ fprintf(os, " -p PREFIX Add PREFIX to blank node IDs\n");
+ fprintf(os, " -c PREFIX Chop PREFIX from matching blank node IDs\n");
fprintf(os, " -v Display version information and exit\n");
return error ? 1 : 0;
}
@@ -58,6 +61,20 @@ file_sink(const void* buf, size_t len, void* stream)
return fwrite(buf, 1, len, file);
}
+bool
+set_syntax(SerdSyntax* syntax, const char* name)
+{
+ if (!strcmp(name, "turtle")) {
+ *syntax = SERD_TURTLE;
+ } else if (!strcmp(name, "ntriples")) {
+ *syntax = SERD_NTRIPLES;
+ } else {
+ fprintf(stderr, "Unknown input format `%s'\n", name);
+ return false;
+ }
+ return true;
+}
+
int
main(int argc, char** argv)
{
@@ -65,14 +82,17 @@ main(int argc, char** argv)
return print_usage(argv[0], true);
}
- FILE* in_fd = NULL;
- SerdSyntax output_syntax = SERD_NTRIPLES;
- bool from_file = true;
- const char* in_name = NULL;
+ FILE* in_fd = NULL;
+ SerdSyntax input_syntax = SERD_TURTLE;
+ SerdSyntax output_syntax = SERD_NTRIPLES;
+ bool from_file = true;
+ const uint8_t* in_name = NULL;
+ const uint8_t* add_prefix = NULL;
+ const uint8_t* chop_prefix = NULL;
int a = 1;
for (; a < argc && argv[a][0] == '-'; ++a) {
if (argv[a][1] == '\0') {
- in_name = "(stdin)";
+ in_name = (const uint8_t*)"(stdin)";
in_fd = stdin;
break;
} else if (argv[a][1] == 'h') {
@@ -80,23 +100,38 @@ main(int argc, char** argv)
} else if (argv[a][1] == 'v') {
return print_version();
} else if (argv[a][1] == 's') {
- in_name = "(string)";
+ in_name = (const uint8_t*)"(string)";
from_file = false;
++a;
break;
+ } else if (argv[a][1] == 'i') {
+ if (++a == argc) {
+ fprintf(stderr, "Missing value for -i\n");
+ return 1;
+ }
+ if (!set_syntax(&input_syntax, argv[a])) {
+ return 1;
+ }
} else if (argv[a][1] == 'o') {
if (++a == argc) {
fprintf(stderr, "Missing value for -o\n");
return 1;
}
- if (!strcmp(argv[a], "turtle")) {
- output_syntax = SERD_TURTLE;
- } else if (!strcmp(argv[a], "ntriples")) {
- output_syntax = SERD_NTRIPLES;
- } else {
- fprintf(stderr, "Unknown output format `%s'\n", argv[a]);
+ if (!set_syntax(&output_syntax, argv[a])) {
return 1;
}
+ } else if (argv[a][1] == 'p') {
+ if (++a == argc) {
+ fprintf(stderr, "Missing value for -p\n");
+ return 1;
+ }
+ add_prefix = (const uint8_t*)argv[a];
+ } else if (argv[a][1] == 'c') {
+ if (++a == argc) {
+ fprintf(stderr, "Missing value for -c\n");
+ return 1;
+ }
+ chop_prefix = (const uint8_t*)argv[a];
} else {
fprintf(stderr, "Unknown option `%s'\n", argv[a]);
return print_usage(argv[0], true);
@@ -105,7 +140,7 @@ main(int argc, char** argv)
const uint8_t* input = (const uint8_t*)argv[a++];
if (from_file) {
- in_name = in_name ? in_name : (const char*)input;
+ in_name = in_name ? in_name : input;
if (!in_fd) {
if (serd_uri_string_has_scheme(input)) {
// INPUT is an absolute URI, ensure it a file and chop scheme
@@ -132,27 +167,25 @@ main(int argc, char** argv)
}
const uint8_t* base_uri_str = NULL;
- SerdURI base_uri;
if (a < argc) { // Base URI given on command line
- const uint8_t* const in_base_uri = (const uint8_t*)argv[a];
- if (serd_uri_parse((const uint8_t*)in_base_uri, &base_uri)) {
- fprintf(stderr, "Invalid base URI <%s>\n", argv[2]);
- return 1;
- }
- base_uri_str = in_base_uri;
+ base_uri_str = (const uint8_t*)argv[a];
} else if (from_file) { // Use input file URI
base_uri_str = input;
} else {
base_uri_str = (const uint8_t*)"";
}
- if (serd_uri_parse(base_uri_str, &base_uri)) {
+ SerdURI base_uri = SERD_URI_NULL;
+ SerdNode base_uri_node = serd_node_new_uri_from_string(
+ base_uri_str, &base_uri, &base_uri);
+
+ if (!base_uri_node.buf) {
fprintf(stderr, "Invalid base URI <%s>\n", base_uri_str);
return 1;
}
FILE* out_fd = stdout;
- SerdEnv* env = serd_env_new();
+ SerdEnv* env = serd_env_new(&base_uri_node);
SerdStyle output_style = SERD_STYLE_RESOLVED;
if (output_syntax == SERD_NTRIPLES) {
@@ -161,24 +194,28 @@ main(int argc, char** argv)
output_style |= SERD_STYLE_ABBREVIATED;
}
- SerdNode base_uri_node = serd_node_from_string(SERD_URI, base_uri_str);
- serd_env_set_base_uri(env, &base_uri_node);
- serd_env_get_base_uri(env, &base_uri);
-
SerdWriter* writer = serd_writer_new(
output_syntax, output_style, env, &base_uri, file_sink, out_fd);
+ if (chop_prefix) {
+ serd_writer_chop_blank_prefix(writer, chop_prefix);
+ }
+
State state = { env, writer };
SerdReader* reader = serd_reader_new(
- SERD_TURTLE, state.writer,
+ input_syntax, state.writer, NULL,
(SerdBaseSink)serd_writer_set_base_uri,
(SerdPrefixSink)serd_writer_set_prefix,
(SerdStatementSink)serd_writer_write_statement,
(SerdEndSink)serd_writer_end_anon);
+ if (add_prefix) {
+ serd_reader_add_blank_prefix(reader, add_prefix);
+ }
+
const SerdStatus status = (from_file)
- ? serd_reader_read_file(reader, in_fd, (const uint8_t*)in_name)
+ ? serd_reader_read_file_handle(reader, in_fd, in_name)
: serd_reader_read_string(reader, input);
serd_reader_free(reader);
@@ -190,6 +227,7 @@ main(int argc, char** argv)
serd_writer_finish(state.writer);
serd_writer_free(state.writer);
serd_env_free(state.env);
+ serd_node_free(&base_uri_node);
return (status == SERD_SUCCESS) ? 0 : 1;
}