2 * This file is part of DisOrder
3 * Copyright (C) 2004, 2007, 2008 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 /* stdio sink *****************************************************************/
60 /** @brief Sink that writes to a stdio @c FILE */
62 /** @brief Base member */
65 /** @brief Filename */
68 /** @brief Stream to write to */
72 /** @brief Reinterpret a @ref sink as a @ref stdio_sink */
73 #define S(s) ((struct stdio_sink *)s)
75 /** @brief Write callback for @ref stdio_sink */
76 static int sink_stdio_write(struct sink
*s
, const void *buffer
, int nbytes
) {
77 int n
= fwrite(buffer
, 1, nbytes
, S(s
)->fp
);
80 disorder_fatal(errno
, "error writing to %s", S(s
)->name
);
87 /** @brief Create a sink that writes to a stdio stream
88 * @param name Filename for use in error messages
89 * @param fp Stream to write to
90 * @return Pointer to new sink
92 struct sink
*sink_stdio(const char *name
, FILE *fp
) {
93 struct stdio_sink
*s
= xmalloc(sizeof *s
);
95 s
->s
.write
= sink_stdio_write
;
98 return (struct sink
*)s
;
101 /* dynstr sink ****************************************************************/
103 /** @brief Sink that writes to a dynamic string */
105 /** @brief Base member */
107 /** @brief Pointer to dynamic string to append to */
111 /** @brief Write callback for @ref dynstr_sink */
112 static int sink_dynstr_write(struct sink
*s
, const void *buffer
, int nbytes
) {
113 dynstr_append_bytes(((struct dynstr_sink
*)s
)->d
, buffer
, nbytes
);
117 /** @brief Create a sink that appends to a @ref dynstr
118 * @param output Dynamic string to append to
119 * @return Pointer to new sink
121 struct sink
*sink_dynstr(struct dynstr
*output
) {
122 struct dynstr_sink
*s
= xmalloc(sizeof *s
);
124 s
->s
.write
= sink_dynstr_write
;
126 return (struct sink
*)s
;
129 /* discard sink **************************************************************/
131 static int sink_discard_write(struct sink
attribute((unused
)) *s
,
132 const void attribute((unused
)) *buffer
,
137 /** @brief Return a sink which discards all output */
138 struct sink
*sink_discard(void) {
139 struct sink
*s
= xmalloc(sizeof *s
);
141 s
->write
= sink_discard_write
;
145 /* error sink **************************************************************/
147 static int sink_error_write(struct sink
attribute((unused
)) *s
,
148 const void attribute((unused
)) *buffer
,
149 int attribute((unused
)) nbytes
) {
153 /** @brief Return a sink which discards all output */
154 struct sink
*sink_error(void) {
155 struct sink
*s
= xmalloc(sizeof *s
);
157 s
->write
= sink_error_write
;