chiark / gitweb /
Merge playlist branch against trunk to date.
[disorder] / lib / sink.c
1 /*
2  * This file is part of DisOrder
3  * Copyright (C) 2004, 2007, 2008 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 "sink.h"
30 #include "log.h"
31 #include "printf.h"
32
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
38  */
39 int sink_vprintf(struct sink *s, const char *fmt, va_list ap) {
40   return byte_vsinkprintf(s, fmt, ap);
41 }
42
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
47  */
48 int sink_printf(struct sink *s, const char *fmt, ...) {
49   va_list ap;
50   int n;
51
52   va_start(ap, fmt);
53   n = byte_vsinkprintf(s, fmt, ap);
54   va_end(ap);
55   return n;
56 }
57
58 /* stdio sink *****************************************************************/
59
60 /** @brief Sink that writes to a stdio @c FILE */
61 struct stdio_sink {
62   /** @brief Base member */
63   struct sink s;
64
65   /** @brief Filename */
66   const char *name;
67
68   /** @brief Stream to write to */
69   FILE *fp;
70 };
71
72 /** @brief Reinterpret a @ref sink as a @ref stdio_sink */
73 #define S(s) ((struct stdio_sink *)s)
74
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);
78   if(n < nbytes) {
79     if(S(s)->name)
80       fatal(errno, "error writing to %s", S(s)->name);
81     else
82       return -1;
83   }
84   return n;
85 }
86
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
91  */
92 struct sink *sink_stdio(const char *name, FILE *fp) {
93   struct stdio_sink *s = xmalloc(sizeof *s);
94
95   s->s.write = sink_stdio_write;
96   s->name = name;
97   s->fp = fp;
98   return (struct sink *)s;
99 }
100
101 /* dynstr sink ****************************************************************/
102
103 /** @brief Sink that writes to a dynamic string */
104 struct dynstr_sink {
105   /** @brief Base member */
106   struct sink s;
107   /** @brief Pointer to dynamic string to append to */
108   struct dynstr *d;
109 };
110
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);
114   return nbytes;
115 }
116
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
120  */
121 struct sink *sink_dynstr(struct dynstr *output) {
122   struct dynstr_sink *s = xmalloc(sizeof *s);
123
124   s->s.write = sink_dynstr_write;
125   s->d = output;
126   return (struct sink *)s;
127 }
128
129 /* discard sink **************************************************************/
130
131 static int sink_discard_write(struct sink attribute((unused)) *s,
132                               const void attribute((unused)) *buffer,
133                               int nbytes) {
134   return nbytes;
135 }
136
137 /** @brief Return a sink which discards all output */
138 struct sink *sink_discard(void) {
139   struct sink *s = xmalloc(sizeof *s);
140
141   s->write = sink_discard_write;
142   return s;
143 }
144
145 /* error sink **************************************************************/
146
147 static int sink_error_write(struct sink attribute((unused)) *s,
148                             const void attribute((unused)) *buffer,
149                             int attribute((unused)) nbytes) {
150   return -1;
151 }
152
153 /** @brief Return a sink which discards all output */
154 struct sink *sink_error(void) {
155   struct sink *s = xmalloc(sizeof *s);
156
157   s->write = sink_error_write;
158   return s;
159 }
160
161 /*
162 Local Variables:
163 c-basic-offset:2
164 comment-column:40
165 End:
166 */