chiark / gitweb /
More general error handling for sinks
[disorder] / lib / sink.c
1 /*
2  * This file is part of DisOrder
3  * Copyright (C) 2004, 2007-9, 2013 Richard Kettlewell
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 /** @file lib/sink.c
19  * @brief Abstract output sink type
20  */
21
22 #include "common.h"
23
24 #include <stdarg.h>
25 #include <errno.h>
26
27 #include "mem.h"
28 #include "vector.h"
29 #include "log.h"
30 #include "sink.h"
31 #include "printf.h"
32
33 /** @brief Formatted output to a sink
34  * @param s Sink to write to
35  * @param fmt Format string
36  * @param ap Argument list
37  * @return Number of bytes written on success, -1 on error
38  */
39 int sink_vprintf(struct sink *s, const char *fmt, va_list ap) {
40   return byte_vsinkprintf(s, fmt, ap);
41 }
42
43 /** @brief Formatted output to a sink
44  * @param s Sink to write to
45  * @param fmt Format string
46  * @return Number of bytes written on success, -1 on error
47  */
48 int sink_printf(struct sink *s, const char *fmt, ...) {
49   va_list ap;
50   int n;
51
52   va_start(ap, fmt);
53   n = byte_vsinkprintf(s, fmt, ap);
54   va_end(ap);
55   return n;
56 }
57
58 static int sink_generic_flush(struct sink attribute((unused)) *s) {
59   return 0;
60 }
61
62 static int sink_generic_error(struct sink attribute((unused)) *s) {
63   return 0;
64 }
65
66 /* stdio sink *****************************************************************/
67
68 /** @brief Sink that writes to a stdio @c FILE */
69 struct stdio_sink {
70   /** @brief Base member */
71   struct sink s;
72
73   /** @brief Filename */
74   const char *name;
75
76   /** @brief Stream to write to */
77   FILE *fp;
78
79   int error;
80 };
81
82 /** @brief Reinterpret a @ref sink as a @ref stdio_sink */
83 #define S(s) ((struct stdio_sink *)s)
84
85 /** @brief Write callback for @ref stdio_sink */
86 static int sink_stdio_write(struct sink *s, const void *buffer, int nbytes) {
87   int n = fwrite(buffer, 1, nbytes, S(s)->fp);
88   if(n < nbytes) {
89     S(s)->error = errno;
90     if(S(s)->name)
91       disorder_fatal(errno, "error writing to %s", S(s)->name);
92     else
93       return -1;
94   }
95   return n;
96 }
97
98 static int sink_stdio_error(struct sink *s) {
99   return S(s)->error;
100 }
101
102 /** @brief Create a sink that writes to a stdio stream
103  * @param name Filename for use in error messages
104  * @param fp Stream to write to
105  * @return Pointer to new sink
106  */
107 struct sink *sink_stdio(const char *name, FILE *fp) {
108   struct stdio_sink *s = xmalloc(sizeof *s);
109
110   s->s.write = sink_stdio_write;
111   s->s.flush = sink_generic_flush;
112   s->s.error = sink_stdio_error;
113   s->s.eclass = ec_errno;
114   s->name = name;
115   s->fp = fp;
116   return (struct sink *)s;
117 }
118
119 /* dynstr sink ****************************************************************/
120
121 /** @brief Sink that writes to a dynamic string */
122 struct dynstr_sink {
123   /** @brief Base member */
124   struct sink s;
125   /** @brief Pointer to dynamic string to append to */
126   struct dynstr *d;
127 };
128
129 /** @brief Write callback for @ref dynstr_sink */
130 static int sink_dynstr_write(struct sink *s, const void *buffer, int nbytes) {
131   dynstr_append_bytes(((struct dynstr_sink *)s)->d, buffer, nbytes);
132   return nbytes;
133 }
134
135 /** @brief Create a sink that appends to a @ref dynstr
136  * @param output Dynamic string to append to
137  * @return Pointer to new sink
138  */
139 struct sink *sink_dynstr(struct dynstr *output) {
140   struct dynstr_sink *s = xmalloc(sizeof *s);
141
142   s->s.write = sink_dynstr_write;
143   s->s.flush = sink_generic_flush;
144   s->s.error = sink_generic_error;
145   s->s.eclass = ec_errno;
146   s->d = output;
147   return (struct sink *)s;
148 }
149
150 /* discard sink **************************************************************/
151
152 static int sink_discard_write(struct sink attribute((unused)) *s,
153                               const void attribute((unused)) *buffer,
154                               int nbytes) {
155   return nbytes;
156 }
157
158 /** @brief Return a sink which discards all output */
159 struct sink *sink_discard(void) {
160   struct sink *s = xmalloc(sizeof *s);
161
162   s->write = sink_discard_write;
163   s->flush = sink_generic_flush;
164   s->error = sink_generic_error;
165   s->eclass = ec_errno;
166   return s;
167 }
168
169 /* error sink **************************************************************/
170
171 static int sink_error_write(struct sink attribute((unused)) *s,
172                             const void attribute((unused)) *buffer,
173                             int attribute((unused)) nbytes) {
174   return -1;
175 }
176
177 /** @brief Return a sink which discards all output */
178 struct sink *sink_error(void) {
179   struct sink *s = xmalloc(sizeof *s);
180
181   s->write = sink_error_write;
182   s->flush = sink_generic_flush;
183   s->error = sink_generic_error;
184   s->eclass = ec_errno;
185   return s;
186 }
187
188 /*
189 Local Variables:
190 c-basic-offset:2
191 comment-column:40
192 End:
193 */