/* ringbuffer.c
   Stewart, August 16, 1982  3:24 PM
 */

#include	"env.h"

struct CBuf {
  char *buf;
  int len, rp, wp;
  };

InitRingBuffer(p, buffer, length)
  struct CBuf *p;
  char *buffer;
  int length;
  {
  p->buf = buffer;
  p->len = length;
  p->wp = p->rp = 0;
  };

int RingBufferEmpty(p)
  struct CBuf *p;
  {
  if (p->wp == p->rp) return(true);
  else return(false);
  };

int RingBufferFull(p)
  struct CBuf *p;
  {
  int np;
  np = p->wp + 1;
  if (np >= p->len) np = 0;
  if (np == p->rp) return(true);
  else return(false);
  };

int WriteRingBuffer(p, c)
  struct CBuf *p;
  char c;
  {
  int np;
  if (RingBufferFull(p)) return(false);
  IWDC();
  np = p->wp + 1;
  if (np >= p->len) np = 0;
  p->buf[np] = c;
  p->wp = np;
  DWDC();
  return(true);
  };

int ReadRingBuffer(p)
  struct CBuf *p;
  {
  int np, c;
  if (RingBufferEmpty(p)) return(-1);
  IWDC();
  np = p->rp + 1;
  if (np >= p->len) np = 0;
  c = p->buf[np];
  p->rp = np;
  DWDC();
  return(c & 0377);
  };