chiark / gitweb /
disorder.h: more consistent approach to function attributes
[disorder] / lib / sink.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder
902b9f3f 3 * Copyright (C) 2004, 2007-9, 2013 Richard Kettlewell
460b9539 4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
460b9539 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
460b9539 8 * (at your option) any later version.
e7eb3a27
RK
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 *
460b9539 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
460b9539 17 */
6049fe0e
RK
18/** @file lib/sink.c
19 * @brief Abstract output sink type
20 */
460b9539 21
05b75f8d 22#include "common.h"
460b9539 23
460b9539 24#include <stdarg.h>
460b9539 25#include <errno.h>
26
27#include "mem.h"
28#include "vector.h"
59ef69ba 29#include "socketio.h"
460b9539 30#include "log.h"
902b9f3f 31#include "sink.h"
460b9539 32#include "printf.h"
33
6049fe0e
RK
34/** @brief Formatted output to a sink
35 * @param s Sink to write to
36 * @param fmt Format string
37 * @param ap Argument list
38 * @return Number of bytes written on success, -1 on error
39 */
460b9539 40int sink_vprintf(struct sink *s, const char *fmt, va_list ap) {
41 return byte_vsinkprintf(s, fmt, ap);
42}
43
6049fe0e
RK
44/** @brief Formatted output to a sink
45 * @param s Sink to write to
46 * @param fmt Format string
47 * @return Number of bytes written on success, -1 on error
48 */
460b9539 49int sink_printf(struct sink *s, const char *fmt, ...) {
50 va_list ap;
51 int n;
52
53 va_start(ap, fmt);
54 n = byte_vsinkprintf(s, fmt, ap);
55 va_end(ap);
56 return n;
57}
58
902b9f3f
RK
59static int sink_generic_flush(struct sink attribute((unused)) *s) {
60 return 0;
61}
62
63static int sink_generic_error(struct sink attribute((unused)) *s) {
64 return 0;
65}
66
460b9539 67/* stdio sink *****************************************************************/
68
6049fe0e 69/** @brief Sink that writes to a stdio @c FILE */
460b9539 70struct stdio_sink {
6049fe0e 71 /** @brief Base member */
460b9539 72 struct sink s;
6049fe0e
RK
73
74 /** @brief Filename */
460b9539 75 const char *name;
6049fe0e
RK
76
77 /** @brief Stream to write to */
460b9539 78 FILE *fp;
902b9f3f
RK
79
80 int error;
460b9539 81};
82
6049fe0e 83/** @brief Reinterpret a @ref sink as a @ref stdio_sink */
460b9539 84#define S(s) ((struct stdio_sink *)s)
85
6049fe0e 86/** @brief Write callback for @ref stdio_sink */
460b9539 87static int sink_stdio_write(struct sink *s, const void *buffer, int nbytes) {
88 int n = fwrite(buffer, 1, nbytes, S(s)->fp);
89 if(n < nbytes) {
902b9f3f 90 S(s)->error = errno;
460b9539 91 if(S(s)->name)
2e9ba080 92 disorder_fatal(errno, "error writing to %s", S(s)->name);
460b9539 93 else
94 return -1;
95 }
96 return n;
97}
98
902b9f3f
RK
99static int sink_stdio_error(struct sink *s) {
100 return S(s)->error;
101}
102
6049fe0e
RK
103/** @brief Create a sink that writes to a stdio stream
104 * @param name Filename for use in error messages
105 * @param fp Stream to write to
106 * @return Pointer to new sink
107 */
460b9539 108struct sink *sink_stdio(const char *name, FILE *fp) {
109 struct stdio_sink *s = xmalloc(sizeof *s);
110
111 s->s.write = sink_stdio_write;
902b9f3f
RK
112 s->s.flush = sink_generic_flush;
113 s->s.error = sink_stdio_error;
114 s->s.eclass = ec_errno;
460b9539 115 s->name = name;
116 s->fp = fp;
117 return (struct sink *)s;
118}
119
120/* dynstr sink ****************************************************************/
121
6049fe0e 122/** @brief Sink that writes to a dynamic string */
460b9539 123struct dynstr_sink {
6049fe0e 124 /** @brief Base member */
460b9539 125 struct sink s;
6049fe0e 126 /** @brief Pointer to dynamic string to append to */
460b9539 127 struct dynstr *d;
128};
129
6049fe0e 130/** @brief Write callback for @ref dynstr_sink */
460b9539 131static int sink_dynstr_write(struct sink *s, const void *buffer, int nbytes) {
132 dynstr_append_bytes(((struct dynstr_sink *)s)->d, buffer, nbytes);
133 return nbytes;
134}
135
6049fe0e
RK
136/** @brief Create a sink that appends to a @ref dynstr
137 * @param output Dynamic string to append to
138 * @return Pointer to new sink
139 */
460b9539 140struct sink *sink_dynstr(struct dynstr *output) {
141 struct dynstr_sink *s = xmalloc(sizeof *s);
142
143 s->s.write = sink_dynstr_write;
902b9f3f
RK
144 s->s.flush = sink_generic_flush;
145 s->s.error = sink_generic_error;
146 s->s.eclass = ec_errno;
460b9539 147 s->d = output;
148 return (struct sink *)s;
149}
150
0d0253c9
RK
151/* discard sink **************************************************************/
152
153static int sink_discard_write(struct sink attribute((unused)) *s,
154 const void attribute((unused)) *buffer,
155 int nbytes) {
156 return nbytes;
157}
158
159/** @brief Return a sink which discards all output */
160struct sink *sink_discard(void) {
161 struct sink *s = xmalloc(sizeof *s);
162
163 s->write = sink_discard_write;
902b9f3f
RK
164 s->flush = sink_generic_flush;
165 s->error = sink_generic_error;
166 s->eclass = ec_errno;
0d0253c9
RK
167 return s;
168}
169
681cb9fb
RK
170/* error sink **************************************************************/
171
172static int sink_error_write(struct sink attribute((unused)) *s,
173 const void attribute((unused)) *buffer,
174 int attribute((unused)) nbytes) {
175 return -1;
176}
177
178/** @brief Return a sink which discards all output */
179struct sink *sink_error(void) {
180 struct sink *s = xmalloc(sizeof *s);
181
182 s->write = sink_error_write;
902b9f3f
RK
183 s->flush = sink_generic_flush;
184 s->error = sink_generic_error;
185 s->eclass = ec_errno;
681cb9fb
RK
186 return s;
187}
188
59ef69ba
RK
189/* socket sink *************************************************************/
190
191/** @brief Sink that writes to a socket handle */
192struct socketio_sink {
193 /** @brief Base member */
194 struct sink s;
195
196 struct socketio *sio;
197};
198
199static int sink_socketio_flush(struct sink *s) {
200 struct socketio_sink *ss = (struct socketio_sink *)s;
201 return socketio_flush(ss->sio);
202}
203
204/** @brief Write callback for @ref stdio_sink */
205static int sink_socketio_write(struct sink *s, const void *buffer, int nbytes) {
206 struct socketio_sink *ss = (struct socketio_sink *)s;
207 return socketio_write(ss->sio, buffer, nbytes);
208}
209
210static int sink_socketio_error(struct sink *s) {
211 struct socketio_sink *ss = (struct socketio_sink *)s;
212 return socketio_error(ss->sio);
213}
214
215/** @brief Create a sink that writes to a socket
216 * @param sio Socket IO context
217 * @return Pointer to new sink
218 */
219struct sink *sink_socketio(struct socketio *sio) {
220 struct socketio_sink *s = xmalloc(sizeof *s);
221
222 s->s.write = sink_socketio_write;
223 s->s.flush = sink_socketio_flush;
224 s->s.error = sink_socketio_error;
225 s->s.eclass = ec_native;
226 s->sio = sio;
227 return &s->s;
228}
229
0720e8ef
RK
230/* stdio source *************************************************************/
231
232/** @brief Source that reads from a socket handle */
233struct stdio_source {
234 /** @brief Base member */
235 struct source s;
236
237 FILE *fp;
238};
239
240static int source_stdio_getc(struct source *s) {
241 return getc(((struct stdio_source *)s)->fp);
242}
243
244static int source_stdio_error(struct source *s) {
245 FILE *fp = ((struct stdio_source *)s)->fp;
246 if(ferror(fp)) {
247#if _WIN32
248 return GetLastError();
249#else
250 return errno;
251#endif
252 }
253 return 0;
254}
255
256static int source_stdio_eof(struct source *s) {
257 FILE *fp = ((struct stdio_source *)s)->fp;
258 return feof(fp);
259}
260
261struct source *source_stdio(FILE *fp) {
262 struct stdio_source *ss = xmalloc(sizeof *ss);
263 ss->s.getch = source_stdio_getc;
264 ss->s.error = source_stdio_error;
265 ss->s.eof = source_stdio_eof;
266 ss->s.eclass = ec_errno;
267 ss->fp = fp;
268 return (struct source *)ss;
269}
270
271/* socket source ***********************************************************/
272
273/** @brief Source that reads from a socket handle */
274struct socket_source {
275 /** @brief Base member */
276 struct source s;
277
278 struct socketio *sio;
279};
280
281static int source_socketio_getc(struct source *s) {
282 return socketio_getc(((struct socket_source *)s)->sio);
283}
284
285static int source_socketio_error(struct source *s) {
286 return socketio_error(((struct socket_source *)s)->sio);
287}
288static int source_socketio_eof(struct source *s) {
289 return socketio_eof(((struct socket_source *)s)->sio);
290}
291
292struct source *source_socketio(struct socketio *sio) {
293 struct socket_source *ss = xmalloc(sizeof *ss);
294 ss->s.getch = source_socketio_getc;
295 ss->s.error = source_socketio_error;
296 ss->s.eof = source_socketio_eof;
297 ss->s.eclass = ec_native;
298 ss->sio = sio;
299 return (struct source *)ss;
300}
301
460b9539 302/*
303Local Variables:
304c-basic-offset:2
305comment-column:40
306End:
307*/