2 * charfuncs.c - Routines for dealing with character streams.
4 * Copyright 2002 Jonathan McDowell <noodles@earth.li>
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <sys/types.h>
26 #include "charfuncs.h"
29 * buffer_fetchchar - Fetches a char from a buffer.
30 * @ctx: Our buffer context structure.
31 * @count: The number of characters to get from the buffer.
32 * @c: Where to put the characters retrieved.
34 int buffer_fetchchar(void *ctx, size_t count, void *c)
36 struct buffer_ctx *buf = NULL;
38 buf = (struct buffer_ctx *) ctx;
40 if (buf->offset + count > buf->size) {
44 memcpy(c, &buf->buffer[buf->offset], count);
51 * buffer_putchar - Puts a char to a buffer.
52 * @ctx: Our buffer context structure.
53 * @count: The number of characters to put into the buffer.
54 * @c: The characters to add to the buffer.
56 * Adds characters to the buffer references by the buffer context. If we
57 * fill it then we double the size of the current buffer and then add the
60 int buffer_putchar(void *ctx, size_t count, void *c)
62 struct buffer_ctx *buf = NULL;
65 buf = (struct buffer_ctx *) ctx;
67 for (newsize = buf->size; newsize < (buf->offset + count);
70 if (newsize != buf->size) {
71 buf->buffer = realloc(buf->buffer, newsize);
75 memcpy(&buf->buffer[buf->offset], c, count);
82 * file_fetchchar - Fetches a char from a file.
84 int file_fetchchar(void *fd, size_t count, void *c)
86 return !(read( *(int *) fd, c, count));
90 * file_putchar - Puts a char to a file.
92 int file_putchar(void *fd, size_t count, void *c)
94 return !(write( *(int *) fd, c, count));
98 * stdin_getchar - Gets a char from stdin.
100 int stdin_getchar(void *ctx, size_t count, void *c)
102 return (fread(c, 1, count, stdin) != count);
106 * stdout_putchar - Puts a char to stdout.
108 int stdout_putchar(void *ctx, size_t count, void *c)
110 return (fwrite(c, 1, count, stdout) != count);