3 * \brief Generic stdio-like output interface
4 * \author Abramo Bagnara <abramo@alsa-project.org>
7 * Generic stdio-like output interface
11 * Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
14 * This library is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License as
16 * published by the Free Software Foundation; either version 2.1 of
17 * the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
37 typedef struct _snd_output_ops {
38 int (*close)(snd_output_t *output);
39 int (*print)(snd_output_t *output, const char *format, va_list args);
40 int (*puts)(snd_output_t *output, const char *str);
41 int (*putch)(snd_output_t *output, int c);
42 int (*flush)(snd_output_t *output);
46 snd_output_type_t type;
47 const snd_output_ops_t *ops;
53 * \brief Closes an output handle.
54 * \param output The output handle to be closed.
55 * \return Zero if successful, otherwise a negative error code.
57 int snd_output_close(snd_output_t *output)
59 int err = output->ops->close(output);
65 * \brief Writes formatted output (like \c fprintf(3)) to an output handle.
66 * \param output The output handle.
67 * \param format Format string in \c fprintf format.
68 * \param ... Other \c fprintf arguments.
69 * \return The number of characters written, or a negative error code.
71 int snd_output_printf(snd_output_t *output, const char *format, ...)
75 va_start(args, format);
76 result = output->ops->print(output, format, args);
82 * \brief Writes formatted output (like \c fprintf(3)) to an output handle.
83 * \param output The output handle.
84 * \param format Format string in \c fprintf format.
85 * \param args Other \c fprintf arguments.
86 * \return The number of characters written, or a negative error code.
88 int snd_output_vprintf(snd_output_t *output, const char *format, va_list args)
90 return output->ops->print(output, format, args);
94 * \brief Writes a string to an output handle (like \c fputs(3)).
95 * \param output The output handle.
96 * \param str Pointer to the string.
97 * \return Zero if successful, otherwise a negative error code or \c EOF.
99 int snd_output_puts(snd_output_t *output, const char *str)
101 return output->ops->puts(output, str);
105 * \brief Writes a character to an output handle (like \c putc(3)).
106 * \param output The output handle.
107 * \param c The character.
108 * \return Zero if successful, otherwise a negative error code or \c EOF.
110 int snd_output_putc(snd_output_t *output, int c)
112 return output->ops->putch(output, c);
116 * \brief Flushes an output handle (like fflush(3)).
117 * \param output The output handle.
118 * \return Zero if successful, otherwise \c EOF.
120 * If the underlying destination is a stdio stream, this function calls
121 * \c fflush. If the underlying destination is a memory buffer, the write
122 * position is reset to the beginning of the buffer. \c =:-o
124 int snd_output_flush(snd_output_t *output)
126 return output->ops->flush(output);
130 typedef struct _snd_output_stdio {
133 } snd_output_stdio_t;
135 static int snd_output_stdio_close(snd_output_t *output)
137 snd_output_stdio_t *stdio = output->private_data;
144 static int snd_output_stdio_print(snd_output_t *output, const char *format, va_list args)
146 snd_output_stdio_t *stdio = output->private_data;
147 return vfprintf(stdio->fp, format, args);
150 static int snd_output_stdio_puts(snd_output_t *output, const char *str)
152 snd_output_stdio_t *stdio = output->private_data;
153 return fputs(str, stdio->fp);
156 static int snd_output_stdio_putc(snd_output_t *output, int c)
158 snd_output_stdio_t *stdio = output->private_data;
159 return putc(c, stdio->fp);
162 static int snd_output_stdio_flush(snd_output_t *output)
164 snd_output_stdio_t *stdio = output->private_data;
165 return fflush(stdio->fp);
168 static const snd_output_ops_t snd_output_stdio_ops = {
169 .close = snd_output_stdio_close,
170 .print = snd_output_stdio_print,
171 .puts = snd_output_stdio_puts,
172 .putch = snd_output_stdio_putc,
173 .flush = snd_output_stdio_flush,
179 * \brief Creates a new output object using an existing stdio \c FILE pointer.
180 * \param outputp The function puts the pointer to the new output object
181 * at the address specified by \p outputp.
182 * \param fp The \c FILE pointer to write to. Characters are written
183 * to the file starting at the current file position.
184 * \param _close Close flag. Set this to 1 if #snd_output_close should close
185 * \p fp by calling \c fclose.
186 * \return Zero if successful, otherwise a negative error code.
188 int snd_output_stdio_attach(snd_output_t **outputp, FILE *fp, int _close)
190 snd_output_t *output;
191 snd_output_stdio_t *stdio;
192 assert(outputp && fp);
193 stdio = calloc(1, sizeof(*stdio));
196 output = calloc(1, sizeof(*output));
202 stdio->close = _close;
203 output->type = SND_OUTPUT_STDIO;
204 output->ops = &snd_output_stdio_ops;
205 output->private_data = stdio;
211 * \brief Creates a new output object writing to a file.
212 * \param outputp The function puts the pointer to the new output object
213 * at the address specified by \p outputp.
214 * \param file The name of the file to open.
215 * \param mode The open mode, like \c fopen(3).
216 * \return Zero if successful, otherwise a negative error code.
218 int snd_output_stdio_open(snd_output_t **outputp, const char *file, const char *mode)
221 FILE *fp = fopen(file, mode);
226 err = snd_output_stdio_attach(outputp, fp, 1);
234 typedef struct _snd_output_buffer {
238 } snd_output_buffer_t;
240 static int snd_output_buffer_close(snd_output_t *output)
242 snd_output_buffer_t *buffer = output->private_data;
248 static int snd_output_buffer_need(snd_output_t *output, size_t size)
250 snd_output_buffer_t *buffer = output->private_data;
251 size_t _free = buffer->alloc - buffer->size;
255 /* use 'size++' to allow to add the '\0' string terminator */
256 /* without reallocation */
260 if (buffer->alloc == 0)
263 alloc = buffer->alloc;
264 while (alloc < buffer->size + size)
266 buf = realloc(buffer->buf, alloc);
270 buffer->alloc = alloc;
271 return buffer->alloc - buffer->size;
274 static int snd_output_buffer_print(snd_output_t *output, const char *format, va_list args)
276 snd_output_buffer_t *buffer = output->private_data;
279 result = snd_output_buffer_need(output, size);
282 result = vsnprintf((char *)buffer->buf + buffer->size, size, format, args);
284 if ((size_t)result <= size) {
285 buffer->size += result;
289 result = snd_output_buffer_need(output, size);
292 result = vsnprintf((char *)buffer->buf + buffer->size, result, format, args);
293 assert(result == (int)size);
294 buffer->size += result;
298 static int snd_output_buffer_puts(snd_output_t *output, const char *str)
300 snd_output_buffer_t *buffer = output->private_data;
301 size_t size = strlen(str);
303 err = snd_output_buffer_need(output, size);
306 memcpy(buffer->buf + buffer->size, str, size);
307 buffer->size += size;
311 static int snd_output_buffer_putc(snd_output_t *output, int c)
313 snd_output_buffer_t *buffer = output->private_data;
315 err = snd_output_buffer_need(output, 1);
318 buffer->buf[buffer->size++] = c;
322 static int snd_output_buffer_flush(snd_output_t *output ATTRIBUTE_UNUSED)
324 snd_output_buffer_t *buffer = output->private_data;
329 static const snd_output_ops_t snd_output_buffer_ops = {
330 .close = snd_output_buffer_close,
331 .print = snd_output_buffer_print,
332 .puts = snd_output_buffer_puts,
333 .putch = snd_output_buffer_putc,
334 .flush = snd_output_buffer_flush,
339 * \brief Returns the address of the buffer of a #SND_OUTPUT_BUFFER output handle.
340 * \param output The output handle.
341 * \param buf The functions puts the current address of the buffer at the
342 * address specified by \p buf.
343 * \return The current size of valid data in the buffer.
345 * The address of the buffer may become invalid when output functions or
346 * #snd_output_close are called.
348 size_t snd_output_buffer_string(snd_output_t *output, char **buf)
350 snd_output_buffer_t *buffer = output->private_data;
351 *buf = (char *)buffer->buf;
356 * \brief Returns the address of the buffer of a #SND_OUTPUT_BUFFER output handle.
357 * \param output The output handle.
358 * \param buf The functions puts the current address of the buffer at the
359 * address specified by \p buf.
360 * \return The current size of valid data in the buffer.
362 * The internal buffer is empty after this call. The caller has the responsibility
363 * to clean the buffer using the free() call.
365 size_t snd_output_buffer_steal(snd_output_t *output, char **buf)
367 snd_output_buffer_t *buffer = output->private_data;
369 *buf = (char *)buffer->buf;
378 * \brief Creates a new output object with an auto-extending memory buffer.
379 * \param outputp The function puts the pointer to the new output object
380 * at the address specified by \p outputp.
381 * \return Zero if successful, otherwise a negative error code.
383 int snd_output_buffer_open(snd_output_t **outputp)
385 snd_output_t *output;
386 snd_output_buffer_t *buffer;
388 buffer = calloc(1, sizeof(*buffer));
391 output = calloc(1, sizeof(*output));
399 output->type = SND_OUTPUT_BUFFER;
400 output->ops = &snd_output_buffer_ops;
401 output->private_data = buffer;