Blame src/input.c

Packit 4a16fb
/**
Packit 4a16fb
 * \file input.c
Packit 4a16fb
 * \brief Generic stdio-like input interface
Packit 4a16fb
 * \author Abramo Bagnara <abramo@alsa-project.org>
Packit 4a16fb
 * \date 2000
Packit 4a16fb
 *
Packit 4a16fb
 * Generic stdio-like input interface
Packit 4a16fb
 */
Packit 4a16fb
/*
Packit 4a16fb
 *  Input 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
Packit 4a16fb
typedef struct _snd_input_ops {
Packit 4a16fb
	int (*close)(snd_input_t *input);
Packit 4a16fb
	int (*scan)(snd_input_t *input, const char *format, va_list args);
Packit 4a16fb
	char *(*(gets))(snd_input_t *input, char *str, size_t size);
Packit 4a16fb
	int (*getch)(snd_input_t *input);
Packit 4a16fb
	int (*ungetch)(snd_input_t *input, int c);
Packit 4a16fb
} snd_input_ops_t;
Packit 4a16fb
Packit 4a16fb
struct _snd_input {
Packit 4a16fb
	snd_input_type_t type;
Packit 4a16fb
	const snd_input_ops_t *ops;
Packit 4a16fb
	void *private_data;
Packit 4a16fb
};
Packit 4a16fb
#endif
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Closes an input handle.
Packit 4a16fb
 * \param input The input handle to be closed.
Packit 4a16fb
 * \return Zero if successful, otherwise a negative error code.
Packit 4a16fb
 */
Packit 4a16fb
int snd_input_close(snd_input_t *input)
Packit 4a16fb
{
Packit 4a16fb
	int err = input->ops->close(input);
Packit 4a16fb
	free(input);
Packit 4a16fb
	return err;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Reads formatted input (like \c fscanf(3)) from an input handle.
Packit 4a16fb
 * \param input The input handle.
Packit 4a16fb
 * \param format Format string in \c fscanf format.
Packit 4a16fb
 * \param ... Other \c fscanf arguments.
Packit 4a16fb
 * \return The number of input items assigned, or \c EOF.
Packit 4a16fb
 *
Packit 4a16fb
 * \bug Reading from a memory buffer doesn't work.
Packit 4a16fb
 */
Packit 4a16fb
int snd_input_scanf(snd_input_t *input, const char *format, ...)
Packit 4a16fb
{
Packit 4a16fb
	int result;
Packit 4a16fb
	va_list args;
Packit 4a16fb
	va_start(args, format);
Packit 4a16fb
	result = input->ops->scan(input, format, args);
Packit 4a16fb
	va_end(args);
Packit 4a16fb
	return result;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Reads a line from an input handle (like \c fgets(3)).
Packit 4a16fb
 * \param input The input handle.
Packit 4a16fb
 * \param str Address of the destination buffer.
Packit 4a16fb
 * \param size The size of the destination buffer.
Packit 4a16fb
 * \return Pointer to the buffer if successful, otherwise \c NULL.
Packit 4a16fb
 *
Packit 4a16fb
 * Like \c fgets, the returned string is zero-terminated, and contains
Packit 4a16fb
 * the new-line character \c '\\n' if the line fits into the buffer.
Packit 4a16fb
 */
Packit 4a16fb
char *snd_input_gets(snd_input_t *input, char *str, size_t size)
Packit 4a16fb
{
Packit 4a16fb
	return (input->ops->gets)(input, str, size);
Packit 4a16fb
}
Packit 4a16fb
			
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Reads a character from an input handle (like \c fgetc(3)).
Packit 4a16fb
 * \param input The input handle.
Packit 4a16fb
 * \return The character read, or \c EOF on end of file or error.
Packit 4a16fb
 */
Packit 4a16fb
int snd_input_getc(snd_input_t *input)
Packit 4a16fb
{
Packit 4a16fb
	return input->ops->getch(input);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Puts the last character read back to an input handle (like \c ungetc(3)).
Packit 4a16fb
 * \param input The input handle.
Packit 4a16fb
 * \param c The character to push back.
Packit 4a16fb
 * \return The character pushed back, or \c EOF on error.
Packit 4a16fb
 */
Packit 4a16fb
int snd_input_ungetc(snd_input_t *input, int c)
Packit 4a16fb
{
Packit 4a16fb
	return input->ops->ungetch(input, c);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
#ifndef DOC_HIDDEN
Packit 4a16fb
typedef struct _snd_input_stdio {
Packit 4a16fb
	int close;
Packit 4a16fb
	FILE *fp;
Packit 4a16fb
} snd_input_stdio_t;
Packit 4a16fb
Packit 4a16fb
static int snd_input_stdio_close(snd_input_t *input ATTRIBUTE_UNUSED)
Packit 4a16fb
{
Packit 4a16fb
	snd_input_stdio_t *stdio = input->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_input_stdio_scan(snd_input_t *input, const char *format, va_list args)
Packit 4a16fb
{
Packit 4a16fb
	snd_input_stdio_t *stdio = input->private_data;
Packit 4a16fb
	extern int vfscanf(FILE *, const char *, va_list);
Packit 4a16fb
	return vfscanf(stdio->fp, format, args);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static char *snd_input_stdio_gets(snd_input_t *input, char *str, size_t size)
Packit 4a16fb
{
Packit 4a16fb
	snd_input_stdio_t *stdio = input->private_data;
Packit 4a16fb
	return fgets(str, (int) size, stdio->fp);
Packit 4a16fb
}
Packit 4a16fb
			
Packit 4a16fb
static int snd_input_stdio_getc(snd_input_t *input)
Packit 4a16fb
{
Packit 4a16fb
	snd_input_stdio_t *stdio = input->private_data;
Packit 4a16fb
	return getc(stdio->fp);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_input_stdio_ungetc(snd_input_t *input, int c)
Packit 4a16fb
{
Packit 4a16fb
	snd_input_stdio_t *stdio = input->private_data;
Packit 4a16fb
	return ungetc(c, stdio->fp);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static const snd_input_ops_t snd_input_stdio_ops = {
Packit 4a16fb
	.close		= snd_input_stdio_close,
Packit 4a16fb
	.scan		= snd_input_stdio_scan,
Packit 4a16fb
	.gets		= snd_input_stdio_gets,
Packit 4a16fb
	.getch		= snd_input_stdio_getc,
Packit 4a16fb
	.ungetch	= snd_input_stdio_ungetc,
Packit 4a16fb
};
Packit 4a16fb
#endif
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Creates a new input object using an existing stdio \c FILE pointer.
Packit 4a16fb
 * \param inputp The function puts the pointer to the new input object
Packit 4a16fb
 *               at the address specified by \p inputp.
Packit 4a16fb
 * \param fp The \c FILE pointer to read from.
Packit 4a16fb
 *           Reading begins at the current file position.
Packit 4a16fb
 * \param _close Close flag. Set this to 1 if #snd_input_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_input_stdio_attach(snd_input_t **inputp, FILE *fp, int _close)
Packit 4a16fb
{
Packit 4a16fb
	snd_input_t *input;
Packit 4a16fb
	snd_input_stdio_t *stdio;
Packit 4a16fb
	assert(inputp && fp);
Packit 4a16fb
	stdio = calloc(1, sizeof(*stdio));
Packit 4a16fb
	if (!stdio)
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	input = calloc(1, sizeof(*input));
Packit 4a16fb
	if (!input) {
Packit 4a16fb
		free(stdio);
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	}
Packit 4a16fb
	stdio->fp = fp;
Packit 4a16fb
	stdio->close = _close;
Packit 4a16fb
	input->type = SND_INPUT_STDIO;
Packit 4a16fb
	input->ops = &snd_input_stdio_ops;
Packit 4a16fb
	input->private_data = stdio;
Packit 4a16fb
	*inputp = input;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
	
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Creates a new input object reading from a file.
Packit 4a16fb
 * \param inputp The functions puts the pointer to the new input object
Packit 4a16fb
 *               at the address specified by \p inputp.
Packit 4a16fb
 * \param file The name of the file to read from.
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_input_stdio_open(snd_input_t **inputp, 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_input_stdio_attach(inputp, 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_input_buffer {
Packit 4a16fb
	unsigned char *buf;
Packit 4a16fb
	unsigned char *ptr;
Packit 4a16fb
	size_t size;
Packit 4a16fb
} snd_input_buffer_t;
Packit 4a16fb
Packit 4a16fb
static int snd_input_buffer_close(snd_input_t *input)
Packit 4a16fb
{
Packit 4a16fb
	snd_input_buffer_t *buffer = input->private_data;
Packit 4a16fb
	free(buffer->buf);
Packit 4a16fb
	free(buffer);
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_input_buffer_scan(snd_input_t *input, const char *format, va_list args)
Packit 4a16fb
{
Packit 4a16fb
	snd_input_buffer_t *buffer = input->private_data;
Packit 4a16fb
	extern int vsscanf(const char *, const char *, va_list);
Packit 4a16fb
	/* FIXME: how can I obtain consumed chars count? */
Packit 4a16fb
	assert(0);
Packit 4a16fb
	return vsscanf((char *)buffer->ptr, format, args);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static char *snd_input_buffer_gets(snd_input_t *input, char *str, size_t size)
Packit 4a16fb
{
Packit 4a16fb
	snd_input_buffer_t *buffer = input->private_data;
Packit 4a16fb
	size_t bsize = buffer->size;
Packit 4a16fb
	while (--size > 0 && bsize > 0) {
Packit 4a16fb
		unsigned char c = *buffer->ptr++;
Packit 4a16fb
		bsize--;
Packit 4a16fb
		*str++ = c;
Packit 4a16fb
		if (c == '\n')
Packit 4a16fb
			break;
Packit 4a16fb
	}
Packit 4a16fb
	if (bsize == buffer->size)
Packit 4a16fb
		return NULL;
Packit 4a16fb
	buffer->size = bsize;
Packit 4a16fb
	*str = '\0';
Packit 4a16fb
	return str;
Packit 4a16fb
}
Packit 4a16fb
			
Packit 4a16fb
static int snd_input_buffer_getc(snd_input_t *input)
Packit 4a16fb
{
Packit 4a16fb
	snd_input_buffer_t *buffer = input->private_data;
Packit 4a16fb
	if (buffer->size == 0)
Packit 4a16fb
		return EOF;
Packit 4a16fb
	buffer->size--;
Packit 4a16fb
	return *buffer->ptr++;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_input_buffer_ungetc(snd_input_t *input, int c)
Packit 4a16fb
{
Packit 4a16fb
	snd_input_buffer_t *buffer = input->private_data;
Packit 4a16fb
	if (buffer->ptr == buffer->buf)
Packit 4a16fb
		return EOF;
Packit 4a16fb
	buffer->ptr--;
Packit 4a16fb
	assert(*buffer->ptr == (unsigned char) c);
Packit 4a16fb
	buffer->size++;
Packit 4a16fb
	return c;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static const snd_input_ops_t snd_input_buffer_ops = {
Packit 4a16fb
	.close		= snd_input_buffer_close,
Packit 4a16fb
	.scan		= snd_input_buffer_scan,
Packit 4a16fb
	.gets		= snd_input_buffer_gets,
Packit 4a16fb
	.getch		= snd_input_buffer_getc,
Packit 4a16fb
	.ungetch	= snd_input_buffer_ungetc,
Packit 4a16fb
};
Packit 4a16fb
#endif
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Creates a new input object from a memory buffer.
Packit 4a16fb
 * \param inputp The function puts the pointer to the new input object
Packit 4a16fb
 *               at the address specified by \p inputp.
Packit 4a16fb
 * \param buf Address of the input buffer.
Packit 4a16fb
 * \param size Size of the input buffer.
Packit 4a16fb
 * \return Zero if successful, otherwise a negative error code.
Packit 4a16fb
 *
Packit 4a16fb
 * This functions creates a copy of the input buffer, so the application is
Packit 4a16fb
 * not required to preserve the buffer after this function has been called.
Packit 4a16fb
 */
Packit 4a16fb
int snd_input_buffer_open(snd_input_t **inputp, const char *buf, ssize_t size)
Packit 4a16fb
{
Packit 4a16fb
	snd_input_t *input;
Packit 4a16fb
	snd_input_buffer_t *buffer;
Packit 4a16fb
	assert(inputp);
Packit 4a16fb
	buffer = calloc(1, sizeof(*buffer));
Packit 4a16fb
	if (!buffer)
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	input = calloc(1, sizeof(*input));
Packit 4a16fb
	if (!input) {
Packit 4a16fb
		free(buffer);
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	}
Packit 4a16fb
	if (size < 0)
Packit 4a16fb
		size = strlen(buf);
Packit 4a16fb
	buffer->buf = malloc((size_t)size + 1);
Packit 4a16fb
	if (!buffer->buf) {
Packit 4a16fb
		free(input);
Packit 4a16fb
		free(buffer);
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	}
Packit 4a16fb
	memcpy(buffer->buf, buf, (size_t) size);
Packit 4a16fb
	buffer->buf[size] = 0;
Packit 4a16fb
	buffer->ptr = buffer->buf;
Packit 4a16fb
	buffer->size = size;
Packit 4a16fb
	input->type = SND_INPUT_BUFFER;
Packit 4a16fb
	input->ops = &snd_input_buffer_ops;
Packit 4a16fb
	input->private_data = buffer;
Packit 4a16fb
	*inputp = input;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb