]> the.earth.li Git - onak.git/blob - charfuncs.c
04255dc37ffde715be1cfe1fd693a029de500f13
[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 <unistd.h>
23
24 #include "charfuncs.h"
25
26 /*
27  * Fetches a char from a buffer.
28  *      @ctx: Our buffer context structure.
29  *      @count: The number of characters to get from the buffer.
30  *      @c: Where to put the characters retrieved.
31  */
32 int buffer_fetchchar(void *ctx, size_t count, void *c)
33 {
34         struct buffer_ctx *buf = NULL;
35         
36         buf = (struct buffer_ctx *) ctx;
37
38         if (buf->offset + count > buf->size) {
39                 return 1;
40         }
41         
42         memcpy(c, &buf->buffer[buf->offset], count);
43         buf->offset += count;
44
45         return 0;
46 }
47
48 /*
49  *      buffer_putchar - Puts a char to a buffer.
50  *      @ctx: Our buffer context structure.
51  *      @count: The number of characters to put into the buffer.
52  *      @c: The characters to add to the buffer.
53  *
54  *      Adds characters to the buffer references by the buffer context. If we
55  *      fill it then we double the size of the current buffer and then add the
56  *      rest.
57  */
58 int buffer_putchar(void *ctx, size_t count, void *c)
59 {
60         struct buffer_ctx *buf = NULL;
61         size_t newsize = 0;
62         
63         buf = (struct buffer_ctx *) ctx;
64
65         for (newsize = buf->size; newsize < (buf->offset + count);
66                         newsize *= 2) ;
67
68         if (newsize != buf->size) {
69                 buf->buffer = realloc(buf->buffer, newsize);
70                 buf->size = newsize;
71         }
72
73         memcpy(&buf->buffer[buf->offset], c, count);
74         buf->offset += count;
75         
76         return 1;
77 }
78
79 /*
80  * Fetches a char from a file.
81  */
82 int file_fetchchar(void *fd, size_t count, void *c)
83 {
84         return !(read( *(int *) fd, c, count));
85 }
86
87 /*
88  * Puts a char to a file.
89  */
90 int file_putchar(void *fd, size_t count, void *c)
91 {
92         return !(write( *(int *) fd, c, count));
93 }
94
95 /*
96  * Gets a char from stdin.
97  */
98 int stdin_getchar(void *ctx, size_t count, void *c)
99 {
100         return (fread(c, 1, count, stdin) != count);
101 }
102
103 /*
104  * Puts a char to stdout.
105  */
106 int stdout_putchar(void *ctx, size_t count, void *c)
107 {
108         return (fwrite(c, 1, count, stdout) != count);
109 }