]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
doveadm-print: Fix doveadm_print_json_print_stream
authorAki Tuomi <aki.tuomi@dovecot.fi>
Tue, 9 Feb 2016 08:03:56 +0000 (10:03 +0200)
committerTimo Sirainen <timo.sirainen@dovecot.fi>
Wed, 10 Feb 2016 12:00:25 +0000 (14:00 +0200)
src/doveadm/doveadm-print-json.c

index a929d27ec0799855858ad24832705b4c487e7ab1..fbfe86ee3347b85e29d7fc757389e3ce317e19e8 100644 (file)
@@ -12,6 +12,8 @@
 struct doveadm_print_json_context {
        unsigned int header_idx, header_count;
        bool first_row;
+       bool in_stream;
+       bool flushed;
        ARRAY(struct doveadm_print_header) headers;
        pool_t pool;
        string_t *str;
@@ -28,6 +30,7 @@ static void doveadm_print_json_init(void)
        ctx.str = str_new(ctx.pool, 256);
        p_array_init(&ctx.headers, ctx.pool, 1);
        ctx.first_row = TRUE;
+       ctx.in_stream = FALSE;
 }
 
 static void
@@ -94,25 +97,23 @@ static void doveadm_print_json_print(const char *value)
 static void
 doveadm_print_json_print_stream(const unsigned char *value, size_t size)
 {
-       if (size == 0) {
-               doveadm_print_json_print("");
-               return;
+       if (!ctx.in_stream) {
+               const struct doveadm_print_header *hdr =
+                       array_idx(&ctx.headers, ctx.header_idx);
+               doveadm_print_json_value_header(hdr);
+               i_assert((hdr->flags & DOVEADM_PRINT_HEADER_FLAG_NUMBER) == 0);
+               str_append_c(ctx.str, '"');
+               ctx.in_stream = TRUE;
        }
 
-       const struct doveadm_print_header *hdr = array_idx(&ctx.headers, ctx.header_idx);
-
-       doveadm_print_json_value_header(hdr);
-
-       if ((hdr->flags & DOVEADM_PRINT_HEADER_FLAG_NUMBER) != 0) {
-               i_assert(str_is_float((const char*)value, (char)value[size]));
-               str_append_data(ctx.str, value, size);
-       } else {
-               str_append_c(ctx.str, '"');
-               json_append_escaped_data(ctx.str, value, size);
+       if (size == 0) {
                str_append_c(ctx.str, '"');
+               doveadm_print_json_value_footer();
+               ctx.in_stream = FALSE;
+               return;
        }
 
-       doveadm_print_json_value_footer();
+       json_append_escaped_data(ctx.str, value, size);
 
        if (str_len(ctx.str) >= IO_BLOCK_SIZE)
                doveadm_print_json_flush_internal();
@@ -126,6 +127,10 @@ static void doveadm_print_json_flush_internal(void)
 
 static void doveadm_print_json_flush(void)
 {
+       if (ctx.flushed)
+               return;
+       ctx.flushed = TRUE;
+
        if (ctx.first_row == FALSE)
                str_append_c(ctx.str,']');
        else {