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