1 Return-Path: <peter.feigl@gmx.at>
\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 69BE2431FC0
\r
6 for <notmuch@notmuchmail.org>; Tue, 10 Jul 2012 11:56:49 -0700 (PDT)
\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org
\r
11 X-Spam-Status: No, score=0.001 tagged_above=-999 required=5
\r
12 tests=[FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001]
\r
14 Received: from olra.theworths.org ([127.0.0.1])
\r
15 by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)
\r
16 with ESMTP id yMOhQ3kgmDbP for <notmuch@notmuchmail.org>;
\r
17 Tue, 10 Jul 2012 11:56:48 -0700 (PDT)
\r
18 Received: from mailout-de.gmx.net (mailout-de.gmx.net [213.165.64.23])
\r
19 by olra.theworths.org (Postfix) with SMTP id 61CE1431FBF
\r
20 for <notmuch@notmuchmail.org>; Tue, 10 Jul 2012 11:56:48 -0700 (PDT)
\r
21 Received: (qmail invoked by alias); 10 Jul 2012 18:56:46 -0000
\r
22 Received: from www.nexoid.at (EHLO mail.nexoid.at) [178.79.130.240]
\r
23 by mail.gmx.net (mp041) with SMTP; 10 Jul 2012 20:56:46 +0200
\r
24 X-Authenticated: #4563876
\r
25 X-Provags-ID: V01U2FsdGVkX18D5ZtgJdoDpVbribBLKxrNVd64ms3Eu0lWmXSckZ
\r
27 Received: from nexoid (localhost [127.0.0.1])
\r
28 by mail.nexoid.at (Postfix) with ESMTP id C9C63E00C
\r
29 for <notmuch@notmuchmail.org>; Tue, 10 Jul 2012 20:56:45 +0200 (CEST)
\r
30 From: <craven@gmx.net>
\r
31 To: notmuch@notmuchmail.org
\r
32 Subject: [PATCH v3 2/3] Adding structured output formatter for JSON.
\r
33 User-Agent: Notmuch/0.11+77~gad6d0d5 (http://notmuchmail.org) Emacs/24.1.50.2
\r
35 Date: Tue, 10 Jul 2012 20:56:45 +0200
\r
36 Message-ID: <87zk77tpg2.fsf@nexoid.at>
\r
38 Content-Type: text/plain; charset=utf-8
\r
39 Content-Transfer-Encoding: quoted-printable
\r
41 X-BeenThere: notmuch@notmuchmail.org
\r
42 X-Mailman-Version: 2.1.13
\r
44 List-Id: "Use and development of the notmuch mail system."
\r
45 <notmuch.notmuchmail.org>
\r
46 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,
\r
47 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
48 List-Archive: <http://notmuchmail.org/pipermail/notmuch>
\r
49 List-Post: <mailto:notmuch@notmuchmail.org>
\r
50 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
51 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,
\r
52 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
53 X-List-Received-Date: Tue, 10 Jul 2012 18:56:49 -0000
\r
55 This formatter prints exactly the same structure as the existing JSON forma=
\r
56 tter, but uses the newly introduced structured formatting primitives.
\r
58 structured-output.c | 159 +++++++++++++++++++++++++++++++++++++++++++++++=
\r
60 structured-output.h | 48 ++++++++++++++++
\r
61 2 files changed, 207 insertions(+)
\r
62 create mode 100644 structured-output.c
\r
64 diff --git a/structured-output.c b/structured-output.c
\r
65 new file mode 100644
\r
66 index 0000000..e10fba4
\r
68 +++ b/structured-output.c
\r
70 +/* notmuch - Not much of an email program, (just index and search)
\r
72 + * Copyright =C2=A9 2009 Carl Worth
\r
74 + * This program is free software: you can redistribute it and/or modify
\r
75 + * it under the terms of the GNU General Public License as published by
\r
76 + * the Free Software Foundation, either version 3 of the License, or
\r
77 + * (at your option) any later version.
\r
79 + * This program is distributed in the hope that it will be useful,
\r
80 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
81 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
82 + * GNU General Public License for more details.
\r
84 + * You should have received a copy of the GNU General Public License
\r
85 + * along with this program. If not, see http://www.gnu.org/licenses/ .
\r
87 + * Author: Carl Worth <cworth@cworth.org>
\r
90 +#include "structured-output.h"
\r
92 +structure_printer_t *
\r
93 +unstructured_text_printer =3D NULL;
\r
95 +structure_printer_t=20
\r
96 +json_structure_printer =3D {
\r
104 + &json_initial_state
\r
107 +enter_level(void *st, const char *marker, int type) {
\r
108 + json_state_t *state =3D (json_state_t*)st;
\r
109 + FILE *output =3D state->output;
\r
110 + if (state->stack !=3D NULL && state->stack->type =3D=3D TYPE_JSON_ARRA=
\r
111 Y && state->stack->first_already_seen) {
\r
112 + fputs(",", output);
\r
113 + if (state->level =3D=3D 1)
\r
114 + fputs("\n", output);
\r
116 + fputs(" ", output);
\r
118 + if (state->stack !=3D NULL) {
\r
119 + state->stack->first_already_seen =3D TRUE;
\r
121 + fputs(marker, output);
\r
122 + void *ctx_json_map =3D talloc_new (0);
\r
123 + json_list_t *el =3D talloc(ctx_json_map, json_list_t);
\r
124 + el->type =3D type;
\r
125 + el->first_already_seen =3D FALSE;
\r
126 + el->rest =3D state->stack;
\r
127 + state->stack =3D el;
\r
128 + return state->level++;
\r
132 +json_map(void *st)
\r
134 + return enter_level(st, "{", TYPE_JSON_MAP);
\r
138 +json_list(void *st)
\r
140 + return enter_level(st, "[", TYPE_JSON_ARRAY);
\r
144 +json_pop(void *st, int level)=20
\r
147 + json_state_t *state =3D (json_state_t*)st;
\r
148 + FILE *output =3D state->output;
\r
149 + for (i =3D state->level; i > level; i--) {
\r
150 + json_list_t *tos =3D state->stack;
\r
151 + if (tos->type =3D=3D TYPE_JSON_MAP) {
\r
152 + fputs("}", output);
\r
154 + if (tos->type =3D=3D TYPE_JSON_ARRAY) {
\r
155 + fputs("]", output);
\r
157 + state->stack =3D tos->rest;
\r
159 + talloc_free(tos);
\r
161 + if (state->level =3D=3D 0)
\r
162 + fputs("\n", output);
\r
166 +json_map_key(void *st, const char *key)
\r
168 + json_state_t *state =3D (json_state_t*)st;
\r
169 + FILE *output =3D state->output;
\r
170 + if (state->stack !=3D NULL && state->stack->first_already_seen) {
\r
171 + fputs(",\n", output);
\r
173 + fputs("\"", output);
\r
174 + fputs(key, output);
\r
175 + fputs("\": ", output);
\r
179 +json_number(void *st, int val)
\r
181 + json_state_t *state =3D (json_state_t*)st;
\r
182 + FILE *output =3D state->output;
\r
183 + if (state->stack !=3D NULL && state->stack->type =3D=3D TYPE_JSON_ARRA=
\r
184 Y && state->stack->first_already_seen) {
\r
185 + fputs(", ", output);
\r
187 + state->stack->first_already_seen =3D TRUE;
\r
188 + fprintf(output, "%i", val);
\r
192 +json_string(void *st, const char *val)
\r
194 + json_state_t *state =3D (json_state_t*)st;
\r
195 + FILE *output =3D state->output;
\r
196 + void *ctx =3D talloc_new(0);
\r
197 + if (state->stack !=3D NULL && state->stack->type =3D=3D TYPE_JSON_ARRA=
\r
198 Y && state->stack->first_already_seen) {
\r
199 + fputs(",", output);
\r
200 + if (state->level =3D=3D 1)
\r
201 + fputs("\n", output);
\r
203 + fputs(" ", output);
\r
206 + state->stack->first_already_seen =3D TRUE;
\r
207 + fprintf(output, "%s", json_quote_str(ctx, val));
\r
208 + talloc_free(ctx);
\r
212 +json_bool(void *st, notmuch_bool_t val)
\r
214 + json_state_t *state =3D (json_state_t*)st;
\r
215 + FILE *output =3D state->output;
\r
217 + fputs("true", output);
\r
219 + fputs("false", output);
\r
223 +json_initial_state(const struct structure_printer *sp, FILE *output)
\r
226 + json_state_t *st =3D talloc(0, json_state_t);
\r
228 + st->stack =3D NULL;
\r
229 + st->output =3D output;
\r
232 diff --git a/structured-output.h b/structured-output.h
\r
233 index b43afe0..cba3882 100644
\r
234 --- a/structured-output.h
\r
235 +++ b/structured-output.h
\r
236 @@ -62,3 +62,51 @@ typedef struct structure_printer {
\r
237 } structure_printer_t;
\r
240 +/* JSON structure printer */
\r
242 +/* single linked list implementation for keeping track of the array/map ne=
\r
245 +typedef struct json_list {
\r
247 + int first_already_seen;
\r
248 + struct json_list *rest;
\r
251 +#define TYPE_JSON_MAP 1
\r
252 +#define TYPE_JSON_ARRAY 2
\r
254 +typedef struct json_state {
\r
256 + json_list_t *stack;
\r
261 +json_map(void *state);
\r
264 +json_list(void *state);
\r
267 +json_pop(void *state, int level);
\r
270 +json_map_key(void *state, const char *key);
\r
273 +json_number(void *state, int val);
\r
276 +json_string(void *state, const char *val);
\r
279 +json_bool(void *state, notmuch_bool_t val);
\r
282 +json_initial_state(const struct structure_printer *sp, FILE *output);
\r
284 +structure_printer_t
\r
285 +json_structure_printer;
\r
287 +structure_printer_t *
\r
288 +unstructured_text_printer;
\r