#include <aesbind.h>
#include <osbind.h>
#include <gemdefs.h>
#include <bios.h>
#include <stdio.h>
#include <strings.h>

char prefix[128],sep[]="\\",suffix[16]="*.TYP",
     filename[128],name[16];
char *cp;

char *text,line[80],clrscr[]="\33E\33e\33q\n",cursoff[]="\33f";

char title[]="     \33p Bob Areddy's Typing Tutor, v. 2.1  (c) 1990   Genesis Software \33q\n";
char instr[]="                           please select file for test\n";
int drv,result,button,fhandle,num=0,lnum,counter,counter2;
int tot_char,event,key,junk1,junk2,flag,flag2,errorflag;
long *ptr,time1,time2,wpm,best_wpm=0,best_wpm_er=0,
	sec,error,keystrokes;

main()
{
	init();						/* do initialization 		*/
	for(;;)
	{
		setup();					/* get text file, etc.		*/
		get_input();				/* start test				*/
		prnt_result();				/* give the result			*/
	}
}	

init()
{
	appl_init();				/* initialize an application*/
	text=(char *)Malloc(5000L); /* reserve space for text   */
	if (text==NULL)
	{
		form_alert(1,"[1][Not enough memory!][Quit]");
		quit();
	}
}

setup()
{	
	num=0;
	keystrokes=0;
	flag=0;
	button=1;
	
	Cconws(clrscr);
	printf("%s",title);
	printf("%s",instr);
	Cconws(cursoff);
	strncpy(filename,"\0",128);
	strncpy(name,"\0",16);
	strncpy(text,"\0",5000);
	
	cp=prefix;					/* build path for directory	*/
	drv=Dgetdrv();				/* get current drive		*/
	*cp++=Dgetdrv()+'A';		/* change into ASCII		*/
	*cp++=':';					/* add a ':' into string	*/
	Dgetpath(cp,drv+1);			/* get name of curr direct	*/
	
	strcpy(filename,prefix);    /* build string 			*/
	strcat(filename,sep);		
	strcat(filename,suffix);
	while(name[0]=='\0' && button==1)
		result=fsel_input(filename,name,&button);
		
	graf_mouse(M_OFF,0);
	if (button==0)
		{
		quit();			    /* if cancel, quit pgm 		*/
		return;
		}
	fhandle=Fopen(name,0);	/* open selected file 		*/
	tot_char=Fread(fhandle,		/* read all the data		*/
			5000L,text);
	Fclose(fhandle);			/* close the file			*/
		
	error=0;					/* reset value for errors   */
	counter2=0;					/* and for total keys typed */
}
	
printline()
{
	Cconws(clrscr);				/* clear screen				*/
	Cconws("\33b3");
	for(num=0;num<tot_char;num++)
	{
		if (text[num]==13)
			{
				printf("\n");
				num++;
			}
		
		Cconout(text[num]);
	}
}

gettime()
{
	*ptr = (*((long *)0x4ba));	/* get value from 200Hz clock */
}

get_input()
{
	counter=0;
	counter2=0;
	errorflag=1;
	
	printline();			/* print the text */
	
	num=0;
	Cconws("\33H\33B\33B\33b1");

	key=(evnt_keybd());
	ptr=&time1;
	Supexec(gettime);		/* start timer */
	flag2=1;

	while(text[num]!=0)
	{
		if (flag2==0)
			key=(evnt_keybd());	/* get key */
		
		flag2=0;

		if (key==24832)		/* was UNDO pressed?		*/
		{
			flag=1;
			ptr=&time2;
			Supexec(gettime);
			return;		/* yes, so quit				*/
		}
		
		else
			key &= 0xff; /* no, so get rid of scan code */

		if ((key>32 && key<127) ||
		 (counter!=0 && (key>31 && key<127)))
			{
			if (key!=text[num])
				{
				Cconws("\33p");	/* change color of letter 		*/
				error+=errorflag;			/* count the error	*/
				Cconout(key);
				Cconws("\33D\7");
				errorflag=0;
				}
			else
			{
				errorflag=1;
				Cconws("\33q");		/* normal character color		*/
				Cconout(key);		/* print the letter				*/
				num++;
	   			counter++;			/* counter for current line		*/
				counter2++;			/* total keys pressed			*/
				keystrokes++;
			}
			
			if (text[num]==13)
			{
				num+=2;
				counter2++;
				counter=0;
				printf("\n\n");
			}
		}
	}
	ptr=&time2;
	Supexec(gettime);			/* get the ending time			*/
	
}

prnt_result()
{
	Cconws(clrscr);				/* clear the screen				*/
	printf("\33b3Total number of words:  %ld\n",keystrokes/5);
	printf("Total number of errors: %ld\n",error);
	sec=(time2-time1)/200;
	printf("Total time (seconds):   %ld\n\n",sec);
	wpm=(sec>0) ? (keystrokes*60/5/sec) : 0;
	if (flag==0)
	{
		best_wpm=(wpm>best_wpm) ? wpm : best_wpm;
		best_wpm_er=((wpm-error)>best_wpm_er) ? wpm-error : best_wpm_er;
	}
	printf("Words per minute:       %ld\n",wpm);
	printf("Best words per minute:  %ld\n\n",best_wpm);
	
	printf("Words per minute with\n");
	printf("1 wpm penalty/error     %ld\n\n",wpm-error);
	
	printf("Best WPM with penalty:  %ld\n\n",best_wpm_er);

	printf("\nAny key to run test again.  RETURN to exit\n");
	
	key=evnt_keybd() & 0xFF;
	
	if (key==13)
		quit();
	graf_mouse(M_ON,0);
}

quit()
{
	Mfree(text);		/* free allocated memory		*/
	appl_exit();
	printf("\33f\n");	/* turn cursor off				*/
	exit(0);			/* quit							*/
}	
