diff options
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; +} |