/*
   This file is part of the IRC Bounce source code
   copyright (c) 1998 v00d00 Net Services
   and is distributed according to the GNU general public license.

   This program 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
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   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., 675 Mass Ave, Cambridge, MA 02139, USA.

   The author (Robert Clark) can be reached at: v00d00@hempseed.com

   This code is based on Irc Proxy (c)1997-98 James Seter
*/

#include "common.h"

void
zero_fifo(fifobuffer *myload)
{
   myload->head=0; myload->tail=0;
}

int
pushchar_fifo(fifobuffer *myload, char d)
{
   int p,c;
   if (myload->tail>=1023) return -1;
   if ((myload->head>512)&&((c=myload->tail-myload->head)<100)) {
      for (p=0;p<c;p++) myload->buffer[p]=myload->buffer[myload->head+p];
      myload->head=0;
      myload->tail=c;
   }
   myload->buffer[myload->tail++]=d;
   return myload->tail;
}

int
popchar_fifo(fifobuffer *myload, char *d)
{
   if (myload->tail<=myload->head) {
      myload->head=0;
      myload->tail=0;
      return 0;
   }
   *d=myload->buffer[myload->head++];
   return 1;
}

int
popline_fifo(fifobuffer *myload, char *d)
{
   int p,f; unsigned char c;
   for(p=0;;p++) {
      f=popchar_fifo(myload,&c);
      if(f==0) break;
      d[p]=c;
   }
   return p;
}
