3 * \brief Generic stdio-like input interface
4 * \author Abramo Bagnara <abramo@alsa-project.org>
7 * Generic stdio-like input interface
11 * Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
14 * This library is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License as
16 * published by the Free Software Foundation; either version 2.1 of
17 * the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
38 typedef struct _snd_input_ops {
39 int (*close)(snd_input_t *input);
40 int (*scan)(snd_input_t *input, const char *format, va_list args);
41 char *(*(gets))(snd_input_t *input, char *str, size_t size);
42 int (*getch)(snd_input_t *input);
43 int (*ungetch)(snd_input_t *input, int c);
47 snd_input_type_t type;
48 const snd_input_ops_t *ops;
54 * \brief Closes an input handle.
55 * \param input The input handle to be closed.
56 * \return Zero if successful, otherwise a negative error code.
58 int snd_input_close(snd_input_t *input)
60 int err = input->ops->close(input);
66 * \brief Reads formatted input (like \c fscanf(3)) from an input handle.
67 * \param input The input handle.
68 * \param format Format string in \c fscanf format.
69 * \param ... Other \c fscanf arguments.
70 * \return The number of input items assigned, or \c EOF.
72 * \bug Reading from a memory buffer doesn't work.
74 int snd_input_scanf(snd_input_t *input, const char *format, ...)
78 va_start(args, format);
79 result = input->ops->scan(input, format, args);
85 * \brief Reads a line from an input handle (like \c fgets(3)).
86 * \param input The input handle.
87 * \param str Address of the destination buffer.
88 * \param size The size of the destination buffer.
89 * \return Pointer to the buffer if successful, otherwise \c NULL.
91 * Like \c fgets, the returned string is zero-terminated, and contains
92 * the new-line character \c '\\n' if the line fits into the buffer.
94 char *snd_input_gets(snd_input_t *input, char *str, size_t size)
96 return (input->ops->gets)(input, str, size);
100 * \brief Reads a character from an input handle (like \c fgetc(3)).
101 * \param input The input handle.
102 * \return The character read, or \c EOF on end of file or error.
104 int snd_input_getc(snd_input_t *input)
106 return input->ops->getch(input);
110 * \brief Puts the last character read back to an input handle (like \c ungetc(3)).
111 * \param input The input handle.
112 * \param c The character to push back.
113 * \return The character pushed back, or \c EOF on error.
115 int snd_input_ungetc(snd_input_t *input, int c)
117 return input->ops->ungetch(input, c);
121 typedef struct _snd_input_stdio {
126 static int snd_input_stdio_close(snd_input_t *input ATTRIBUTE_UNUSED)
128 snd_input_stdio_t *stdio = input->private_data;
135 static int snd_input_stdio_scan(snd_input_t *input, const char *format, va_list args)
137 snd_input_stdio_t *stdio = input->private_data;
138 extern int vfscanf(FILE *, const char *, va_list);
139 return vfscanf(stdio->fp, format, args);
142 static char *snd_input_stdio_gets(snd_input_t *input, char *str, size_t size)
144 snd_input_stdio_t *stdio = input->private_data;
145 return fgets(str, (int) size, stdio->fp);
148 static int snd_input_stdio_getc(snd_input_t *input)
150 snd_input_stdio_t *stdio = input->private_data;
151 return getc(stdio->fp);
154 static int snd_input_stdio_ungetc(snd_input_t *input, int c)
156 snd_input_stdio_t *stdio = input->private_data;
157 return ungetc(c, stdio->fp);
160 static const snd_input_ops_t snd_input_stdio_ops = {
161 .close = snd_input_stdio_close,
162 .scan = snd_input_stdio_scan,
163 .gets = snd_input_stdio_gets,
164 .getch = snd_input_stdio_getc,
165 .ungetch = snd_input_stdio_ungetc,
170 * \brief Creates a new input object using an existing stdio \c FILE pointer.
171 * \param inputp The function puts the pointer to the new input object
172 * at the address specified by \p inputp.
173 * \param fp The \c FILE pointer to read from.
174 * Reading begins at the current file position.
175 * \param _close Close flag. Set this to 1 if #snd_input_close should close
176 * \p fp by calling \c fclose.
177 * \return Zero if successful, otherwise a negative error code.
179 int snd_input_stdio_attach(snd_input_t **inputp, FILE *fp, int _close)
182 snd_input_stdio_t *stdio;
183 assert(inputp && fp);
184 stdio = calloc(1, sizeof(*stdio));
187 input = calloc(1, sizeof(*input));
193 stdio->close = _close;
194 input->type = SND_INPUT_STDIO;
195 input->ops = &snd_input_stdio_ops;
196 input->private_data = stdio;
202 * \brief Creates a new input object reading from a file.
203 * \param inputp The functions puts the pointer to the new input object
204 * at the address specified by \p inputp.
205 * \param file The name of the file to read from.
206 * \param mode The open mode, like \c fopen(3).
207 * \return Zero if successful, otherwise a negative error code.
209 int snd_input_stdio_open(snd_input_t **inputp, const char *file, const char *mode)
212 FILE *fp = fopen(file, mode);
217 err = snd_input_stdio_attach(inputp, fp, 1);
225 typedef struct _snd_input_buffer {
229 } snd_input_buffer_t;
231 static int snd_input_buffer_close(snd_input_t *input)
233 snd_input_buffer_t *buffer = input->private_data;
239 static int snd_input_buffer_scan(snd_input_t *input, const char *format, va_list args)
241 snd_input_buffer_t *buffer = input->private_data;
242 extern int vsscanf(const char *, const char *, va_list);
243 /* FIXME: how can I obtain consumed chars count? */
245 return vsscanf((char *)buffer->ptr, format, args);
248 static char *snd_input_buffer_gets(snd_input_t *input, char *str, size_t size)
250 snd_input_buffer_t *buffer = input->private_data;
251 size_t bsize = buffer->size;
252 while (--size > 0 && bsize > 0) {
253 unsigned char c = *buffer->ptr++;
259 if (bsize == buffer->size)
261 buffer->size = bsize;
266 static int snd_input_buffer_getc(snd_input_t *input)
268 snd_input_buffer_t *buffer = input->private_data;
269 if (buffer->size == 0)
272 return *buffer->ptr++;
275 static int snd_input_buffer_ungetc(snd_input_t *input, int c)
277 snd_input_buffer_t *buffer = input->private_data;
278 if (buffer->ptr == buffer->buf)
281 assert(*buffer->ptr == (unsigned char) c);
286 static const snd_input_ops_t snd_input_buffer_ops = {
287 .close = snd_input_buffer_close,
288 .scan = snd_input_buffer_scan,
289 .gets = snd_input_buffer_gets,
290 .getch = snd_input_buffer_getc,
291 .ungetch = snd_input_buffer_ungetc,
296 * \brief Creates a new input object from a memory buffer.
297 * \param inputp The function puts the pointer to the new input object
298 * at the address specified by \p inputp.
299 * \param buf Address of the input buffer.
300 * \param size Size of the input buffer.
301 * \return Zero if successful, otherwise a negative error code.
303 * This functions creates a copy of the input buffer, so the application is
304 * not required to preserve the buffer after this function has been called.
306 int snd_input_buffer_open(snd_input_t **inputp, const char *buf, ssize_t size)
309 snd_input_buffer_t *buffer;
311 buffer = calloc(1, sizeof(*buffer));
314 input = calloc(1, sizeof(*input));
321 buffer->buf = malloc((size_t)size + 1);
327 memcpy(buffer->buf, buf, (size_t) size);
328 buffer->buf[size] = 0;
329 buffer->ptr = buffer->buf;
331 input->type = SND_INPUT_BUFFER;
332 input->ops = &snd_input_buffer_ops;
333 input->private_data = buffer;