diff options
-rw-r--r-- | serd/serd.h | 35 | ||||
-rw-r--r-- | src/serdi.c | 55 | ||||
-rw-r--r-- | src/syntax.c | 67 | ||||
-rw-r--r-- | wscript | 1 |
4 files changed, 108 insertions, 50 deletions
diff --git a/serd/serd.h b/serd/serd.h index ff417b6c..ba143d8d 100644 --- a/serd/serd.h +++ b/serd/serd.h @@ -402,6 +402,41 @@ typedef size_t (*SerdWriteFunc)(const void* buf, /** @} + @name Syntax Utilities + @{ +*/ + +/** + Get a syntax by name. + + Case-insensitive, supports "Turtle", "NTriples", "NQuads", and "TriG". Zero + is returned if the name is not recognized. +*/ +SERD_API +SerdSyntax +serd_syntax_by_name(const char* name); + +/** + Guess a syntax from a filename. + + This uses the file extension to guess the syntax of a file. Zero is + returned if the extension is not recognized. +*/ +SERD_API +SerdSyntax +serd_guess_syntax(const char* filename); + +/** + Return whether a syntax can represent multiple graphs. + + @return True for SERD_NQUADS and SERD_TRIG, false otherwise. +*/ +SERD_API +bool +serd_syntax_has_graphs(SerdSyntax syntax); + +/** + @} @name URI @{ */ diff --git a/src/serdi.c b/src/serdi.c index fcb4274e..71c4e683 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -14,8 +14,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include "serd_config.h" -#include "string_utils.h" #include "system.h" #include "serd/serd.h" @@ -29,51 +27,10 @@ #include <stdbool.h> #include <stdio.h> #include <stdlib.h> -#include <string.h> #define SERDI_ERROR(msg) fprintf(stderr, "serdi: " msg) #define SERDI_ERRORF(fmt, ...) fprintf(stderr, "serdi: " fmt, __VA_ARGS__) -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} -}; - -static SerdSyntax -get_syntax(const char* name) -{ - for (const Syntax* s = syntaxes; s->name; ++s) { - if (!serd_strncasecmp(s->name, name, strlen(name))) { - return s->syntax; - } - } - SERDI_ERRORF("unknown syntax `%s'\n", name); - return (SerdSyntax)0; -} - -static SerdSyntax -guess_syntax(const char* 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; -} - static int print_version(void) { @@ -175,7 +132,7 @@ main(int argc, char** argv) } else if (argv[a][1] == 'i') { if (++a == argc) { return missing_arg(argv[0], 'i'); - } else if (!(input_syntax = get_syntax(argv[a]))) { + } else if (!(input_syntax = serd_syntax_by_name(argv[a]))) { return print_usage(argv[0], true); } } else if (argv[a][1] == 'k') { @@ -192,7 +149,7 @@ main(int argc, char** argv) } else if (argv[a][1] == 'o') { if (++a == argc) { return missing_arg(argv[0], 'o'); - } else if (!(output_syntax = get_syntax(argv[a]))) { + } else if (!(output_syntax = serd_syntax_by_name(argv[a]))) { return print_usage(argv[0], true); } } else if (argv[a][1] == 'p') { @@ -228,15 +185,13 @@ main(int argc, char** argv) const char* input = argv[a++]; - if (!input_syntax && !(input_syntax = guess_syntax(input))) { + if (!input_syntax && !(input_syntax = serd_guess_syntax(input))) { input_syntax = SERD_TRIG; } + const bool input_has_graphs = serd_syntax_has_graphs(input_syntax); if (!output_syntax) { - output_syntax = ( - (input_syntax == SERD_TURTLE || input_syntax == SERD_NTRIPLES) - ? SERD_NTRIPLES - : SERD_NQUADS); + output_syntax = input_has_graphs ? SERD_NQUADS : SERD_NTRIPLES; } SerdNode* base = NULL; diff --git a/src/syntax.c b/src/syntax.c new file mode 100644 index 00000000..8f091a1d --- /dev/null +++ b/src/syntax.c @@ -0,0 +1,67 @@ +/* + Copyright 2011-2020 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" + +#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) +{ + 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; +} + +bool +serd_syntax_has_graphs(const SerdSyntax syntax) +{ + return syntax == SERD_NQUADS || syntax == SERD_TRIG; +} @@ -150,6 +150,7 @@ lib_source = ['src/base64.c', 'src/node.c', 'src/reader.c', 'src/string.c', + 'src/syntax.c', 'src/system.c', 'src/uri.c', 'src/world.c', |