/* mcPj008A.c */

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

/* Now, get into the Real World Business, porting PC "4Eraser" 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.

 *  (2005-01-11, added note)

 *    This is true for TextEdit(OSX)-> SimpleText(OS8) mcPj007C.c, but not 

 *    fully tested in "MacDualWay".

 */

/* (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.

 */





#include <stdio.h>

#include <stdlib.h>

#include <string.h>

/* #define MgPathSDW 28  */   

/* #define MgPathZIP 15  */

                         /* When define, I shouldn't declare variable 'int MgPathSDW' */

                         /* While it is open to user's input, I couldn't declare 'define' */

                         /* Also, as I saw in "PROGRAMMING.01"(in SDW) and "Programming.01" */

                         /* (in ZIP) are recognized differently, better cut off this disk */

                         /* label by including into the MagicPath number.                */





char path [128], line [256];

/* Following three lines, 6 variables plus jumpThere are for Different Path Eraser */

char workingFD [128], shadowFD [128], zipLabel [128];

char workingFDP [128], shadowFDP [128], zipLabelP [128];

char jumpThere [128];

/* Following three lines are for SIZE Stamp Eraser */

char wordColumnBegin [128];

char wordColumnErase [128];

char wordColumnFinish [128];



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

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

char openedFNWithPath [64];

char writingFile [64];







extern char *firstwd (char *to, char *from);      /* to capture path */

//extern char *primedFirstwd (char *to, char *from);    /* to capture primed path */

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

extern char *shiftedLine (char *to, char *from);  /* To jump and erase the frontal Path difference */

extern char *WDsBegin (char *to, char *from);

extern char *WDsErase (char *to, char *from);

extern char *WDsFinish (char *to, char *from);



main()

{

    int WrkFDLen, SdwFDLen, zipLBLen;

    int MgPathSDW, MgPathZIP;

    

    FILE *infpPATH, *infpSDW, *infpZIP;

    infpPATH=fopen("Path", "r");

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

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

    

//	i=0;  

    fgets(path, 128, infpPATH);

      if( strncmp(path, "Working Folder", 14)==0 ) firstwd (workingFD, &path[16]);

    fgets(path, 128, infpPATH);

      if( strncmp(path, "Working FolderP", 15)==0 ) firstwd (workingFDP, &path[16]);

      

    fgets(path, 128, infpPATH);

      if( strncmp(path, "Shadow Folder", 13)==0 ) firstwd (shadowFD, &path[16]);

    fgets(path, 128, infpPATH);

      if( strncmp(path, "Shadow FolderP", 13)==0 ) firstwd (shadowFDP, &path[16]);

               

    fgets(path, 128, infpPATH);

      if( strncmp(path, "ZIP Disk Label", 14)==0 ) firstwd (zipLabel, &path[16]);

    fgets(path, 128, infpPATH);

      if( strncmp(path, "ZIP Disk LabelP", 15)==0 ) firstwd (zipLabelP, &path[16]);

      

/* Debugger, checking 

*    printf("%s, %s, %s\n\n", workingFD, shadowFD, zipLabel);

*    printf("%s, %s, %s\n", workingFDP, shadowFDP, zipLabelP);

*/

      

    WrkFDLen = strlen (workingFD);

    SdwFDLen = strlen (shadowFD);

    zipLBLen = strlen (zipLabel);

                                  



    printf("Now we need a little adjustment about Path differences in files.\n");

    printf("For example, your files in :SDW: and :ZIP: looks like this.\n\n");

    printf("\'Mac-3:1DaulWay:GRAPHICS.01:BalticSeal:\'   (SDW in Hard disk)\n");

    printf("\'Graphics.01:BalticSeal:\'                    (From ZIP disk)\n");

    printf("1...5....0....5....0....5....0....5\n");

    printf("1        10        20        30    \n\n");

    printf("In this case, if we erase 28 characters for SDW and 13 for ZIP, \n");

    printf("then, they become the same BalticSeal:.   Thus, erase the path \n");

    printf("difference by setting, \n");

    printf("     MgPathSDW is 28,\n");

    printf("     MgPathZIP is 13.\n");

    printf("Count carefully by using a cursor.  And come back here to go to \n");

    printf("the next step below.  To exit now, [File](menu) -> Quit.\n\n");                  



	printf("Please type Number of MgPathSDW:  ");

	scanf("%d", &MgPathSDW);

	printf("Magic Path for SDW is set to   :  %d\n\n", MgPathSDW);

	 

	printf("Please type Number of MgPathZIP:  ");

	scanf("%d", &MgPathZIP);

	printf("Magic Path for ZIP is set to   :  %d\n\n", MgPathZIP); 

	

	printf("One moment.  Then, go to see if SIZE stamps erased files are \n");

	printf("there at :SDW:Erased and :ZIP:Erased.  If there, we can go to \n");

	printf("the final step \"5_Diff\".  In case folder Path difference are \n");

	printf("not erased, count Magic Path (SDW and ZIP) again, and run this \n");

	printf("Program again. \n\n");

    printf("(Optional):   To open SIZE stamps erased files by double clicks,\n");

    printf("go to MPW Shell and select all command lines under :SDW-Erased: and \n");

    printf(":ZIP-Erased: by a mouse.  Execute them by the right-most [enter]. \n\n");





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

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

	{

	    FILE *infp1, *outfp1;             /* This fp1s are for SDW */

	    

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

	    

	    sprintf (openedFNWithPath, "%s:SDW:%s", workingFD, openedFN);

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

	   

	    infp1=fopen(openedFNWithPath, "r");

	    outfp1=fopen(writingFile, "w");

	

		fprintf (outfp1, "SDW\n");

		while( fgets(line, 256, infp1)!=NULL ){     /* NULL applies to the end of Pointer, while EOF applies to variables */

		   if ( strncmp(line, "Name", 4)==0 )

		      fprintf (outfp1, "%s", line);

		   else if ( strncmp(line, "-----", 5)==0 )

		      fprintf (outfp1, "%s", line);

		   else if ( strncmp(line, shadowFD, (SdwFDLen-1) )==0 ){

		      shiftedLine (jumpThere, &line[MgPathSDW]);

		      fprintf (outfp1, "%s", jumpThere);

		   }else if ( strncmp(line, shadowFDP, (SdwFDLen-1) )==0 ){

		      shiftedLine (jumpThere, &line[MgPathSDW+1]);

		      fprintf (outfp1, "%s", jumpThere);

		   }else if ( strncmp(line, "\n", 1)==0 )

		      fprintf (outfp1, "\n");

		   else{     /* At here, I need to apply SIZE Stamp Erasing */

		       WDsBegin  (wordColumnBegin, &line[0]);

		       WDsErase  (wordColumnErase, &line[43]);

		       WDsFinish (wordColumnFinish, &line[50]);

		       

		       fprintf (outfp1, "%s", wordColumnBegin);

		       fprintf (outfp1, "       ");

		       fprintf (outfp1, "%s", wordColumnFinish);

		       

		       *wordColumnBegin  =0;   /* Reset */

               *wordColumnErase  =0;

               *wordColumnFinish =0;

           }  /* closing Else-loop */

        }     /* closing While-loop */

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

        *openedFNWithPath=0;

        fclose (infp1);

        fclose (outfp1);

    }   /* closing, one-by-one file opening While-loop */

    fclose (infpSDW);

    

    

    

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

	{

	    FILE *infp2, *outfp2;             /* This fp2s are for ZIP */

	    

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

	    

	    sprintf (openedFNWithPath, "%s:ZIP:%s", workingFD, openedFN);

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

	   

	    infp2=fopen(openedFNWithPath, "r");

	    outfp2=fopen(writingFile, "w");

	

		fprintf (outfp2, "ZIP\n");

		while( fgets(line, 256, infp2)!=NULL ){     /* NULL applies to the end of Pointer, while EOF applies to variables */

		   if ( strncmp(line, "Name", 4)==0 )

		      fprintf (outfp2, "%s", line);

		   else if ( strncmp(line, "-----", 5)==0 )

		      fprintf (outfp2, "%s", line);

		   else if ( strncmp(line, zipLabel, (zipLBLen-1) )==0 ){

		      shiftedLine (jumpThere, &line[MgPathZIP]);

		      fprintf (outfp2, "%s", jumpThere);

		   }else if ( strncmp(line, zipLabelP, (zipLBLen-1) )==0 ){

		      shiftedLine (jumpThere, &line[MgPathZIP+1]);

		      fprintf (outfp2, "%s", jumpThere);

		   }else if ( strncmp(line, "\n", 1)==0 )

		      fprintf (outfp2, "\n");

		   else{     /* At here, I need to apply SIZE Stamp Erasing */

		       WDsBegin  (wordColumnBegin, &line[0]);

		       WDsErase  (wordColumnErase, &line[43]);

		       WDsFinish (wordColumnFinish, &line[50]);

		       

		       fprintf (outfp2, "%s", wordColumnBegin);

		       fprintf (outfp2, "       ");

		       fprintf (outfp2, "%s", wordColumnFinish);

		       

		       *wordColumnBegin  =0;   /* Reset */

               *wordColumnErase  =0;

               *wordColumnFinish =0;

           }  /* closing Else-loop */

        }     /* closing While-loop */

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

        *openedFNWithPath=0;

        fclose (infp2);

        fclose (outfp2);

    }   /* closing, one-by-one file opening While-loop */

    fclose (infpZIP);

}