1 Return-Path: <tomi.ollila@iki.fi>
\r
2 X-Original-To: notmuch@notmuchmail.org
\r
3 Delivered-To: notmuch@notmuchmail.org
\r
4 Received: from localhost (localhost [127.0.0.1])
\r
5 by olra.theworths.org (Postfix) with ESMTP id BBB41429E25
\r
6 for <notmuch@notmuchmail.org>; Thu, 12 Jul 2012 03:10:16 -0700 (PDT)
\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org
\r
11 X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]
\r
13 Received: from olra.theworths.org ([127.0.0.1])
\r
14 by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)
\r
15 with ESMTP id GjXzP1qn9fnk for <notmuch@notmuchmail.org>;
\r
16 Thu, 12 Jul 2012 03:10:16 -0700 (PDT)
\r
17 Received: from guru.guru-group.fi (guru.guru-group.fi [46.183.73.34])
\r
18 by olra.theworths.org (Postfix) with ESMTP id ACAC8431FAE
\r
19 for <notmuch@notmuchmail.org>; Thu, 12 Jul 2012 03:10:15 -0700 (PDT)
\r
20 Received: by guru.guru-group.fi (Postfix, from userid 501)
\r
21 id 14508100386; Thu, 12 Jul 2012 13:10:27 +0300 (EEST)
\r
22 From: Tomi Ollila <tomi.ollila@iki.fi>
\r
23 To: craven@gmx.net, notmuch@notmuchmail.org
\r
24 Subject: Re: [PATCH v4 2/3] Add structured output formatter for JSON.
\r
25 In-Reply-To: <1342079004-5300-3-git-send-email-craven@gmx.net>
\r
26 References: <87d34hsdx8.fsf@awakening.csail.mit.edu>
\r
27 <1342079004-5300-1-git-send-email-craven@gmx.net>
\r
28 <1342079004-5300-3-git-send-email-craven@gmx.net>
\r
29 User-Agent: Notmuch/0.13.2+74~g65b26b0 (http://notmuchmail.org) Emacs/23.1.1
\r
30 (x86_64-redhat-linux-gnu)
\r
31 X-Face: HhBM'cA~<r"^Xv\KRN0P{vn'Y"Kd;zg_y3S[4)KSN~s?O\"QPoL
\r
32 $[Xv_BD:i/F$WiEWax}R(MPS`^UaptOGD`*/=@\1lKoVa9tnrg0TW?"r7aRtgk[F
\r
33 !)g;OY^,BjTbr)Np:%c_o'jj,Z
\r
34 Date: Thu, 12 Jul 2012 13:10:27 +0300
\r
35 Message-ID: <m2vchtthm4.fsf@guru.guru-group.fi>
\r
37 Content-Type: text/plain; charset=us-ascii
\r
38 X-BeenThere: notmuch@notmuchmail.org
\r
39 X-Mailman-Version: 2.1.13
\r
41 List-Id: "Use and development of the notmuch mail system."
\r
42 <notmuch.notmuchmail.org>
\r
43 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,
\r
44 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
45 List-Archive: <http://notmuchmail.org/pipermail/notmuch>
\r
46 List-Post: <mailto:notmuch@notmuchmail.org>
\r
47 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
48 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,
\r
49 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
50 X-List-Received-Date: Thu, 12 Jul 2012 10:10:16 -0000
\r
52 On Thu, Jul 12 2012, craven@gmx.net wrote:
\r
54 > Using the new structured printer support in sprinter.h, implement
\r
55 > sprinter_json_new, which returns a new JSON structured output
\r
58 > The formatter prints output similar to the existing JSON, but with
\r
59 > differences in whitespace (mostly newlines).
\r
61 > Makefile.local | 1 +
\r
62 > sprinter.c | 172 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
\r
63 > 2 files changed, 173 insertions(+)
\r
64 > create mode 100644 sprinter.c
\r
66 > diff --git a/Makefile.local b/Makefile.local
\r
67 > index a890df2..8baf0c2 100644
\r
68 > --- a/Makefile.local
\r
69 > +++ b/Makefile.local
\r
70 > @@ -290,6 +290,7 @@ notmuch_client_srcs = \
\r
78 > diff --git a/sprinter.c b/sprinter.c
\r
79 > new file mode 100644
\r
80 > index 0000000..649f79a
\r
84 > +#include <stdbool.h>
\r
85 > +#include <stdio.h>
\r
86 > +#include <talloc.h>
\r
87 > +#include "sprinter.h"
\r
89 > +#define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
\r
91 You're including sprinter.h which includes notmuch-client.h which
\r
92 defines ARRAY_SIZE (Interesting that you did not get error(/warning?)
\r
95 Rest looks good -- except the whitespace -- as I looked through Austin's
\r
96 code yesterday you're just replicated the same lines :D. Easiest
\r
97 to fix is probably just running
\r
98 uncrustify -c devel/uncrustify.cfg --replace sprinter.c
\r
103 > +struct sprinter *
\r
104 > +sprinter_text = NULL;
\r
107 > + * Every below here is the implementation of the JSON printer.
\r
110 > +struct sprinter_json
\r
112 > + struct sprinter vtable;
\r
114 > + /* Top of the state stack, or NULL if the printer is not currently
\r
115 > + * inside any aggregate types. */
\r
116 > + struct json_state *state;
\r
119 > +struct json_state
\r
121 > + struct json_state *parent;
\r
122 > + /* True if nothing has been printed in this aggregate yet.
\r
123 > + * Suppresses the comma before a value. */
\r
124 > + notmuch_bool_t first;
\r
125 > + /* The character that closes the current aggregate. */
\r
129 > +/* Helper function to set up the stream to print a value. If this
\r
130 > + * value follows another value, prints a comma. */
\r
131 > +static struct sprinter_json *
\r
132 > +json_begin_value(struct sprinter *sp)
\r
134 > + struct sprinter_json *spj = (struct sprinter_json*)sp;
\r
135 > + if (spj->state) {
\r
136 > + if (!spj->state->first)
\r
137 > + fputs (", ", spj->stream);
\r
139 > + spj->state->first = false;
\r
144 > +/* Helper function to begin an aggregate type. Prints the open
\r
145 > + * character and pushes a new state frame. */
\r
147 > +json_begin_aggregate(struct sprinter *sp, char open, char close)
\r
149 > + struct sprinter_json *spj = json_begin_value (sp);
\r
150 > + struct json_state *state = talloc (spj, struct json_state);
\r
152 > + fputc (open, spj->stream);
\r
153 > + state->parent = spj->state;
\r
154 > + state->first = true;
\r
155 > + state->close = close;
\r
156 > + spj->state = state;
\r
160 > +json_begin_map(struct sprinter *sp)
\r
162 > + json_begin_aggregate (sp, '{', '}');
\r
166 > +json_begin_list(struct sprinter *sp)
\r
168 > + json_begin_aggregate (sp, '[', ']');
\r
172 > +json_end(struct sprinter *sp)
\r
174 > + struct sprinter_json *spj = (struct sprinter_json*)sp;
\r
175 > + struct json_state *state = spj->state;
\r
177 > + fputc (spj->state->close, spj->stream);
\r
178 > + spj->state = state->parent;
\r
179 > + talloc_free (state);
\r
180 > + if(spj->state == NULL)
\r
181 > + fputc ('\n', spj->stream);
\r
185 > +json_string(struct sprinter *sp, const char *val)
\r
187 > + static const char * const escapes[] = {
\r
188 > + ['\"'] = "\\\"", ['\\'] = "\\\\", ['\b'] = "\\b",
\r
189 > + ['\f'] = "\\f", ['\n'] = "\\n", ['\t'] = "\\t"
\r
191 > + struct sprinter_json *spj = json_begin_value (sp);
\r
192 > + fputc ('"', spj->stream);
\r
193 > + for (; *val; ++val) {
\r
194 > + unsigned char ch = *val;
\r
195 > + if (ch < ARRAY_SIZE(escapes) && escapes[ch])
\r
196 > + fputs (escapes[ch], spj->stream);
\r
197 > + else if (ch >= 32)
\r
198 > + fputc (ch, spj->stream);
\r
200 > + fprintf (spj->stream, "\\u%04x", ch);
\r
202 > + fputc ('"', spj->stream);
\r
206 > +json_integer(struct sprinter *sp, int val)
\r
208 > + struct sprinter_json *spj = json_begin_value (sp);
\r
209 > + fprintf (spj->stream, "%d", val);
\r
213 > +json_boolean(struct sprinter *sp, notmuch_bool_t val)
\r
215 > + struct sprinter_json *spj = json_begin_value (sp);
\r
216 > + fputs (val ? "true" : "false", spj->stream);
\r
220 > +json_null(struct sprinter *sp)
\r
222 > + struct sprinter_json *spj = json_begin_value (sp);
\r
223 > + fputs ("null", spj->stream);
\r
227 > +json_map_key(struct sprinter *sp, const char *key)
\r
229 > + struct sprinter_json *spj = (struct sprinter_json*)sp;
\r
230 > + json_string (sp, key);
\r
231 > + fputs (": ", spj->stream);
\r
232 > + spj->state->first = true;
\r
236 > +json_frame(struct sprinter *sp)
\r
238 > + struct sprinter_json *spj = (struct sprinter_json*)sp;
\r
239 > + fputc ('\n', spj->stream);
\r
242 > +struct sprinter *
\r
243 > +sprinter_json_new(const void *ctx, FILE *stream)
\r
245 > + static const struct sprinter_json template = {
\r
247 > + .begin_map = json_begin_map,
\r
248 > + .begin_list = json_begin_list,
\r
249 > + .end = json_end,
\r
250 > + .string = json_string,
\r
251 > + .integer = json_integer,
\r
252 > + .boolean = json_boolean,
\r
253 > + .null = json_null,
\r
254 > + .map_key = json_map_key,
\r
255 > + .frame = json_frame,
\r
258 > + struct sprinter_json *res;
\r
260 > + res = talloc (ctx, struct sprinter_json);
\r
264 > + *res = template;
\r
265 > + res->stream = stream;
\r
266 > + return &res->vtable;
\r
271 > _______________________________________________
\r
272 > notmuch mailing list
\r
273 > notmuch@notmuchmail.org
\r
274 > http://notmuchmail.org/mailman/listinfo/notmuch
\r