]> git.notmuchmail.org Git - notmuch/blob - sprinter-text.c
NEWS: discuss changes for dump and restore syntax.
[notmuch] / sprinter-text.c
1 #include <stdbool.h>
2 #include <stdio.h>
3 #include <talloc.h>
4 #include "sprinter.h"
5
6 /* "Structured printer" interface for unstructured text printing.
7  * Note that --output=summary is dispatched and formatted in
8  * notmuch-search.c, the code in this file is only used for all other
9  * output types.
10  */
11
12 struct sprinter_text {
13     struct sprinter vtable;
14     FILE *stream;
15
16     /* The current prefix to be printed with string/integer/boolean
17      * data.
18      */
19     const char *current_prefix;
20
21     /* A flag to indicate if this is the first tag. Used in list of tags
22      * for summary.
23      */
24     notmuch_bool_t first_tag;
25 };
26
27 static void
28 text_string_len (struct sprinter *sp, const char *val, size_t len)
29 {
30     struct sprinter_text *sptxt = (struct sprinter_text *) sp;
31
32     if (sptxt->current_prefix != NULL)
33         fprintf (sptxt->stream, "%s:", sptxt->current_prefix);
34
35     fwrite (val, len, 1, sptxt->stream);
36 }
37
38 static void
39 text_string (struct sprinter *sp, const char *val)
40 {
41     text_string_len (sp, val, strlen (val));
42 }
43
44 static void
45 text_integer (struct sprinter *sp, int val)
46 {
47     struct sprinter_text *sptxt = (struct sprinter_text *) sp;
48
49     fprintf (sptxt->stream, "%d", val);
50 }
51
52 static void
53 text_boolean (struct sprinter *sp, notmuch_bool_t val)
54 {
55     struct sprinter_text *sptxt = (struct sprinter_text *) sp;
56
57     fputs (val ? "true" : "false", sptxt->stream);
58 }
59
60 static void
61 text_separator (struct sprinter *sp)
62 {
63     struct sprinter_text *sptxt = (struct sprinter_text *) sp;
64
65     fputc ('\n', sptxt->stream);
66 }
67
68 static void
69 text_set_prefix (struct sprinter *sp, const char *prefix)
70 {
71     struct sprinter_text *sptxt = (struct sprinter_text *) sp;
72
73     sptxt->current_prefix = prefix;
74 }
75
76 /* The structure functions begin_map, begin_list, end and map_key
77  * don't do anything in the text formatter.
78  */
79
80 static void
81 text_begin_map (unused (struct sprinter *sp))
82 {
83 }
84
85 static void
86 text_begin_list (unused (struct sprinter *sp))
87 {
88 }
89
90 static void
91 text_end (unused (struct sprinter *sp))
92 {
93 }
94
95 static void
96 text_null (unused (struct sprinter *sp))
97 {
98 }
99
100 static void
101 text_map_key (unused (struct sprinter *sp), unused (const char *key))
102 {
103 }
104
105 struct sprinter *
106 sprinter_text_create (const void *ctx, FILE *stream)
107 {
108     static const struct sprinter_text template = {
109         .vtable = {
110             .begin_map = text_begin_map,
111             .begin_list = text_begin_list,
112             .end = text_end,
113             .string = text_string,
114             .string_len = text_string_len,
115             .integer = text_integer,
116             .boolean = text_boolean,
117             .null = text_null,
118             .map_key = text_map_key,
119             .separator = text_separator,
120             .set_prefix = text_set_prefix,
121             .is_text_printer = TRUE,
122         },
123     };
124     struct sprinter_text *res;
125
126     res = talloc (ctx, struct sprinter_text);
127     if (! res)
128         return NULL;
129
130     *res = template;
131     res->stream = stream;
132     return &res->vtable;
133 }