From ef758859f54c37e90deb419fa4e58a9609ea1999 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 27 May 2018 15:32:12 +0200 Subject: Move syntax name/extension utilities to public API --- src/serdi.c | 57 ++++++--------------------------------------------------- 1 file changed, 6 insertions(+), 51 deletions(-) (limited to 'src/serdi.c') diff --git a/src/serdi.c b/src/serdi.c index d2828ea2..628ae004 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -15,7 +15,6 @@ */ #include "serd_config.h" -#include "string_utils.h" #include "system.h" #include "serd/serd.h" @@ -32,52 +31,10 @@ #include #include #include -#include #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 SERD_PURE_FUNC -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) { @@ -213,7 +170,7 @@ main(int argc, char** argv) return missing_arg(argv[0], 'i'); } - if (!(input_syntax = get_syntax(argv[a]))) { + if (!(input_syntax = serd_syntax_by_name(argv[a]))) { return print_usage(argv[0], true); } } else if (argv[a][1] == 'k') { @@ -232,7 +189,7 @@ main(int argc, char** argv) return missing_arg(argv[0], 'o'); } - if (!(output_syntax = get_syntax(argv[a]))) { + if (!(output_syntax = serd_syntax_by_name(argv[a]))) { return print_usage(argv[0], true); } } else if (argv[a][1] == 'p') { @@ -271,15 +228,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; } const SerdWriterFlags writer_flags = @@ -287,7 +242,7 @@ main(int argc, char** argv) SerdNode* base = NULL; if (a < argc) { // Base URI given on command line - base = serd_new_uri(SERD_MEASURE_STRING((const char*)argv[a])); + base = serd_new_uri(SERD_MEASURE_STRING(argv[a])); } else if (!from_string && !from_stdin) { // Use input file URI base = serd_new_file_uri(SERD_MEASURE_STRING(input), SERD_EMPTY_STRING()); } -- cgit v1.2.1