/*
 *   This comparison program compares digits from two ASCII files for equality.
 *   All non-digit characters are skipped, so formatting and interviening 
 *   characters will have no effect on the comparison.  This allows the digits
 *   in the two files to be grouped or arranged in any way desired and to be
 *   labeled with any non-digit text--and the comparison will not be affected.
 *   Originally this program was written to compare two text files that each
 *   supposedly contain the value for Pi.
 */

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

int sum = 0;

char  file_name[2][64];

FILE  *fptr[2];

int   lincnt[2];
int   digndx[2];
int   digcnt[2];
long  filepos[2];
char  lin[2][300];

int   pi_digit = -1;

void  digit_mismatch();
void  end_file();
void  open_input_files();
int   read_file();

/* -------------------------------------------------------------------------- */

int main(argc,argv)
int   argc;
char  *argv[];
{
   int  chr_one, chr_two, i;

   if(argc != 3)
     {
       fprintf(stderr," -->  Usage:  compi  <fileone>  <filetwo>\n");
       exit(-3);
     }

   open_input_files(argv);


   for(;;)
     {
       chr_one = read_file(0);
       chr_two = read_file(1);
       pi_digit++;

       if(chr_one != chr_two)
         {
           for(i = 0; i < 2; i++)
             {
               fseek(fptr[i],filepos[i],SEEK_SET);
               fgets(lin[i],300,fptr[i]);
             }

           digit_mismatch();
         }
     }
}

/* -------------------------------------------------------------------------- */

void  open_input_files(argv)
char  *argv[];
{
   int  i;

   for(i = 0; i < 2; i++)
     {
       strcpy(file_name[i],argv[i+1]);

       if(!(fptr[i] = fopen(file_name[i],"r")))
         {
           fprintf(stderr," *** Unable to open file %s\n",file_name[i]);
           exit(-5);
         }
     }  /* for(i) */
}

/* -------------------------------------------------------------------------- */

int  read_file(fndx)
int  fndx;
{
   static int  chr;


   for(;;)
     {
       if((chr = fgetc(fptr[fndx])) == EOF)
         {
           end_file(fndx);
         }

       digndx[fndx]++;


       if(isdigit(chr))
         {
           digcnt[fndx]++;
           return  chr;
         }
 

       if(chr == '\n')
         {
           filepos[fndx] = ftell(fptr[fndx]);

           digcnt[fndx] = 0;
           digndx[fndx] = 0;
           lincnt[fndx]++;
         }
    }  /* end for(;;) */
}

/* -------------------------------------------------------------------------- */

void  digit_mismatch()
{
   int  i;

   digndx[0]--;
   digndx[1]--;

   printf("\n *** mismatch in decimal digit %d ***\n",pi_digit);
   printf(" File one digit:  %c  --  File two digit:  %c\n\n",
          lin[0][digndx[0]],lin[1][digndx[1]]);

   for(i = 0; i < 2; i++)
     {
       printf("File: %s -- line %d -- char %d -- digit %d\n",
              file_name[i],lincnt[i],digndx[i],digcnt[i]);
       printf("%s",lin[i]);
  /*   printf("%*.*s%c\n",digndx[i],digndx[i]," ",'^');  */
       printf("%*.*s%c\n\n",digndx[i],digndx[i]," ",'|');
     }  /* end for(i) */

   exit(1);
}

/* -------------------------------------------------------------------------- */

void  end_file(fndx)
int  fndx;
{
   fprintf(stderr,"Successful compare until file <%s> ended.\n",file_name[fndx]);

   exit(1);
}

/* -------------------------------------------------------------------------- */
