| 1 | /* |
| 2 | * This file is part of DisOrder |
| 3 | * Copyright (C) 2004, 2007, 2008, 2013 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 3 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, |
| 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 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | /** @file lib/sink.h |
| 19 | * @brief Abstract output sink type |
| 20 | */ |
| 21 | |
| 22 | #ifndef SINK_H |
| 23 | #define SINK_H |
| 24 | |
| 25 | #include <stdarg.h> |
| 26 | |
| 27 | struct dynstr; |
| 28 | struct socketio; |
| 29 | |
| 30 | /** @brief Sink type |
| 31 | * |
| 32 | * A sink is something you write bytes to; the opposite would be a |
| 33 | * source. We provide sink_stdio() and sink_dynstr() to create sinks |
| 34 | * to write to stdio streams and dynamic strings. |
| 35 | */ |
| 36 | struct sink { |
| 37 | /** @brief Write callback |
| 38 | * @param s Sink to write to |
| 39 | * @param buffer First byte to write |
| 40 | * @param nbytes Number of bytes to write |
| 41 | * @return non-negative on success, -1 on error |
| 42 | */ |
| 43 | int (*write)(struct sink *s, const void *buffer, int nbytes); |
| 44 | |
| 45 | /** @brief Flush callback |
| 46 | * @param s Sink to write to |
| 47 | * @return non-negative on success, -1 on error |
| 48 | */ |
| 49 | int (*flush)(struct sink *s); |
| 50 | |
| 51 | /** @brief Error callback |
| 52 | * @param s Sink |
| 53 | * @return Last error code |
| 54 | */ |
| 55 | int (*error)(struct sink *s); |
| 56 | |
| 57 | enum error_class eclass; |
| 58 | }; |
| 59 | |
| 60 | struct sink *sink_stdio(const char *name, FILE *fp); |
| 61 | /* return a sink which writes to @fp@. If @name@ is not a null |
| 62 | * pointer, it will be used in (fatal) error messages; if it is a null |
| 63 | * pointer then errors will be signalled by returning -1. */ |
| 64 | |
| 65 | struct sink *sink_dynstr(struct dynstr *output); |
| 66 | /* return a sink which appends to @output@. */ |
| 67 | |
| 68 | struct sink *sink_discard(void); |
| 69 | /* return a sink which junks everything */ |
| 70 | |
| 71 | struct sink *sink_error(void); |
| 72 | /* return a sink which fails all writes */ |
| 73 | |
| 74 | struct sink *sink_socketio(struct socketio *sio); |
| 75 | /* return a sink which writes to a socket */ |
| 76 | |
| 77 | int sink_vprintf(struct sink *s, const char *fmt, va_list ap); |
| 78 | int sink_printf(struct sink *s, const char *fmt, ...) |
| 79 | attribute((format (printf, 2, 3))); |
| 80 | /* equivalent of vfprintf/fprintf for sink @s@ */ |
| 81 | |
| 82 | /** @brief Write bytes to a sink |
| 83 | * @param s Sink to write to |
| 84 | * @param buffer First byte to write |
| 85 | * @param nbytes Number of bytes to write |
| 86 | * @return non-negative on success, -1 on error |
| 87 | */ |
| 88 | static inline int sink_write(struct sink *s, const void *buffer, int nbytes) { |
| 89 | return s->write(s, buffer, nbytes); |
| 90 | } |
| 91 | |
| 92 | /** @brief Write string to a sink |
| 93 | * @param s Sink to write to |
| 94 | * @param str String to write |
| 95 | * @return non-negative on success, -1 on error |
| 96 | */ |
| 97 | static inline int sink_writes(struct sink *s, const char *str) { |
| 98 | return s->write(s, str, strlen(str)); |
| 99 | } |
| 100 | |
| 101 | static inline int sink_flush(struct sink *s) { |
| 102 | return s->flush(s); |
| 103 | } |
| 104 | |
| 105 | /** @brief Write one byte to a sink |
| 106 | * @param s Sink to write to |
| 107 | * @param c Byte to write (as a @c char) |
| 108 | * @return non-negative on success, -1 on error |
| 109 | */ |
| 110 | static inline int sink_writec(struct sink *s, char c) { |
| 111 | return s->write(s, &c, 1); |
| 112 | } |
| 113 | |
| 114 | static inline int sink_err(struct sink *s) { |
| 115 | return s->error(s); |
| 116 | } |
| 117 | |
| 118 | struct source { |
| 119 | int (*getch)(struct source *s); |
| 120 | int (*error)(struct source *s); |
| 121 | int (*eof)(struct source *s); |
| 122 | |
| 123 | enum error_class eclass; |
| 124 | }; |
| 125 | |
| 126 | struct source *source_stdio(FILE *fp); |
| 127 | struct source *source_socketio(struct socketio *sio); |
| 128 | |
| 129 | static inline int source_getc(struct source *s) { |
| 130 | return s->getch(s); |
| 131 | } |
| 132 | |
| 133 | static inline int source_err(struct source *s) { |
| 134 | return s->error(s); |
| 135 | } |
| 136 | |
| 137 | static inline int source_eof(struct source *s) { |
| 138 | return s->eof(s); |
| 139 | } |
| 140 | |
| 141 | #endif /* SINK_H */ |
| 142 | |
| 143 | /* |
| 144 | Local Variables: |
| 145 | c-basic-offset:2 |
| 146 | comment-column:40 |
| 147 | End: |
| 148 | */ |