2 * This file is part of DisOrder
3 * Copyright (C) 2004, 2007-9, 2013 Richard Kettlewell
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.
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.
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/>.
19 * @brief Abstract output sink type
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
39 int sink_vprintf(struct sink *s, const char *fmt, va_list ap) {
40 return byte_vsinkprintf(s, fmt, ap);
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
48 int sink_printf(struct sink *s, const char *fmt, ...) {
53 n = byte_vsinkprintf(s, fmt, ap);
58 static int sink_generic_flush(struct sink attribute((unused)) *s) {
62 static int sink_generic_error(struct sink attribute((unused)) *s) {
66 /* stdio sink *****************************************************************/
68 /** @brief Sink that writes to a stdio @c FILE */
70 /** @brief Base member */
73 /** @brief Filename */
76 /** @brief Stream to write to */
82 /** @brief Reinterpret a @ref sink as a @ref stdio_sink */
83 #define S(s) ((struct stdio_sink *)s)
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);
91 disorder_fatal(errno, "error writing to %s", S(s)->name);
98 static int sink_stdio_error(struct sink *s) {
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
107 struct sink *sink_stdio(const char *name, FILE *fp) {
108 struct stdio_sink *s = xmalloc(sizeof *s);
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;
116 return (struct sink *)s;
119 /* dynstr sink ****************************************************************/
121 /** @brief Sink that writes to a dynamic string */
123 /** @brief Base member */
125 /** @brief Pointer to dynamic string to append to */
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);
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
139 struct sink *sink_dynstr(struct dynstr *output) {
140 struct dynstr_sink *s = xmalloc(sizeof *s);
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;
147 return (struct sink *)s;
150 /* discard sink **************************************************************/
152 static int sink_discard_write(struct sink attribute((unused)) *s,
153 const void attribute((unused)) *buffer,
158 /** @brief Return a sink which discards all output */
159 struct sink *sink_discard(void) {
160 struct sink *s = xmalloc(sizeof *s);
162 s->write = sink_discard_write;
163 s->flush = sink_generic_flush;
164 s->error = sink_generic_error;
165 s->eclass = ec_errno;
169 /* error sink **************************************************************/
171 static int sink_error_write(struct sink attribute((unused)) *s,
172 const void attribute((unused)) *buffer,
173 int attribute((unused)) nbytes) {
177 /** @brief Return a sink which discards all output */
178 struct sink *sink_error(void) {
179 struct sink *s = xmalloc(sizeof *s);
181 s->write = sink_error_write;
182 s->flush = sink_generic_flush;
183 s->error = sink_generic_error;
184 s->eclass = ec_errno;