chiark / gitweb /
Memory management for hands-off reader
[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 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
460b9539 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
460b9539 8 * (at your option) any later version.
e7eb3a27
RK
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 *
460b9539 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
460b9539 17 */
6049fe0e
RK
18/** @file lib/sink.c
19 * @brief Abstract output sink type
20 */
460b9539 21
05b75f8d 22#include "common.h"
460b9539 23
460b9539 24#include <stdarg.h>
460b9539 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
6049fe0e
RK
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 */
460b9539 39int sink_vprintf(struct sink *s, const char *fmt, va_list ap) {
40 return byte_vsinkprintf(s, fmt, ap);
41}
42
6049fe0e
RK
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 */
460b9539 48int 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
6049fe0e 60/** @brief Sink that writes to a stdio @c FILE */
460b9539 61struct stdio_sink {
6049fe0e 62 /** @brief Base member */
460b9539 63 struct sink s;
6049fe0e
RK
64
65 /** @brief Filename */
460b9539 66 const char *name;
6049fe0e
RK
67
68 /** @brief Stream to write to */
460b9539 69 FILE *fp;
70};
71
6049fe0e 72/** @brief Reinterpret a @ref sink as a @ref stdio_sink */
460b9539 73#define S(s) ((struct stdio_sink *)s)
74
6049fe0e 75/** @brief Write callback for @ref stdio_sink */
460b9539 76static 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)
2e9ba080 80 disorder_fatal(errno, "error writing to %s", S(s)->name);
460b9539 81 else
82 return -1;
83 }
84 return n;
85}
86
6049fe0e
RK
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 */
460b9539 92struct 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
6049fe0e 103/** @brief Sink that writes to a dynamic string */
460b9539 104struct dynstr_sink {
6049fe0e 105 /** @brief Base member */
460b9539 106 struct sink s;
6049fe0e 107 /** @brief Pointer to dynamic string to append to */
460b9539 108 struct dynstr *d;
109};
110
6049fe0e 111/** @brief Write callback for @ref dynstr_sink */
460b9539 112static 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
6049fe0e
RK
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 */
460b9539 121struct 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
0d0253c9
RK
129/* discard sink **************************************************************/
130
131static 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 */
138struct sink *sink_discard(void) {
139 struct sink *s = xmalloc(sizeof *s);
140
141 s->write = sink_discard_write;
142 return s;
143}
144
681cb9fb
RK
145/* error sink **************************************************************/
146
147static 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 */
154struct sink *sink_error(void) {
155 struct sink *s = xmalloc(sizeof *s);
156
157 s->write = sink_error_write;
158 return s;
159}
160
460b9539 161/*
162Local Variables:
163c-basic-offset:2
164comment-column:40
165End:
166*/