alsa-lib/src/input.c

323 lines
7.7 KiB
C
Raw Normal View History

2001-03-25 14:13:55 +00:00
/**
* \file input.c
* \author Abramo Bagnara <abramo@alsa-project.org>
* \date 2000
*
* Generic stdio-like input interface
*/
2001-01-17 11:00:32 +00:00
/*
* Input object
* Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
2001-03-25 14:13:55 +00:00
#ifndef DOC_HIDDEN
2001-01-17 11:00:32 +00:00
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "local.h"
typedef struct _snd_input_ops {
int (*close)(snd_input_t *input);
int (*scanf)(snd_input_t *input, const char *format, va_list args);
char *(*gets)(snd_input_t *input, char *str, size_t size);
int (*getch)(snd_input_t *input);
int (*ungetch)(snd_input_t *input, int c);
} snd_input_ops_t;
struct _snd_input {
snd_input_type_t type;
snd_input_ops_t *ops;
void *private_data;
2001-01-17 11:00:32 +00:00
};
2001-03-25 14:13:55 +00:00
#endif
2001-01-17 11:00:32 +00:00
2001-03-25 14:13:55 +00:00
/**
* \brief close input handle
* \param input Input handle
* \return 0 on success otherwise a negative error code
2001-03-25 14:13:55 +00:00
*/
2001-01-17 11:00:32 +00:00
int snd_input_close(snd_input_t *input)
{
int err = input->ops->close(input);
free(input);
return err;
}
2001-03-25 14:13:55 +00:00
/**
* \brief fscanf(3) like on an input handle
* \param input Input handle
* \param format fscanf format
* \param ... other fscanf arguments
* \return number of input items assigned otherwise a negative error code
2001-03-25 14:13:55 +00:00
*/
2001-01-17 11:00:32 +00:00
int snd_input_scanf(snd_input_t *input, const char *format, ...)
{
int result;
va_list args;
va_start(args, format);
result = input->ops->scanf(input, format, args);
va_end(args);
return result;
}
2001-03-25 14:13:55 +00:00
/**
* \brief fgets(3) like on an input handle
* \param input Input handle
* \param str Destination buffer pointer
* \param size Buffer size
* \return Pointer to buffer or NULL on error
*/
2001-01-17 11:00:32 +00:00
char *snd_input_gets(snd_input_t *input, char *str, size_t size)
{
return input->ops->gets(input, str, size);
}
2001-03-25 14:13:55 +00:00
/**
* \brief fgetc(3) like on an input handle
* \param input Input handle
* \return character read or EOF on end of file or error
*/
2001-01-17 11:00:32 +00:00
int snd_input_getc(snd_input_t *input)
{
return input->ops->getch(input);
}
2001-03-25 14:13:55 +00:00
/**
* \brief ungetc(3) like on an input handle
* \param input Input handle
* \param c Char to push back
* \return character pushed back or EOF on error
*/
2001-01-17 11:00:32 +00:00
int snd_input_ungetc(snd_input_t *input, int c)
{
return input->ops->ungetch(input, c);
}
2001-03-25 14:13:55 +00:00
#ifndef DOC_HIDDEN
2001-01-17 11:00:32 +00:00
typedef struct _snd_input_stdio {
int close;
FILE *fp;
} snd_input_stdio_t;
2001-03-25 14:13:55 +00:00
static int snd_input_stdio_close(snd_input_t *input ATTRIBUTE_UNUSED)
2001-01-17 11:00:32 +00:00
{
snd_input_stdio_t *stdio = input->private_data;
2001-01-17 11:00:32 +00:00
if (close)
fclose(stdio->fp);
free(stdio);
return 0;
}
2001-03-25 14:13:55 +00:00
static int snd_input_stdio_scanf(snd_input_t *input, const char *format, va_list args)
2001-01-17 11:00:32 +00:00
{
snd_input_stdio_t *stdio = input->private_data;
2001-03-29 17:50:28 +00:00
extern int vfscanf(FILE *, const char *, va_list);
2001-01-17 11:00:32 +00:00
return vfscanf(stdio->fp, format, args);
}
2001-03-25 14:13:55 +00:00
static char *snd_input_stdio_gets(snd_input_t *input, char *str, size_t size)
2001-01-17 11:00:32 +00:00
{
snd_input_stdio_t *stdio = input->private_data;
2001-03-29 17:50:28 +00:00
return fgets(str, (int) size, stdio->fp);
2001-01-17 11:00:32 +00:00
}
2001-03-25 14:13:55 +00:00
static int snd_input_stdio_getc(snd_input_t *input)
2001-01-17 11:00:32 +00:00
{
snd_input_stdio_t *stdio = input->private_data;
2001-01-17 11:00:32 +00:00
return getc(stdio->fp);
}
2001-03-25 14:13:55 +00:00
static int snd_input_stdio_ungetc(snd_input_t *input, int c)
2001-01-17 11:00:32 +00:00
{
snd_input_stdio_t *stdio = input->private_data;
2001-01-17 11:00:32 +00:00
return ungetc(c, stdio->fp);
}
2001-03-25 14:13:55 +00:00
static snd_input_ops_t snd_input_stdio_ops = {
2001-01-17 11:00:32 +00:00
close: snd_input_stdio_close,
scanf: snd_input_stdio_scanf,
gets: snd_input_stdio_gets,
getch: snd_input_stdio_getc,
ungetch: snd_input_stdio_ungetc,
};
2001-03-25 14:13:55 +00:00
#endif
2001-01-17 11:00:32 +00:00
2001-03-25 14:13:55 +00:00
/**
* \brief Create a new input using an existing stdio FILE pointer
* \param inputp Pointer to returned input handle
* \param fp FILE pointer
* \param close Close flag (1 if FILE is fclose'd when input handle is closed)
* \return 0 on success otherwise a negative error code
*/
2001-03-29 17:50:28 +00:00
int snd_input_stdio_attach(snd_input_t **inputp, FILE *fp, int _close)
2001-01-17 11:00:32 +00:00
{
snd_input_t *input;
snd_input_stdio_t *stdio;
assert(inputp && fp);
stdio = calloc(1, sizeof(*stdio));
if (!stdio)
return -ENOMEM;
input = calloc(1, sizeof(*input));
if (!input) {
free(stdio);
return -ENOMEM;
}
stdio->fp = fp;
2001-03-29 17:50:28 +00:00
stdio->close = _close;
2001-01-17 11:00:32 +00:00
input->type = SND_INPUT_STDIO;
input->ops = &snd_input_stdio_ops;
input->private_data = stdio;
2001-01-17 11:00:32 +00:00
*inputp = input;
return 0;
}
2001-03-25 14:13:55 +00:00
/**
* \brief Open a new input from a file
* \param inputp Pointer to returned input handle
* \param file File name
* \param mode fopen(3) open mode
2001-03-25 14:13:55 +00:00
* \return 0 on success otherwise a negative error code
*/
int snd_input_stdio_open(snd_input_t **inputp, const char *file, const char *mode)
2001-01-17 11:00:32 +00:00
{
int err;
FILE *fp = fopen(file, mode);
2001-01-17 11:00:32 +00:00
if (!fp) {
2001-01-17 19:04:56 +00:00
//SYSERR("fopen");
2001-01-17 11:00:32 +00:00
return -errno;
}
err = snd_input_stdio_attach(inputp, fp, 1);
if (err < 0)
fclose(fp);
return err;
}
2001-03-25 14:13:55 +00:00
#ifndef DOC_HIDDEN
2001-01-17 11:00:32 +00:00
typedef struct _snd_input_buffer {
unsigned char *buf;
unsigned char *ptr;
size_t size;
} snd_input_buffer_t;
2001-03-25 14:13:55 +00:00
static int snd_input_buffer_close(snd_input_t *input)
2001-01-17 11:00:32 +00:00
{
snd_input_buffer_t *buffer = input->private_data;
2001-01-17 11:00:32 +00:00
free(buffer->buf);
free(buffer);
return 0;
}
2001-03-25 14:13:55 +00:00
static int snd_input_buffer_scanf(snd_input_t *input, const char *format, va_list args)
2001-01-17 11:00:32 +00:00
{
snd_input_buffer_t *buffer = input->private_data;
2001-03-29 17:50:28 +00:00
extern int vsscanf(const char *, const char *, va_list);
2001-01-17 11:00:32 +00:00
/* FIXME: how can I obtain consumed chars count? */
assert(0);
return vsscanf(buffer->ptr, format, args);
}
2001-03-25 14:13:55 +00:00
static char *snd_input_buffer_gets(snd_input_t *input, char *str, size_t size)
2001-01-17 11:00:32 +00:00
{
snd_input_buffer_t *buffer = input->private_data;
2001-01-17 11:00:32 +00:00
size_t bsize = buffer->size;
while (--size > 0 && bsize > 0) {
unsigned char c = *buffer->ptr++;
bsize--;
*str++ = c;
if (c == '\n')
break;
}
if (bsize == buffer->size)
return NULL;
buffer->size = bsize;
*str = '\0';
return str;
}
2001-03-25 14:13:55 +00:00
static int snd_input_buffer_getc(snd_input_t *input)
2001-01-17 11:00:32 +00:00
{
snd_input_buffer_t *buffer = input->private_data;
2001-01-17 11:00:32 +00:00
if (buffer->size == 0)
return EOF;
buffer->size--;
return *buffer->ptr++;
}
2001-03-25 14:13:55 +00:00
static int snd_input_buffer_ungetc(snd_input_t *input, int c)
2001-01-17 11:00:32 +00:00
{
snd_input_buffer_t *buffer = input->private_data;
2001-01-17 11:00:32 +00:00
if (buffer->ptr == buffer->buf)
return EOF;
buffer->ptr--;
assert(*buffer->ptr == (unsigned char) c);
buffer->size++;
return c;
}
2001-03-25 14:13:55 +00:00
static snd_input_ops_t snd_input_buffer_ops = {
2001-01-17 11:00:32 +00:00
close: snd_input_buffer_close,
scanf: snd_input_buffer_scanf,
gets: snd_input_buffer_gets,
getch: snd_input_buffer_getc,
ungetch: snd_input_buffer_ungetc,
};
2001-03-25 14:13:55 +00:00
#endif
2001-01-17 11:00:32 +00:00
2001-03-25 14:13:55 +00:00
/**
* \brief Open a new input from a memory buffer
* \param inputp Pointer to returned input handle
* \param buf Buffer pointer
* \param size Buffer size
* \return 0 on success otherwise a negative error code
*/
2001-03-29 17:50:28 +00:00
int snd_input_buffer_open(snd_input_t **inputp, const char *buf, ssize_t size)
2001-01-17 11:00:32 +00:00
{
snd_input_t *input;
snd_input_buffer_t *buffer;
assert(inputp);
buffer = calloc(1, sizeof(*buffer));
if (!buffer)
return -ENOMEM;
input = calloc(1, sizeof(*input));
if (!input) {
free(buffer);
return -ENOMEM;
}
if (size < 0)
size = strlen(buf);
2001-03-29 17:50:28 +00:00
buffer->buf = malloc((size_t)size + 1);
2001-01-17 11:00:32 +00:00
if (!buffer->buf) {
free(input);
free(buffer);
return -ENOMEM;
}
2001-03-29 17:50:28 +00:00
memcpy(buffer->buf, buf, (size_t) size);
2001-01-17 11:00:32 +00:00
buffer->buf[size] = 0;
buffer->ptr = buffer->buf;
buffer->size = size;
input->type = SND_INPUT_BUFFER;
input->ops = &snd_input_buffer_ops;
input->private_data = buffer;
2001-01-17 11:00:32 +00:00
*inputp = input;
return 0;
}