
	/*
	** Checks a given GT file to see how well it conforms to the
	** handedness of the subject.  In other words, did the person
	** eat with the instrumented hand or not.
	*/

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

int main(int argc, char *argv[])

{
FILE	*fpt;
char	hand[320],utensil[320],container[320],food[320];
char	text[640];
int	i,j,bite,index;
int	TotalSubjects;
char	SubjectID[300][16],Handedness[300][16],SubjectHand[16];
int	CorrectHand,BothHand,OffHand;

if (argc != 2)
  {
  printf("Usage:  handtest [file]\n");
  printf("[file] should be a path ended by gt_union.txt or another GT\n");
  exit(0);
  }

	/* read the handedness of each subject */
if ((fpt=fopen("handedness.txt","r")) == NULL)
  {
  printf("Unable to open handedness.txt\n");
  exit(0);
  }
TotalSubjects=0;
fscanf(fpt,"%s %s",hand,utensil);	/* do not need headers */
while (1)
  {
  i=fscanf(fpt,"%s %s",SubjectID[TotalSubjects],Handedness[TotalSubjects]);
  if (i != 2)
    break;
  TotalSubjects++;
  }
fclose(fpt);

	/* identify subjectID of given file through filename */
strcpy(text,argv[1]);
i=strlen(text)-1;
while (i >= 0  &&  text[i] != 'p')
  i--;
text[i+4]=0;
for (j=0; j<TotalSubjects; j++)
  if (strcmp(&(text[i]),SubjectID[j]) == 0)
    break;
if (j == TotalSubjects)
  {
  printf("%s not found in handedness.txt file?\n",&(text[i]));
  exit(0);
  }

	/* try to open the given file */
if ((fpt=fopen(argv[1],"r")) == NULL)
  {
  printf("Unable to open %s for reading\n",argv[1]);
  exit(0);
  }

CorrectHand=BothHand=OffHand=0;
while (1)
  {
  i=fscanf(fpt,"%d %d %s %s %s %s",&bite,&index,hand,utensil,container,food);
  if (i != 6)
    break;
  if (strcasecmp(hand,"both") == 0)
    BothHand++;
  else if (strcasecmp(hand,Handedness[j]) == 0)
    CorrectHand++;
  else 
    OffHand++;
  }
fclose(fpt);
printf("%s(%s):  Both %d  Correct %d  Off %d",
	argv[1],Handedness[j],BothHand,CorrectHand,OffHand);
if (OffHand > CorrectHand)
  printf(" BAD %.0lf\n",(double)(CorrectHand+BothHand)/((double)(CorrectHand+OffHand+BothHand))*100.0);
else
  printf("\n");
}
