diff options
author | David Robillard <d@drobilla.net> | 2018-05-27 15:32:12 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2022-01-13 23:03:39 -0500 |
commit | e2b1c1de25366f70db4cd00f1bb61eda513eb74b (patch) | |
tree | 68223fdbe052b8149b5ebe5bf5d2a68eeb505ce5 | |
parent | a10fddf0f697e78325ddcfbc71af8f154ffd2a82 (diff) | |
download | serd-e2b1c1de25366f70db4cd00f1bb61eda513eb74b.tar.gz serd-e2b1c1de25366f70db4cd00f1bb61eda513eb74b.tar.bz2 serd-e2b1c1de25366f70db4cd00f1bb61eda513eb74b.zip |
Move syntax name/extension utilities to public API
-rw-r--r-- | include/serd/serd.h | 51 | ||||
-rw-r--r-- | meson.build | 1 | ||||
-rw-r--r-- | src/serdi.c | 57 | ||||
-rw-r--r-- | src/syntax.c | 67 |
4 files changed, 117 insertions, 59 deletions
diff --git a/include/serd/serd.h b/include/serd/serd.h index 57f5af75..766844be 100644 --- a/include/serd/serd.h +++ b/include/serd/serd.h @@ -73,14 +73,6 @@ extern "C" { @{ */ -/// RDF syntax type -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/ -} SerdSyntax; - /// Flags indicating certain string properties relevant to serialisation typedef enum { SERD_HAS_NEWLINE = 1u << 0u, ///< Contains line breaks ('\\n' or '\\r') @@ -309,6 +301,49 @@ typedef size_t (*SerdWriteFunc)(const void* SERD_NONNULL buf, /** @} + @defgroup serd_syntax Syntax Utilities + @{ +*/ + +/// 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/ +} 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. + + @return True for #SERD_NQUADS and #SERD_TRIG, false otherwise. +*/ +SERD_CONST_API +bool +serd_syntax_has_graphs(SerdSyntax syntax); + +/** + @} @defgroup serd_uri URI @{ */ diff --git a/meson.build b/meson.build index 0e1e9471..2708b5a7 100644 --- a/meson.build +++ b/meson.build @@ -98,6 +98,7 @@ sources = [ '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 61fb8f09..e46ef5e5 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 <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* 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) { @@ -223,7 +180,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; @@ -245,7 +202,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; @@ -282,15 +239,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 = @@ -298,7 +253,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..61814709 --- /dev/null +++ b/src/syntax.c @@ -0,0 +1,67 @@ +/* + Copyright 2011-2020 David Robillard <d@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; +} |