From: Adam Dickmeiss Date: Tue, 28 Mar 1995 11:42:23 +0000 (+0000) Subject: First version of string-queue utility. X-Git-Url: http://lists.indexdata.dk/?a=commitdiff_plain;h=7642975237712e83cead59c1bc07379f938bcbde;p=egate.git First version of string-queue utility. --- diff --git a/include/strqueue.h b/include/strqueue.h new file mode 100644 index 0000000..b701d24 --- /dev/null +++ b/include/strqueue.h @@ -0,0 +1,22 @@ +/* Gateway utility + * Europagate, 1995 + * + * $Log: strqueue.h,v $ + * Revision 1.1 1995/03/28 11:42:23 adam + * First version of string-queue utility. + * + */ + +#ifndef STRQUEUE_H +#define STRQUEUE_H +struct str_queue { + struct str_entry *first; + struct str_entry *last; +}; + +struct str_queue *str_queue_mk (void); +void str_queue_rm (struct str_queue **sq); +char *str_queue_enq (struct str_queue *sq, const char *msg); +int str_queue_deq (struct str_queue *sq, char *dst, int max); +char *str_queue_get (struct str_queue *sq, int index); +#endif diff --git a/util/Makefile b/util/Makefile index 271bdd4..f0c53e0 100644 --- a/util/Makefile +++ b/util/Makefile @@ -2,7 +2,10 @@ # Europagate, 1995 # # $Log: Makefile,v $ -# Revision 1.10 1995/03/27 12:52:18 adam +# Revision 1.11 1995/03/28 11:42:41 adam +# First version of string-queue utility. +# +# Revision 1.10 1995/03/27 12:52:18 adam # A little more verbose in marc dump. # # Revision 1.9 1995/03/27 08:24:57 adam @@ -38,7 +41,7 @@ INCLUDE=-I../include TPROG1=iso2709dump TPROG2=gwdbtest LIB=../lib/util.a -PO=iso2709.o iso27dis.o gw-db.o gip.o gips.o gipc.o +PO=iso2709.o iso27dis.o gw-db.o gip.o gips.o gipc.o strqueue.o CPP=$(CC) -E DEFS=$(INCLUDE) -DSTUPID_ISO_DBC=1 diff --git a/util/strqueue.c b/util/strqueue.c new file mode 100644 index 0000000..60f2218 --- /dev/null +++ b/util/strqueue.c @@ -0,0 +1,95 @@ +/* Gateway utility + * Europagate, 1995 + * + * $Log: strqueue.c,v $ + * Revision 1.1 1995/03/28 11:42:41 adam + * First version of string-queue utility. + * + */ + +#include +#include +#include +#include + +#include + +struct str_entry { + char *buf; + struct str_entry *prev; +}; + +struct str_queue *str_queue_mk (void) +{ + struct str_queue *sq; + + sq = malloc (sizeof(*sq)); + if (!sq) + return NULL; + sq->first = NULL; + sq->last = NULL; + return sq; +} + +void str_queue_rm (struct str_queue **sq) +{ + free (*sq); + *sq = NULL; +} + +char *str_queue_enq (struct str_queue *sq, const char *msg) +{ + struct str_entry *n; + + assert (sq); + assert (msg); + n = malloc (sizeof(*n)); + if (!n) + return NULL; + n->buf = malloc (strlen(msg)+1); + if (!n->buf) + { + free (n); + return NULL; + } + strcpy (n->buf, msg); + n->prev = NULL; + if (!sq->first) + sq->last = n; + else + sq->first->prev = n; + sq->first = n; + return n->buf; +} + +char *str_queue_get (struct str_queue *sq, int index) +{ + struct str_entry *n; + + assert (sq); + for (n = sq->last; n && index > 0; --index) + n = n->prev; + if (!n) + return NULL; + return n->buf; +} + +int str_queue_deq (struct str_queue *sq, char *dst, int max) +{ + struct str_entry *n; + + assert (sq); + if (!(n = sq->last)) + return 0; + sq->last = n->prev; + if (!sq->last) + sq->first = NULL; + if (dst && max > 1) + { + strncpy (dst, n->buf, max-1); + dst[max-1] = '\0'; + } + free (n->buf); + free (n); + return 1; +}