/* mcPj009A.c */

/* After the success of File Reading and Folder Structure Handling,        */

/* Now, get into the Real World Business, porting PC "5Diff" to Macintosh.   */



/* (2004-11-30)  For better handling of Macintosh foldernames, filenames, 

 *    I modified 'firstwd.c'(First Word), to 'spacedwd.c'(Spaced Words).

 *    Right now, most of space including foldernames are properly handled.

 */

/* (2004-12-17)  In this occasion of porting, I also want to simplify the 

 *    Columns chopping into 7 parts to 3 parts.  Beginning-Erasing-Ending.

 *    Many chopping doesn't make sense, although it did have a testing purpose.

 */

/* (2004-12-20)  While testing the codes, I used ABSOLUTE, now is RELATIVE.

 *    Here are the testing Fixed Filename Codes, which worked well.

 *   

 *	    sprintf (openedSDWFNWithPath, "Mac-3:1DualWay:SDW:Erased:%s", openedFN);

 *	    sprintf (openedZIPFNWithPath, "Mac-3:1DualWay:ZIP:Erased:%s", openedFN);

 *	    sprintf (writingFile, "Mac-3:1DualWay:DIFF:%s", openedOutputFN);

 *

 */

/* (2004-01-22)  Read through to the longer file by Pointer is introduced.

 *    I noticed this bug a few weeks ago, and finally fixed it.

 */

/* (2005-01-31)  I ported "Grand_Summary.Txt" codes here from PC version.

 *    And it works fine.   (2005-02-05) And I am finalizing it for the 

 *    convenience of users.  Note the end of this, and MPW command line 

 *    in "3_MPWBatch" to make it Double-Clicks open.

 */





#include <stdio.h>

#include <stdlib.h>

#include <string.h>



char lineSDW [256];

char lineZIP [256];



char FNToOpen [64];  /* = filenamesToOpen */

char openedFN [64];  /* = opendFilename */

char openedSDWFNWithPath [64], openedZIPFNWithPath [64];

char outputFNToOpen [64];

char openedOutputFN [64];

char writingFile [64];









extern char *spacedwd (char *to, char *from);    /* = spaced words, to capture blank-containing foldername */



main()

{

    int lengthSDW, iFlag;           /* iFlag is counting output lines in a file to see which folders doesn't change. */

    

    FILE *infpSDW, *infpOUT;        /* These Pointers read fileNames list */

    FILE *infp1, *infp2, *outfp, *outfp2;   /* infp1->SDW_files; infp2->ZIP_files; outfp->Diff_Output; outfp2->Grand_Summary */

    infpSDW=fopen("HEADER", "r");    /* infpSDW reads Any and All Filenames at 'HEADER.TXT', the same as 'Dual-Way.Exe' */

    infpOUT=fopen("HEADER", "r");    /* infpOUT reads corresponding Filenames at 'OUT-HEAD.TXT', actually this is a trick for 8+3 rule to comply with .TXT extension appending.  In Macintosh I con modify. */

    outfp2=fopen("Grand_Summary.Txt", "w");

    

    printf("Making final reports at each folder level at :DIFF:.\n");

    printf("From the size of each report file, you can estimate which folder \n");

    printf("likely have a big change.  No change folder appears as 2 lines.\n");

    printf("Any file change should appear.\n\n");

    printf("To make final reports openable by double clicks, go to MPW Shell and \n");

    printf("select all command lines under :DIFF: by a mouse.  Execute them by \n");

    printf("the right-most [enter]. \n\n");

    printf("The mechanism of this \'diff\' report is based on reading two files \n");

    printf("at the same time from top of both files.  So, in case there is a \n");

    printf("line shift near the top, then all lines followed are reported. \n");

    printf("So, in a sense, this is not a sophisticated program, but just to \n");

    printf("quickly review whether any change is there at given folders. \n");

    printf("The good thing is that you always have the Master ZIP disk safely.\n");

    printf("And this dual way is somehow different from backup-&-restoration. \n\n");

    printf("To review \"Grand_Summary.Txt\" by SimpleText, you may use a line\n");

    printf("at the beginning of \"Batch\" file, which looks like.\n\n");

    printf("  SetFile -c ttxt \"(Your_working_folder_Path):Grand_Summary.Txt\"\n\n"); 

    

    



/*  Read all lines from the file stream pointing to "Ref.txt"             */

	while( (fgets(FNToOpen, 64, infpSDW))!=NULL )

	{

	    spacedwd (openedFN, &FNToOpen[0]);    /* Opening filename, one-by-one */

	    fgets (outputFNToOpen, 64, infpOUT);  /* For output filename */

	    spacedwd (openedOutputFN, &outputFNToOpen[0]);

	    

	    sprintf (openedSDWFNWithPath, ":SDW:Erased:%s", openedFN);

	    sprintf (openedZIPFNWithPath, ":ZIP:Erased:%s", openedFN);

	    sprintf (writingFile, ":DIFF:%s", openedOutputFN);

	   

	    infp1=fopen(openedSDWFNWithPath, "r");

	    infp2=fopen(openedZIPFNWithPath, "r");

	    outfp=fopen(writingFile, "w");

	    lengthSDW=iFlag=0;

	

	    /* Reading both lines from two files simultaneously, the pair */

		while( (fgets(lineSDW, 256, infp1))!=NULL && (fgets(lineZIP, 256, infp2))!=NULL ){     

		   lengthSDW = strlen(lineSDW);

		   if ( strncmp(lineSDW, lineZIP, (lengthSDW-1) )==0 )

		      ;

		   else{     /* reporting difference */

		       fprintf (outfp, "SDW:%s", lineSDW);

		       fprintf (outfp, "ZIP:%s", lineZIP);

		       iFlag++;          /* Counter for Grand_Summary.Txt */

           }

        }     /* closing Two Lines Comparison WHILE-LOOP */



        /* In case SDW Catalog is longer */

        if(infp1!=NULL){

           while( (fgets(lineSDW, 256, infp1))!=NULL ){

              fprintf(outfp, "SDW: %s", lineSDW);

		      iFlag++;

           }

        }

        *lineSDW=0;

        

        /* In case ZIP Catalog is longer */

        if(infp2!=NULL){

           while( (fgets(lineZIP, 256, infp2))!=NULL ){

              fprintf(outfp, "ZIP: %s", lineZIP);

		      iFlag++;

           }

        }

        *lineZIP=0;





        /* Since iFlag counter is still counting up when either catalog is longer,    */

        /*   so, the following Grand_Sum.Txt must be after them.                      */

        /* The Grand Summary report only needs to know iFlag count for each one set of comparison. */

        if (iFlag==1) fprintf (outfp2, "  No change at :  %s\n", openedFN);

        else          fprintf (outfp2, "  Watch out at :  ****  %s\n", openedFN);

        iFlag=0;







        *FNToOpen=*openedFN=0;   /* Reset, in case */

        *openedSDWFNWithPath=0;

        *openedZIPFNWithPath=0;

        fclose (infp1);

        fclose (infp2);

        fclose (outfp);

    }   /* closing, filenames opened by While-loop */

    fclose (infpSDW);

    fclose (infpOUT);

}