diff options
author | David Robillard <d@drobilla.net> | 2020-08-14 17:32:01 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2021-03-08 23:23:05 -0500 |
commit | 0daee5d67f5924f260651556a46f0af354fca341 (patch) | |
tree | 2e000a4090e7d9ee2d57b1757e831942a70bc2c2 | |
parent | 2ab2b5f2c7bd4d26aa99eee959f0e5192b3813dc (diff) | |
download | serd-0daee5d67f5924f260651556a46f0af354fca341.tar.gz serd-0daee5d67f5924f260651556a46f0af354fca341.tar.bz2 serd-0daee5d67f5924f260651556a46f0af354fca341.zip |
Add empty syntax type for suppressing output
-rw-r--r-- | doc/serdi.1 | 6 | ||||
-rw-r--r-- | include/serd/serd.h | 17 | ||||
-rw-r--r-- | src/serdi.c | 13 | ||||
-rw-r--r-- | src/syntax.c | 8 | ||||
-rw-r--r-- | src/writer.c | 8 | ||||
-rw-r--r-- | test/meson.build | 8 | ||||
-rwxr-xr-x | test/test_empty.py | 27 |
7 files changed, 70 insertions, 17 deletions
diff --git a/doc/serdi.1 b/doc/serdi.1 index 22ca352f..7cb2c923 100644 --- a/doc/serdi.1 +++ b/doc/serdi.1 @@ -67,6 +67,7 @@ Case is ignored, valid values are: .Dq NQuads , .Dq NTriples , .Dq TriG , +and .Dq Turtle . .Pp .It Fl k Ar bytes @@ -86,10 +87,15 @@ Note that data may be lost when using this option. Write output as .Ar syntax . Case is ignored, valid values are: +.Dq empty , .Dq NQuads , .Dq NTriples , .Dq TriG , +and .Dq Turtle . +When +.Dq empty +is given, output is suppressed, so only errors will be printed. .Pp .It Fl p Ar prefix Add diff --git a/include/serd/serd.h b/include/serd/serd.h index ba1b4ddc..79353e50 100644 --- a/include/serd/serd.h +++ b/include/serd/serd.h @@ -127,10 +127,11 @@ typedef enum { /// 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/ + SERD_SYNTAX_EMPTY = 0, ///< Empty syntax (suppress input or output) + SERD_TURTLE = 1, ///< Terse triples http://www.w3.org/TR/turtle + SERD_NTRIPLES = 2, ///< Flat triples http://www.w3.org/TR/n-triples/ + SERD_NQUADS = 3, ///< Flat quads http://www.w3.org/TR/n-quads/ + SERD_TRIG = 4 ///< Terse quads http://www.w3.org/TR/trig/ } SerdSyntax; /// Flags indicating inline abbreviation information for a statement @@ -461,8 +462,8 @@ serd_byte_sink_free(SerdByteSink* SERD_NULLABLE sink); /** Get a syntax by name. - Case-insensitive, supports "Turtle", "NTriples", "NQuads", and "TriG". Zero - is returned if the name is not recognized. + Case-insensitive, supports "Turtle", "NTriples", "NQuads", and "TriG". + `SERD_SYNTAX_EMPTY` is returned if the name is not recognized. */ SERD_PURE_API SerdSyntax @@ -471,8 +472,8 @@ 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. + This uses the file extension to guess the syntax of a file. + `SERD_SYNTAX_EMPTY` is returned if the extension is not recognized. */ SERD_PURE_API SerdSyntax diff --git a/src/serdi.c b/src/serdi.c index 72b7afe1..e546f463 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -31,6 +31,7 @@ #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__) @@ -95,14 +96,15 @@ main(int argc, char** argv) return print_usage(argv[0], true); } - SerdSyntax input_syntax = (SerdSyntax)0; - SerdSyntax output_syntax = (SerdSyntax)0; + SerdSyntax input_syntax = SERD_SYNTAX_EMPTY; + SerdSyntax output_syntax = SERD_SYNTAX_EMPTY; SerdReaderFlags reader_flags = 0; SerdWriterFlags writer_flags = 0; bool from_string = false; bool from_stdin = false; bool bulk_read = true; bool bulk_write = false; + bool osyntax_set = false; bool quiet = false; size_t stack_size = 4194304; const char* add_prefix = NULL; @@ -158,11 +160,14 @@ main(int argc, char** argv) } stack_size = (size_t)size; } else if (argv[a][1] == 'o') { + osyntax_set = true; if (++a == argc) { return missing_arg(argv[0], 'o'); } - if (!(output_syntax = serd_syntax_by_name(argv[a]))) { + if (!strcmp(argv[a], "empty")) { + output_syntax = SERD_SYNTAX_EMPTY; + } else if (!(output_syntax = serd_syntax_by_name(argv[a]))) { return print_usage(argv[0], true); } } else if (argv[a][1] == 'p') { @@ -206,7 +211,7 @@ main(int argc, char** argv) } const bool input_has_graphs = serd_syntax_has_graphs(input_syntax); - if (!output_syntax) { + if (!output_syntax && !osyntax_set) { output_syntax = input_has_graphs ? SERD_NQUADS : SERD_NTRIPLES; } diff --git a/src/syntax.c b/src/syntax.c index 61814709..59453bca 100644 --- a/src/syntax.c +++ b/src/syntax.c @@ -32,7 +32,7 @@ static const Syntax syntaxes[] = { {"ntriples", ".nt", SERD_NTRIPLES}, {"nquads", ".nq", SERD_NQUADS}, {"trig", ".trig", SERD_TRIG}, - {NULL, NULL, (SerdSyntax)0}, + {NULL, NULL, SERD_SYNTAX_EMPTY}, }; SerdSyntax @@ -43,7 +43,7 @@ serd_syntax_by_name(const char* const name) return s->syntax; } } - return (SerdSyntax)0; + return SERD_SYNTAX_EMPTY; } SerdSyntax @@ -52,12 +52,12 @@ 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))) { + if (s->extension && !serd_strncasecmp(s->extension, ext, strlen(ext))) { return s->syntax; } } } - return (SerdSyntax)0; + return SERD_SYNTAX_EMPTY; } bool diff --git a/src/writer.c b/src/writer.c index 71766d9d..afeee33d 100644 --- a/src/writer.c +++ b/src/writer.c @@ -843,6 +843,8 @@ write_curie(SerdWriter* const writer, if (is_inline_start(writer, field, flags)) { TRY(st, esink(" ;", 2, writer)); } + case SERD_SYNTAX_EMPTY: + break; } return st; } @@ -946,6 +948,10 @@ serd_writer_write_statement(SerdWriter* writer, assert(!((flags & SERD_ANON_S) && (flags & SERD_LIST_S))); assert(!((flags & SERD_ANON_O) && (flags & SERD_LIST_O))); + if (writer->syntax == SERD_SYNTAX_EMPTY) { + return SERD_SUCCESS; + } + SerdStatus st = SERD_SUCCESS; const SerdNode* const subject = serd_statement_subject(statement); const SerdNode* const predicate = serd_statement_predicate(statement); @@ -1093,7 +1099,7 @@ serd_writer_write_statement(SerdWriter* writer, SERD_WARN_UNUSED_RESULT static SerdStatus serd_writer_end_anon(SerdWriter* writer, const SerdNode* node) { - if (writer->syntax == SERD_NTRIPLES || writer->syntax == SERD_NQUADS) { + if (writer->syntax != SERD_TURTLE && writer->syntax != SERD_TRIG) { return SERD_SUCCESS; } diff --git a/test/meson.build b/test/meson.build index 3926888d..49a3fe0c 100644 --- a/test/meson.build +++ b/test/meson.build @@ -107,6 +107,14 @@ if get_option('utils') should_fail: true, suite: ['serdi', 'input']) + # Output + + test('empty', files('test_empty.py'), + args: script_args + [serd_ttl], + suite: 'output') + + # FIXME: Old base URI argument? + # IO errors test('read_dir', serdi, diff --git a/test/test_empty.py b/test/test_empty.py new file mode 100755 index 00000000..a7978e6c --- /dev/null +++ b/test/test_empty.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 + +"""Test writing empty output.""" + +import argparse +import sys +import shlex +import subprocess +import tempfile + +parser = argparse.ArgumentParser(description=__doc__) + +parser.add_argument("--serdi", default="./serdi", help="path to serdi") +parser.add_argument("--wrapper", default="", help="executable wrapper") +parser.add_argument("input", help="valid input file") + +args = parser.parse_args(sys.argv[1:]) +command = shlex.split(args.wrapper) + [args.serdi, "-o", "empty", args.input] + +with tempfile.TemporaryFile() as out: + + proc = subprocess.run(command, check=False, stdout=out) + + out.seek(0, 2) # Seek to end + + assert proc.returncode == 0 + assert out.tell() == 0 |