]> git.notmuchmail.org Git - notmuch/blob - sprinter-text.c
notmuch-emacs-mua: make --auto-daemon imply --create-frame
[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     if (val == NULL)
42         val = "";
43     text_string_len (sp, val, strlen (val));
44 }
45
46 static void
47 text_integer (struct sprinter *sp, int val)
48 {
49     struct sprinter_text *sptxt = (struct sprinter_text *) sp;
50
51     fprintf (sptxt->stream, "%d", val);
52 }
53
54 static void
55 text_boolean (struct sprinter *sp, notmuch_bool_t val)
56 {
57     struct sprinter_text *sptxt = (struct sprinter_text *) sp;
58
59     fputs (val ? "true" : "false", sptxt->stream);
60 }
61
62 static void
63 text_separator (struct sprinter *sp)
64 {
65     struct sprinter_text *sptxt = (struct sprinter_text *) sp;
66
67     fputc ('\n', sptxt->stream);
68 }
69
70 static void
71 text0_separator (struct sprinter *sp)
72 {
73     struct sprinter_text *sptxt = (struct sprinter_text *) sp;
74
75     fputc ('\0', sptxt->stream);
76 }
77
78 static void
79 text_set_prefix (struct sprinter *sp, const char *prefix)
80 {
81     struct sprinter_text *sptxt = (struct sprinter_text *) sp;
82
83     sptxt->current_prefix = prefix;
84 }
85
86 /* The structure functions begin_map, begin_list, end and map_key
87  * don't do anything in the text formatter.
88  */
89
90 static void
91 text_begin_map (unused (struct sprinter *sp))
92 {
93 }
94
95 static void
96 text_begin_list (unused (struct sprinter *sp))
97 {
98 }
99
100 static void
101 text_end (unused (struct sprinter *sp))
102 {
103 }
104
105 static void
106 text_null (unused (struct sprinter *sp))
107 {
108 }
109
110 static void
111 text_map_key (unused (struct sprinter *sp), unused (const char *key))
112 {
113 }
114
115 struct sprinter *
116 sprinter_text_create (const void *ctx, FILE *stream)
117 {
118     static const struct sprinter_text template = {
119         .vtable = {
120             .begin_map = text_begin_map,
121             .begin_list = text_begin_list,
122             .end = text_end,
123             .string = text_string,
124             .string_len = text_string_len,
125             .integer = text_integer,
126             .boolean = text_boolean,
127             .null = text_null,
128             .map_key = text_map_key,
129             .separator = text_separator,
130             .set_prefix = text_set_prefix,
131             .is_text_printer = TRUE,
132         },
133     };
134     struct sprinter_text *res;
135
136     res = talloc (ctx, struct sprinter_text);
137     if (! res)
138         return NULL;
139
140     *res = template;
141     res->stream = stream;
142     return &res->vtable;
143 }
144
145 struct sprinter *
146 sprinter_text0_create (const void *ctx, FILE *stream)
147 {
148     struct sprinter *sp;
149
150     sp = sprinter_text_create (ctx, stream);
151     if (! sp)
152         return NULL;
153
154     sp->separator = text0_separator;
155
156     return sp;
157 }