diff options
author | David Robillard <d@drobilla.net> | 2021-08-01 20:09:38 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2022-01-28 21:57:07 -0500 |
commit | dc01b7e301e91d0d7bfc358f569f4f3849471c52 (patch) | |
tree | 4819b060d20c79e38e1ccab373f3b84a85c0b074 /tools/console.c | |
parent | a8ffb15c4a0566fabb50c94524e881ba2036dd1a (diff) | |
download | serd-dc01b7e301e91d0d7bfc358f569f4f3849471c52.tar.gz serd-dc01b7e301e91d0d7bfc358f569f4f3849471c52.tar.bz2 serd-dc01b7e301e91d0d7bfc358f569f4f3849471c52.zip |
Collapse input and output options into a single flag
Diffstat (limited to 'tools/console.c')
-rw-r--r-- | tools/console.c | 71 |
1 files changed, 71 insertions, 0 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) |