diff options
author | David Robillard <d@drobilla.net> | 2018-05-27 15:32:12 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2023-12-02 16:27:02 -0500 |
commit | 6552a427d194572b4408150512efea17d884d35f (patch) | |
tree | 57cebfc6a08e8af5577c54cf0916745dd5664b45 | |
parent | 58d63871ab308d24624ebfa322301281d85ec07c (diff) | |
download | serd-6552a427d194572b4408150512efea17d884d35f.tar.gz serd-6552a427d194572b4408150512efea17d884d35f.tar.bz2 serd-6552a427d194572b4408150512efea17d884d35f.zip |
Move syntax name/extension utilities to public API
-rw-r--r-- | include/serd/syntax.h | 38 | ||||
-rw-r--r-- | meson.build | 1 | ||||
-rw-r--r-- | src/serdi.c | 56 | ||||
-rw-r--r-- | src/syntax.c | 58 | ||||
-rw-r--r-- | test/meson.build | 1 | ||||
-rw-r--r-- | test/test_syntax.c | 59 |
6 files changed, 158 insertions, 55 deletions
diff --git a/include/serd/syntax.h b/include/serd/syntax.h index 21847438..c3a33ff5 100644 --- a/include/serd/syntax.h +++ b/include/serd/syntax.h @@ -6,6 +6,8 @@ #include "serd/attributes.h" +#include <stdbool.h> + SERD_BEGIN_DECLS /** @@ -14,15 +16,41 @@ SERD_BEGIN_DECLS @{ */ -/// RDF syntax type +/// Syntax supported by serd typedef enum { - SERD_TURTLE = 1, ///< Terse triples http://www.w3.org/TR/turtle - SERD_NTRIPLES = 2, ///< Line-based triples http://www.w3.org/TR/n-triples/ - SERD_NQUADS = 3, ///< Line-based quads http://www.w3.org/TR/n-quads/ - SERD_TRIG = 4, ///< Terse quads http://www.w3.org/TR/trig/ + SERD_TURTLE = 1U, ///< Terse triples http://www.w3.org/TR/turtle + SERD_NTRIPLES = 2U, ///< Line-based triples http://www.w3.org/TR/n-triples/ + SERD_NQUADS = 3U, ///< Line-based quads http://www.w3.org/TR/n-quads/ + SERD_TRIG = 4U, ///< Terse quads http://www.w3.org/TR/trig/ } SerdSyntax; /** + Get a syntax by name. + + Case-insensitive, supports "Turtle", "NTriples", "NQuads", and "TriG". Zero + is returned if the name is not recognized. +*/ +SERD_PURE_API SerdSyntax +serd_syntax_by_name(const char* SERD_NONNULL 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_PURE_API SerdSyntax +serd_guess_syntax(const char* SERD_NONNULL filename); + +/** + Return whether a syntax can represent multiple graphs in one document. + + @return True for #SERD_NQUADS and #SERD_TRIG, false otherwise. +*/ +SERD_CONST_API bool +serd_syntax_has_graphs(SerdSyntax syntax); + +/** @} */ diff --git a/meson.build b/meson.build index 70f8835e..a4b7d168 100644 --- a/meson.build +++ b/meson.build @@ -134,6 +134,7 @@ sources = files( 'src/reader.c', 'src/sink.c', 'src/string.c', + 'src/syntax.c', 'src/system.c', 'src/uri.c', 'src/writer.c', 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; +} diff --git a/test/meson.build b/test/meson.build index ffd65a17..ad3797df 100644 --- a/test/meson.build +++ b/test/meson.build @@ -126,6 +126,7 @@ unit_tests = [ 'reader_writer', 'sink', 'string', + 'syntax', 'uri', 'writer', ] diff --git a/test/test_syntax.c b/test/test_syntax.c new file mode 100644 index 00000000..19f4a463 --- /dev/null +++ b/test/test_syntax.c @@ -0,0 +1,59 @@ +// Copyright 2023 David Robillard <d@drobilla.net> +// SPDX-License-Identifier: ISC + +#undef NDEBUG + +#include "serd/syntax.h" + +#include <assert.h> + +static void +test_syntax_by_name(void) +{ + assert(serd_syntax_by_name("unknown") == (SerdSyntax)0); + assert(serd_syntax_by_name("") == (SerdSyntax)0); + + assert(serd_syntax_by_name("Turtle") == SERD_TURTLE); + assert(serd_syntax_by_name("NTriples") == SERD_NTRIPLES); + assert(serd_syntax_by_name("NQuads") == SERD_NQUADS); + assert(serd_syntax_by_name("TriG") == SERD_TRIG); + + assert(serd_syntax_by_name("turtle") == SERD_TURTLE); + assert(serd_syntax_by_name("ntriples") == SERD_NTRIPLES); + assert(serd_syntax_by_name("nquads") == SERD_NQUADS); + assert(serd_syntax_by_name("trig") == SERD_TRIG); +} + +static void +test_guess_syntax(void) +{ + assert(serd_guess_syntax("file.txt") == (SerdSyntax)0); + assert(serd_guess_syntax("") == (SerdSyntax)0); + assert(serd_guess_syntax("nodot.") == (SerdSyntax)0); + assert(serd_guess_syntax("noext.") == (SerdSyntax)0); + assert(serd_guess_syntax(".hidden") == (SerdSyntax)0); + + assert(serd_guess_syntax("file.ttl") == SERD_TURTLE); + assert(serd_guess_syntax("file.nt") == SERD_NTRIPLES); + assert(serd_guess_syntax("file.nq") == SERD_NQUADS); + assert(serd_guess_syntax("file.trig") == SERD_TRIG); +} + +static void +test_syntax_has_graphs(void) +{ + assert(!serd_syntax_has_graphs((SerdSyntax)0)); + assert(!serd_syntax_has_graphs(SERD_TURTLE)); + assert(!serd_syntax_has_graphs(SERD_NTRIPLES)); + assert(serd_syntax_has_graphs(SERD_NQUADS)); + assert(serd_syntax_has_graphs(SERD_TRIG)); +} + +int +main(void) +{ + test_syntax_by_name(); + test_guess_syntax(); + test_syntax_has_graphs(); + return 0; +} |