Update Debian Vcs-* fields to point to git repository
[onak.git] / charfuncs.c
1 /*
2  * charfuncs.c - Routines for dealing with character streams.
3  *
4  * Copyright 2002 Jonathan McDowell <noodles@earth.li>
5  *
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.
9  *
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
13  * more details.
14  *
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.
18  */
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <sys/types.h>
23 #include <sys/uio.h>
24 #include <unistd.h>
25
26 #include "charfuncs.h"
27
28 /*
29  * 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.
33  */
34 int buffer_fetchchar(void *ctx, size_t count, void *c)
35 {
36         struct buffer_ctx *buf = NULL;
37         
38         buf = (struct buffer_ctx *) ctx;
39
40         if (buf->offset + count > buf->size) {
41                 return 1;
42         }
43         
44         memcpy(c, &buf->buffer[buf->offset], count);
45         buf->offset += count;
46
47         return 0;
48 }
49
50 /*
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.
55  *
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
58  *      rest.
59  */
60 int buffer_putchar(void *ctx, size_t count, void *c)
61 {
62         struct buffer_ctx *buf = NULL;
63         size_t newsize = 0;
64         
65         buf = (struct buffer_ctx *) ctx;
66
67         for (newsize = buf->size; newsize < (buf->offset + count);
68                         newsize *= 2) ;
69
70         if (newsize != buf->size) {
71                 buf->buffer = realloc(buf->buffer, newsize);
72                 buf->size = newsize;
73         }
74
75         memcpy(&buf->buffer[buf->offset], c, count);
76         buf->offset += count;
77         
78         return 1;
79 }
80
81 /*
82  * Fetches a char from a file.
83  */
84 int file_fetchchar(void *fd, size_t count, void *c)
85 {
86         return !(read( *(int *) fd, c, count));
87 }
88
89 /*
90  * Puts a char to a file.
91  */
92 int file_putchar(void *fd, size_t count, void *c)
93 {
94         return !(write( *(int *) fd, c, count));
95 }
96
97 /*
98  * Gets a char from stdin.
99  */
100 int stdin_getchar(void *ctx, size_t count, void *c)
101 {
102         return (fread(c, 1, count, stdin) != count);
103 }
104
105 /*
106  * Puts a char to stdout.
107  */
108 int stdout_putchar(void *ctx, size_t count, void *c)
109 {
110         return (fwrite(c, 1, count, stdout) != count);
111 }