]> git.pond.sub.org Git - empserver/blob - src/client/ioqueue.c
Update copyright notice.
[empserver] / src / client / ioqueue.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2004, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  ioqueue.c: Manage an i/o queue
29  * 
30  *  Known contributors to this file:
31  *     Steve McClure, 1998
32  */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <sys/types.h>
38 #include "misc.h"
39 #include "queue.h"
40 #include "ioqueue.h"
41
42 static int ioqtobuf(register struct ioqueue *ioq, s_char *buf, int cc);
43 static void enqueuecc(struct ioqueue *ioq, s_char *buf, int cc);
44 static int dequeuecc(register struct ioqueue *ioq, register int cc);
45
46
47 void
48 ioq_init(struct ioqueue *ioq, int bsize)
49 {
50     initque(&ioq->queue);
51     ioq->cc = 0;
52     ioq->bsize = bsize;
53     *num_teles = '\0';
54 }
55
56 /*
57  * Copy the specified number of characters into the buffer
58  * provided, without actually dequeueing the data.  Return
59  * number of bytes actually found.
60  */
61 int
62 ioq_peek(struct ioqueue *ioq, s_char *buf, int cc)
63 {
64     return ioqtobuf(ioq, buf, cc);
65 }
66
67 int
68 ioq_dequeue(struct ioqueue *ioq, int cc)
69 {
70     if (dequeuecc(ioq, cc) != cc)
71         return 0;
72     return cc;
73 }
74
75 int
76 ioq_read(struct ioqueue *ioq, s_char *buf, int cc)
77 {
78     int n;
79
80     n = ioqtobuf(ioq, buf, cc);
81     if (n > 0)
82         dequeuecc(ioq, n);
83     return n;
84 }
85
86 void
87 ioq_write(struct ioqueue *ioq, s_char *buf, int cc)
88 {
89     enqueuecc(ioq, buf, cc);
90 }
91
92 int
93 ioq_qsize(struct ioqueue *ioq)
94 {
95     return ioq->cc;
96 }
97
98 void
99 ioq_drain(struct ioqueue *ioq)
100 {
101     struct io *io;
102     struct qelem *qp;
103
104     while ((qp = ioq->queue.q_forw) != &ioq->queue) {
105         io = (struct io *)qp;
106         free(io->data);
107         (void)remque(&io->queue);
108         (void)free(io);
109     }
110     ioq->cc = 0;
111 }
112
113 s_char *
114 ioq_gets(struct ioqueue *ioq, s_char *buf, int cc)
115 {
116     register s_char *p;
117     register s_char *end;
118     int nbytes;
119
120     nbytes = ioqtobuf(ioq, buf, cc);
121     if (nbytes < cc)
122         cc = nbytes;
123     end = &buf[cc];
124     for (p = buf; p < end && *p; p++) {
125         if (*p == '\n') {
126             *p = '\0';
127             dequeuecc(ioq, (p - buf) + 1);
128             return buf;
129         }
130     }
131     return 0;
132 }
133
134 /*
135  * all the rest are local to this module
136  */
137
138
139 /*
140  * copy cc bytes from ioq to buf.
141  * this routine doesn't free memory; this is
142  * left for a higher level.
143  */
144 static int
145 ioqtobuf(register struct ioqueue *ioq, s_char *buf, int cc)
146 {
147     register struct io *io;
148     struct qelem *qp;
149     s_char *offset;
150     int nbytes;
151     int nleft;
152
153     nleft = cc;
154     offset = buf;
155     for (qp = ioq->queue.q_forw; qp != &ioq->queue; qp = qp->q_forw) {
156         io = (struct io *)qp;
157         if ((nbytes = io->nbytes - io->offset) < 0) {
158             fprintf(stderr, "ioqtobuf: offset %d nbytes %d\n",
159                     io->offset, io->nbytes);
160             continue;
161         }
162         if (nbytes > 0) {
163             if (nleft < nbytes)
164                 nbytes = nleft;
165             memcpy(offset, io->data + io->offset, nbytes);
166             offset += nbytes;
167             nleft -= nbytes;
168         }
169     }
170     return offset - buf;
171 }
172
173 /*
174  * append a buffer to the end of the ioq.
175  */
176 static void
177 enqueuecc(struct ioqueue *ioq, s_char *buf, int cc)
178 {
179     struct io *io;
180
181     io = (struct io *)malloc(sizeof(*io));
182     io->nbytes = cc;
183     io->offset = 0;
184     io->data = buf;
185     insque(&io->queue, ioq->queue.q_back);
186     ioq->cc += cc;
187 }
188
189 /*
190  * remove cc bytes from ioqueue ioq
191  * free memory, dequeue io elements
192  * which are no longer used.
193  */
194 static int
195 dequeuecc(register struct ioqueue *ioq, register int cc)
196 {
197     register struct io *io;
198     register struct qelem *qp;
199     register int nbytes;
200     register int there;
201
202     nbytes = 0;
203     while ((qp = ioq->queue.q_forw) != &ioq->queue) {
204         io = (struct io *)qp;
205         there = io->nbytes - io->offset;
206         if (there < 0) {
207             fprintf(stderr, "dequeuecc: nbytes %d, offset %d\n",
208                     io->nbytes, io->offset);
209             continue;
210         }
211         if (cc > there) {
212             cc -= there;
213             nbytes += there;
214             (void)remque(&io->queue);
215             free(io->data);
216         } else {
217             io->offset += cc;
218             nbytes += cc;
219             break;
220         }
221     }
222     ioq->cc -= nbytes;
223     return nbytes;
224 }