Remove CVS Id tags.
[onak.git] / charfuncs.c
1 /*
2  * charfuncs.c - Routines for dealing with character streams.
3  *
4  * Jonathan McDowell <noodles@earth.li>
5  *
6  * Copyright 2002 Project Purple
7  */
8
9 #include <stdio.h>
10 #include <sys/types.h>
11 #include <sys/uio.h>
12 #include <unistd.h>
13
14 #include "charfuncs.h"
15
16 /**
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.
21  */
22 int buffer_fetchchar(void *ctx, size_t count, unsigned char *c)
23 {
24         struct buffer_ctx *buf = NULL;
25         size_t i;
26         
27         buf = (struct buffer_ctx *) ctx;
28         for (i = 0; i < count; i++) {
29                 c[i] = buf->buffer[buf->offset++];
30         }
31
32         return (((buf->offset) == (buf->size)) ? 1 : 0);
33 }
34
35 /**
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.
40  *
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
43  *      rest.
44  */
45 int buffer_putchar(void *ctx, size_t count, unsigned char *c)
46 {
47         struct buffer_ctx *buf = NULL;
48         size_t newsize = 0;
49         size_t i;
50         
51         buf = (struct buffer_ctx *) ctx;
52
53         for (newsize = buf->size; newsize < (buf->offset + count);
54                         newsize *= 2) ;
55
56         if (newsize != buf->size) {
57                 buf->buffer = realloc(buf->buffer, newsize);
58                 buf->size = newsize;
59         }
60         
61         for (i = 0; i < count; i++) {
62                 buf->buffer[buf->offset++] = c[i];
63         }
64
65         return 1;
66 }
67
68 /**
69  *      file_fetchchar - Fetches a char from a file.
70  */
71 int file_fetchchar(void *fd, size_t count, unsigned char *c)
72 {
73         return !(read( *(int *) fd, c, count));
74 }
75
76 /**
77  *      file_putchar - Puts a char to a file.
78  */
79 int file_putchar(void *fd, size_t count, unsigned char *c)
80 {
81         return !(write( *(int *) fd, c, count));
82 }
83
84 /**
85  *      stdin_getchar - Gets a char from stdin.
86  */
87 int stdin_getchar(void *ctx, size_t count, unsigned char *c)
88 {
89         return (fread(c, 1, count, stdin) != count);
90 }
91
92 /**
93  *      stdout_putchar - Puts a char to stdout.
94  */
95 int stdout_putchar(void *ctx, size_t count, unsigned char *c)
96 {
97         return (fwrite(c, 1, count, stdout) != count);
98 }