summaryrefslogtreecommitdiffstats
path: root/src/sordi.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2011-05-16 22:48:36 +0000
committerDavid Robillard <d@drobilla.net>2011-05-16 22:48:36 +0000
commit9f095d02e31865762cc7964862c85cd33d7ae5cd (patch)
tree8708e5d17e9bd94df9f12ecd7e5d7e92c28ff64b /src/sordi.c
parent754e8db3d586b8f1b0df29f0c7d64da8e9ebf49d (diff)
downloadsord-9f095d02e31865762cc7964862c85cd33d7ae5cd.tar.gz
sord-9f095d02e31865762cc7964862c85cd33d7ae5cd.tar.bz2
sord-9f095d02e31865762cc7964862c85cd33d7ae5cd.zip
Add name parameter to sord_read_file_handle (for error messages).
Support reading from string parameter or stdin in sordi. git-svn-id: http://svn.drobilla.net/sord/trunk@119 3d64ff67-21c5-427c-a301-fe4f08042e5a
Diffstat (limited to 'src/sordi.c')
-rw-r--r--src/sordi.c60
1 files changed, 54 insertions, 6 deletions
diff --git a/src/sordi.c b/src/sordi.c
index 2549549..7db7543 100644
--- a/src/sordi.c
+++ b/src/sordi.c
@@ -46,9 +46,11 @@ print_usage(const char* name, bool error)
{
FILE* const os = error ? stderr : stdout;
fprintf(os, "Usage: %s [OPTION]... INPUT [BASE_URI]\n", name);
- fprintf(os, "Load and re-serialise RDF syntax.\n\n");
+ fprintf(os, "Load and re-serialise 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, " -o SYNTAX Output syntax (`turtle' or `ntriples')\n");
+ fprintf(os, " -s INPUT Parse INPUT as string (terminates options)\n");
fprintf(os, " -v Display version information and exit\n");
return error ? 1 : 0;
}
@@ -67,13 +69,25 @@ main(int argc, char** argv)
return print_usage(argv[0], true);
}
- SerdSyntax output_syntax = SERD_NTRIPLES;
+ FILE* in_fd = NULL;
+ SerdSyntax output_syntax = SERD_NTRIPLES;
+ bool from_file = true;
+ const uint8_t* in_name = NULL;
int a = 1;
for (; a < argc && argv[a][0] == '-'; ++a) {
- if (argv[a][1] == 'h') {
+ if (argv[a][1] == '\0') {
+ in_name = (const uint8_t*)"(stdin)";
+ in_fd = stdin;
+ break;
+ } else if (argv[a][1] == 'h') {
return print_usage(argv[0], false);
} else if (argv[a][1] == 'v') {
return print_version();
+ } else if (argv[a][1] == 's') {
+ in_name = (const uint8_t*)"(string)";
+ from_file = false;
+ ++a;
+ break;
} else if (argv[a][1] == 'o') {
if (++a == argc) {
fprintf(stderr, "Missing value for -o\n");
@@ -94,6 +108,32 @@ main(int argc, char** argv)
}
const uint8_t* input = (const uint8_t*)argv[a++];
+ if (from_file) {
+ 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
+ if (strncmp((const char*)input, "file:", 5)) {
+ fprintf(stderr, "Unsupported URI scheme `%s'\n", input);
+ return 1;
+#ifdef __WIN32__
+ } else if (!strncmp((const char*)input, "file:///", 8)) {
+ input += 8;
+#else
+ } else if (!strncmp((const char*)input, "file://", 7)) {
+ input += 7;
+#endif
+ } else {
+ input += 5;
+ }
+ }
+ in_fd = fopen((const char*)input, "r");
+ if (!in_fd) {
+ fprintf(stderr, "Failed to open file %s\n", input);
+ return 1;
+ }
+ }
+ }
const uint8_t* base_uri_str = NULL;
SerdURI base_uri;
@@ -104,20 +144,28 @@ main(int argc, char** argv)
return 1;
}
base_uri_str = in_base_uri;
- } else { // Use input file URI
+ } 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)) {
fprintf(stderr, "Invalid base URI <%s>\n", base_uri_str);
return 1;
}
-
+
SordWorld* world = sord_world_new();
SordModel* sord = sord_new(world, SORD_SPO|SORD_OPS, false);
SerdEnv* env = serd_env_new();
- bool success = sord_read_file(sord, env, input, base_uri_str, NULL, NULL);
+ bool success = false;
+ if (from_file) {
+ sord_read_file_handle(sord, env, in_fd, in_name,
+ base_uri_str, NULL, NULL);
+ } else {
+ sord_read_string(sord, env, input, base_uri_str);
+ }
fprintf(stderr, "Loaded %zu statements\n", sord_num_quads(sord));