2 * charfuncs.c - Routines for dealing with character streams.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2002 Project Purple
11 #include <sys/types.h>
15 #include "charfuncs.h"
18 * buffer_fetchchar - Fetches a char from a buffer.
19 * @ctx: Our buffer context structure.
20 * @count: The number of characters to get from the buffer.
21 * @c: Where to put the characters retrieved.
23 int buffer_fetchchar(void *ctx, size_t count, void *c)
25 struct buffer_ctx *buf = NULL;
27 buf = (struct buffer_ctx *) ctx;
29 if (buf->offset + count > buf->size) {
33 memcpy(c, &buf->buffer[buf->offset], count);
40 * buffer_putchar - Puts a char to a buffer.
41 * @ctx: Our buffer context structure.
42 * @count: The number of characters to put into the buffer.
43 * @c: The characters to add to the buffer.
45 * Adds characters to the buffer references by the buffer context. If we
46 * fill it then we double the size of the current buffer and then add the
49 int buffer_putchar(void *ctx, size_t count, void *c)
51 struct buffer_ctx *buf = NULL;
54 buf = (struct buffer_ctx *) ctx;
56 for (newsize = buf->size; newsize < (buf->offset + count);
59 if (newsize != buf->size) {
60 buf->buffer = realloc(buf->buffer, newsize);
64 memcpy(&buf->buffer[buf->offset], c, count);
71 * file_fetchchar - Fetches a char from a file.
73 int file_fetchchar(void *fd, size_t count, void *c)
75 return !(read( *(int *) fd, c, count));
79 * file_putchar - Puts a char to a file.
81 int file_putchar(void *fd, size_t count, void *c)
83 return !(write( *(int *) fd, c, count));
87 * stdin_getchar - Gets a char from stdin.
89 int stdin_getchar(void *ctx, size_t count, void *c)
91 return (fread(c, 1, count, stdin) != count);
95 * stdout_putchar - Puts a char to stdout.
97 int stdout_putchar(void *ctx, size_t count, void *c)
99 return (fwrite(c, 1, count, stdout) != count);