aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2019-03-10 13:21:51 +0100
committerDavid Robillard <d@drobilla.net>2020-06-21 18:12:04 +0200
commit14c2faeed5d835450cbc081e32d60e22186c3766 (patch)
treea82ebde7f90182e3157dc73447f96c9da5c1fb82
parentdf9c9c409dcf8f1637b127734fbe680d429215d9 (diff)
downloadserd-14c2faeed5d835450cbc081e32d60e22186c3766.tar.gz
serd-14c2faeed5d835450cbc081e32d60e22186c3766.tar.bz2
serd-14c2faeed5d835450cbc081e32d60e22186c3766.zip
Add option for writing terse output without newlines
-rw-r--r--NEWS1
-rw-r--r--doc/serdi.14
-rw-r--r--serd/serd.h1
-rw-r--r--src/n3.c2
-rw-r--r--src/serdi.c39
-rw-r--r--src/writer.c12
6 files changed, 35 insertions, 24 deletions
diff --git a/NEWS b/NEWS
index cf50f430..5bcd4ee2 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 8e07e273..3736c1b7 100644
--- a/doc/serdi.1
+++ b/doc/serdi.1
@@ -80,6 +80,10 @@ Keep relative URIs within \fIROOT_URI\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 d8cba126..67ecf1db 100644
--- a/serd/serd.h
+++ b/serd/serd.h
@@ -261,6 +261,7 @@ typedef struct {
*/
typedef enum {
SERD_WRITE_ASCII = 1 << 0, ///< Escape all non-ASCII characters
+ SERD_WRITE_TERSE = 1 << 1, ///< Write terser output without newlines
} SerdWriterFlag;
/// Bitwise OR of SerdWriterFlag values
diff --git a/src/n3.c b/src/n3.c
index 467a6ffd..7e64b0f0 100644
--- a/src/n3.c
+++ b/src/n3.c
@@ -1068,7 +1068,7 @@ read_object(SerdReader* reader, ReadContext* ctx, bool emit, bool* ate_dot)
if (!emit) {
ctx->object = o;
- } else if (!ret && emit && simple && o) {
+ } else if (!ret && simple && o) {
ret = emit_statement(reader, *ctx, o);
serd_stack_pop_to(&reader->stack, orig_stack_size);
}
diff --git a/src/serdi.c b/src/serdi.c
index 8f4b9d64..384fc1c7 100644
--- a/src/serdi.c
+++ b/src/serdi.c
@@ -66,6 +66,7 @@ print_usage(const char* name, 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;
}
@@ -84,28 +85,28 @@ 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 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 = 0;
+ 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') {
@@ -126,6 +127,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');
@@ -202,8 +205,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);
-
const SerdSerialisationFlags serialisation_flags =
no_inline ? SERD_NO_INLINE_OBJECTS : 0U;
diff --git a/src/writer.c b/src/writer.c
index fcd0e507..8f155743 100644
--- a/src/writer.c
+++ b/src/writer.c
@@ -412,16 +412,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);
}
}