chiark / gitweb /
More general error handling for sinks
[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"
460b9539 29#include "log.h"
902b9f3f 30#include "sink.h"
460b9539 31#include "printf.h"
32
6049fe0e
RK
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 */
460b9539 39int sink_vprintf(struct sink *s, const char *fmt, va_list ap) {
40 return byte_vsinkprintf(s, fmt, ap);
41}
42
6049fe0e
RK
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 */
460b9539 48int 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
902b9f3f
RK
58static int sink_generic_flush(struct sink attribute((unused)) *s) {
59 return 0;
60}
61
62static int sink_generic_error(struct sink attribute((unused)) *s) {
63 return 0;
64}
65
460b9539 66/* stdio sink *****************************************************************/
67
6049fe0e 68/** @brief Sink that writes to a stdio @c FILE */
460b9539 69struct stdio_sink {
6049fe0e 70 /** @brief Base member */
460b9539 71 struct sink s;
6049fe0e
RK
72
73 /** @brief Filename */
460b9539 74 const char *name;
6049fe0e
RK
75
76 /** @brief Stream to write to */
460b9539 77 FILE *fp;
902b9f3f
RK
78
79 int error;
460b9539 80};
81
6049fe0e 82/** @brief Reinterpret a @ref sink as a @ref stdio_sink */
460b9539 83#define S(s) ((struct stdio_sink *)s)
84
6049fe0e 85/** @brief Write callback for @ref stdio_sink */
460b9539 86static 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) {
902b9f3f 89 S(s)->error = errno;
460b9539 90 if(S(s)->name)
2e9ba080 91 disorder_fatal(errno, "error writing to %s", S(s)->name);
460b9539 92 else
93 return -1;
94 }
95 return n;
96}
97
902b9f3f
RK
98static int sink_stdio_error(struct sink *s) {
99 return S(s)->error;
100}
101
6049fe0e
RK
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 */
460b9539 107struct sink *sink_stdio(const char *name, FILE *fp) {
108 struct stdio_sink *s = xmalloc(sizeof *s);
109
110 s->s.write = sink_stdio_write;
902b9f3f
RK
111 s->s.flush = sink_generic_flush;
112 s->s.error = sink_stdio_error;
113 s->s.eclass = ec_errno;
460b9539 114 s->name = name;
115 s->fp = fp;
116 return (struct sink *)s;
117}
118
119/* dynstr sink ****************************************************************/
120
6049fe0e 121/** @brief Sink that writes to a dynamic string */
460b9539 122struct dynstr_sink {
6049fe0e 123 /** @brief Base member */
460b9539 124 struct sink s;
6049fe0e 125 /** @brief Pointer to dynamic string to append to */
460b9539 126 struct dynstr *d;
127};
128
6049fe0e 129/** @brief Write callback for @ref dynstr_sink */
460b9539 130static 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
6049fe0e
RK
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 */
460b9539 139struct sink *sink_dynstr(struct dynstr *output) {
140 struct dynstr_sink *s = xmalloc(sizeof *s);
141
142 s->s.write = sink_dynstr_write;
902b9f3f
RK
143 s->s.flush = sink_generic_flush;
144 s->s.error = sink_generic_error;
145 s->s.eclass = ec_errno;
460b9539 146 s->d = output;
147 return (struct sink *)s;
148}
149
0d0253c9
RK
150/* discard sink **************************************************************/
151
152static 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 */
159struct sink *sink_discard(void) {
160 struct sink *s = xmalloc(sizeof *s);
161
162 s->write = sink_discard_write;
902b9f3f
RK
163 s->flush = sink_generic_flush;
164 s->error = sink_generic_error;
165 s->eclass = ec_errno;
0d0253c9
RK
166 return s;
167}
168
681cb9fb
RK
169/* error sink **************************************************************/
170
171static 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 */
178struct sink *sink_error(void) {
179 struct sink *s = xmalloc(sizeof *s);
180
181 s->write = sink_error_write;
902b9f3f
RK
182 s->flush = sink_generic_flush;
183 s->error = sink_generic_error;
184 s->eclass = ec_errno;
681cb9fb
RK
185 return s;
186}
187
460b9539 188/*
189Local Variables:
190c-basic-offset:2
191comment-column:40
192End:
193*/