diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/serdi.c | 56 | ||||
-rw-r--r-- | src/syntax.c | 58 |
2 files changed, 64 insertions, 50 deletions
diff --git a/src/serdi.c b/src/serdi.c index aece6d9d..bcd9cb8d 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -1,10 +1,8 @@ // Copyright 2011-2023 David Robillard <d@drobilla.net> // SPDX-License-Identifier: ISC -#include "string_utils.h" #include "system.h" -#include "serd/attributes.h" #include "serd/env.h" #include "serd/error.h" #include "serd/node.h" @@ -33,46 +31,6 @@ #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* const 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* 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; -} - static int print_version(void) { @@ -236,7 +194,7 @@ main(int argc, char** argv) return missing_arg(prog, 'i'); } - if (!(input_syntax = get_syntax(argv[a]))) { + if (!(input_syntax = serd_syntax_by_name(argv[a]))) { return print_usage(prog, true); } break; @@ -258,7 +216,7 @@ main(int argc, char** argv) return missing_arg(prog, 'o'); } - if (!(output_syntax = get_syntax(argv[a]))) { + if (!(output_syntax = serd_syntax_by_name(argv[a]))) { return print_usage(prog, true); } break; @@ -295,15 +253,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 = choose_style( @@ -311,7 +267,7 @@ main(int argc, char** argv) SerdNode* base = NULL; if (a < argc) { // Base URI given on command line - base = serd_new_uri(serd_string((const char*)argv[a])); + base = serd_new_uri(serd_string(argv[a])); } else if (!from_string && !from_stdin) { // Use input file URI base = serd_new_file_uri(serd_string(input), serd_empty_string()); } 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; +} |