Blame src/output.c

Packit 4a16fb
/**
Packit 4a16fb
 * \file output.c
Packit 4a16fb
 * \brief Generic stdio-like output interface
Packit 4a16fb
 * \author Abramo Bagnara <abramo@alsa-project.org>
Packit 4a16fb
 * \date 2000
Packit 4a16fb
 *
Packit 4a16fb
 * Generic stdio-like output interface
Packit 4a16fb
 */
Packit 4a16fb
/*
Packit 4a16fb
 *  Output object
Packit 4a16fb
 *  Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
Packit 4a16fb
 *
Packit 4a16fb
 *
Packit 4a16fb
 *   This library is free software; you can redistribute it and/or modify
Packit 4a16fb
 *   it under the terms of the GNU Lesser General Public License as
Packit 4a16fb
 *   published by the Free Software Foundation; either version 2.1 of
Packit 4a16fb
 *   the License, or (at your option) any later version.
Packit 4a16fb
 *
Packit 4a16fb
 *   This program is distributed in the hope that it will be useful,
Packit 4a16fb
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 4a16fb
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 4a16fb
 *   GNU Lesser General Public License for more details.
Packit 4a16fb
 *
Packit 4a16fb
 *   You should have received a copy of the GNU Lesser General Public
Packit 4a16fb
 *   License along with this library; if not, write to the Free Software
Packit 4a16fb
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Packit 4a16fb
 *
Packit 4a16fb
 */
Packit 4a16fb
Packit 4a16fb
#include <stdarg.h>
Packit 4a16fb
#include <stdio.h>
Packit 4a16fb
#include <stdlib.h>
Packit 4a16fb
#include <unistd.h>
Packit 4a16fb
#include "local.h"
Packit 4a16fb
Packit 4a16fb
#ifndef DOC_HIDDEN
Packit 4a16fb
typedef struct _snd_output_ops {
Packit 4a16fb
	int (*close)(snd_output_t *output);
Packit 4a16fb
	int (*print)(snd_output_t *output, const char *format, va_list args);
Packit 4a16fb
	int (*puts)(snd_output_t *output, const char *str);
Packit 4a16fb
	int (*putch)(snd_output_t *output, int c);
Packit 4a16fb
	int (*flush)(snd_output_t *output);
Packit 4a16fb
} snd_output_ops_t;
Packit 4a16fb
Packit 4a16fb
struct _snd_output {
Packit 4a16fb
	snd_output_type_t type;
Packit 4a16fb
	const snd_output_ops_t *ops;
Packit 4a16fb
	void *private_data;
Packit 4a16fb
};
Packit 4a16fb
#endif
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Closes an output handle.
Packit 4a16fb
 * \param output The output handle to be closed.
Packit 4a16fb
 * \return Zero if successful, otherwise a negative error code.
Packit 4a16fb
 */
Packit 4a16fb
int snd_output_close(snd_output_t *output)
Packit 4a16fb
{
Packit 4a16fb
	int err = output->ops->close(output);
Packit 4a16fb
	free(output);
Packit 4a16fb
	return err;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Writes formatted output (like \c fprintf(3)) to an output handle.
Packit 4a16fb
 * \param output The output handle.
Packit 4a16fb
 * \param format Format string in \c fprintf format.
Packit 4a16fb
 * \param ... Other \c fprintf arguments.
Packit 4a16fb
 * \return The number of characters written, or a negative error code.
Packit 4a16fb
 */
Packit 4a16fb
int snd_output_printf(snd_output_t *output, const char *format, ...)
Packit 4a16fb
{
Packit 4a16fb
	int result;
Packit 4a16fb
	va_list args;
Packit 4a16fb
	va_start(args, format);
Packit 4a16fb
	result = output->ops->print(output, format, args);
Packit 4a16fb
	va_end(args);
Packit 4a16fb
	return result;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Writes formatted output (like \c fprintf(3)) to an output handle.
Packit 4a16fb
 * \param output The output handle.
Packit 4a16fb
 * \param format Format string in \c fprintf format.
Packit 4a16fb
 * \param args Other \c fprintf arguments.
Packit 4a16fb
 * \return The number of characters written, or a negative error code.
Packit 4a16fb
 */
Packit 4a16fb
int snd_output_vprintf(snd_output_t *output, const char *format, va_list args)
Packit 4a16fb
{
Packit 4a16fb
	return output->ops->print(output, format, args);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Writes a string to an output handle (like \c fputs(3)).
Packit 4a16fb
 * \param output The output handle.
Packit 4a16fb
 * \param str Pointer to the string.
Packit 4a16fb
 * \return Zero if successful, otherwise a negative error code or \c EOF.
Packit 4a16fb
 */
Packit 4a16fb
int snd_output_puts(snd_output_t *output, const char *str)
Packit 4a16fb
{
Packit 4a16fb
	return output->ops->puts(output, str);
Packit 4a16fb
}
Packit 4a16fb
			
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Writes a character to an output handle (like \c putc(3)).
Packit 4a16fb
 * \param output The output handle.
Packit 4a16fb
 * \param c The character.
Packit 4a16fb
 * \return Zero if successful, otherwise a negative error code or \c EOF.
Packit 4a16fb
 */
Packit 4a16fb
int snd_output_putc(snd_output_t *output, int c)
Packit 4a16fb
{
Packit 4a16fb
	return output->ops->putch(output, c);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Flushes an output handle (like fflush(3)).
Packit 4a16fb
 * \param output The output handle.
Packit 4a16fb
 * \return Zero if successful, otherwise \c EOF.
Packit 4a16fb
 *
Packit 4a16fb
 * If the underlying destination is a stdio stream, this function calls
Packit 4a16fb
 * \c fflush. If the underlying destination is a memory buffer, the write
Packit 4a16fb
 * position is reset to the beginning of the buffer. \c =:-o
Packit 4a16fb
 */
Packit 4a16fb
int snd_output_flush(snd_output_t *output)
Packit 4a16fb
{
Packit 4a16fb
	return output->ops->flush(output);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
#ifndef DOC_HIDDEN
Packit 4a16fb
typedef struct _snd_output_stdio {
Packit 4a16fb
	int close;
Packit 4a16fb
	FILE *fp;
Packit 4a16fb
} snd_output_stdio_t;
Packit 4a16fb
Packit 4a16fb
static int snd_output_stdio_close(snd_output_t *output)
Packit 4a16fb
{
Packit 4a16fb
	snd_output_stdio_t *stdio = output->private_data;
Packit 4a16fb
	if (stdio->close)
Packit 4a16fb
		fclose(stdio->fp);
Packit 4a16fb
	free(stdio);
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_output_stdio_print(snd_output_t *output, const char *format, va_list args)
Packit 4a16fb
{
Packit 4a16fb
	snd_output_stdio_t *stdio = output->private_data;
Packit 4a16fb
	return vfprintf(stdio->fp, format, args);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_output_stdio_puts(snd_output_t *output, const char *str)
Packit 4a16fb
{
Packit 4a16fb
	snd_output_stdio_t *stdio = output->private_data;
Packit 4a16fb
	return fputs(str, stdio->fp);
Packit 4a16fb
}
Packit 4a16fb
			
Packit 4a16fb
static int snd_output_stdio_putc(snd_output_t *output, int c)
Packit 4a16fb
{
Packit 4a16fb
	snd_output_stdio_t *stdio = output->private_data;
Packit 4a16fb
	return putc(c, stdio->fp);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_output_stdio_flush(snd_output_t *output)
Packit 4a16fb
{
Packit 4a16fb
	snd_output_stdio_t *stdio = output->private_data;
Packit 4a16fb
	return fflush(stdio->fp);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static const snd_output_ops_t snd_output_stdio_ops = {
Packit 4a16fb
	.close		= snd_output_stdio_close,
Packit 4a16fb
	.print		= snd_output_stdio_print,
Packit 4a16fb
	.puts		= snd_output_stdio_puts,
Packit 4a16fb
	.putch		= snd_output_stdio_putc,
Packit 4a16fb
	.flush		= snd_output_stdio_flush,
Packit 4a16fb
};
Packit 4a16fb
Packit 4a16fb
#endif
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Creates a new output object using an existing stdio \c FILE pointer.
Packit 4a16fb
 * \param outputp The function puts the pointer to the new output object
Packit 4a16fb
 *                at the address specified by \p outputp.
Packit 4a16fb
 * \param fp The \c FILE pointer to write to. Characters are written
Packit 4a16fb
 *           to the file starting at the current file position.
Packit 4a16fb
 * \param _close Close flag. Set this to 1 if #snd_output_close should close
Packit 4a16fb
 *              \p fp by calling \c fclose.
Packit 4a16fb
 * \return Zero if successful, otherwise a negative error code.
Packit 4a16fb
 */
Packit 4a16fb
int snd_output_stdio_attach(snd_output_t **outputp, FILE *fp, int _close)
Packit 4a16fb
{
Packit 4a16fb
	snd_output_t *output;
Packit 4a16fb
	snd_output_stdio_t *stdio;
Packit 4a16fb
	assert(outputp && fp);
Packit 4a16fb
	stdio = calloc(1, sizeof(*stdio));
Packit 4a16fb
	if (!stdio)
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	output = calloc(1, sizeof(*output));
Packit 4a16fb
	if (!output) {
Packit 4a16fb
		free(stdio);
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	}
Packit 4a16fb
	stdio->fp = fp;
Packit 4a16fb
	stdio->close = _close;
Packit 4a16fb
	output->type = SND_OUTPUT_STDIO;
Packit 4a16fb
	output->ops = &snd_output_stdio_ops;
Packit 4a16fb
	output->private_data = stdio;
Packit 4a16fb
	*outputp = output;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
	
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Creates a new output object writing to a file.
Packit 4a16fb
 * \param outputp The function puts the pointer to the new output object
Packit 4a16fb
 *                at the address specified by \p outputp.
Packit 4a16fb
 * \param file The name of the file to open.
Packit 4a16fb
 * \param mode The open mode, like \c fopen(3).
Packit 4a16fb
 * \return Zero if successful, otherwise a negative error code.
Packit 4a16fb
 */
Packit 4a16fb
int snd_output_stdio_open(snd_output_t **outputp, const char *file, const char *mode)
Packit 4a16fb
{
Packit 4a16fb
	int err;
Packit 4a16fb
	FILE *fp = fopen(file, mode);
Packit 4a16fb
	if (!fp) {
Packit 4a16fb
		//SYSERR("fopen");
Packit 4a16fb
		return -errno;
Packit 4a16fb
	}
Packit 4a16fb
	err = snd_output_stdio_attach(outputp, fp, 1);
Packit 4a16fb
	if (err < 0)
Packit 4a16fb
		fclose(fp);
Packit 4a16fb
	return err;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
#ifndef DOC_HIDDEN
Packit 4a16fb
Packit 4a16fb
typedef struct _snd_output_buffer {
Packit 4a16fb
	unsigned char *buf;
Packit 4a16fb
	size_t alloc;
Packit 4a16fb
	size_t size;
Packit 4a16fb
} snd_output_buffer_t;
Packit 4a16fb
Packit 4a16fb
static int snd_output_buffer_close(snd_output_t *output)
Packit 4a16fb
{
Packit 4a16fb
	snd_output_buffer_t *buffer = output->private_data;
Packit 4a16fb
	free(buffer->buf);
Packit 4a16fb
	free(buffer);
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_output_buffer_need(snd_output_t *output, size_t size)
Packit 4a16fb
{
Packit 4a16fb
	snd_output_buffer_t *buffer = output->private_data;
Packit 4a16fb
	size_t _free = buffer->alloc - buffer->size;
Packit 4a16fb
	size_t alloc;
Packit 4a16fb
	unsigned char *buf;
Packit 4a16fb
Packit 4a16fb
	if (_free >= size)
Packit 4a16fb
		return _free;
Packit 4a16fb
	if (buffer->alloc == 0)
Packit 4a16fb
		alloc = 256;
Packit 4a16fb
	else
Packit 4a16fb
		alloc = buffer->alloc;
Packit 4a16fb
	while (alloc < buffer->size + size)
Packit 4a16fb
		alloc *= 2;
Packit 4a16fb
	buf = realloc(buffer->buf, alloc);
Packit 4a16fb
	if (!buf)
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	buffer->buf = buf;
Packit 4a16fb
	buffer->alloc = alloc;
Packit 4a16fb
	return buffer->alloc - buffer->size;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_output_buffer_print(snd_output_t *output, const char *format, va_list args)
Packit 4a16fb
{
Packit 4a16fb
	snd_output_buffer_t *buffer = output->private_data;
Packit 4a16fb
	size_t size = 256;
Packit 4a16fb
	int result;
Packit 4a16fb
	result = snd_output_buffer_need(output, size);
Packit 4a16fb
	if (result < 0)
Packit 4a16fb
		return result;
Packit 4a16fb
	result = vsnprintf((char *)buffer->buf + buffer->size, size, format, args);
Packit 4a16fb
	assert(result >= 0);
Packit 4a16fb
	if ((size_t)result <= size) {
Packit 4a16fb
		buffer->size += result;
Packit 4a16fb
		return result;
Packit 4a16fb
	}
Packit 4a16fb
	size = result;
Packit 4a16fb
	result = snd_output_buffer_need(output, size);
Packit 4a16fb
	if (result < 0)
Packit 4a16fb
		return result;
Packit 4a16fb
	result = vsnprintf((char *)buffer->buf + buffer->size, result, format, args);
Packit 4a16fb
	assert(result == (int)size);
Packit 4a16fb
	buffer->size += result;
Packit 4a16fb
	return result;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_output_buffer_puts(snd_output_t *output, const char *str)
Packit 4a16fb
{
Packit 4a16fb
	snd_output_buffer_t *buffer = output->private_data;
Packit 4a16fb
	size_t size = strlen(str);
Packit 4a16fb
	int err;
Packit 4a16fb
	err = snd_output_buffer_need(output, size);
Packit 4a16fb
	if (err < 0)
Packit 4a16fb
		return err;
Packit 4a16fb
	memcpy(buffer->buf + buffer->size, str, size);
Packit 4a16fb
	buffer->size += size;
Packit 4a16fb
	return size;
Packit 4a16fb
}
Packit 4a16fb
			
Packit 4a16fb
static int snd_output_buffer_putc(snd_output_t *output, int c)
Packit 4a16fb
{
Packit 4a16fb
	snd_output_buffer_t *buffer = output->private_data;
Packit 4a16fb
	int err;
Packit 4a16fb
	err = snd_output_buffer_need(output, 1);
Packit 4a16fb
	if (err < 0)
Packit 4a16fb
		return err;
Packit 4a16fb
	buffer->buf[buffer->size++] = c;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_output_buffer_flush(snd_output_t *output ATTRIBUTE_UNUSED)
Packit 4a16fb
{
Packit 4a16fb
	snd_output_buffer_t *buffer = output->private_data;
Packit 4a16fb
	buffer->size = 0;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static const snd_output_ops_t snd_output_buffer_ops = {
Packit 4a16fb
	.close		= snd_output_buffer_close,
Packit 4a16fb
	.print		= snd_output_buffer_print,
Packit 4a16fb
	.puts		= snd_output_buffer_puts,
Packit 4a16fb
	.putch		= snd_output_buffer_putc,
Packit 4a16fb
	.flush		= snd_output_buffer_flush,
Packit 4a16fb
};
Packit 4a16fb
#endif
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Returns the address of the buffer of a #SND_OUTPUT_BUFFER output handle.
Packit 4a16fb
 * \param output The output handle.
Packit 4a16fb
 * \param buf The functions puts the current address of the buffer at the
Packit 4a16fb
 *            address specified by \p buf.
Packit 4a16fb
 * \return The current size of valid data in the buffer.
Packit 4a16fb
 *
Packit 4a16fb
 * The address of the buffer may become invalid when output functions or
Packit 4a16fb
 * #snd_output_close are called.
Packit 4a16fb
 */
Packit 4a16fb
size_t snd_output_buffer_string(snd_output_t *output, char **buf)
Packit 4a16fb
{
Packit 4a16fb
	snd_output_buffer_t *buffer = output->private_data;
Packit 4a16fb
	*buf = (char *)buffer->buf;
Packit 4a16fb
	return buffer->size;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Creates a new output object with an auto-extending memory buffer.
Packit 4a16fb
 * \param outputp The function puts the pointer to the new output object
Packit 4a16fb
 *                at the address specified by \p outputp.
Packit 4a16fb
 * \return Zero if successful, otherwise a negative error code.
Packit 4a16fb
 */
Packit 4a16fb
int snd_output_buffer_open(snd_output_t **outputp)
Packit 4a16fb
{
Packit 4a16fb
	snd_output_t *output;
Packit 4a16fb
	snd_output_buffer_t *buffer;
Packit 4a16fb
	assert(outputp);
Packit 4a16fb
	buffer = calloc(1, sizeof(*buffer));
Packit 4a16fb
	if (!buffer)
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	output = calloc(1, sizeof(*output));
Packit 4a16fb
	if (!output) {
Packit 4a16fb
		free(buffer);
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	}
Packit 4a16fb
	buffer->buf = NULL;
Packit 4a16fb
	buffer->alloc = 0;
Packit 4a16fb
	buffer->size = 0;
Packit 4a16fb
	output->type = SND_OUTPUT_BUFFER;
Packit 4a16fb
	output->ops = &snd_output_buffer_ops;
Packit 4a16fb
	output->private_data = buffer;
Packit 4a16fb
	*outputp = output;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb