diff options
Diffstat (limited to 'tools')
-rw-r--r-- | tools/console.c | 70 | ||||
-rw-r--r-- | tools/console.h | 13 | ||||
-rw-r--r-- | tools/serd-pipe.c | 70 |
3 files changed, 112 insertions, 41 deletions
diff --git a/tools/console.c b/tools/console.c index 72b9b222..56464696 100644 --- a/tools/console.c +++ b/tools/console.c @@ -85,6 +85,76 @@ serd_choose_syntax(SerdWorld* const world, return SERD_TRIG; } +SerdStatus +serd_set_input_option(const SerdStringView name, + SerdSyntax* const syntax, + SerdReaderFlags* const flags) +{ + typedef struct { + const char* name; + SerdReaderFlag flag; + } InputOption; + + static const InputOption input_options[] = { + {"lax", SERD_READ_LAX}, + {"variables", SERD_READ_VARIABLES}, + {"verbatim", SERD_READ_VERBATIM}, + {NULL, SERD_READ_LAX}, + }; + + const SerdSyntax named_syntax = serd_syntax_by_name(name.data); + if (!serd_strncasecmp(name.data, "empty", name.length) || + named_syntax != SERD_SYNTAX_EMPTY) { + *syntax = named_syntax; + return SERD_SUCCESS; + } + + for (const InputOption* o = input_options; o->name; ++o) { + if (!serd_strncasecmp(o->name, name.data, name.length)) { + *flags |= o->flag; + return SERD_SUCCESS; + } + } + + return SERD_FAILURE; +} + +SerdStatus +serd_set_output_option(const SerdStringView name, + SerdSyntax* const syntax, + SerdWriterFlags* const flags) +{ + typedef struct { + const char* name; + SerdWriterFlag flag; + } OutputOption; + + static const OutputOption output_options[] = { + {"ascii", SERD_WRITE_ASCII}, + {"expanded", SERD_WRITE_EXPANDED}, + {"lax", SERD_WRITE_LAX}, + {"terse", SERD_WRITE_TERSE}, + {"verbatim", SERD_WRITE_VERBATIM}, + {NULL, SERD_WRITE_ASCII}, + }; + + const SerdSyntax named_syntax = serd_syntax_by_name(name.data); + if (!serd_strncasecmp(name.data, "empty", name.length) || + named_syntax != SERD_SYNTAX_EMPTY) { + *syntax = named_syntax; + return SERD_SUCCESS; + } + + for (const OutputOption* o = output_options; o->name; ++o) { + if (!serd_strncasecmp(o->name, name.data, name.length)) { + *flags |= o->flag; + return SERD_SUCCESS; + } + } + + return SERD_FAILURE; +} + /// Wrapper for getc that is compatible with SerdReadFunc but faster than fread static size_t serd_file_read_byte(void* buf, size_t size, size_t nmemb, void* stream) diff --git a/tools/console.h b/tools/console.h index 29b0a7df..a7e8423f 100644 --- a/tools/console.h +++ b/tools/console.h @@ -7,9 +7,12 @@ #include "serd/env.h" #include "serd/input_stream.h" #include "serd/output_stream.h" +#include "serd/reader.h" #include "serd/status.h" +#include "serd/string_view.h" #include "serd/syntax.h" #include "serd/world.h" +#include "serd/writer.h" #include <stdio.h> @@ -27,6 +30,16 @@ serd_choose_syntax(SerdWorld* world, SerdSyntax requested, const char* filename); +SerdStatus +serd_set_input_option(SerdStringView name, + SerdSyntax* syntax, + SerdReaderFlags* flags); + +SerdStatus +serd_set_output_option(SerdStringView name, + SerdSyntax* syntax, + SerdWriterFlags* flags); + SerdInputStream serd_open_tool_input(const char* filename); diff --git a/tools/serd-pipe.c b/tools/serd-pipe.c index bdb871fa..16abbd2c 100644 --- a/tools/serd-pipe.c +++ b/tools/serd-pipe.c @@ -39,23 +39,20 @@ print_usage(const char* const name, const bool error) "Use - for INPUT to read from standard input.\n\n" " -B BASE_URI Base URI.\n" " -C Convert literals to canonical form.\n" - " -a Write ASCII output.\n" + " -I SYNTAX Input syntax (turtle/ntriples/trig/nquads),\n" + " or flag (lax/variables/verbatim).\n" + " -O SYNTAX Output syntax (empty/turtle/ntriples/nquads),\n" + " or flag (ascii/expanded/verbatim/terse/lax).\n" " -b BYTES I/O block size.\n" " -c PREFIX Chop PREFIX from matching blank node IDs.\n" - " -f Fast and loose URI pass-through.\n" " -h Display this help and exit.\n" - " -i SYNTAX Input syntax: turtle/ntriples/trig/nquads.\n" " -k BYTES Parser stack size.\n" - " -l Lax (non-strict) parsing.\n" - " -o SYNTAX Output syntax: empty/turtle/ntriples/nquads.\n" " -p PREFIX Add PREFIX to blank node IDs.\n" " -q Suppress all output except data.\n" " -r ROOT_URI Keep relative URIs within ROOT_URI.\n" " -s STRING Parse STRING as input.\n" - " -t Write terser output without newlines.\n" " -v Display version information and exit.\n" - " -w FILENAME Write output to FILENAME instead of stdout.\n" - " -x Support parsing variable nodes like \"?x\".\n"; + " -w FILENAME Write output to FILENAME instead of stdout.\n"; FILE* const os = error ? stderr : stdout; fprintf(os, "%s", error ? "\n" : ""); @@ -145,24 +142,12 @@ main(int argc, char** argv) if (opt == 'C') { canonical = true; - } else if (opt == 'a') { - writer_flags |= SERD_WRITE_ASCII; - } else if (opt == 'f') { - writer_flags |= (SERD_WRITE_EXPANDED | SERD_WRITE_VERBATIM); } else if (opt == 'h') { return print_usage(prog, false); - } else if (opt == 'l') { - reader_flags |= SERD_READ_LAX; - writer_flags |= SERD_WRITE_LAX; } else if (opt == 'q') { quiet = true; - } else if (opt == 't') { - writer_flags |= SERD_WRITE_TERSE; } else if (opt == 'v') { return serd_print_version(argv[0]); - } else if (opt == 'x') { - reader_flags |= SERD_READ_VARIABLES; - break; } else if (argv[a][1] == 'B') { if (++a == argc) { return missing_arg(prog, 'B'); @@ -170,6 +155,30 @@ main(int argc, char** argv) base = serd_node_new(NULL, serd_a_uri_string(argv[a])); break; + } else if (opt == 'I') { + if (argv[a][o + 1] || ++a == argc) { + return missing_arg(prog, 'I'); + } + + if (serd_set_input_option( + serd_string(argv[a]), &input_syntax, &reader_flags)) { + return print_usage(argv[0], true); + } + break; + } else if (opt == 'O') { + if (argv[a][o + 1] || ++a == argc) { + return missing_arg(prog, 'O'); + } + + if (serd_set_output_option( + serd_string(argv[a]), &output_syntax, &writer_flags)) { + return print_usage(argv[0], true); + } + + osyntax_set = + output_syntax != SERD_SYNTAX_EMPTY || !strcmp(argv[a], "empty"); + + break; } else if (opt == 'b') { if (argv[a][o + 1] || ++a == argc) { return missing_arg(prog, 'b'); @@ -190,15 +199,6 @@ main(int argc, char** argv) chop_prefix = argv[a]; break; - } else if (opt == 'i') { - if (argv[a][o + 1] || ++a == argc) { - return missing_arg(prog, 'i'); - } - - if (!(input_syntax = serd_syntax_by_name(argv[a]))) { - return print_usage(prog, true); - } - break; } else if (opt == 'k') { if (argv[a][o + 1] || ++a == argc) { return missing_arg(prog, 'k'); @@ -212,18 +212,6 @@ main(int argc, char** argv) } stack_size = (size_t)size; break; - } else if (opt == 'o') { - osyntax_set = true; - if (argv[a][o + 1] || ++a == argc) { - return missing_arg(prog, 'o'); - } - - if (!strcmp(argv[a], "empty")) { - output_syntax = SERD_SYNTAX_EMPTY; - } else if (!(output_syntax = serd_syntax_by_name(argv[a]))) { - return print_usage(argv[0], true); - } - break; } else if (opt == 'p') { if (argv[a][o + 1] || ++a == argc) { return missing_arg(prog, 'p'); |