]> git.notmuchmail.org Git - notmuch/blob - sprinter-json.c
build system: remove configure output in Make distclean.
[notmuch] / sprinter-json.c
1 #include <stdbool.h>
2 #include <stdio.h>
3 #include <talloc.h>
4 #include "sprinter.h"
5
6 struct sprinter_json {
7     struct sprinter vtable;
8     FILE *stream;
9     /* Top of the state stack, or NULL if the printer is not currently
10      * inside any aggregate types. */
11     struct json_state *state;
12
13     /* A flag to signify that a separator should be inserted in the
14      * output as soon as possible.
15      */
16     notmuch_bool_t insert_separator;
17 };
18
19 struct json_state {
20     struct json_state *parent;
21     /* True if nothing has been printed in this aggregate yet.
22      * Suppresses the comma before a value. */
23     notmuch_bool_t first;
24     /* The character that closes the current aggregate. */
25     char close;
26 };
27
28 /* Helper function to set up the stream to print a value.  If this
29  * value follows another value, prints a comma. */
30 static struct sprinter_json *
31 json_begin_value (struct sprinter *sp)
32 {
33     struct sprinter_json *spj = (struct sprinter_json *) sp;
34
35     if (spj->state) {
36         if (! spj->state->first) {
37             fputc (',', spj->stream);
38             if (spj->insert_separator) {
39                 fputc ('\n', spj->stream);
40                 spj->insert_separator = FALSE;
41             } else {
42                 fputc (' ', spj->stream);
43             }
44         } else {
45             spj->state->first = FALSE;
46         }
47     }
48     return spj;
49 }
50
51 /* Helper function to begin an aggregate type.  Prints the open
52  * character and pushes a new state frame. */
53 static void
54 json_begin_aggregate (struct sprinter *sp, char open, char close)
55 {
56     struct sprinter_json *spj = json_begin_value (sp);
57     struct json_state *state = talloc (spj, struct json_state);
58
59     fputc (open, spj->stream);
60     state->parent = spj->state;
61     state->first = TRUE;
62     state->close = close;
63     spj->state = state;
64 }
65
66 static void
67 json_begin_map (struct sprinter *sp)
68 {
69     json_begin_aggregate (sp, '{', '}');
70 }
71
72 static void
73 json_begin_list (struct sprinter *sp)
74 {
75     json_begin_aggregate (sp, '[', ']');
76 }
77
78 static void
79 json_end (struct sprinter *sp)
80 {
81     struct sprinter_json *spj = (struct sprinter_json *) sp;
82     struct json_state *state = spj->state;
83
84     fputc (spj->state->close, spj->stream);
85     spj->state = state->parent;
86     talloc_free (state);
87     if (spj->state == NULL)
88         fputc ('\n', spj->stream);
89 }
90
91 static void
92 json_string (struct sprinter *sp, const char *val)
93 {
94     static const char *const escapes[] = {
95         ['\"'] = "\\\"", ['\\'] = "\\\\", ['\b'] = "\\b",
96         ['\f'] = "\\f",  ['\n'] = "\\n",  ['\t'] = "\\t"
97     };
98     struct sprinter_json *spj = json_begin_value (sp);
99
100     fputc ('"', spj->stream);
101     for (; *val; ++val) {
102         unsigned char ch = *val;
103         if (ch < ARRAY_SIZE (escapes) && escapes[ch])
104             fputs (escapes[ch], spj->stream);
105         else if (ch >= 32)
106             fputc (ch, spj->stream);
107         else
108             fprintf (spj->stream, "\\u%04x", ch);
109     }
110     fputc ('"', spj->stream);
111 }
112
113 static void
114 json_integer (struct sprinter *sp, int val)
115 {
116     struct sprinter_json *spj = json_begin_value (sp);
117
118     fprintf (spj->stream, "%d", val);
119 }
120
121 static void
122 json_boolean (struct sprinter *sp, notmuch_bool_t val)
123 {
124     struct sprinter_json *spj = json_begin_value (sp);
125
126     fputs (val ? "true" : "false", spj->stream);
127 }
128
129 static void
130 json_null (struct sprinter *sp)
131 {
132     struct sprinter_json *spj = json_begin_value (sp);
133
134     fputs ("null", spj->stream);
135 }
136
137 static void
138 json_map_key (struct sprinter *sp, const char *key)
139 {
140     struct sprinter_json *spj = (struct sprinter_json *) sp;
141
142     json_string (sp, key);
143     fputs (": ", spj->stream);
144     spj->state->first = TRUE;
145 }
146
147 static void
148 json_set_prefix (unused (struct sprinter *sp), unused (const char *name))
149 {
150 }
151
152 static void
153 json_separator (struct sprinter *sp)
154 {
155     struct sprinter_json *spj = (struct sprinter_json *) sp;
156
157     spj->insert_separator = TRUE;
158 }
159
160 struct sprinter *
161 sprinter_json_create (const void *ctx, FILE *stream)
162 {
163     static const struct sprinter_json template = {
164         .vtable = {
165             .begin_map = json_begin_map,
166             .begin_list = json_begin_list,
167             .end = json_end,
168             .string = json_string,
169             .integer = json_integer,
170             .boolean = json_boolean,
171             .null = json_null,
172             .map_key = json_map_key,
173             .separator = json_separator,
174             .set_prefix = json_set_prefix,
175             .is_text_printer = FALSE,
176         }
177     };
178     struct sprinter_json *res;
179
180     res = talloc (ctx, struct sprinter_json);
181     if (! res)
182         return NULL;
183
184     *res = template;
185     res->stream = stream;
186     return &res->vtable;
187 }