File: input.c

package info (click to toggle)
alsa-lib 1.1.3-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 8,656 kB
  • sloc: ansic: 84,132; sh: 11,192; makefile: 711
file content (337 lines) | stat: -rw-r--r-- 9,127 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/**
 * \file input.c
 * \brief Generic stdio-like input interface
 * \author Abramo Bagnara <abramo@alsa-project.org>
 * \date 2000
 *
 * Generic stdio-like input interface
 */
/*
 *  Input object
 *  Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
 *
 *
 *   This library is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as
 *   published by the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "local.h"

#ifndef DOC_HIDDEN

typedef struct _snd_input_ops {
	int (*close)(snd_input_t *input);
	int (*scan)(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;
	const snd_input_ops_t *ops;
	void *private_data;
};
#endif

/**
 * \brief Closes an input handle.
 * \param input The input handle to be closed.
 * \return Zero if successful, otherwise a negative error code.
 */
int snd_input_close(snd_input_t *input)
{
	int err = input->ops->close(input);
	free(input);
	return err;
}

/**
 * \brief Reads formatted input (like \c fscanf(3)) from an input handle.
 * \param input The input handle.
 * \param format Format string in \c fscanf format.
 * \param ... Other \c fscanf arguments.
 * \return The number of input items assigned, or \c EOF.
 *
 * \bug Reading from a memory buffer doesn't work.
 */
int snd_input_scanf(snd_input_t *input, const char *format, ...)
{
	int result;
	va_list args;
	va_start(args, format);
	result = input->ops->scan(input, format, args);
	va_end(args);
	return result;
}

/**
 * \brief Reads a line from an input handle (like \c fgets(3)).
 * \param input The input handle.
 * \param str Address of the destination buffer.
 * \param size The size of the destination buffer.
 * \return Pointer to the buffer if successful, otherwise \c NULL.
 *
 * Like \c fgets, the returned string is zero-terminated, and contains
 * the new-line character \c '\\n' if the line fits into the buffer.
 */
char *snd_input_gets(snd_input_t *input, char *str, size_t size)
{
	return (input->ops->gets)(input, str, size);
}
			
/**
 * \brief Reads a character from an input handle (like \c fgetc(3)).
 * \param input The input handle.
 * \return The character read, or \c EOF on end of file or error.
 */
int snd_input_getc(snd_input_t *input)
{
	return input->ops->getch(input);
}

/**
 * \brief Puts the last character read back to an input handle (like \c ungetc(3)).
 * \param input The input handle.
 * \param c The character to push back.
 * \return The character pushed back, or \c EOF on error.
 */
int snd_input_ungetc(snd_input_t *input, int c)
{
	return input->ops->ungetch(input, c);
}

#ifndef DOC_HIDDEN
typedef struct _snd_input_stdio {
	int close;
	FILE *fp;
} snd_input_stdio_t;

static int snd_input_stdio_close(snd_input_t *input ATTRIBUTE_UNUSED)
{
	snd_input_stdio_t *stdio = input->private_data;
	if (stdio->close)
		fclose(stdio->fp);
	free(stdio);
	return 0;
}

static int snd_input_stdio_scan(snd_input_t *input, const char *format, va_list args)
{
	snd_input_stdio_t *stdio = input->private_data;
	extern int vfscanf(FILE *, const char *, va_list);
	return vfscanf(stdio->fp, format, args);
}

static char *snd_input_stdio_gets(snd_input_t *input, char *str, size_t size)
{
	snd_input_stdio_t *stdio = input->private_data;
	return fgets(str, (int) size, stdio->fp);
}
			
static int snd_input_stdio_getc(snd_input_t *input)
{
	snd_input_stdio_t *stdio = input->private_data;
	return getc(stdio->fp);
}

static int snd_input_stdio_ungetc(snd_input_t *input, int c)
{
	snd_input_stdio_t *stdio = input->private_data;
	return ungetc(c, stdio->fp);
}

static const snd_input_ops_t snd_input_stdio_ops = {
	.close		= snd_input_stdio_close,
	.scan		= snd_input_stdio_scan,
	.gets		= snd_input_stdio_gets,
	.getch		= snd_input_stdio_getc,
	.ungetch	= snd_input_stdio_ungetc,
};
#endif

/**
 * \brief Creates a new input object using an existing stdio \c FILE pointer.
 * \param inputp The function puts the pointer to the new input object
 *               at the address specified by \p inputp.
 * \param fp The \c FILE pointer to read from.
 *           Reading begins at the current file position.
 * \param _close Close flag. Set this to 1 if #snd_input_close should close
 *              \p fp by calling \c fclose.
 * \return Zero if successful, otherwise a negative error code.
 */
int snd_input_stdio_attach(snd_input_t **inputp, FILE *fp, int _close)
{
	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;
	stdio->close = _close;
	input->type = SND_INPUT_STDIO;
	input->ops = &snd_input_stdio_ops;
	input->private_data = stdio;
	*inputp = input;
	return 0;
}
	
/**
 * \brief Creates a new input object reading from a file.
 * \param inputp The functions puts the pointer to the new input object
 *               at the address specified by \p inputp.
 * \param file The name of the file to read from.
 * \param mode The open mode, like \c fopen(3).
 * \return Zero if successful, otherwise a negative error code.
 */
int snd_input_stdio_open(snd_input_t **inputp, const char *file, const char *mode)
{
	int err;
	FILE *fp = fopen(file, mode);
	if (!fp) {
		//SYSERR("fopen");
		return -errno;
	}
	err = snd_input_stdio_attach(inputp, fp, 1);
	if (err < 0)
		fclose(fp);
	return err;
}

#ifndef DOC_HIDDEN

typedef struct _snd_input_buffer {
	unsigned char *buf;
	unsigned char *ptr;
	size_t size;
} snd_input_buffer_t;

static int snd_input_buffer_close(snd_input_t *input)
{
	snd_input_buffer_t *buffer = input->private_data;
	free(buffer->buf);
	free(buffer);
	return 0;
}

static int snd_input_buffer_scan(snd_input_t *input, const char *format, va_list args)
{
	snd_input_buffer_t *buffer = input->private_data;
	extern int vsscanf(const char *, const char *, va_list);
	/* FIXME: how can I obtain consumed chars count? */
	assert(0);
	return vsscanf((char *)buffer->ptr, format, args);
}

static char *snd_input_buffer_gets(snd_input_t *input, char *str, size_t size)
{
	snd_input_buffer_t *buffer = input->private_data;
	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;
}
			
static int snd_input_buffer_getc(snd_input_t *input)
{
	snd_input_buffer_t *buffer = input->private_data;
	if (buffer->size == 0)
		return EOF;
	buffer->size--;
	return *buffer->ptr++;
}

static int snd_input_buffer_ungetc(snd_input_t *input, int c)
{
	snd_input_buffer_t *buffer = input->private_data;
	if (buffer->ptr == buffer->buf)
		return EOF;
	buffer->ptr--;
	assert(*buffer->ptr == (unsigned char) c);
	buffer->size++;
	return c;
}

static const snd_input_ops_t snd_input_buffer_ops = {
	.close		= snd_input_buffer_close,
	.scan		= snd_input_buffer_scan,
	.gets		= snd_input_buffer_gets,
	.getch		= snd_input_buffer_getc,
	.ungetch	= snd_input_buffer_ungetc,
};
#endif

/**
 * \brief Creates a new input object from a memory buffer.
 * \param inputp The function puts the pointer to the new input object
 *               at the address specified by \p inputp.
 * \param buf Address of the input buffer.
 * \param size Size of the input buffer.
 * \return Zero if successful, otherwise a negative error code.
 *
 * This functions creates a copy of the input buffer, so the application is
 * not required to preserve the buffer after this function has been called.
 */
int snd_input_buffer_open(snd_input_t **inputp, const char *buf, ssize_t size)
{
	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);
	buffer->buf = malloc((size_t)size + 1);
	if (!buffer->buf) {
		free(input);
		free(buffer);
		return -ENOMEM;
	}
	memcpy(buffer->buf, buf, (size_t) size);
	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;
	*inputp = input;
	return 0;
}