]> git.notmuchmail.org Git - notmuch/blob - sprinter-text.c
debian: update changelog for cleaning changes
[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 (struct sprinter *sp, const char *val)
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     fputs(val, sptxt->stream);
36 }
37
38 static void
39 text_integer (struct sprinter *sp, int val)
40 {
41     struct sprinter_text *sptxt = (struct sprinter_text *) sp;
42
43     fprintf (sptxt->stream, "%d", val);
44 }
45
46 static void
47 text_boolean (struct sprinter *sp, notmuch_bool_t val)
48 {
49     struct sprinter_text *sptxt = (struct sprinter_text *) sp;
50
51     fputs (val ? "true" : "false", sptxt->stream);
52 }
53
54 static void
55 text_separator (struct sprinter *sp)
56 {
57     struct sprinter_text *sptxt = (struct sprinter_text *) sp;
58
59     fputc ('\n', sptxt->stream);
60 }
61
62 static void
63 text_set_prefix (struct sprinter *sp, const char *prefix)
64 {
65     struct sprinter_text *sptxt = (struct sprinter_text *) sp;
66
67     sptxt->current_prefix = prefix;
68 }
69
70 /* The structure functions begin_map, begin_list, end and map_key
71  * don't do anything in the text formatter.
72  */
73
74 static void
75 text_begin_map (unused (struct sprinter *sp))
76 {
77 }
78
79 static void
80 text_begin_list (unused (struct sprinter *sp))
81 {
82 }
83
84 static void
85 text_end (unused (struct sprinter *sp))
86 {
87 }
88
89 static void
90 text_null (unused (struct sprinter *sp))
91 {
92 }
93
94 static void
95 text_map_key (unused (struct sprinter *sp), unused (const char *key))
96 {
97 }
98
99 struct sprinter *
100 sprinter_text_create (const void *ctx, FILE *stream)
101 {
102     static const struct sprinter_text template = {
103         .vtable = {
104             .begin_map = text_begin_map,
105             .begin_list = text_begin_list,
106             .end = text_end,
107             .string = text_string,
108             .integer = text_integer,
109             .boolean = text_boolean,
110             .null = text_null,
111             .map_key = text_map_key,
112             .separator = text_separator,
113             .set_prefix = text_set_prefix,
114             .is_text_printer = TRUE,
115         },
116     };
117     struct sprinter_text *res;
118
119     res = talloc (ctx, struct sprinter_text);
120     if (! res)
121         return NULL;
122
123     *res = template;
124     res->stream = stream;
125     return &res->vtable;
126 }