]> git.pond.sub.org Git - empserver/blobdiff - src/lib/gen/ioqueue.c
Update copyright notice
[empserver] / src / lib / gen / ioqueue.c
index 78d02393b985322e25a42c46556a503330089b41..731c5d83dbbc4b86200d1f063722d22dfbf32ed4 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2018, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *                Ken Stevens, Steve McClure, Markus Armbruster
  *
- *  This program is free software; you can redistribute it and/or modify
+ *  Empire is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
+ *  the Free Software Foundation, either version 3 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
@@ -14,8 +14,7 @@
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  *  ---
  *
  *  ---
  *
  *  ioqueue.c: Read and write i/o queues
- * 
+ *
  *  Known contributors to this file:
- *     
+ *
  */
 
 /*
- * Read and write onto io queues.  Note that
- * the io queues don't actually do any writing;
+ * Read and write onto I/O queues.  Note that
+ * the I/O queues don't actually do any writing;
  * that is left for a higher level.
  */
 
 #include <config.h>
 
-#include <stdio.h>
-#include <stdlib.h>            /* malloc free */
-#include <sys/types.h>
-#if !defined(_WIN32)
+#include <stdlib.h>
+#include <string.h>
 #include <sys/uio.h>
-#endif
+#include "ioqueue.h"
 #include "misc.h"
 #include "queue.h"
-#include "ioqueue.h"
 
-static int ioqtocbuf(struct ioqueue *ioq, char *buf, int cc,
-                    register int stopc);
-#if !defined(_WIN32)
-static int ioqtoiov(struct ioqueue *ioq, struct iovec *iov,
-                   register int max);
-#endif
+struct io {
+    struct emp_qelem queue;
+    int size;
+    int nbytes;
+    int offset;
+    char *data;
+};
+
+struct ioqueue {
+    struct io list;
+    int bufsize;
+    int cc;
+};
+
+static int ioqtocbuf(struct ioqueue *ioq, char *buf, int cc, int stopc);
+static int ioqtoiov(struct ioqueue *ioq, struct iovec *iov, int max);
 static int ioqtobuf(struct ioqueue *ioq, char *buf, int cc);
 static int appendcc(struct ioqueue *ioq, char *buf, int cc);
-static int removecc(struct ioqueue *ioq, register int cc);
-
-#if defined(_WIN32)
-static void loc_StripDels(char *pBuf);
-#endif
+static int removecc(struct ioqueue *ioq, int cc);
 
 struct ioqueue *
 ioq_create(int size)
@@ -73,7 +75,7 @@ ioq_create(int size)
     ioq->list.nbytes = 0;
     ioq->list.offset = 0;
     ioq->list.size = 0;
-    ioq->list.data = 0;
+    ioq->list.data = NULL;
     ioq->bufsize = size;
     ioq->cc = 0;
     return ioq;
@@ -107,7 +109,6 @@ ioq_drain(struct ioqueue *ioq)
  * iovec, but don't actually dequeue the data.
  * return # of iovec initialized.
  */
-#if !defined(_WIN32)
 int
 ioq_makeiov(struct ioqueue *ioq, struct iovec *iov, int cc)
 {
@@ -115,7 +116,6 @@ ioq_makeiov(struct ioqueue *ioq, struct iovec *iov, int cc)
        return 0;
     return ioqtoiov(ioq, iov, cc);
 }
-#endif
 
 /*
  * Copy the specified number of characters into the buffer
@@ -128,10 +128,11 @@ ioq_peek(struct ioqueue *ioq, char *buf, int cc)
     return ioqtobuf(ioq, buf, cc);
 }
 
-int
+void
 ioq_dequeue(struct ioqueue *ioq, int cc)
 {
-    return removecc(ioq, cc);
+    int res = removecc(ioq, cc);
+    CANT_HAPPEN(res != cc);
 }
 
 void
@@ -178,11 +179,6 @@ ioq_puts(struct ioqueue *ioq, char *buf)
     return appendcc(ioq, buf, strlen(buf));
 }
 
-/*
- * all the rest are local to this module
- */
-
-
 /*
  * copy cc bytes from ioq to buf.
  * this routine doesn't free memory; this is
@@ -194,8 +190,7 @@ ioqtobuf(struct ioqueue *ioq, char *buf, int cc)
     struct io *io;
     struct emp_qelem *qp;
     struct emp_qelem *head;
-    register int nbytes;
-    register int nleft;
+    int nbytes, nleft;
     char *offset;
 
     nleft = cc;
@@ -223,11 +218,11 @@ ioqtobuf(struct ioqueue *ioq, char *buf, int cc)
  * terminating on the stop character.
  */
 static int
-ioqtocbuf(struct ioqueue *ioq, char *buf, int cc, register int stopc)
+ioqtocbuf(struct ioqueue *ioq, char *buf, int cc, int stopc)
 {
-    register int nbytes;
-    register s_char *p;
-    register int n;
+    int nbytes;
+    char *p;
+    int n;
     struct io *io;
     struct emp_qelem *qp;
     struct emp_qelem *head;
@@ -259,14 +254,11 @@ ioqtocbuf(struct ioqueue *ioq, char *buf, int cc, register int stopc)
  * initialize an iovec to point at max bytes worth
  * of data from the ioqueue.
  */
-#if !defined(_WIN32)
 static int
-ioqtoiov(struct ioqueue *ioq, struct iovec *iov, register int max)
+ioqtoiov(struct ioqueue *ioq, struct iovec *iov, int max)
 {
     struct io *io;
-    register int cc;
-    register int niov;
-    register int len;
+    int cc, niov, len;
     struct emp_qelem *qp;
 
     cc = max;
@@ -288,7 +280,6 @@ ioqtoiov(struct ioqueue *ioq, struct iovec *iov, register int max)
     }
     return niov;
 }
-#endif
 
 /*
  * append a buffer to the end of the ioq.
@@ -298,7 +289,7 @@ appendcc(struct ioqueue *ioq, char *buf, int cc)
 {
     struct io *io;
     int len;
-    s_char *ptr;
+    char *ptr;
     int avail;
 
     /* determine if any space is left */
@@ -330,17 +321,15 @@ appendcc(struct ioqueue *ioq, char *buf, int cc)
 
 /*
  * remove cc bytes from ioqueue ioq
- * free memory, dequeue io elements
+ * free memory, dequeue I/O elements
  * which are no longer used.
  */
 static int
-removecc(struct ioqueue *ioq, register int cc)
+removecc(struct ioqueue *ioq, int cc)
 {
     struct io *io;
     struct emp_qelem *qp;
-    register int nbytes;
-    register int there;
-    register int remain;
+    int nbytes, there, remain;
 
     nbytes = 0;
     remain = cc;
@@ -372,47 +361,3 @@ removecc(struct ioqueue *ioq, register int cc)
     ioq->cc -= nbytes;
     return nbytes;
 }
-
-#if defined(_WIN32)
-/*
- * Make an (output) buffer up to the
- * maximum size of the buffer.
- *
- * We don't free the bytes...
- */
-int
-ioq_makebuf(struct ioqueue *ioq, char *pBuf, int nBufLen)
-{
-    struct io *io;
-    struct emp_qelem *qp;
-    struct emp_qelem *head;
-    int nbytes;
-    int nleft;
-    int ncopied;
-    s_char *offset;
-
-    ncopied = 0;
-    nleft = nBufLen;
-    offset = pBuf;
-    head = &ioq->list.queue;
-
-    for (qp = head->q_forw; (qp != head) && (nleft > 0); qp = qp->q_forw) {
-       io = (struct io *)qp;
-       nbytes = io->nbytes - io->offset;
-       if (nbytes < 0) {
-           /* Paranoid check for bad buffer. */
-           continue;
-       }
-
-       /* too many bytes, wait till next time. */
-       if (nbytes > nleft)
-           break;
-
-       memcpy(offset, io->data + io->offset, nbytes);
-       offset += nbytes;
-       nleft -= nbytes;
-       ncopied += nbytes;
-    }
-    return ncopied;
-}
-#endif /* _WIN32 */