/*******************************************************/
/*                                                     */
/* File        : farmyboslocate.c                     */
/* Description : YBOS data extraction library          */
/*                                                     */
/* Author: Stefano Miscetti                            */
/*                                                     */
/* Created      : 18.07.1997                           */
/* Last modified: 22.08.1997                           */
/*                                                     */
/*******************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef DEBUG
#define DPRINTF(x) printf x
#else
#define DPRINTF(x)
#endif

#include "farmyboslocate.h"
#include "farmybos_types.h"

#define MAXNUM_YBOSBANK 6
#define YBHEADSIZ 5


YBlocator *YBLOCS[MAXNUM_YBOSBANK];


/********************************************************************************/
int farm_yboslocate_(int *ybospointer, char*bankname, int *nwords, int **indat_pointer)
     /***************************************************************************/
{
int runsize = 0, record_datasize = 0, datasize=0,jlocator = 0;
int bnum, ybstart,ybhead,ybnext,words;
YBhead *head;


if( ybospointer==NULL)
  {
    printf("Farm_Yboslocate: FYB pointer not initialized");
    return -1;
  }

/*----------------------------------
  search is finished ..
  please proceeds with the simple pointer for users
  */
if( strcmp(bankname,"LRID")==0) 
  {
    jlocator=0;
  }
else if( strcmp(bankname,"EVCL")==0)
  {
    jlocator=1;
  }
else if( strcmp(bankname,"CALR")==0)
  {
    jlocator=2;
  }
else if( strcmp(bankname,"DCHR")==0)
  {
    jlocator=3;
  }
else if( strcmp(bankname,"TRGR")==0)
  {
    jlocator=4;
  }
else if( strcmp(bankname,"QUAR")==0)
  {
    jlocator=5;
  }
else
  {
    printf("Fatal in FARM_YBOSLOCATE: (%s)\n",bankname);
    return -1;
  }
if( YBLOCS[jlocator]==NULL)
  return -1;
else
  {
    char locname[16];
    int i;
    for (i=0; i<16; i++)
      locname[i] = '\0';
    memcpy(locname,YBLOCS[jlocator]->head.bname,4);
    words = YBLOCS[jlocator]->head.blen - 1;
    *nwords = words;
    *indat_pointer = ybospointer+YBLOCS[jlocator]->start;
    DPRINTF((" Banca Name %s Jlocator %s %d nwords %d indat_pointer %p\n",
	     locname,bankname,jlocator,*nwords,indat_pointer));
  }
return 0;
}


/*---------------------------------------------------*/
/********************************************************************************/
int farm_yboslocateinit_(int *ybospointer )
     /***************************************************************************/
{
int runsize = 0, record_datasize = 0, datasize=0,jlocator = 0;
int bnum, ybstart,ybhead,ybnext,words;
int *indat_pointer;
char locname[16];
YBhead *head;

if( ybospointer==NULL)
  {
    printf("Farm_Yboslocate: FYB pointer not initialized");
    return -1;
  }
record_datasize = *ybospointer;
indat_pointer = ybospointer;
DPRINTF(("1: Record Datasize %d \n",record_datasize));
/*--------------------------------------------------------------- 
   make now a concatenated search inside the YB Event Buffer
   --------------------------------------------------------------*/
/*end_loop = 0;*/

for(bnum=0;bnum<MAXNUM_YBOSBANK;bnum++)
  	YBLOCS[bnum]=NULL;

if( record_datasize > 0 )
  {
    runsize = 0;  /*First word of Logical Record is Buff Datasize*/
    ybhead  = 1;  /* skip one word to jump to LRID header*/
    ybnext  = 0;  /* at the beginning assume no NEXT banks */ 
    ybstart = ybhead;  /* we WILL start from LRID header */
    for (bnum=0; bnum<MAXNUM_YBOSBANK; bnum++)
      {
	/*
	  The catcher in the rye ..we match in memory our YBHEAD 
	  to the running YBHEADER following Sfiligoi sintax
	  */

	head = (YBhead* ) &(indat_pointer[ybhead]); 

	datasize = head->blen-1; /* Num Words of data*/
	runsize += (YBHEADSIZ+datasize);  /* running size*/

	if( runsize <= record_datasize )
	  {
	    int i;
	    ybstart = ybhead +YBHEADSIZ;  /* running datastart */
	    ybhead  = ybstart+datasize;   /* beginning of next YBHEADER if any */
	    ybnext  = ybhead+YBHEADSIZ;
	    for (i=0;i<16; i++)
	      locname[i]='\0';
	    memcpy(locname,head->bname,4);
	    DPRINTF(("YbHeader %s %d  %d %d %d \n",head->bname,head->bnum,
		     head->bnext,head->blen,head->btyp));
	    DPRINTF(("Datasize %d runsize %d \n ",datasize,runsize));
	    if( !strcmp(locname,"LRID")) 
	      {
		jlocator=0;
	      }
	    else if( strcmp(locname,"EVCL")==0)
	      {
		jlocator=1;
	      }
	    else if( strcmp(locname,"CALR")==0)
	      {
		jlocator=2;
	      }
	    else if( strcmp(locname,"DCHR")==0)
	      {
		jlocator=3;
	      }
	    else if( strcmp(locname,"TRGR")==0)
	      {
		jlocator=4;
	      }
	    else if( strcmp(locname,"QUAR")==0)
	      {
		jlocator=5;
	      }
	    else
	      {
	      printf("Fatal in FARM_YBOSLOCATEINIT (%s):\n",locname);
	      return -1;
	      }
	    YBLOCS[jlocator] = (YBlocator*)malloc(sizeof(YBlocator));
	    YBLOCS[jlocator]->head = *head;
	    YBLOCS[jlocator]->start= ybstart;
	    YBLOCS[jlocator]->next = ybhead;

	    if( runsize == record_datasize) break;
	  }
	else
	  {
	    printf(" FATAL: exceeding RecordSIze \n");
	  }
      }
  }
return 0;
}

/*-----------------------------------------------------------------*/
int farm_yboslocatedone_(int *ybospointer )
     /***************************************************************************/
{
int bnum;

if( ybospointer==NULL)
  {
    printf("Farm_Yboslocate: FYB pointer not initialized");
    return -1;
  }

for(bnum=0;bnum<MAXNUM_YBOSBANK;bnum++)
  	if(YBLOCS[bnum]!=NULL)
	  free(YBLOCS[bnum]);
return 0;
}