
/*
     readfile.c : read the autograf data file into internal storage

	 By Joel Swank September 3, 1988
	 Version 1.0

	 */

#include <exec/types.h>
#include <intuition/intuition.h>
#include <stdio.h>
#include <fcntl.h>
#include <ctype.h> 
#include "fileio.h"

#define ARRAYSIZE 10     /* sizes for running averages arrays */
#define ODARRAYSIZE 11

/*************

 *  Hooks to the rest of the program

 *************/

extern struct Window	*Wind;
extern struct FileIOSupport *FIOSupp;

/* save areas for graphing data */
extern int yrsave[1000];		/* save year of each entry */
extern float costsave[1000];	/* save averaged cost data of each entry */
extern float milesave[1000];	/* save averaged mile data of each entry */
extern float pricesave[1000];	/* save price data of each entry */
extern float odsave[1000];		/* save odometer data of each entry */
extern float rawcost[1000];		/* save cost data of each entry */


extern struct IntuiText oktxt;
extern struct IntuiText FioText1;
extern struct RastPort *rp;
extern int numrecs;

/* Local Data */

char linebuf[200];
char linenum[40];

struct IntuiText ffmtmsg3 = {
	0,1,JAM2,	/* front and back text pens, drawmode and fill byte */
	14,30,	/* XY origin relative to container TopLeft */
	NULL,	/* font pointer or NULL for default */
	(UBYTE *) linebuf,	/* pointer to text */
	NULL	/* next IntuiText structure */
};

struct IntuiText ffmtmsg2 = {
	0,1,JAM2,	/* front and back text pens, drawmode and fill byte */
	14,20,	/* XY origin relative to container TopLeft */
	NULL,	/* font pointer or NULL for default */
	(UBYTE *) linenum,	/* pointer to text */
	&ffmtmsg3	/* next IntuiText structure */
};

struct IntuiText ffmtmsg = {
	0,1,JAM2,	/* front and back text pens, drawmode and fill byte */
	14,10,	/* XY origin relative to container TopLeft */
	NULL,	/* font pointer or NULL for default */
	(UBYTE *)"File has format error",	/* pointer to text */
	&ffmtmsg2	/* next IntuiText structure */
};

char filename[80];

struct IntuiText openfimsg2 = {
	0,1,JAM2,	/* front and back text pens, drawmode and fill byte */
	14,26,	/* XY origin relative to container TopLeft */
	NULL,	/* font pointer or NULL for default */
	(UBYTE *) filename,
	NULL	/* next IntuiText structure */
};

struct IntuiText openfimsg = {
	0,1,JAM2,	/* front and back text pens, drawmode and fill byte */
	14,16,	/* XY origin relative to container TopLeft */
	NULL,	/* font pointer or NULL for default */
	(UBYTE *)"Can't open file:",	/* pointer to text */
	&openfimsg2	/* next IntuiText structure */
};

struct IntuiText cantxt = {
	2,1,JAM2,	/* front and back text pens, drawmode and fill byte */
	5,3,	/* XY origin relative to container TopLeft */
	NULL,	/* font pointer or NULL for default */
	(UBYTE *)"CANCEL",	/* pointer to text */
	NULL	/* next IntuiText structure */
};

struct IntuiText retrytxt = {
	2,1,JAM2,	/* front and back text pens, drawmode and fill byte */
	5,3,	/* XY origin relative to container TopLeft */
	NULL,	/* font pointer or NULL for default */
	(UBYTE *)"RETRY",	/* pointer to text */
	NULL	/* next IntuiText structure */
};

/* arrays to save data for running averages */
float tengal[ARRAYSIZE];
float tenod[ODARRAYSIZE];
float tencost[ARRAYSIZE];
int odi, gali, costi;	/* indices for above */

int fileflag;
int fileread = FALSE;
int count, reccnt ;
int year;
char reqtitle[] = "Select an Input Data File";
FILE *fopen(), *fp;
float firstod = 0, miles, od;
float lastod, atof();
float gallons, cost, price;

/*
 * read_file : Read one file into memory
 */

read_file()
{
   char *pp, *tp;
   int i, elapsemiles, stopi;
   float temp ;
   float previous = 0;
   int previousyr = 0;

   SetWindowTitles(Wind,(UBYTE *) "AutoGraf File Selection", -1L);
   clr_grf();
   PrintIText(rp,&FioText1,0L,0L);	/* Print fileio text */
   FIOSupp->ReqTitle = (UBYTE *) reqtitle;

   Retry:
   if (GetFileIOName(FIOSupp,Wind))
		BuildFileIOPathname(FIOSupp,filename);
   else return;
 
   while ((fp = fopen(filename,"r")) == NULL)
		{
		if (AutoRequest(Wind,&openfimsg,&retrytxt,&cantxt,0L,0L,300L,75L))
			continue;
		goto Retry;
		}


   odi = 0;
   gali = 0;
   costi = 0;
 
/*************************************************
     Read data and save in arrays;
*************************************************/

   clr_grf();
   SetWindowTitles(Wind,(UBYTE *) "AutoGraf Reading Data file", -1L);
   SetWaitPointer(Wind);
   Move(rp,150L,100L);
   Text(rp,"READING FILE ",13L);
   Text(rp,filename,strlen(filename));

   count = 0;
   reccnt = 0;
   while (0 != fgets(linebuf,100,fp))
      {
	  if (count >= 1000) break;	/* the limit - needs error info */

	  reccnt++;
	  pp = linebuf;
	  while (*(++pp) != ',') if (badchk(*pp)) return ;	/* to end of date */

	  pp -= 4;
	  year = atoi(pp);	/* get year */
	  if (previousyr)
	  	{
	  	if (year != previousyr && year != previousyr+1 )
	  		{
			badchk('\0');	/* force format error */
			return;
			} else previousyr = year;
		} else previousyr = year;

	  pp += 5;			/* past comma */

	  od = atof(pp);	/* get odometer reading */
	  if (od < previous)
	  	{
		badchk('\0');	/* force format error */
		return;
		} else previous = od;

	  if (!firstod) firstod = od;
	  lastod = od;
	  tenod[odi++] = od;
	  if (odi == ODARRAYSIZE) odi=0;
	  pp++;

	  while (*(++pp) != ',') if (badchk(*pp)) return ;
	  while (*(++pp) != '$') if (badchk(*pp)) return ;
	  pp++;
	  price = atof(pp);		/* get price */

	  while (*(++pp) != ',') if (badchk(*pp)) return ;
	  while (*(++pp) != '$') if (badchk(*pp)) return ;
	  pp++;
	  cost = atof(pp);		/* get cost */
	  if (reccnt == 1) cost = 0;	/* throw out first one */
	  tencost[costi++] = cost;
	  if (costi == ARRAYSIZE) costi=0;

	  gallons = cost/price;	/* get gallons  */
	  if (reccnt == 1) gallons = 0;	/* throw out first one */
	  tengal[gali++] = gallons;
	  if (gali == ARRAYSIZE) gali=0;



	  if (reccnt > 10) 
	  	elapsemiles =(tenod[(odi-1 > 0) ? odi-1 : 10]-tenod[odi]);
	  else
	  	elapsemiles = tenod[odi-1] - tenod[0];

	  if (reccnt > 10) stopi = 10;
	  else stopi = reccnt;
	  /* calc cost/mile for last 10 entrys */
	  temp = 0;
	  if (elapsemiles != 0)
	  	{
		for (i=0; i<stopi; i++) temp += tencost[i];
		temp = temp/elapsemiles;
		temp *= 100; /* make it cents per mile */
		}
	  costsave[count] = temp;

		/* calc miles/gallon for last 10 entrys */
	  temp = 0;
	  if (elapsemiles != 0)
	  	{
		for (i=0; i<stopi; i++) temp += tengal[i];
		if (temp == 0)
			{
			badchk('\0');	/* force file format error */
			return;
			}
		temp = elapsemiles/temp;
		}
	  milesave[count] = temp;

	  yrsave[count] = year;
	  odsave[count] = od;
	  rawcost[count] = cost;
	  pricesave[count] = price;

	  count++;
	  }	/* end of read a line loop */

	  fclose(fp);

	  numrecs = count;
	  ClearPointer(Wind);

	  /* enable other two menu items */
	  OnMenu(Wind, (USHORT) SHIFTITEM(1));
	  OnMenu(Wind, (USHORT) SHIFTITEM(2));
	  fileread = TRUE;
}

/*
 * badchk : check for invalid character - cleanup and return
 *          TRUE if found, else return FALSE
 */

badchk(ch)
char ch;
{
	if ( isprint(ch) ) return(FALSE);
	sprintf(linenum,"Line number %d:",reccnt);
	linebuf[32] = '\0'; /* Limit to requester width */
	AutoRequest(Wind,&ffmtmsg,0L,&oktxt,0L,0L,300L,75L);
	ClearPointer(Wind);
	fclose(fp);
	return(TRUE);
}

