chiark / gitweb /
Quieten over-pick compiler.
[disorder] / lib / sink.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder
5aff007d 3 * Copyright (C) 2004, 2007, 2008 Richard Kettlewell
460b9539 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 2 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, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * 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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 */
6049fe0e
RK
20/** @file lib/sink.c
21 * @brief Abstract output sink type
22 */
460b9539 23
24#include <config.h>
25#include "types.h"
26
27#include <stdio.h>
28#include <stdarg.h>
29#include <string.h>
30#include <errno.h>
31
32#include "mem.h"
33#include "vector.h"
34#include "sink.h"
35#include "log.h"
36#include "printf.h"
37
6049fe0e
RK
38/** @brief Formatted output to a sink
39 * @param s Sink to write to
40 * @param fmt Format string
41 * @param ap Argument list
42 * @return Number of bytes written on success, -1 on error
43 */
460b9539 44int sink_vprintf(struct sink *s, const char *fmt, va_list ap) {
45 return byte_vsinkprintf(s, fmt, ap);
46}
47
6049fe0e
RK
48/** @brief Formatted output to a sink
49 * @param s Sink to write to
50 * @param fmt Format string
51 * @return Number of bytes written on success, -1 on error
52 */
460b9539 53int sink_printf(struct sink *s, const char *fmt, ...) {
54 va_list ap;
55 int n;
56
57 va_start(ap, fmt);
58 n = byte_vsinkprintf(s, fmt, ap);
59 va_end(ap);
60 return n;
61}
62
63/* stdio sink *****************************************************************/
64
6049fe0e 65/** @brief Sink that writes to a stdio @c FILE */
460b9539 66struct stdio_sink {
6049fe0e 67 /** @brief Base member */
460b9539 68 struct sink s;
6049fe0e
RK
69
70 /** @brief Filename */
460b9539 71 const char *name;
6049fe0e
RK
72
73 /** @brief Stream to write to */
460b9539 74 FILE *fp;
75};
76
6049fe0e 77/** @brief Reinterpret a @ref sink as a @ref stdio_sink */
460b9539 78#define S(s) ((struct stdio_sink *)s)
79
6049fe0e 80/** @brief Write callback for @ref stdio_sink */
460b9539 81static int sink_stdio_write(struct sink *s, const void *buffer, int nbytes) {
82 int n = fwrite(buffer, 1, nbytes, S(s)->fp);
83 if(n < nbytes) {
84 if(S(s)->name)
85 fatal(errno, "error writing to %s", S(s)->name);
86 else
87 return -1;
88 }
89 return n;
90}
91
6049fe0e
RK
92/** @brief Create a sink that writes to a stdio stream
93 * @param name Filename for use in error messages
94 * @param fp Stream to write to
95 * @return Pointer to new sink
96 */
460b9539 97struct sink *sink_stdio(const char *name, FILE *fp) {
98 struct stdio_sink *s = xmalloc(sizeof *s);
99
100 s->s.write = sink_stdio_write;
101 s->name = name;
102 s->fp = fp;
103 return (struct sink *)s;
104}
105
106/* dynstr sink ****************************************************************/
107
6049fe0e 108/** @brief Sink that writes to a dynamic string */
460b9539 109struct dynstr_sink {
6049fe0e 110 /** @brief Base member */
460b9539 111 struct sink s;
6049fe0e 112 /** @brief Pointer to dynamic string to append to */
460b9539 113 struct dynstr *d;
114};
115
6049fe0e 116/** @brief Write callback for @ref dynstr_sink */
460b9539 117static int sink_dynstr_write(struct sink *s, const void *buffer, int nbytes) {
118 dynstr_append_bytes(((struct dynstr_sink *)s)->d, buffer, nbytes);
119 return nbytes;
120}
121
6049fe0e
RK
122/** @brief Create a sink that appends to a @ref dynstr
123 * @param output Dynamic string to append to
124 * @return Pointer to new sink
125 */
460b9539 126struct sink *sink_dynstr(struct dynstr *output) {
127 struct dynstr_sink *s = xmalloc(sizeof *s);
128
129 s->s.write = sink_dynstr_write;
130 s->d = output;
131 return (struct sink *)s;
132}
133
0d0253c9
RK
134/* discard sink **************************************************************/
135
136static int sink_discard_write(struct sink attribute((unused)) *s,
137 const void attribute((unused)) *buffer,
138 int nbytes) {
139 return nbytes;
140}
141
142/** @brief Return a sink which discards all output */
143struct sink *sink_discard(void) {
144 struct sink *s = xmalloc(sizeof *s);
145
146 s->write = sink_discard_write;
147 return s;
148}
149
681cb9fb
RK
150/* error sink **************************************************************/
151
152static int sink_error_write(struct sink attribute((unused)) *s,
153 const void attribute((unused)) *buffer,
154 int attribute((unused)) nbytes) {
155 return -1;
156}
157
158/** @brief Return a sink which discards all output */
159struct sink *sink_error(void) {
160 struct sink *s = xmalloc(sizeof *s);
161
162 s->write = sink_error_write;
163 return s;
164}
165
460b9539 166/*
167Local Variables:
168c-basic-offset:2
169comment-column:40
170End:
171*/