From 5142013daed72b5a01944fd9dff17594f23029b2 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 10 Mar 2019 13:21:51 +0100 Subject: Add option for writing terse output without newlines --- NEWS | 1 + doc/serdi.1 | 5 ++++- include/serd/serd.h | 3 ++- src/serdi.c | 40 +++++++++++++++++++--------------------- src/writer.c | 12 ++++++++---- 5 files changed, 34 insertions(+), 27 deletions(-) diff --git a/NEWS b/NEWS index 98526af7..3f677a08 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,7 @@ serd (1.0.1) unstable; * Add SerdBuffer for mutable buffers to keep SerdChunk const-correct * Add SerdWorld for shared library state + * Add option for writing terse output without newlines * Add support for xsd:float and xsd:double literals * Bring read/write interface closer to C standard * Make nodes opaque diff --git a/doc/serdi.1 b/doc/serdi.1 index 55fe6492..b3c852d7 100644 --- a/doc/serdi.1 +++ b/doc/serdi.1 @@ -6,7 +6,7 @@ .Nd read and write RDF syntax .Sh SYNOPSIS .Nm serdi -.Op Fl abefhlqv +.Op Fl abefhlqtv .Op Fl c Ar prefix .Op Fl i Ar syntax .Op Fl k Ar bytes @@ -114,6 +114,9 @@ Parse .Ar string input instead of a file (terminates options). .Pp +.It Fl t +Write terser output without newlines. +.Pp .It Fl v Display version information and exit. .El diff --git a/include/serd/serd.h b/include/serd/serd.h index 06e11966..45af4069 100644 --- a/include/serd/serd.h +++ b/include/serd/serd.h @@ -1592,7 +1592,8 @@ typedef struct SerdWriterImpl SerdWriter; typedef enum { SERD_WRITE_ASCII = 1u << 0u, ///< Escape all non-ASCII characters SERD_WRITE_UNQUALIFIED = 1u << 1u, ///< Do not shorten URIs into CURIEs - SERD_WRITE_UNRESOLVED = 1u << 2u ///< Do not make URIs relative + SERD_WRITE_UNRESOLVED = 1u << 2u, ///< Do not make URIs relative + SERD_WRITE_TERSE = 1u << 3u ///< Write terser output without newlines } SerdWriterFlag; /// Bitwise OR of SerdWriterFlag values diff --git a/src/serdi.c b/src/serdi.c index 4b201237..eb0b0185 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -68,6 +68,7 @@ print_usage(const char* const name, const bool error) fprintf(os, " -q Suppress all output except data.\n"); fprintf(os, " -r ROOT_URI Keep relative URIs within ROOT_URI.\n"); fprintf(os, " -s INPUT Parse INPUT as string (terminates options).\n"); + fprintf(os, " -t Write terser output without newlines.\n"); fprintf(os, " -v Display version information and exit.\n"); return error ? 1 : 0; } @@ -95,21 +96,20 @@ main(int argc, char** argv) return print_usage(prog, true); } - SerdSyntax input_syntax = (SerdSyntax)0; - SerdSyntax output_syntax = (SerdSyntax)0; - bool from_string = false; - bool from_stdin = false; - bool ascii = false; - bool bulk_read = true; - bool bulk_write = false; - bool full_uris = false; - bool lax = false; - bool quiet = false; - size_t stack_size = 4194304; - const char* add_prefix = NULL; - const char* chop_prefix = NULL; - const char* root_uri = NULL; - int a = 1; + SerdSyntax input_syntax = (SerdSyntax)0; + SerdSyntax output_syntax = (SerdSyntax)0; + SerdWriterFlags writer_flags = 0u; + bool from_string = false; + bool from_stdin = false; + bool bulk_read = true; + bool bulk_write = false; + bool lax = false; + bool quiet = false; + size_t stack_size = 4194304; + const char* add_prefix = NULL; + const char* chop_prefix = NULL; + const char* root_uri = NULL; + int a = 1; for (; a < argc && !from_string && argv[a][0] == '-'; ++a) { if (argv[a][1] == '\0') { from_stdin = true; @@ -120,19 +120,21 @@ main(int argc, char** argv) const char opt = argv[a][o]; if (opt == 'a') { - ascii = true; + writer_flags |= SERD_WRITE_ASCII; } else if (opt == 'b') { bulk_write = true; } else if (opt == 'e') { bulk_read = false; } else if (opt == 'f') { - full_uris = true; + writer_flags |= (SERD_WRITE_UNQUALIFIED | SERD_WRITE_UNRESOLVED); } else if (opt == 'h') { return print_usage(prog, false); } else if (opt == 'l') { lax = true; } else if (opt == 'q') { quiet = true; + } else if (opt == 't') { + writer_flags |= SERD_WRITE_TERSE; } else if (opt == 'v') { return print_version(); } else if (opt == 's') { @@ -218,10 +220,6 @@ main(int argc, char** argv) output_syntax = input_has_graphs ? SERD_NQUADS : SERD_NTRIPLES; } - const SerdWriterFlags writer_flags = - ((ascii ? SERD_WRITE_ASCII : 0u) | // - (full_uris ? (SERD_WRITE_UNQUALIFIED | SERD_WRITE_UNRESOLVED) : 0u)); - SerdNode* base = NULL; if (a < argc) { // Base URI given on command line base = serd_new_uri(SERD_STRING(argv[a])); diff --git a/src/writer.c b/src/writer.c index b682f6a8..60746c0a 100644 --- a/src/writer.c +++ b/src/writer.c @@ -474,16 +474,20 @@ uri_sink(const void* buf, size_t size, size_t nmemb, void* stream) static void write_newline(SerdWriter* writer) { - sink("\n", 1, writer); - for (int i = 0; i < writer->indent; ++i) { - sink("\t", 1, writer); + if (writer->flags & SERD_WRITE_TERSE) { + sink(" ", 1, writer); + } else { + sink("\n", 1, writer); + for (int i = 0; i < writer->indent; ++i) { + sink("\t", 1, writer); + } } } static void write_top_level_sep(SerdWriter* writer) { - if (!writer->empty) { + if (!writer->empty && !(writer->flags & SERD_WRITE_TERSE)) { write_newline(writer); } } -- cgit v1.2.1