aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2021-08-01 20:09:38 -0400
committerDavid Robillard <d@drobilla.net>2022-01-28 21:57:07 -0500
commitdc01b7e301e91d0d7bfc358f569f4f3849471c52 (patch)
tree4819b060d20c79e38e1ccab373f3b84a85c0b074 /tools
parenta8ffb15c4a0566fabb50c94524e881ba2036dd1a (diff)
downloadserd-dc01b7e301e91d0d7bfc358f569f4f3849471c52.tar.gz
serd-dc01b7e301e91d0d7bfc358f569f4f3849471c52.tar.bz2
serd-dc01b7e301e91d0d7bfc358f569f4f3849471c52.zip
Collapse input and output options into a single flag
Diffstat (limited to 'tools')
-rw-r--r--tools/console.c71
-rw-r--r--tools/console.h10
-rw-r--r--tools/serdi.c36
3 files changed, 95 insertions, 22 deletions
diff --git a/tools/console.c b/tools/console.c
index df1bc2ff..339aca29 100644
--- a/tools/console.c
+++ b/tools/console.c
@@ -56,6 +56,77 @@ serd_print_version(const char* const program)
return 0;
}
+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.buf);
+ if (!serd_strncasecmp(name.buf, "empty", name.len) ||
+ 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.buf, name.len)) {
+ *flags |= o->flag;
+ return SERD_SUCCESS;
+ }
+ }
+
+ // SERDI_ERRORF("invalid input option `%s'\n", name.buf);
+ 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},
+ {"verbatim", SERD_WRITE_VERBATIM},
+ {"terse", SERD_WRITE_TERSE},
+ {"lax", SERD_WRITE_LAX},
+ {NULL, SERD_WRITE_ASCII},
+ };
+
+ const SerdSyntax named_syntax = serd_syntax_by_name(name.buf);
+ if (!serd_strncasecmp(name.buf, "empty", name.len) ||
+ 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.buf, name.len)) {
+ *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 31076b24..5d174f1e 100644
--- a/tools/console.h
+++ b/tools/console.h
@@ -24,6 +24,16 @@ serd_set_stream_utf8_mode(FILE* stream);
int
serd_print_version(const char* program);
+SerdStatus
+serd_set_input_option(SerdStringView name,
+ SerdSyntax* syntax,
+ SerdReaderFlags* flags);
+
+SerdStatus
+serd_set_output_option(SerdStringView name,
+ SerdSyntax* syntax,
+ SerdWriterFlags* flags);
+
SerdByteSource*
serd_open_input(const char* filename, size_t block_size);
diff --git a/tools/serdi.c b/tools/serdi.c
index f46cab48..db1cfde0 100644
--- a/tools/serdi.c
+++ b/tools/serdi.c
@@ -45,24 +45,22 @@ print_usage(const char* const name, const bool error)
" -F PATTERN Filter out statements that match PATTERN.\n"
" -G PATTERN Only include statements matching PATTERN.\n"
" -I BASE_URI Input base URI.\n"
- " -a Write ASCII output if possible.\n"
" -b BYTES I/O block size.\n"
" -c PREFIX Chop PREFIX from matching blank node IDs.\n"
" -f Fast and loose mode (possibly ugly output).\n"
" -h Display this help and exit.\n"
- " -i SYNTAX Input syntax: turtle/ntriples/trig/nquads.\n"
+ " -i SYNTAX Input syntax (turtle/ntriples/trig/nquads),\n"
+ " or flag (lax/variables/verbatim).\n"
" -k BYTES Parser stack size.\n"
- " -l Lax (non-strict) parsing.\n"
" -m Build a model in memory before writing.\n"
- " -o SYNTAX Output syntax: empty/turtle/ntriples/nquads.\n"
+ " -o SYNTAX Output syntax (empty/turtle/ntriples/nquads),\n"
+ " or flag (ascii/expanded/verbatim/terse/lax).\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" : "");
@@ -207,26 +205,17 @@ main(int argc, char** argv)
if (opt == 'C') {
canonical = true;
- } else if (opt == 'a') {
- writer_flags |= SERD_WRITE_ASCII;
} else if (opt == 'f') {
no_inline = true;
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 (argv[a][1] == 'm') {
use_model = true;
} 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;
} else if (argv[a][1] == 'F') {
if (++a == argc) {
return missing_arg(argv[0], 'F');
@@ -273,8 +262,9 @@ main(int argc, char** argv)
return missing_arg(prog, 'i');
}
- if (!(input_syntax = serd_syntax_by_name(argv[a]))) {
- return print_usage(prog, true);
+ if (serd_set_input_option(
+ SERD_STRING(argv[a]), &input_syntax, &reader_flags)) {
+ return print_usage(argv[0], true);
}
break;
} else if (opt == 'k') {
@@ -291,16 +281,18 @@ 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]))) {
+ 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 == 'p') {
if (argv[a][o + 1] || ++a == argc) {