2 * charfuncs.c - Routines for dealing with character streams.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2002 Project Purple
10 #include <sys/types.h>
14 #include "charfuncs.h"
17 * buffer_fetchchar - Fetches a char from a buffer.
18 * @ctx: Our buffer context structure.
19 * @count: The number of characters to get from the buffer.
20 * @c: Where to put the characters retrieved.
22 int buffer_fetchchar(void *ctx, size_t count, unsigned char *c)
24 struct buffer_ctx *buf = NULL;
27 buf = (struct buffer_ctx *) ctx;
28 for (i = 0; i < count; i++) {
29 c[i] = buf->buffer[buf->offset++];
32 return (((buf->offset) == (buf->size)) ? 1 : 0);
36 * buffer_putchar - Puts a char to a buffer.
37 * @ctx: Our buffer context structure.
38 * @count: The number of characters to put into the buffer.
39 * @c: The characters to add to the buffer.
41 * Adds characters to the buffer references by the buffer context. If we
42 * fill it then we double the size of the current buffer and then add the
45 int buffer_putchar(void *ctx, size_t count, unsigned char *c)
47 struct buffer_ctx *buf = NULL;
51 buf = (struct buffer_ctx *) ctx;
53 for (newsize = buf->size; newsize < (buf->offset + count);
56 if (newsize != buf->size) {
57 buf->buffer = realloc(buf->buffer, newsize);
61 for (i = 0; i < count; i++) {
62 buf->buffer[buf->offset++] = c[i];
69 * file_fetchchar - Fetches a char from a file.
71 int file_fetchchar(void *fd, size_t count, unsigned char *c)
73 return !(read( *(int *) fd, c, count));
77 * file_putchar - Puts a char to a file.
79 int file_putchar(void *fd, size_t count, unsigned char *c)
81 return !(write( *(int *) fd, c, count));
85 * stdin_getchar - Gets a char from stdin.
87 int stdin_getchar(void *ctx, size_t count, unsigned char *c)
89 return (fread(c, 1, count, stdin) != count);
93 * stdout_putchar - Puts a char to stdout.
95 int stdout_putchar(void *ctx, size_t count, unsigned char *c)
97 return (fwrite(c, 1, count, stdout) != count);