lib/configuration.c, etc.: Remove arguments from `config_userconf'.
[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 "socketio.h"
30 #include "log.h"
31 #include "sink.h"
32 #include "printf.h"
33
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 */
40 int sink_vprintf(struct sink *s, const char *fmt, va_list ap) {
41 return byte_vsinkprintf(s, fmt, ap);
42 }
43
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 */
49 int 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
59 static int sink_generic_flush(struct sink attribute((unused)) *s) {
60 return 0;
61 }
62
63 static int sink_generic_error(struct sink attribute((unused)) *s) {
64 return 0;
65 }
66
67 /* stdio sink *****************************************************************/
68
69 /** @brief Sink that writes to a stdio @c FILE */
70 struct stdio_sink {
71 /** @brief Base member */
72 struct sink s;
73
74 /** @brief Filename */
75 const char *name;
76
77 /** @brief Stream to write to */
78 FILE *fp;
79
80 int error;
81 };
82
83 /** @brief Reinterpret a @ref sink as a @ref stdio_sink */
84 #define S(s) ((struct stdio_sink *)s)
85
86 /** @brief Write callback for @ref stdio_sink */
87 static 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) {
90 S(s)->error = errno;
91 if(S(s)->name)
92 disorder_fatal(errno, "error writing to %s", S(s)->name);
93 else
94 return -1;
95 }
96 return n;
97 }
98
99 static int sink_stdio_error(struct sink *s) {
100 return S(s)->error;
101 }
102
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 */
108 struct sink *sink_stdio(const char *name, FILE *fp) {
109 struct stdio_sink *s = xmalloc(sizeof *s);
110
111 s->s.write = sink_stdio_write;
112 s->s.flush = sink_generic_flush;
113 s->s.error = sink_stdio_error;
114 s->s.eclass = ec_errno;
115 s->name = name;
116 s->fp = fp;
117 return (struct sink *)s;
118 }
119
120 /* dynstr sink ****************************************************************/
121
122 /** @brief Sink that writes to a dynamic string */
123 struct dynstr_sink {
124 /** @brief Base member */
125 struct sink s;
126 /** @brief Pointer to dynamic string to append to */
127 struct dynstr *d;
128 };
129
130 /** @brief Write callback for @ref dynstr_sink */
131 static 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
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 */
140 struct sink *sink_dynstr(struct dynstr *output) {
141 struct dynstr_sink *s = xmalloc(sizeof *s);
142
143 s->s.write = sink_dynstr_write;
144 s->s.flush = sink_generic_flush;
145 s->s.error = sink_generic_error;
146 s->s.eclass = ec_errno;
147 s->d = output;
148 return (struct sink *)s;
149 }
150
151 /* discard sink **************************************************************/
152
153 static 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 */
160 struct sink *sink_discard(void) {
161 struct sink *s = xmalloc(sizeof *s);
162
163 s->write = sink_discard_write;
164 s->flush = sink_generic_flush;
165 s->error = sink_generic_error;
166 s->eclass = ec_errno;
167 return s;
168 }
169
170 /* error sink **************************************************************/
171
172 static 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 */
179 struct sink *sink_error(void) {
180 struct sink *s = xmalloc(sizeof *s);
181
182 s->write = sink_error_write;
183 s->flush = sink_generic_flush;
184 s->error = sink_generic_error;
185 s->eclass = ec_errno;
186 return s;
187 }
188
189 /* socket sink *************************************************************/
190
191 /** @brief Sink that writes to a socket handle */
192 struct socketio_sink {
193 /** @brief Base member */
194 struct sink s;
195
196 struct socketio *sio;
197 };
198
199 static 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 */
205 static 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
210 static 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 */
219 struct 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
230 /* stdio source *************************************************************/
231
232 /** @brief Source that reads from a socket handle */
233 struct stdio_source {
234 /** @brief Base member */
235 struct source s;
236
237 FILE *fp;
238 };
239
240 static int source_stdio_getc(struct source *s) {
241 return getc(((struct stdio_source *)s)->fp);
242 }
243
244 static 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
256 static int source_stdio_eof(struct source *s) {
257 FILE *fp = ((struct stdio_source *)s)->fp;
258 return feof(fp);
259 }
260
261 struct 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 */
274 struct socket_source {
275 /** @brief Base member */
276 struct source s;
277
278 struct socketio *sio;
279 };
280
281 static int source_socketio_getc(struct source *s) {
282 return socketio_getc(((struct socket_source *)s)->sio);
283 }
284
285 static int source_socketio_error(struct source *s) {
286 return socketio_error(((struct socket_source *)s)->sio);
287 }
288 static int source_socketio_eof(struct source *s) {
289 return socketio_eof(((struct socket_source *)s)->sio);
290 }
291
292 struct 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
302 /*
303 Local Variables:
304 c-basic-offset:2
305 comment-column:40
306 End:
307 */