diff options
author | David Robillard <d@drobilla.net> | 2021-08-01 20:09:38 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2023-12-02 18:49:08 -0500 |
commit | fac7901dca0b4821582c6062600d37ac987082d9 (patch) | |
tree | 177f20f43d11e65857be9a6d71a5e27c370212c5 /tools/console.c | |
parent | aa96c067439252b1955cfa11e75948d15f1b1656 (diff) | |
download | serd-fac7901dca0b4821582c6062600d37ac987082d9.tar.gz serd-fac7901dca0b4821582c6062600d37ac987082d9.tar.bz2 serd-fac7901dca0b4821582c6062600d37ac987082d9.zip |
Collapse input and output options into single flags
Diffstat (limited to 'tools/console.c')
-rw-r--r-- | tools/console.c | 70 |
1 files changed, 70 insertions, 0 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) |