diff options
author | David Robillard <d@drobilla.net> | 2018-05-27 15:32:12 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2023-12-02 16:27:02 -0500 |
commit | 6552a427d194572b4408150512efea17d884d35f (patch) | |
tree | 57cebfc6a08e8af5577c54cf0916745dd5664b45 /src/syntax.c | |
parent | 58d63871ab308d24624ebfa322301281d85ec07c (diff) | |
download | serd-6552a427d194572b4408150512efea17d884d35f.tar.gz serd-6552a427d194572b4408150512efea17d884d35f.tar.bz2 serd-6552a427d194572b4408150512efea17d884d35f.zip |
Move syntax name/extension utilities to public API
Diffstat (limited to 'src/syntax.c')
-rw-r--r-- | src/syntax.c | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/syntax.c b/src/syntax.c new file mode 100644 index 00000000..1599df08 --- /dev/null +++ b/src/syntax.c @@ -0,0 +1,58 @@ +// Copyright 2011-2020 David Robillard <d@drobilla.net> +// SPDX-License-Identifier: ISC + +#include "string_utils.h" + +#include "serd/syntax.h" + +#include <stdbool.h> +#include <string.h> + +typedef struct { + const char* name; + const char* extension; + SerdSyntax syntax; +} Syntax; + +static const Syntax syntaxes[] = { + {"turtle", ".ttl", SERD_TURTLE}, + {"ntriples", ".nt", SERD_NTRIPLES}, + {"nquads", ".nq", SERD_NQUADS}, + {"trig", ".trig", SERD_TRIG}, + {NULL, NULL, (SerdSyntax)0}, +}; + +SerdSyntax +serd_syntax_by_name(const char* const name) +{ + const size_t len = strlen(name); + if (len) { + for (const Syntax* s = syntaxes; s->name; ++s) { + if (!serd_strncasecmp(s->name, name, strlen(name))) { + return s->syntax; + } + } + } + return (SerdSyntax)0; +} + +SerdSyntax +serd_guess_syntax(const char* const filename) +{ + const char* ext = strrchr(filename, '.'); + if (ext && ext[1]) { + const size_t ext_len = strlen(ext); + for (const Syntax* s = syntaxes; s->name; ++s) { + if (!serd_strncasecmp(s->extension, ext, ext_len)) { + return s->syntax; + } + } + } + return (SerdSyntax)0; +} + +bool +serd_syntax_has_graphs(const SerdSyntax syntax) +{ + return syntax == SERD_NQUADS || syntax == SERD_TRIG; +} |