fehQlibs 26
Qlibs
Loading...
Searching...
No Matches
buffer.c
Go to the documentation of this file.
1#include <unistd.h>
2#include "buffer.h"
3#include "str.h"
4#include "byte.h"
5#include "error.h"
6
13void buffer_init(buffer *s,ssize_t (*op)(),int fd,char *buf,size_t len)
14{
15 s->x = buf;
16 s->fd = fd;
17 s->op = op;
18 s->p = 0;
19 s->n = len;
20}
21
22static ssize_t buffer_0_read(int fd,char *buf,size_t len)
23{
24 if (buffer_flush(buffer_1) == -1) return -1;
25 return read(fd,buf,len);
26}
27
28ssize_t buffer_unixread(int fd,char *buf,size_t len)
29{
30 return (ssize_t) read(fd,buf,len);
31}
32
33ssize_t buffer_unixwrite(int fd,char *buf,size_t len)
34{
35 return (ssize_t) write(fd,buf,len);
36}
37
39static buffer it0 = BUFFER_INIT(buffer_0_read,0,buffer_0_space,sizeof(buffer_0_space));
41
45
49
51static buffer is0 = BUFFER_INIT(buffer_0_read,0,buffer_0_small,sizeof(buffer_0_small));
53
57
61
62int buffer_copy(buffer *bout,buffer *bin)
63{
64 int n;
65 char *x;
66
67 for (;;) {
68 n = buffer_feed(bin);
69 if (n < 0) return -2;
70 if (!n) return 0;
71 x = buffer_PEEK(bin);
72 if (buffer_put(bout,x,n) == -1) return -3;
73 buffer_SEEK(bin,n);
74 }
75}
76
77static int oneread(ssize_t (*op)(),int fd,char *buf,size_t len)
78{
79 int r;
80
81 for (;;) {
82 r = op(fd,buf,len);
83 if (r == -1) if (errno == EINTR) continue;
84 return r;
85 }
86}
87
88static int getthis(buffer *s,char *buf,size_t len)
89{
90 if (len > s->p) len = s->p;
91 s->p -= len;
92 byte_copy(buf,len,s->x + s->n);
93 s->n += len;
94 return len;
95}
96
98{
99 int r;
100
101 if (s->p) return s->p;
102 r = oneread(s->op,s->fd,s->x,s->n);
103 if (r <= 0) return r;
104 s->p = r;
105 s->n -= r;
106 if (s->n > 0) byte_copyr(s->x + s->n,r,s->x);
107 return r;
108}
109
110int buffer_bget(buffer *s,char *buf,size_t len)
111{
112 int r;
113
114 if (s->p > 0) return getthis(s,buf,len);
115 if (s->n <= len) return oneread(s->op,s->fd,buf,s->n);
116 r = buffer_feed(s);
117 if (r <= 0) return r;
118 return getthis(s,buf,len);
119}
120
121int buffer_get(buffer *s,char *buf,size_t len)
122{
123 int r;
124
125 if (s->p > 0) return getthis(s,buf,len);
126 if (s->n <= len) return oneread(s->op,s->fd,buf,len);
127 r = buffer_feed(s);
128 if (r <= 0) return r;
129 return getthis(s,buf,len);
130}
131
133{
134 return s->x + s->n;
135}
136
137void buffer_seek(buffer *s,size_t len)
138{
139 s->n += len;
140 s->p -= len;
141}
142
143static int allwrite(ssize_t (*op)(),int fd,const char *buf,size_t len)
144{
145 int w;
146
147 while (len) {
148 w = op(fd,buf,len);
149 if (w == -1) {
150 if (errno == EINTR) continue;
151 return -1; /* note that some data may have been written */
152 }
153 if (w == 0) /* luser's fault */
154 ;
155 buf += w;
156 len -= w;
157 }
158 return 0;
159}
160
162{
163 int p;
164
165 p = s->p;
166 if (!p) return 0;
167 s->p = 0;
168 return allwrite(s->op,s->fd,s->x,p);
169}
170
171int buffer_putalign(buffer *s,const char *buf,size_t len)
172{
173 unsigned int n;
174
175 while (len > (n = s->n - s->p)) {
176 byte_copy(s->x + s->p,n,buf); s->p += n; buf += n; len -= n;
177 if (buffer_flush(s) == -1) return -1;
178 }
179 /* now len <= s->n - s->p */
180 byte_copy(s->x + s->p,len,buf);
181 s->p += len;
182 return 0;
183}
184
185int buffer_put(buffer *s,const char *buf,size_t len)
186{
187 unsigned int n;
188
189 n = s->n;
190 if (len > n - s->p) {
191 if (buffer_flush(s) == -1) return -1;
192 /* now s->p == 0 */
193 if (n < BUFFER_OUTSIZE) n = BUFFER_OUTSIZE;
194 while (len > s->n) {
195 if (n > len) n = len;
196 if (allwrite(s->op,s->fd,buf,n) == -1) return -1;
197 buf += n;
198 len -= n;
199 }
200 }
201 /* now len <= s->n - s->p */
202 byte_copy(s->x + s->p,len,buf);
203 s->p += len;
204 return 0;
205}
206
207int buffer_putflush(buffer *s,const char *buf,size_t len)
208{
209 if (buffer_flush(s) == -1) return -1;
210 return allwrite(s->op,s->fd,buf,len);
211}
212
213int buffer_putsalign(buffer *s,const char *buf)
214{
215 return buffer_putalign(s,buf,str_len(buf));
216}
217
218int buffer_puts(buffer *s,const char *buf)
219{
220 return buffer_put(s,buf,str_len(buf));
221}
222
223int buffer_putsflush(buffer *s,const char *buf)
224{
225 return buffer_putflush(s,buf,str_len(buf));
226}
int read(int _str, void *_buf, int _b)
char buffer_2_space[BUFFER_OUTSIZE]
Definition: buffer.c:46
char buffer_0_small[BUFFER_SMALL]
Definition: buffer.c:50
int buffer_putalign(buffer *s, const char *buf, size_t len)
Definition: buffer.c:171
buffer * buffer_1small
Definition: buffer.c:56
int buffer_get(buffer *s, char *buf, size_t len)
Definition: buffer.c:121
ssize_t buffer_unixread(int fd, char *buf, size_t len)
Definition: buffer.c:28
int buffer_putsflush(buffer *s, const char *buf)
Definition: buffer.c:223
buffer * buffer_1
Definition: buffer.c:44
void buffer_init(buffer *s, ssize_t(*op)(), int fd, char *buf, size_t len)
Definition: buffer.c:13
char buffer_2_small[BUFFER_SMALL]
Definition: buffer.c:58
int buffer_putflush(buffer *s, const char *buf, size_t len)
Definition: buffer.c:207
buffer * buffer_2small
Definition: buffer.c:60
int buffer_copy(buffer *bout, buffer *bin)
Definition: buffer.c:62
char buffer_1_space[BUFFER_OUTSIZE]
Definition: buffer.c:42
int buffer_puts(buffer *s, const char *buf)
Definition: buffer.c:218
buffer * buffer_2
Definition: buffer.c:48
buffer * buffer_0small
Definition: buffer.c:52
char buffer_0_space[BUFFER_INSIZE]
Definition: buffer.c:38
char buffer_1_small[BUFFER_SMALL]
Definition: buffer.c:54
int buffer_flush(buffer *s)
Definition: buffer.c:161
int buffer_bget(buffer *s, char *buf, size_t len)
Definition: buffer.c:110
void buffer_seek(buffer *s, size_t len)
Definition: buffer.c:137
ssize_t buffer_unixwrite(int fd, char *buf, size_t len)
Definition: buffer.c:33
int buffer_put(buffer *s, const char *buf, size_t len)
Definition: buffer.c:185
int buffer_feed(buffer *s)
Definition: buffer.c:97
int buffer_putsalign(buffer *s, const char *buf)
Definition: buffer.c:213
char * buffer_peek(buffer *s)
Definition: buffer.c:132
buffer * buffer_0
Definition: buffer.c:40
#define BUFFER_INSIZE
Definition: buffer.h:15
#define BUFFER_OUTSIZE
Definition: buffer.h:16
#define BUFFER_INIT(op, fd, buf, len)
Definition: buffer.h:13
#define buffer_SEEK(s, len)
Definition: buffer.h:43
#define BUFFER_SMALL
Definition: buffer.h:14
#define buffer_PEEK(s)
Definition: buffer.h:42
void byte_copy(void *, unsigned int, const void *)
Definition: byte.c:20
void byte_copyr(void *, unsigned int, const void *)
Definition: byte.c:30
unsigned int str_len(const char *)
Definition: str.c:68
Definition: buffer.h:5
size_t n
Definition: buffer.h:8
int fd
Definition: buffer.h:9
unsigned int p
Definition: buffer.h:7
char * x
Definition: buffer.h:6
ssize_t(* op)()
Definition: buffer.h:10