2 * This file is part of DisOrder
3 * Copyright (C) 2004 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 2 of the License, or
8 * (at your option) any later version.
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.
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
21 * @brief Abstract output sink type
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
44 int sink_vprintf(struct sink *s, const char *fmt, va_list ap) {
45 return byte_vsinkprintf(s, fmt, ap);
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
53 int sink_printf(struct sink *s, const char *fmt, ...) {
58 n = byte_vsinkprintf(s, fmt, ap);
63 /* stdio sink *****************************************************************/
65 /** @brief Sink that writes to a stdio @c FILE */
67 /** @brief Base member */
70 /** @brief Filename */
73 /** @brief Stream to write to */
77 /** @brief Reinterpret a @ref sink as a @ref stdio_sink */
78 #define S(s) ((struct stdio_sink *)s)
80 /** @brief Write callback for @ref stdio_sink */
81 static int sink_stdio_write(struct sink *s, const void *buffer, int nbytes) {
82 int n = fwrite(buffer, 1, nbytes, S(s)->fp);
85 fatal(errno, "error writing to %s", S(s)->name);
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
97 struct sink *sink_stdio(const char *name, FILE *fp) {
98 struct stdio_sink *s = xmalloc(sizeof *s);
100 s->s.write = sink_stdio_write;
103 return (struct sink *)s;
106 /* dynstr sink ****************************************************************/
108 /** @brief Sink that writes to a dynamic string */
110 /** @brief Base member */
112 /** @brief Pointer to dynamic string to append to */
116 /** @brief Write callback for @ref dynstr_sink */
117 static int sink_dynstr_write(struct sink *s, const void *buffer, int nbytes) {
118 dynstr_append_bytes(((struct dynstr_sink *)s)->d, buffer, nbytes);
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
126 struct sink *sink_dynstr(struct dynstr *output) {
127 struct dynstr_sink *s = xmalloc(sizeof *s);
129 s->s.write = sink_dynstr_write;
131 return (struct sink *)s;