From b58e832527b850eca5e2c5a6f2157d307b0f45b5 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 | 4 ++++ serd/serd.h | 3 ++- src/serdi.c | 44 +++++++++++++++++++++----------------------- src/writer.c | 12 ++++++++---- 5 files changed, 36 insertions(+), 28 deletions(-) diff --git a/NEWS b/NEWS index 5c0a39e6..6e21665a 100644 --- a/NEWS +++ b/NEWS @@ -5,6 +5,7 @@ serd (1.0.1) unstable; * Add extensible logging API * Add extensible logging API * Add model for storing statements in memory + * Add option for writing terse output without newlines * Add support for validation * Bring read/write interface closer to C standard * Make nodes opaque diff --git a/doc/serdi.1 b/doc/serdi.1 index 95d681b0..74ddae3b 100644 --- a/doc/serdi.1 +++ b/doc/serdi.1 @@ -80,6 +80,10 @@ Statements will be written in simple sorted order, which is faster, but may resu .BR \-s " " \fIINPUT\fR Parse \fIINPUT\fR as a string (terminates options). +.TP +.BR \-t +Write terser output without newlines. + .TP .BR \-v Display version information and exit. diff --git a/serd/serd.h b/serd/serd.h index 9b8ab66a..65c1c8b5 100644 --- a/serd/serd.h +++ b/serd/serd.h @@ -263,7 +263,8 @@ typedef struct { typedef enum { SERD_WRITE_ASCII = 1 << 0, ///< Escape all non-ASCII characters SERD_WRITE_UNQUALIFIED = 1 << 1, ///< Do not shorten URIs into CURIEs - SERD_WRITE_UNRESOLVED = 1 << 2 ///< Do not make URIs relative + SERD_WRITE_UNRESOLVED = 1 << 2, ///< Do not make URIs relative + SERD_WRITE_TERSE = 1 << 3 ///< Write terser output without newlines } SerdWriterFlag; /// Bitwise OR of SerdWriterFlag values diff --git a/src/serdi.c b/src/serdi.c index cc7c14cc..7621b41e 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -66,6 +66,7 @@ print_usage(const char* name, bool error) fprintf(os, " -r ROOT_URI Keep relative URIs within ROOT_URI.\n"); fprintf(os, " -S Stream model quickly without inlining.\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; } @@ -84,35 +85,34 @@ main(int argc, char** argv) return print_usage(argv[0], 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 no_inline = false; - bool lax = false; - bool use_model = 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 no_inline = false; + bool lax = false; + bool use_model = 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 && argv[a][0] == '-'; ++a) { if (argv[a][1] == '\0') { from_stdin = true; break; } else if (argv[a][1] == 'a') { - ascii = true; + writer_flags |= SERD_WRITE_ASCII; } else if (argv[a][1] == 'b') { bulk_write = true; } else if (argv[a][1] == 'e') { bulk_read = false; } else if (argv[a][1] == 'f') { - full_uris = true; + writer_flags |= (SERD_WRITE_UNQUALIFIED | SERD_WRITE_UNRESOLVED); } else if (argv[a][1] == 'h') { return print_usage(argv[0], false); } else if (argv[a][1] == 'l') { @@ -129,6 +129,8 @@ main(int argc, char** argv) from_string = true; ++a; break; + } else if (argv[a][1] == 't') { + writer_flags |= SERD_WRITE_TERSE; } else if (argv[a][1] == 'i') { if (++a == argc) { return missing_arg(argv[0], 'i'); @@ -205,10 +207,6 @@ main(int argc, char** argv) SerdWorld* world = serd_world_new(); SerdEnv* env = serd_env_new(base); - const SerdWriterFlags writer_flags = - ((ascii ? SERD_WRITE_ASCII : 0u) | // - (full_uris ? (SERD_WRITE_UNQUALIFIED | SERD_WRITE_UNRESOLVED) : 0u)); - const SerdSerialisationFlags serialisation_flags = no_inline ? SERD_NO_INLINE_OBJECTS : 0u; diff --git a/src/writer.c b/src/writer.c index a0206578..a12f39b9 100644 --- a/src/writer.c +++ b/src/writer.c @@ -413,16 +413,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