#ifndef	lint
static char sccsid[] = "@(#)queue.c 1.3 88/12/21";
#endif

/* Routines to manage 'read' input queues */

#include "cbratp.h"

/*
 * bputc(cq, c)
 *	- put character c on queue cq
 */
bputc(cq, c)
    register struct cqueue *cq;
    u_char	c;
{
    cq->size++;
    *cq->tail++ = c;
    if (cq->tail == cq->buf + cq->bufsize)
	cq->tail = cq->buf;
}

/*
 * bgetc(cq)
 *	- returns the next character on queue cq
 */
u_char
bgetc(cq)
    register struct cqueue *cq;
{
    u_char c;

    cq->size--;
    c = *cq->head++;
    if (cq->head == cq->buf + cq->bufsize)
	cq->head = cq->buf;
    return c;
}

/*
 * cq_free(cq)
 *	- deallocates the queue cq
 */
cq_free(cq)
    register struct cqueue *cq;
{
    free((char *) cq->buf);
    free((char *) cq);
}

/*
 * cq_alloc()
 *	- returns a new queue. initial length QBUFSIZE
 */
struct cqueue *
cq_alloc()
{
    register struct cqueue *cq = (struct cqueue *)malloc(sizeof(struct cqueue));

    cq->size = 0;
    cq->bufsize = QBUFSIZE;
    cq->buf = (u_char *)malloc(QBUFSIZE);
    cq->head = cq->buf;
    cq->tail = cq->buf;

    return cq;
}
