2 * charfuncs.c - Routines for dealing with character streams.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2002 Project Purple
13 #include "charfuncs.h"
16 * buffer_fetchchar - Fetches a char from a buffer.
17 * @ctx: Our buffer context structure.
18 * @count: The number of characters to get from the buffer.
19 * @c: Where to put the characters retrieved.
21 int buffer_fetchchar(void *ctx, size_t count, unsigned char *c)
23 struct buffer_ctx *buf = NULL;
26 buf = (struct buffer_ctx *) ctx;
27 for (i = 0; i < count; i++) {
28 c[i] = buf->buffer[buf->offset++];
31 return (((buf->offset) == (buf->size)) ? 1 : 0);
35 * buffer_putchar - Puts a char to a buffer.
36 * @ctx: Our buffer context structure.
37 * @count: The number of characters to put into the buffer.
38 * @c: The characters to add to the buffer.
40 * Adds characters to the buffer references by the buffer context. If we
41 * fill it then we double the size of the current buffer and then add the
44 int buffer_putchar(void *ctx, size_t count, unsigned char *c)
46 struct buffer_ctx *buf = NULL;
50 buf = (struct buffer_ctx *) ctx;
52 for (newsize = buf->size; newsize < (buf->offset + count);
55 if (newsize != buf->size) {
56 buf->buffer = realloc(buf->buffer, newsize);
60 for (i = 0; i < count; i++) {
61 buf->buffer[buf->offset++] = c[i];
68 * file_fetchchar - Fetches a char from a file.
70 int file_fetchchar(void *fd, size_t count, unsigned char *c)
72 return !(read( *(int *) fd, c, count));
76 * file_putchar - Puts a char to a file.
78 int file_putchar(void *fd, size_t count, unsigned char *c)
80 return !(write( *(int *) fd, c, count));