diff options
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) |