diff options
author | David Robillard <d@drobilla.net> | 2011-05-16 22:48:36 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2011-05-16 22:48:36 +0000 |
commit | 9f095d02e31865762cc7964862c85cd33d7ae5cd (patch) | |
tree | 8708e5d17e9bd94df9f12ecd7e5d7e92c28ff64b | |
parent | 754e8db3d586b8f1b0df29f0c7d64da8e9ebf49d (diff) | |
download | sord-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
-rw-r--r-- | sord/sord.h | 1 | ||||
-rw-r--r-- | src/sordi.c | 60 | ||||
-rw-r--r-- | src/syntax.c | 5 |
3 files changed, 58 insertions, 8 deletions
diff --git a/sord/sord.h b/sord/sord.h index d14e64a..62bef78 100644 --- a/sord/sord.h +++ b/sord/sord.h @@ -447,6 +447,7 @@ bool sord_read_file_handle(SordModel* model, SerdEnv* env, FILE* fd, + const uint8_t* name, const uint8_t* base_uri, SordNode* graph, const uint8_t* blank_prefix); 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)); diff --git a/src/syntax.c b/src/syntax.c index 73c5229..d515b47 100644 --- a/src/syntax.c +++ b/src/syntax.c @@ -153,7 +153,7 @@ sord_read_file(SordModel* model, } const bool ret = sord_read_file_handle( - model, env, fd, base_uri, graph, blank_prefix); + model, env, fd, path, base_uri, graph, blank_prefix); fclose(fd); return ret; } @@ -163,6 +163,7 @@ bool sord_read_file_handle(SordModel* model, SerdEnv* env, FILE* fd, + const uint8_t* name, const uint8_t* base_uri_str_in, SordNode* graph, const uint8_t* blank_prefix) @@ -189,7 +190,7 @@ sord_read_file_handle(SordModel* model, serd_reader_set_blank_prefix(state.reader, blank_prefix); } - const SerdStatus ret = serd_reader_read_file(state.reader, fd, base_uri_str); + const SerdStatus ret = serd_reader_read_file(state.reader, fd, name); serd_reader_free(state.reader); free(base_uri_str); |