diff --git a/include/output.h b/include/output.h index 74ff78a2..4a970dc4 100644 --- a/include/output.h +++ b/include/output.h @@ -65,6 +65,7 @@ int snd_output_stdio_open(snd_output_t **outputp, const char *file, const char * int snd_output_stdio_attach(snd_output_t **outputp, FILE *fp, int _close); int snd_output_buffer_open(snd_output_t **outputp); size_t snd_output_buffer_string(snd_output_t *output, char **buf); +size_t snd_output_buffer_steal(snd_output_t *output, char **buf); int snd_output_close(snd_output_t *output); int snd_output_printf(snd_output_t *output, const char *format, ...) #ifndef DOC_HIDDEN diff --git a/src/output.c b/src/output.c index 7e3a91b3..70e6d65d 100644 --- a/src/output.c +++ b/src/output.c @@ -252,6 +252,9 @@ static int snd_output_buffer_need(snd_output_t *output, size_t size) size_t alloc; unsigned char *buf; + /* use 'size++' to allow to add the '\0' string terminator */ + /* without reallocation */ + size++; if (_free >= size) return _free; if (buffer->alloc == 0) @@ -349,6 +352,28 @@ size_t snd_output_buffer_string(snd_output_t *output, char **buf) return buffer->size; } +/** + * \brief Returns the address of the buffer of a #SND_OUTPUT_BUFFER output handle. + * \param output The output handle. + * \param buf The functions puts the current address of the buffer at the + * address specified by \p buf. + * \return The current size of valid data in the buffer. + * + * The internal buffer is empty after this call. The caller has the responsibility + * to clean the buffer using the free() call. + */ +size_t snd_output_buffer_steal(snd_output_t *output, char **buf) +{ + snd_output_buffer_t *buffer = output->private_data; + size_t size; + *buf = (char *)buffer->buf; + size = buffer->size; + buffer->buf = NULL; + buffer->alloc = 0; + buffer->size = 0; + return size; +} + /** * \brief Creates a new output object with an auto-extending memory buffer. * \param outputp The function puts the pointer to the new output object @@ -377,4 +402,3 @@ int snd_output_buffer_open(snd_output_t **outputp) *outputp = output; return 0; } -