chiark / gitweb /
Move player/decoder PIDs back into the main queue_entry structure, now
[disorder] / lib / sink.h
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.h
19  * @brief Abstract output sink type
20  */
21
22 #ifndef SINK_H
23 #define SINK_H
24
25 #include <stdarg.h>
26
27 struct dynstr;
28
29 /** @brief Sink type
30  *
31  * A sink is something you write bytes to; the opposite would be a
32  * source.  We provide sink_stdio() and sink_dynstr() to create sinks
33  * to write to stdio streams and dynamic strings.
34  */
35 struct sink {
36   /** @brief Write callback
37    * @param s Sink to write to
38    * @param buffer First byte to write
39    * @param nbytes Number of bytes to write
40    * @return non-negative on success, -1 on error
41    */
42   int (*write)(struct sink *s, const void *buffer, int nbytes);
43 };
44
45 struct sink *sink_stdio(const char *name, FILE *fp);
46 /* return a sink which writes to @fp@.  If @name@ is not a null
47  * pointer, it will be used in (fatal) error messages; if it is a null
48  * pointer then errors will be signalled by returning -1. */
49
50 struct sink *sink_dynstr(struct dynstr *output);
51 /* return a sink which appends to @output@. */
52
53 struct sink *sink_discard(void);
54 /* return a sink which junks everything */
55
56 struct sink *sink_error(void);
57 /* return a sink which fails all writes */
58
59 int sink_vprintf(struct sink *s, const char *fmt, va_list ap);
60 int sink_printf(struct sink *s, const char *fmt, ...)
61   attribute((format (printf, 2, 3)));
62 /* equivalent of vfprintf/fprintf for sink @s@ */
63
64 /** @brief Write bytes to a sink
65  * @param s Sink to write to
66  * @param buffer First byte to write
67  * @param nbytes Number of bytes to write
68  * @return non-negative on success, -1 on error
69  */
70 static inline int sink_write(struct sink *s, const void *buffer, int nbytes) {
71   return s->write(s, buffer, nbytes);
72 }
73
74 /** @brief Write string to a sink
75  * @param s Sink to write to
76  * @param str String to write
77  * @return non-negative on success, -1 on error
78  */
79 static inline int sink_writes(struct sink *s, const char *str) {
80   return s->write(s, str, strlen(str));
81 }
82
83 /** @brief Write one byte to a sink
84  * @param s Sink to write to
85  * @param c Byte to write (as a @c char)
86  * @return non-negative on success, -1 on error
87  */
88 static inline int sink_writec(struct sink *s, char c) {
89   return s->write(s, &c, 1);
90 }
91
92 #endif /* SINK_H */
93
94
95 /*
96 Local Variables:
97 c-basic-offset:2
98 comment-column:40
99 End:
100 */