Commit | Line | Data |
---|---|---|
460b9539 | 1 | /* |
2 | * This file is part of DisOrder | |
902b9f3f | 3 | * Copyright (C) 2004, 2007, 2008, 2013 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.h |
19 | * @brief Abstract output sink type | |
20 | */ | |
460b9539 | 21 | |
22 | #ifndef SINK_H | |
23 | #define SINK_H | |
24 | ||
05b75f8d RK |
25 | #include <stdarg.h> |
26 | ||
460b9539 | 27 | struct dynstr; |
28 | ||
6049fe0e RK |
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 | */ | |
460b9539 | 35 | struct sink { |
6049fe0e RK |
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 | */ | |
460b9539 | 42 | int (*write)(struct sink *s, const void *buffer, int nbytes); |
902b9f3f RK |
43 | |
44 | /** @brief Flush callback | |
45 | * @param s Sink to write to | |
46 | * @return non-negative on success, -1 on error | |
47 | */ | |
48 | int (*flush)(struct sink *s); | |
49 | ||
50 | /** @brief Error callback | |
51 | * @param s Sink | |
52 | * @return Last error code | |
53 | */ | |
54 | int (*error)(struct sink *s); | |
55 | ||
56 | enum error_class eclass; | |
460b9539 | 57 | }; |
58 | ||
59 | struct sink *sink_stdio(const char *name, FILE *fp); | |
60 | /* return a sink which writes to @fp@. If @name@ is not a null | |
61 | * pointer, it will be used in (fatal) error messages; if it is a null | |
62 | * pointer then errors will be signalled by returning -1. */ | |
63 | ||
64 | struct sink *sink_dynstr(struct dynstr *output); | |
65 | /* return a sink which appends to @output@. */ | |
66 | ||
0d0253c9 | 67 | struct sink *sink_discard(void); |
681cb9fb RK |
68 | /* return a sink which junks everything */ |
69 | ||
70 | struct sink *sink_error(void); | |
71 | /* return a sink which fails all writes */ | |
0d0253c9 | 72 | |
460b9539 | 73 | int sink_vprintf(struct sink *s, const char *fmt, va_list ap); |
74 | int sink_printf(struct sink *s, const char *fmt, ...) | |
75 | attribute((format (printf, 2, 3))); | |
76 | /* equivalent of vfprintf/fprintf for sink @s@ */ | |
77 | ||
6049fe0e RK |
78 | /** @brief Write bytes to a sink |
79 | * @param s Sink to write to | |
80 | * @param buffer First byte to write | |
81 | * @param nbytes Number of bytes to write | |
82 | * @return non-negative on success, -1 on error | |
83 | */ | |
460b9539 | 84 | static inline int sink_write(struct sink *s, const void *buffer, int nbytes) { |
85 | return s->write(s, buffer, nbytes); | |
86 | } | |
87 | ||
6049fe0e RK |
88 | /** @brief Write string to a sink |
89 | * @param s Sink to write to | |
90 | * @param str String to write | |
91 | * @return non-negative on success, -1 on error | |
92 | */ | |
460b9539 | 93 | static inline int sink_writes(struct sink *s, const char *str) { |
94 | return s->write(s, str, strlen(str)); | |
95 | } | |
96 | ||
902b9f3f RK |
97 | static inline int sink_flush(struct sink *s) { |
98 | return s->flush(s); | |
99 | } | |
100 | ||
6049fe0e RK |
101 | /** @brief Write one byte to a sink |
102 | * @param s Sink to write to | |
103 | * @param c Byte to write (as a @c char) | |
104 | * @return non-negative on success, -1 on error | |
105 | */ | |
460b9539 | 106 | static inline int sink_writec(struct sink *s, char c) { |
107 | return s->write(s, &c, 1); | |
108 | } | |
109 | ||
902b9f3f RK |
110 | static inline int sink_err(struct sink *s) { |
111 | return s->error(s); | |
112 | } | |
460b9539 | 113 | |
902b9f3f | 114 | #endif /* SINK_H */ |
460b9539 | 115 | |
116 | /* | |
117 | Local Variables: | |
118 | c-basic-offset:2 | |
119 | comment-column:40 | |
120 | End: | |
121 | */ |