]> git.pond.sub.org Git - empserver/blob - src/client/ringbuf.c
f26e712f5e2054376908a161d9e5897c82031f09
[empserver] / src / client / ringbuf.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  ringbuf.c: Simple ring buffer
28  *
29  *  Known contributors to this file:
30  *     Markus Armbruster, 2007-2009
31  */
32
33 #include <config.h>
34
35 #include <assert.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <sys/uio.h>
39 #include "ringbuf.h"
40
41 /*
42  * Initialize empty ring buffer.
43  * Not necessary if *R is already zeroed.
44  */
45 void
46 ring_init(struct ring *r)
47 {
48     r->cons = r->prod = 0;
49 }
50
51 /*
52  * Return number of bytes held in ring buffer.
53  */
54 int
55 ring_len(struct ring *r)
56 {
57     assert(r->prod - r->cons <= RING_SIZE);
58     return r->prod - r->cons;
59 }
60
61 /*
62  * Return how much space is left in ring buffer.
63  */
64 int
65 ring_space(struct ring *r)
66 {
67     return RING_SIZE - (r->prod - r->cons);
68 }
69
70 /*
71  * Peek at ring buffer contents.
72  * N must be between -RING_SIZE-1 and RING_SIZE.
73  * If N>=0, peek at the (N+1)-th byte to be gotten.
74  * If N<0, peek at the -N-th byte that has been put in.
75  * Return the byte as unsigned char coverted to int, or EOF if there
76  * aren't that many bytes in the ring buffer.
77  */
78 int
79 ring_peek(struct ring *r, int n)
80 {
81     unsigned idx;
82
83     assert(-RING_SIZE - 1 <= n && n <= RING_SIZE);
84
85     idx = n >= 0 ? r->cons + n : r->prod - -n;
86     if (idx < r->cons && idx >= r->prod)
87         return EOF;
88     return r->buf[idx % RING_SIZE];
89 }
90
91 /*
92  * Get and remove the oldest byte from the ring buffer.
93  * Return it as unsigned char coverted to int, or EOF if the buffer was
94  * empty.
95  */
96 int
97 ring_getc(struct ring *r)
98 {
99     if (r->cons == r->prod)
100         return EOF;
101     return r->buf[r->cons++ % RING_SIZE];
102 }
103
104 /*
105  * Attempt to put byte C into the ring buffer.
106  * Return EOF when the buffer is full, else C.
107  */
108 int
109 ring_putc(struct ring *r, unsigned char c)
110 {
111     if (r->prod - r->cons == RING_SIZE)
112         return EOF;
113     return r->buf[r->prod++ % RING_SIZE] = c;
114 }
115
116 /*
117  * Attempt to put SZ bytes from BUF into the ring buffer.
118  * Return space left in ring buffer when it fits, else don't change
119  * the ring buffer and return how much space is missing times -1.
120  */
121 int
122 ring_putm(struct ring *r, void *buf, size_t sz)
123 {
124     char *p = buf;
125     int left = ring_space(r) - sz;
126     int res;
127     size_t i;
128
129     if (left >= 0) {
130         res = 0;
131         for (i = 0; i < sz; i++)
132             res = ring_putc(r, p[i]);
133         assert(res != EOF);
134     }
135
136     return left;
137 }
138
139 /*
140  * Discard the N oldest bytes from the ring buffer.
141  * It must hold at least that many.
142  */
143 void
144 ring_discard(struct ring *r, int n)
145 {
146     assert(0 <= n && n <= ring_len(r));
147     r->cons += n;
148 }
149
150 /*
151  * Search the ring buffer for zero-terminated string S.
152  * If found, return a non-negative value referring to the beginning of
153  * S in the buffer when passed to ring_peek().  Else return -1.
154  */
155 int
156 ring_search(struct ring *r, char *s)
157 {
158     size_t len = strlen(s);
159     size_t i, j;
160
161     for (i = r->cons; i + len <= r->prod; i++) {
162         for (j = 0; j < len && s[j] == r->buf[(i + j) % RING_SIZE]; j++) ;
163         if (!s[j])
164             return i - r->cons;
165     }
166     return -1;
167 }
168
169 /*
170  * Fill ring buffer from file referred by file descriptor FD.
171  * If ring buffer is already full, do nothing and return 0.
172  * Else attempt to read as many bytes as space permits, with readv(),
173  * and return its value.
174  */
175 int
176 ring_from_file(struct ring *r, int fd)
177 {
178     unsigned cons = r->cons % RING_SIZE;
179     unsigned prod = r->prod % RING_SIZE;
180     struct iovec iov[2];
181     int cnt;
182     ssize_t res;
183
184     if (r->prod == r->cons + RING_SIZE)
185         return 0;
186
187     iov[0].iov_base = r->buf + prod;
188     if (cons <= prod) {
189         /* r->buf[prod..] */
190         iov[0].iov_len = RING_SIZE - prod;
191         /* r->buf[..cons-1] */
192         iov[1].iov_base = r->buf;
193         iov[1].iov_len = cons;
194         cnt = 2;
195     } else {
196         /* r->buf[prod..cons-1] */
197         iov[0].iov_len = cons - prod;
198         cnt = 1;
199     }
200     res = readv(fd, iov, cnt);
201     if (res < 0)
202         return res;
203     r->prod += res;
204     return res;
205 }
206
207 /*
208  * Drain ring buffer to file referred by file descriptor FD.
209  * If ring buffer is already empty, do nothing and return 0.
210  * Else attempt to write complete contents with writev(), and return
211  * its value.
212  */
213 int
214 ring_to_file(struct ring *r, int fd)
215 {
216     unsigned cons = r->cons % RING_SIZE;
217     unsigned prod = r->prod % RING_SIZE;
218     struct iovec iov[2];
219     int cnt;
220     ssize_t res;
221
222     if (r->cons == r->prod)
223         return 0;
224
225     iov[0].iov_base = r->buf + cons;
226     if (prod <= cons) {
227         /* r->buf[cons..] */
228         iov[0].iov_len = RING_SIZE - cons;
229         /* r->buf[..prod-1] */
230         iov[1].iov_base = r->buf;
231         iov[1].iov_len = prod;
232         cnt = 2;
233     } else {
234         /* r->buf[cons..prod-1] */
235         iov[0].iov_len = prod - cons;
236         cnt = 1;
237     }
238     res = writev(fd, iov, cnt);
239     if (res < 0)
240         return res;
241     r->cons += res;
242     return res;
243 }