/*   -*- coding: utf-8 -*-  */

/******************************************************************************
*
*               Module   : Buffer
*               Fichier  : BufferParser.c
*               Auteur   : J. DUCLOY
*               Date     : Septembre 2001
*
******************************************************************************/
#include <stdio.h>
#include "Except.h"
#include "BufferParser.h"

BufferParser *BufferParserStdin=NULL;
BufferParser *BufferParserCreate()
{
  BufferParser *b1; 
  if ((b1 = (BufferParser *)malloc(sizeof(BufferParser))))
    {
      b1->output=BufferCreate(1024,512);
      b1->input=BufferCreate(128,128);
      b1->tag=BufferCreate(10,10);
      b1->tag2=BufferCreate(10,10);
      b1->inputChar='\0';
      b1->file=NULL;
    }
  else
      {
	ExceptSetError("BufferParser","MA", "memory allocation failed","","",2); 
	b1=NULL;
      }

  return b1;
}

void BufferParserFree(b1)
BufferParser *b1;
{
  BufferParserClose(b1);
  BufferFree(b1->output);
  BufferFree(b1->input);
  BufferFree(b1->tag);
  BufferFree(b1->tag2);
  free(b1);
}

void BufferParserClose(b1)
     BufferParser  *b1;
{
  if(b1&&b1->file)
    {
      fclose(b1->file);
      b1->file=NULL;
    }
  BufferReset(b1->output);
  BufferReset(b1->input);
  BufferReset(b1->tag);
  BufferReset(b1->tag2);
  b1->inputChar='\0';
}

BufferParser *BufferParserOpen( BufferParser *b1, char *str)
{
  BufferParserClose(b1);
  if((b1->file=fopen(str,"r")))
    {
      b1->inputChar='\0';
      return b1;
    }
  else
    {
      /*	ExceptSetError("BufferParser","FI", "file",str,"does not exist or fit",2); */
	b1=NULL;
    }
  return b1;
}

char *BufferParserJumpUntilChar(BufferParser *b1, int c1)
{
  int c2;
  while((c2=getc(b1->file))!=EOF)
    {
      BufferCharCat(b1->input,c2);
      if(c2==c1)return BufferString(b1->input);
    }
  /* ExceptSetError("BufferParser","SY", "premature EOF","","",2);
     exit (2); */
  return NULL;
}

char *BufferParserGetUntilChar(BufferParser *b1, int c1)
{
  int c2;
  while((c2=getc(b1->file))!=EOF)
    {
      BufferCharCat(b1->output,c2);
      if(c2==c1)return BufferString(b1->output);
    }
  /* ExceptSetError("BufferParser","SY", "premature EOF","","",2);
     exit (2); */
  return NULL;
}


char *BufferParserGetUntilStr(BufferParser *b1, char *s1)
{
  int lStr;

  lStr=strlen(s1);
  while(1)
    {
      if ((BufferParserGetUntilChar(b1,s1[lStr-1])))
	{
	  if (BufferTailCmp(b1->output,s1)==0)
	    return BufferString(b1->output);
	}
    }
  return NULL;
}

char *BufferParserJumpUntilStr(BufferParser *b1, char *s1)
{
  int lStr;

  lStr=strlen(s1);
  while(1)
    {
      if ((BufferParserJumpUntilChar(b1,s1[lStr-1])))
	{
	  if (BufferTailCmp(b1->input,s1)==0)
	    return BufferString(b1->input);
	}
    }
  return NULL;
}


char *BufferParserGetMandStr(b1,str)
     BufferParser *b1;
     char *str;
{
  int c1;
  int c2;
  while ((c1=*str++))
    {
      if ((c2=getc(b1->file))!=EOF)
	{
	  if (c1==c2){BufferCharCat(b1->output,c2);}
	  else
	    {
	      return NULL;
	    }
	}
      else
	{
	  ExceptSetError("BufferParser","SY", "premature EOF, looking for",str,"",2);
	  exit (2);
	}
    }
  return BufferString(b1->output);
}

BufferParser *BufferParserSetFile(b1,f1)
     BufferParser *b1;
     FILE *f1;
{
  BufferParserClose(b1);
  b1->inputChar='\0';
  b1->file=f1;
  return b1;
}

BufferParser *BufferParserStdinCreate()
{
  BufferParserStdin=BufferParserCreate();
  BufferParserStdin->file=stdin;
  return BufferParserStdin;
}

char *BufferParserGetBloc(BufferParser *b1, char *fromStr, char *toStr)
{
  BufferReset(b1->output);
  BufferReset(b1->input);
  if (BufferParserJumpUntilStr(b1, fromStr))
    {
      BufferStrcpy(b1->output, fromStr);
      if (BufferParserGetUntilStr(b1, toStr))return BufferString (b1->output);
    }
  return NULL;
}

char *BufferParserStdinGetBloc(char *fromStr, char *toStr)
{
  if (!BufferParserStdin)BufferParserStdinCreate();
  return BufferParserGetBloc(BufferParserStdin, fromStr, toStr);
}
