Lines Matching +full:- +full:- +full:output +full:- +full:format
2 * \file output.c
3 * \brief Generic stdio-like output interface
4 * \author Abramo Bagnara <abramo@alsa-project.org>
7 * Generic stdio-like output interface
10 * Output object
11 * Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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);
53 * \brief Closes an output handle.
54 * \param output The output handle to be closed.
57 int snd_output_close(snd_output_t *output) in snd_output_close() argument
59 int err = output->ops->close(output); in snd_output_close()
60 free(output); in snd_output_close()
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.
71 int snd_output_printf(snd_output_t *output, const char *format, ...) in snd_output_printf() argument
75 va_start(args, format); in snd_output_printf()
76 result = output->ops->print(output, format, args); in snd_output_printf()
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.
88 int snd_output_vprintf(snd_output_t *output, const char *format, va_list args) in snd_output_vprintf() argument
90 return output->ops->print(output, format, args); in snd_output_vprintf()
94 * \brief Writes a string to an output handle (like \c fputs(3)).
95 * \param output The output handle.
99 int snd_output_puts(snd_output_t *output, const char *str) in snd_output_puts() argument
101 return output->ops->puts(output, str); in snd_output_puts()
105 * \brief Writes a character to an output handle (like \c putc(3)).
106 * \param output The output handle.
110 int snd_output_putc(snd_output_t *output, int c) in snd_output_putc() argument
112 return output->ops->putch(output, c); in snd_output_putc()
116 * \brief Flushes an output handle (like fflush(3)).
117 * \param output The output handle.
122 * position is reset to the beginning of the buffer. \c =:-o
124 int snd_output_flush(snd_output_t *output) in snd_output_flush() argument
126 return output->ops->flush(output); in snd_output_flush()
135 static int snd_output_stdio_close(snd_output_t *output) in snd_output_stdio_close() argument
137 snd_output_stdio_t *stdio = output->private_data; in snd_output_stdio_close()
138 if (stdio->close) in snd_output_stdio_close()
139 fclose(stdio->fp); in snd_output_stdio_close()
144 static int snd_output_stdio_print(snd_output_t *output, const char *format, va_list args) in snd_output_stdio_print() argument
146 snd_output_stdio_t *stdio = output->private_data; in snd_output_stdio_print()
147 return vfprintf(stdio->fp, format, args); in snd_output_stdio_print()
150 static int snd_output_stdio_puts(snd_output_t *output, const char *str) in snd_output_stdio_puts() argument
152 snd_output_stdio_t *stdio = output->private_data; in snd_output_stdio_puts()
153 return fputs(str, stdio->fp); in snd_output_stdio_puts()
156 static int snd_output_stdio_putc(snd_output_t *output, int c) in snd_output_stdio_putc() argument
158 snd_output_stdio_t *stdio = output->private_data; in snd_output_stdio_putc()
159 return putc(c, stdio->fp); in snd_output_stdio_putc()
162 static int snd_output_stdio_flush(snd_output_t *output) in snd_output_stdio_flush() argument
164 snd_output_stdio_t *stdio = output->private_data; in snd_output_stdio_flush()
165 return fflush(stdio->fp); in 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
190 snd_output_t *output; in snd_output_stdio_attach() local
195 return -ENOMEM; in snd_output_stdio_attach()
196 output = calloc(1, sizeof(*output)); in snd_output_stdio_attach()
197 if (!output) { in snd_output_stdio_attach()
199 return -ENOMEM; in snd_output_stdio_attach()
201 stdio->fp = fp; in snd_output_stdio_attach()
202 stdio->close = _close; in snd_output_stdio_attach()
203 output->type = SND_OUTPUT_STDIO; in snd_output_stdio_attach()
204 output->ops = &snd_output_stdio_ops; in snd_output_stdio_attach()
205 output->private_data = stdio; in snd_output_stdio_attach()
206 *outputp = output; in snd_output_stdio_attach()
211 * \brief Creates a new output object writing to a file.
212 * \param outputp The function puts the pointer to the new output object
224 return -errno; in snd_output_stdio_open()
240 static int snd_output_buffer_close(snd_output_t *output) in snd_output_buffer_close() argument
242 snd_output_buffer_t *buffer = output->private_data; in snd_output_buffer_close()
243 free(buffer->buf); in snd_output_buffer_close()
248 static int snd_output_buffer_need(snd_output_t *output, size_t size) in snd_output_buffer_need() argument
250 snd_output_buffer_t *buffer = output->private_data; in snd_output_buffer_need()
251 size_t _free = buffer->alloc - buffer->size; in snd_output_buffer_need()
260 if (buffer->alloc == 0) in snd_output_buffer_need()
263 alloc = buffer->alloc; in snd_output_buffer_need()
264 while (alloc < buffer->size + size) in snd_output_buffer_need()
266 buf = realloc(buffer->buf, alloc); in snd_output_buffer_need()
268 return -ENOMEM; in snd_output_buffer_need()
269 buffer->buf = buf; in snd_output_buffer_need()
270 buffer->alloc = alloc; in snd_output_buffer_need()
271 return buffer->alloc - buffer->size; in snd_output_buffer_need()
274 static int snd_output_buffer_print(snd_output_t *output, const char *format, va_list args) in snd_output_buffer_print() argument
276 snd_output_buffer_t *buffer = output->private_data; in snd_output_buffer_print()
279 result = snd_output_buffer_need(output, size); in snd_output_buffer_print()
282 result = vsnprintf((char *)buffer->buf + buffer->size, size, format, args); in snd_output_buffer_print()
285 buffer->size += result; in snd_output_buffer_print()
289 result = snd_output_buffer_need(output, size); in snd_output_buffer_print()
292 result = vsnprintf((char *)buffer->buf + buffer->size, result, format, args); in snd_output_buffer_print()
294 buffer->size += result; in snd_output_buffer_print()
298 static int snd_output_buffer_puts(snd_output_t *output, const char *str) in snd_output_buffer_puts() argument
300 snd_output_buffer_t *buffer = output->private_data; in snd_output_buffer_puts()
303 err = snd_output_buffer_need(output, size); in snd_output_buffer_puts()
306 memcpy(buffer->buf + buffer->size, str, size); in snd_output_buffer_puts()
307 buffer->size += size; in snd_output_buffer_puts()
311 static int snd_output_buffer_putc(snd_output_t *output, int c) in snd_output_buffer_putc() argument
313 snd_output_buffer_t *buffer = output->private_data; in snd_output_buffer_putc()
315 err = snd_output_buffer_need(output, 1); in snd_output_buffer_putc()
318 buffer->buf[buffer->size++] = c; in snd_output_buffer_putc()
322 static int snd_output_buffer_flush(snd_output_t *output ATTRIBUTE_UNUSED) in snd_output_buffer_flush()
324 snd_output_buffer_t *buffer = output->private_data; in snd_output_buffer_flush()
325 buffer->size = 0; in 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.
345 * The address of the buffer may become invalid when output functions or
348 size_t snd_output_buffer_string(snd_output_t *output, char **buf) in snd_output_buffer_string() argument
350 snd_output_buffer_t *buffer = output->private_data; in snd_output_buffer_string()
351 *buf = (char *)buffer->buf; in snd_output_buffer_string()
352 return buffer->size; in snd_output_buffer_string()
356 * \brief Returns the address of the buffer of a #SND_OUTPUT_BUFFER output handle.
357 * \param output The output handle.
365 size_t snd_output_buffer_steal(snd_output_t *output, char **buf) in snd_output_buffer_steal() argument
367 snd_output_buffer_t *buffer = output->private_data; in snd_output_buffer_steal()
369 *buf = (char *)buffer->buf; in snd_output_buffer_steal()
370 size = buffer->size; in snd_output_buffer_steal()
371 buffer->buf = NULL; in snd_output_buffer_steal()
372 buffer->alloc = 0; in snd_output_buffer_steal()
373 buffer->size = 0; in snd_output_buffer_steal()
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
385 snd_output_t *output; in snd_output_buffer_open() local
390 return -ENOMEM; in snd_output_buffer_open()
391 output = calloc(1, sizeof(*output)); in snd_output_buffer_open()
392 if (!output) { in snd_output_buffer_open()
394 return -ENOMEM; in snd_output_buffer_open()
396 buffer->buf = NULL; in snd_output_buffer_open()
397 buffer->alloc = 0; in snd_output_buffer_open()
398 buffer->size = 0; in snd_output_buffer_open()
399 output->type = SND_OUTPUT_BUFFER; in snd_output_buffer_open()
400 output->ops = &snd_output_buffer_ops; in snd_output_buffer_open()
401 output->private_data = buffer; in snd_output_buffer_open()
402 *outputp = output; in snd_output_buffer_open()