From 7018614174d735f6a62144fd2fd1197628cbfa5a Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 5 May 2019 15:42:40 +0200 Subject: Add empty syntax type for suppressing output --- doc/serdi.1 | 2 +- serd/serd.h | 21 +++++++++++---------- src/serdi.c | 11 ++++++++--- src/syntax.c | 9 +++++---- src/writer.c | 13 +++++++++---- wscript | 6 ++++++ 6 files changed, 40 insertions(+), 22 deletions(-) diff --git a/doc/serdi.1 b/doc/serdi.1 index 11718dae..3e1d2ebd 100644 --- a/doc/serdi.1 +++ b/doc/serdi.1 @@ -65,7 +65,7 @@ performance and memory consumption. .TP .BR \-o " " \fISYNTAX\fR Write output as \fISYNTAX\fR. -Valid values (case-insensitive): \*(lqturtle\*(rq, \*(lqntriples\*(rq, \*(lqtrig\*(rq, \*(lqnquads\*(rq +Valid values (case-insensitive): \*(lqturtle\*(rq, \*(lqntriples\*(rq, \*(lqtrig\*(rq, \*(lqnquads\*(rq, , \*(lqempty\*(rq. When \*(lqempty\*(rq is given, output is suppressed, so only errors will be printed. .TP .BR \-p " " \fIPREFIX\fR diff --git a/serd/serd.h b/serd/serd.h index 462732ce..838b1f11 100644 --- a/serd/serd.h +++ b/serd/serd.h @@ -117,10 +117,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 @@ -471,8 +472,8 @@ serd_byte_sink_free(SerdByteSink* 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_API SerdSyntax @@ -481,8 +482,8 @@ serd_syntax_by_name(const char* 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_API SerdSyntax @@ -491,8 +492,8 @@ serd_guess_syntax(const char* filename); /** 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_API bool diff --git a/src/serdi.c b/src/serdi.c index 865ef599..81b4dc1f 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -29,6 +29,7 @@ #include #include #include +#include #define SERDI_ERROR(msg) fprintf(stderr, "serdi: " msg); #define SERDI_ERRORF(fmt, ...) fprintf(stderr, "serdi: " fmt, __VA_ARGS__); @@ -96,14 +97,15 @@ main(int argc, char** argv) } SerdNode* base = NULL; - 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_stdin = false; bool bulk_read = true; bool bulk_write = false; bool no_inline = false; + bool osyntax_set = false; bool use_model = false; bool quiet = false; size_t stack_size = 4194304; @@ -165,8 +167,11 @@ 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'); + } else 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); } @@ -208,7 +213,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 a4e9f5da..e672d9d7 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,13 @@ 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 2e1e817b..5a82933e 100644 --- a/src/writer.c +++ b/src/writer.c @@ -683,6 +683,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; } @@ -741,7 +743,7 @@ write_node(SerdWriter* writer, break; case SERD_BLANK: st = write_blank(writer, node, field, flags); - default: break; + break; } return st; } @@ -812,6 +814,10 @@ serd_writer_write_statement(SerdWriter* writer, SerdStatementFlags flags, const SerdStatement* statement) { + if (writer->syntax == SERD_SYNTAX_EMPTY) { + return SERD_SUCCESS; + } + const SerdNode* const subject = serd_statement_get_subject(statement); const SerdNode* const predicate = serd_statement_get_predicate(statement); const SerdNode* const object = serd_statement_get_object(statement); @@ -944,10 +950,9 @@ serd_writer_write_statement(SerdWriter* writer, } SERD_WARN_UNUSED_RESULT static SerdStatus -serd_writer_end_anon(SerdWriter* writer, - const SerdNode* node) +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; } else if (serd_stack_is_empty(&writer->anon_stack)) { return SERD_LOG_ERROR(writer->world, SERD_ERR_UNKNOWN, diff --git a/wscript b/wscript index 69c6ed12..d2279769 100644 --- a/wscript +++ b/wscript @@ -538,9 +538,15 @@ def test(tst): check([serdi, '-h']) check([serdi, '-k', '512', '-s', ' a <#Thingie> .']) check([serdi, os.devnull]) + with tempfile.TemporaryFile(mode='r') as stdin: check([serdi, '-'], stdin=stdin) + with tempfile.TemporaryFile(mode='w') as stdout: + check([serdi, '-o', 'empty', '%s/serd.ttl' % srcdir], stdout=stdout) + stdout.seek(0, 2) # Seek to end + check(lambda: stdout.tell() == 0, name='empty output') + with tst.group('BadCommands', expected=1, stderr=autowaf.NONEMPTY) as check: check([serdi]) check([serdi, '/no/such/file']) -- cgit v1.2.1