/* Pro139C.c   */
/* "5DIFF.Exe" */
/* Compare Two Similar Files Line-by-Line */
/* This write all 'diff' results into '\DIFF\ -directory. */
/* 2005-01-22
 * I fixed the BUG in which Pointer stops at the shorter catalog
 *    becomes NULL and didn't report the Longer catalog (file).
 *    However, between the WHILE-Loops switch, there have One
 *    line omission.  I tried it out, but didn't come out well,
 *    so leave it later.  I think that is trivial.
 */
/* 2005-02-06
 * From MacDualWay, I learned that Flag Counter for "GrandSum.Txt"
 *    must be placed in all three LOOPS (Comparison, SDW-long, ZIP-long).
 *    So I do.
 */

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

/* FN stands for = Filename */
char FNtoOpen [64], openedFN [64], openedFNGrndSum [256];
char openedSDWFNP [64], openedZIPFNP [64];
char writingFile [64];
char lineSDW [256], lineZIP[256];

extern char *firstwd  (char *to, char *from);   /* Recycling Object  */
extern char *spacedwd (char *to, char *from);   /* Recycling Object  */


void main (void)
{
  char strPause[16];
  int lengthSDW, iFlag, iSDW, iZIP;          /* Flag Counters */
  FILE *infp;      /* This Pointer reads fileNames in "LOOKUP.Txt" */
  FILE *infp1, *infp2, *outfp, *outfp2;
  /* infp1 for SDW;  inpf2 for ZIP;  outfp for DIFF;  outfp2 for GrandSum.Txt */

  infp=fopen ("LOOKUP.TXT", "r");  /* Catalog names are the same between SDW and ZIP, just locations are different */
  outfp2=fopen("GrandSum.TXT", "w");
				   /* This output Pointer is Once in the whole main.*/
				   /* Can't repeat opening in the following Sub-WHILE-loop */
  printf("\n");
  printf("Making final reports at each directory level at \\DIFF. \n");
  printf("Please review \"GrandSum.Txt\".\n\n");
  printf("(Optional) To restore the original directory names.  Run  \n");
  printf("\"5Rename.Exe\", then double click \"Rename.Bat\" file. \n");
  printf("\n");
  printf("Type any character key and [ENTER] to close this message: ");
  scanf("%s", &strPause);
  while( (fgets(FNtoOpen, 64, infp))!=NULL )  /* Opening files two at a time, a pair */
  {
	   firstwd (openedFN, &FNtoOpen[2]);          /* This is processing. */
	   spacedwd (openedFNGrndSum, &FNtoOpen[2]);  /* This is for "GrandSum.Txt". */
	   /* The two 'sprintf()' for Reading, one 'sprintf()' for Writing */
	   sprintf(openedSDWFNP, "SDW\\Erased\\%s.Txt", openedFN);
	   sprintf(openedZIPFNP, "ZIP_FL\\Erased\\%s.Txt", openedFN);
	   sprintf(writingFile, "DIFF\\%s.TXT", openedFN);
	   /* All three pass to the Pointers */
	   infp1 =fopen(openedSDWFNP, "r");
	   infp2 =fopen(openedZIPFNP, "r");
	   outfp =fopen(writingFile, "w");
	   lengthSDW=0;
	   iFlag=iSDW=iZIP=0;       /* Counter Initiation */

	   /* Reading both lines from two files simultaneously, the pair */
	   while( (fgets(lineSDW, 256, infp1))!=NULL &&  (fgets(lineZIP, 256, infp2))!=NULL )
	   {
		   lengthSDW=strlen(lineSDW);              /* Measuring the size of line in the first one */
		   if( strncmp(lineSDW, lineZIP, (lengthSDW-1))==0)
			  ;
		   else{
			  fprintf (outfp, "SDW   : %s", lineSDW);
			  fprintf (outfp, "ZIP_FL: %s", lineZIP);
			  iFlag++;
		   }
	   }   /* Closing Two Lines Comparison WHILE-LOOP */

	   /* In case ZIP catalog is longer, ZIP continues. */
	   if (infp2!=NULL){
		  while(  (fgets(lineZIP, 265, infp2))!=NULL ){
			 fprintf(outfp, "ZIP_FL: %s", lineZIP);
			 iFlag++; iZIP++;
		  }
	   }
	  *lineZIP=0;   /* reset in case */

	   /* In case SDW catalog is longer, SDW continues. */
	   if (infp1!=NULL){
		  while(  (fgets(lineSDW, 265, infp1))!=NULL ){
			 fprintf(outfp, "SDW   : %s", lineSDW);
			 iFlag++; iSDW++;
		  }
	   }
	   *lineSDW=0;  /* reset in case */

	   /* Now, after Three(3) Loops we can report "GrandSum.Txt".  */
	   if (iFlag==2)   fprintf (outfp2, "  No change at :  %s\n", openedFNGrndSum);
	   else if (iFlag>=2 && iSDW==0 && iZIP==0){
					   fprintf (outfp2, "  May be O.K.  :  - %s\n", openedFNGrndSum);
	   }else           fprintf (outfp2, "  Watch out at :  ****  %s\n", openedFNGrndSum);
	   iFlag=iSDW=iZIP=0;


	   *FNtoOpen=*openedFN=*openedFNGrndSum=0;
	   *openedSDWFNP=0;
	   *openedZIPFNP=0;
	   *writingFile=0;

	   fclose(infp1);
	   fclose(infp2);
	   fclose(outfp);
  }    /* Closing WHILE-LOOP, which reads directory names list in "Header.Txt" */
  fclose(outfp2);
}