Re: Hi all
[notmuch-archives.git] / 9c / 8127e59eb94482cd7fb5d476fdb8d7e82a19f1
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
8 X-Spam-Flag: NO\r
9 X-Spam-Score: 0.001\r
10 X-Spam-Level: \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
13         autolearn=disabled\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
26         MjfNwa30lEc1cA\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
34         (i686-pc-linux-gnu)\r
35 Date: Tue, 10 Jul 2012 20:56:45 +0200\r
36 Message-ID: <87zk77tpg2.fsf@nexoid.at>\r
37 MIME-Version: 1.0\r
38 Content-Type: text/plain; charset=utf-8\r
39 Content-Transfer-Encoding: quoted-printable\r
40 X-Y-GMX-Trusted: 0\r
41 X-BeenThere: notmuch@notmuchmail.org\r
42 X-Mailman-Version: 2.1.13\r
43 Precedence: list\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
54 \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
57 ---\r
58  structured-output.c |  159 +++++++++++++++++++++++++++++++++++++++++++++++=\r
59 ++++\r
60  structured-output.h |   48 ++++++++++++++++\r
61  2 files changed, 207 insertions(+)\r
62  create mode 100644 structured-output.c\r
63 \r
64 diff --git a/structured-output.c b/structured-output.c\r
65 new file mode 100644\r
66 index 0000000..e10fba4\r
67 --- /dev/null\r
68 +++ b/structured-output.c\r
69 @@ -0,0 +1,159 @@\r
70 +/* notmuch - Not much of an email program, (just index and search)\r
71 + *\r
72 + * Copyright =C2=A9 2009 Carl Worth\r
73 + *\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
78 + *\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
83 + *\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
86 + *\r
87 + * Author: Carl Worth <cworth@cworth.org>\r
88 + */\r
89 +\r
90 +#include "structured-output.h"\r
91 +\r
92 +structure_printer_t *\r
93 +unstructured_text_printer =3D NULL;\r
94 +\r
95 +structure_printer_t=20\r
96 +json_structure_printer =3D {\r
97 +    &json_map,\r
98 +    &json_list,\r
99 +    &json_pop,\r
100 +    &json_map_key,\r
101 +    &json_number,\r
102 +    &json_string,\r
103 +    &json_bool,\r
104 +    &json_initial_state\r
105 +};\r
106 +static int\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
115 +       else\r
116 +           fputs(" ", output);\r
117 +    }\r
118 +    if (state->stack !=3D NULL) {\r
119 +       state->stack->first_already_seen =3D TRUE;\r
120 +    }\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
129 +}\r
130 +\r
131 +int\r
132 +json_map(void *st)\r
133 +{\r
134 +    return enter_level(st, "{", TYPE_JSON_MAP);\r
135 +}\r
136 +\r
137 +int\r
138 +json_list(void *st)\r
139 +{\r
140 +    return enter_level(st, "[", TYPE_JSON_ARRAY);\r
141 +}\r
142 +\r
143 +void\r
144 +json_pop(void *st, int level)=20\r
145 +{\r
146 +    int i;\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
153 +       }\r
154 +       if (tos->type =3D=3D TYPE_JSON_ARRAY) {\r
155 +           fputs("]", output);\r
156 +       }\r
157 +       state->stack =3D tos->rest;\r
158 +       state->level--;\r
159 +       talloc_free(tos);\r
160 +    }\r
161 +    if (state->level =3D=3D 0)\r
162 +       fputs("\n", output);\r
163 +}\r
164 +\r
165 +void\r
166 +json_map_key(void *st, const char *key)\r
167 +{\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
172 +    }\r
173 +    fputs("\"", output);\r
174 +    fputs(key, output);\r
175 +    fputs("\": ", output);\r
176 +}\r
177 +\r
178 +void\r
179 +json_number(void *st, int val)\r
180 +{\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
186 +    }\r
187 +    state->stack->first_already_seen =3D TRUE;\r
188 +    fprintf(output, "%i", val);\r
189 +}\r
190 +\r
191 +void\r
192 +json_string(void *st, const char *val)\r
193 +{\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
202 +       else\r
203 +           fputs(" ", output);\r
204 +    }\r
205 +\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
209 +}\r
210 +\r
211 +void\r
212 +json_bool(void *st, notmuch_bool_t val)\r
213 +{\r
214 +    json_state_t *state =3D (json_state_t*)st;\r
215 +    FILE *output =3D state->output;\r
216 +    if (val)\r
217 +       fputs("true", output);\r
218 +    else\r
219 +       fputs("false", output);\r
220 +}\r
221 +\r
222 +void *\r
223 +json_initial_state(const struct structure_printer *sp, FILE *output)\r
224 +{\r
225 +    (void)sp;\r
226 +    json_state_t *st =3D talloc(0, json_state_t);\r
227 +    st->level =3D 0;\r
228 +    st->stack =3D NULL;\r
229 +    st->output =3D output;\r
230 +    return st;\r
231 +}\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
238 =20\r
239 =20\r
240 +/* JSON structure printer */\r
241 +\r
242 +/* single linked list implementation for keeping track of the array/map ne=\r
243 sting state */\r
244 +\r
245 +typedef struct json_list {\r
246 +    int type;\r
247 +    int first_already_seen;\r
248 +    struct json_list *rest;\r
249 +} json_list_t;\r
250 +\r
251 +#define TYPE_JSON_MAP 1\r
252 +#define TYPE_JSON_ARRAY 2\r
253 +\r
254 +typedef struct json_state {\r
255 +    FILE *output;\r
256 +    json_list_t *stack;\r
257 +    int level;\r
258 +} json_state_t;\r
259 +\r
260 +int\r
261 +json_map(void *state);\r
262 +\r
263 +int\r
264 +json_list(void *state);\r
265 +\r
266 +void\r
267 +json_pop(void *state, int level);\r
268 +\r
269 +void\r
270 +json_map_key(void *state, const char *key);\r
271 +\r
272 +void\r
273 +json_number(void *state, int val);\r
274 +\r
275 +void\r
276 +json_string(void *state, const char *val);\r
277 +\r
278 +void\r
279 +json_bool(void *state, notmuch_bool_t val);\r
280 +\r
281 +void *\r
282 +json_initial_state(const struct structure_printer *sp, FILE *output);\r
283 +\r
284 +structure_printer_t\r
285 +json_structure_printer;\r
286 +\r
287 +structure_printer_t *\r
288 +unstructured_text_printer;\r
289 --=20\r
290 1.7.10.4\r
291 \r