Commit | Line | Data |
---|---|---|
460b9539 | 1 | /* |
2 | * This file is part of DisOrder | |
3 | * Copyright (C) 2004 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 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.h |
21 | * @brief Abstract output sink type | |
22 | */ | |
460b9539 | 23 | |
24 | #ifndef SINK_H | |
25 | #define SINK_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); |
460b9539 | 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 | int sink_vprintf(struct sink *s, const char *fmt, va_list ap); | |
54 | int sink_printf(struct sink *s, const char *fmt, ...) | |
55 | attribute((format (printf, 2, 3))); | |
56 | /* equivalent of vfprintf/fprintf for sink @s@ */ | |
57 | ||
6049fe0e RK |
58 | /** @brief Write bytes to a sink |
59 | * @param s Sink to write to | |
60 | * @param buffer First byte to write | |
61 | * @param nbytes Number of bytes to write | |
62 | * @return non-negative on success, -1 on error | |
63 | */ | |
460b9539 | 64 | static inline int sink_write(struct sink *s, const void *buffer, int nbytes) { |
65 | return s->write(s, buffer, nbytes); | |
66 | } | |
67 | ||
6049fe0e RK |
68 | /** @brief Write string to a sink |
69 | * @param s Sink to write to | |
70 | * @param str String to write | |
71 | * @return non-negative on success, -1 on error | |
72 | */ | |
460b9539 | 73 | static inline int sink_writes(struct sink *s, const char *str) { |
74 | return s->write(s, str, strlen(str)); | |
75 | } | |
76 | ||
6049fe0e RK |
77 | /** @brief Write one byte to a sink |
78 | * @param s Sink to write to | |
79 | * @param c Byte to write (as a @c char) | |
80 | * @return non-negative on success, -1 on error | |
81 | */ | |
460b9539 | 82 | static inline int sink_writec(struct sink *s, char c) { |
83 | return s->write(s, &c, 1); | |
84 | } | |
85 | ||
86 | #endif /* SINK_H */ | |
87 | ||
88 | ||
89 | /* | |
90 | Local Variables: | |
91 | c-basic-offset:2 | |
92 | comment-column:40 | |
93 | End: | |
94 | */ |