chiark / gitweb /
queue() now uses the resolved name if available. This is looked up in
[disorder] / lib / sink.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder
5aff007d 3 * Copyright (C) 2004, 2007, 2008 Richard Kettlewell
460b9539 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 2 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, 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.
14 *
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
18 * USA
19 */
6049fe0e
RK
20/** @file lib/sink.c
21 * @brief Abstract output sink type
22 */
460b9539 23
05b75f8d 24#include "common.h"
460b9539 25
460b9539 26#include <stdarg.h>
460b9539 27#include <errno.h>
28
29#include "mem.h"
30#include "vector.h"
31#include "sink.h"
32#include "log.h"
33#include "printf.h"
34
6049fe0e
RK
35/** @brief Formatted output to a sink
36 * @param s Sink to write to
37 * @param fmt Format string
38 * @param ap Argument list
39 * @return Number of bytes written on success, -1 on error
40 */
460b9539 41int sink_vprintf(struct sink *s, const char *fmt, va_list ap) {
42 return byte_vsinkprintf(s, fmt, ap);
43}
44
6049fe0e
RK
45/** @brief Formatted output to a sink
46 * @param s Sink to write to
47 * @param fmt Format string
48 * @return Number of bytes written on success, -1 on error
49 */
460b9539 50int sink_printf(struct sink *s, const char *fmt, ...) {
51 va_list ap;
52 int n;
53
54 va_start(ap, fmt);
55 n = byte_vsinkprintf(s, fmt, ap);
56 va_end(ap);
57 return n;
58}
59
60/* stdio sink *****************************************************************/
61
6049fe0e 62/** @brief Sink that writes to a stdio @c FILE */
460b9539 63struct stdio_sink {
6049fe0e 64 /** @brief Base member */
460b9539 65 struct sink s;
6049fe0e
RK
66
67 /** @brief Filename */
460b9539 68 const char *name;
6049fe0e
RK
69
70 /** @brief Stream to write to */
460b9539 71 FILE *fp;
72};
73
6049fe0e 74/** @brief Reinterpret a @ref sink as a @ref stdio_sink */
460b9539 75#define S(s) ((struct stdio_sink *)s)
76
6049fe0e 77/** @brief Write callback for @ref stdio_sink */
460b9539 78static int sink_stdio_write(struct sink *s, const void *buffer, int nbytes) {
79 int n = fwrite(buffer, 1, nbytes, S(s)->fp);
80 if(n < nbytes) {
81 if(S(s)->name)
82 fatal(errno, "error writing to %s", S(s)->name);
83 else
84 return -1;
85 }
86 return n;
87}
88
6049fe0e
RK
89/** @brief Create a sink that writes to a stdio stream
90 * @param name Filename for use in error messages
91 * @param fp Stream to write to
92 * @return Pointer to new sink
93 */
460b9539 94struct sink *sink_stdio(const char *name, FILE *fp) {
95 struct stdio_sink *s = xmalloc(sizeof *s);
96
97 s->s.write = sink_stdio_write;
98 s->name = name;
99 s->fp = fp;
100 return (struct sink *)s;
101}
102
103/* dynstr sink ****************************************************************/
104
6049fe0e 105/** @brief Sink that writes to a dynamic string */
460b9539 106struct dynstr_sink {
6049fe0e 107 /** @brief Base member */
460b9539 108 struct sink s;
6049fe0e 109 /** @brief Pointer to dynamic string to append to */
460b9539 110 struct dynstr *d;
111};
112
6049fe0e 113/** @brief Write callback for @ref dynstr_sink */
460b9539 114static int sink_dynstr_write(struct sink *s, const void *buffer, int nbytes) {
115 dynstr_append_bytes(((struct dynstr_sink *)s)->d, buffer, nbytes);
116 return nbytes;
117}
118
6049fe0e
RK
119/** @brief Create a sink that appends to a @ref dynstr
120 * @param output Dynamic string to append to
121 * @return Pointer to new sink
122 */
460b9539 123struct sink *sink_dynstr(struct dynstr *output) {
124 struct dynstr_sink *s = xmalloc(sizeof *s);
125
126 s->s.write = sink_dynstr_write;
127 s->d = output;
128 return (struct sink *)s;
129}
130
0d0253c9
RK
131/* discard sink **************************************************************/
132
133static int sink_discard_write(struct sink attribute((unused)) *s,
134 const void attribute((unused)) *buffer,
135 int nbytes) {
136 return nbytes;
137}
138
139/** @brief Return a sink which discards all output */
140struct sink *sink_discard(void) {
141 struct sink *s = xmalloc(sizeof *s);
142
143 s->write = sink_discard_write;
144 return s;
145}
146
681cb9fb
RK
147/* error sink **************************************************************/
148
149static int sink_error_write(struct sink attribute((unused)) *s,
150 const void attribute((unused)) *buffer,
151 int attribute((unused)) nbytes) {
152 return -1;
153}
154
155/** @brief Return a sink which discards all output */
156struct sink *sink_error(void) {
157 struct sink *s = xmalloc(sizeof *s);
158
159 s->write = sink_error_write;
160 return s;
161}
162
460b9539 163/*
164Local Variables:
165c-basic-offset:2
166comment-column:40
167End:
168*/