aboutsummaryrefslogtreecommitdiffstats
path: root/src/syntax.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-05-27 15:32:12 +0200
committerDavid Robillard <d@drobilla.net>2018-05-27 21:10:21 +0200
commit7a4582a570657cee6e62842a98a0c7be3da7cb03 (patch)
tree23dbc4451c0764caf387675495f6ea67438b95f4 /src/syntax.c
parentc03c2f77ac51ac12b46408aa6a63a06dc7eefdb8 (diff)
downloadserd-7a4582a570657cee6e62842a98a0c7be3da7cb03.tar.gz
serd-7a4582a570657cee6e62842a98a0c7be3da7cb03.tar.bz2
serd-7a4582a570657cee6e62842a98a0c7be3da7cb03.zip
Move syntax from string/filename utilities to public API
Diffstat (limited to 'src/syntax.c')
-rw-r--r--src/syntax.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/syntax.c b/src/syntax.c
new file mode 100644
index 00000000..f049c417
--- /dev/null
+++ b/src/syntax.c
@@ -0,0 +1,59 @@
+/*
+ Copyright 2011-2018 David Robillard <http://drobilla.net>
+
+ Permission to use, copy, modify, and/or distribute this software for any
+ purpose with or without fee is hereby granted, provided that the above
+ copyright notice and this permission notice appear in all copies.
+
+ THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+
+#include "string_utils.h"
+
+#include "serd/serd.h"
+
+typedef struct {
+ SerdSyntax syntax;
+ const char* name;
+ const char* extension;
+} Syntax;
+
+static const Syntax syntaxes[] = {
+ {SERD_TURTLE, "turtle", ".ttl"},
+ {SERD_NTRIPLES, "ntriples", ".nt"},
+ {SERD_NQUADS, "nquads", ".nq"},
+ {SERD_TRIG, "trig", ".trig"},
+ {(SerdSyntax)0, NULL, NULL}
+};
+
+SerdSyntax
+serd_syntax_by_name(const char* const name)
+{
+ 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) {
+ for (const Syntax* s = syntaxes; s->name; ++s) {
+ if (!serd_strncasecmp(s->extension, ext, strlen(ext))) {
+ return s->syntax;
+ }
+ }
+ }
+ return (SerdSyntax)0;
+}
+