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