/* gccPj002.c */
/* Revised with brand new Field(s) Erasing Objects in 2008  */
/* 2Er - executable file posted  */

/* History:   PC "4Eraser" => Classic Macintosh by CodeWarrior 3  =>  MacOSX Xcode GCC  */

/* (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.
 */
/* (2005-03-05)  Since this "4_Eraser" is working fine for MacOSX (<< ported from 
 *    MacOS7/8/9 CodeWarrior version) , I will edit the internal message for OSX 
 *    here.   Namely, no Path Difference Eraser required at OSX.
 */
/* (2005-03-12)  Since a part of OWNER stamps which I want to erase is not 
 *    fixed width but flexibly floating in size, so I introduced a flexible 
 *    Sandwitch (*fild_no1, fild_no2) and Liquid Paper.   Here "JUMP_NO" is 
 *    not necessary, but it came from the root of Sandwitch, PC Win3.1 codes.
 */


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


char line [256];
char upField [128];
/* there were upField[] & downField[] in 2005, now I only need one, so I kept upField.  Didn't rename.  */

char FNToOpen [64];  /* = filenamesToOpen */
char openedFN [64];  /* = opendFilename */
char openedFNP [64]; /* = opendFilenameWithPath */
char writingFile [64];
int OPTION;



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


    /* Erasing Field,  1 */
extern char *copy_a_2_3_4_5_6_7_8_9  (char *to, char *from); 

    /* Erasing Field,  4 */
extern char *copy_b_1_2_3_and_5_6_7_8_9  (char *to, char *from);

    /* Erasing Fields,  3+4 */
extern char *copy_c_1_2_and_5_6_7_8_9  (char *to, char *from);

    /* Erasing Fields, 1, 3+4 */
extern char *copy_d_2_and_5_6_7_8_9  (char *to, char *from);      

    /* Erasing Fields, 1, 6+7+8 */
extern char *copy_e_2_3_4_5_and_9  (char *to, char *from);     

    /* Erasing Fields, 2, 4+5 */
extern char *copy_f_1_and_3_and_6_7_8_9  (char *to, char *from);     



main()
{

    int JUMP_NO;
    FILE *infpSDW, *infpZIP_FLASH;

    JUMP_NO=0;

/*  The folllowing ";" are meant to continue, not for the end of sentence. 
 *   I use this, because I saw Five Time of opening, so I like the compact space.
 *   Maybe I replace to continue for clearness.
 */
    if      (  (infpSDW=fopen("Header",     "r"))!= NULL)      ; 
    else if (  (infpSDW=fopen("Header.Txt", "r"))!= NULL)      ; 
    else{
       printf("\n!!!!!   Please put a \"Header(.txt)\" file about your Folder information.\n");
       printf("!!!!!   And do this again.\n\n");
       exit (0);
    }

    if      (  (infpZIP_FLASH=fopen("Header",     "r"))!= NULL)      ; 
    else if (  (infpZIP_FLASH=fopen("Header.Txt", "r"))!= NULL)      ; 
    else{
       exit (0);
    }
	
	
  /*  Now, let's get started. */

   printf("To be a flexible eraser to the slightly different formats of \n");
   printf("\"Catalog.txt\", here are a couple of examples of erasing field. \n");
   printf("Please examine your catalogs at \"SDW\" \n");
   printf("and \"ZIP_FLASH\" folders, and find out which columns are \n");
   printf("different among total nine(9) columns(fields), and come \n");
   printf("back here to continue. \n\n");
   printf("Please select which combination of column(s)(fields) you \n");
   printf("want to erase for easy comparison. \n\n");

   printf("  1. Erasing Field   No. 1 \n");
   printf("  2. Erasing Field   No. 4 \n");
   printf("  3. Erasing Fields  No. 3+4    \n");
   printf("  4. Erasing Fields  No. 1, 3+4  \n");
   printf("  5. Erasing Fields  No. 1, 6+7+8, Permission + Date,Time  \n");
   printf("  6. Erasing Fields  No. 2, 4+5, (5th is byte field)  \n\n");

   printf("Select Your Field(s)_Erasing_Option by Number [1, 2, 3, 4, 5 or 6] : ");
   scanf("%d", &OPTION);
   printf(" \n\n\n");
    
	
                                  
	printf("\n\n\nOne moment.  Then, go to see how the designated fields were erased \n");
	printf("in files at ./SDW/Erased/ and ./ZIP_FLASH/Erased/.  If erased files are there, we \n");
	printf("can go to the last step \"3Diff\".  \n\n\n");


/*  Read all lines in each catalogs in the SDW folder, and apply Eraser  ===============  */

	while( (fgets(FNToOpen, 64, infpSDW))!=NULL )
	{
	    FILE *infp1, *outfp1;             /* This fp1s are for SDW */
	    
	    spacedwd (openedFN, &FNToOpen[0]);    /* Opening filename, one-by-one */
	    
	    sprintf (openedFNP, "./SDW/%s.txt", openedFN);
	    sprintf (writingFile, "./SDW/Erased/%s.txt", openedFN);
	   
	    infp1=fopen(openedFNP, "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, "total", 5)==0 )
		          fprintf (outfp1, "total\n");
		      else if ( strncmp(line, "./", 2)==0 )
		         fprintf (outfp1, "%s", line);
		      else if ( strncmp(line, "\n", 1)==0 )
		         fprintf (outfp1, "\n");
		      else{     /* At here, I apply FIELD(s) Erasing Options  */
                   
					/* ***************************************************************** */

					   if (OPTION==1) {
						   copy_a_2_3_4_5_6_7_8_9 (upField, &line[0]);          /* Erasing Field No.1 Only */
						   fprintf (outfp1, "%s", upField);
						   /* Blanks for "  -rwxrwxr-x" */
					   }
					   if (OPTION==2) {
						   copy_b_1_2_3_and_5_6_7_8_9 (upField, &line[0]);      /* Erasing Field, No.4 Only */
						   fprintf (outfp1, "%s", upField);
						   /* Blanks for "mkido" OR else */
					   }
					   if (OPTION==3) {
						   copy_c_1_2_and_5_6_7_8_9 (upField, &line[0]);        /* Erasing Fields, 3+4   */
						   fprintf (outfp1, "%s", upField);
						   /* Blanks for "mkido  mkido"  OR else */
					   }
					   if (OPTION==4) {
						   copy_d_2_and_5_6_7_8_9 (upField, &line[0]);     /* Erasing Fields, 1, 3+4   */
						   fprintf (outfp1, "%s", upField);
						   /* Blanks for "-rwxrwxr-x  mkido mkido" OR else */
					   }
					   if (OPTION==5) {
						   copy_e_2_3_4_5_and_9 (upField, &line[0]);     /* Erasing Fields, 1, 6+7+8 */
						   fprintf (outfp1, "%s", upField);   /*  DEBUG Switch  */
						   /* Blanks for 1st "-rwxrwxr-x"  6th_7th_8th "Apr 11 20:23" */
					   }
					   if (OPTION==6) {
						   copy_f_1_and_3_and_6_7_8_9 (upField, &line[0]);     /* Erasing Fields, 2, 4+5   */
						   fprintf (outfp1, "%s", upField);
						   /* Blanks for 2nd "3" and 4th_5th "mitsuruk 102" OR else */
					   }
					/* ********************************************************************* */

                   		       
			   }  /* closing Else-loop */
                
		}     /* closing While-loop of SDW catalog Reading*/
		*openedFN=0;   /* Reset, in case */
		*openedFNP=0;
		fclose (infp1);
		fclose (outfp1);
		
	}   /* closing, one-by-one file opening While-loop */
	fclose (infpSDW);


    
/*  Read all lines in each catalogs in the ZIP_FLASH folder, and apply Eraser  ===============  */
    
	while( (fgets(FNToOpen, 64, infpZIP_FLASH))!=NULL )
	{
	    FILE *infp2, *outfp2;             /* This fp2s are for ZIP_FLASH */
	    
	    spacedwd (openedFN, &FNToOpen[0]);    /* Opening filename, one-by-one */
	    
	    sprintf (openedFNP, "./ZIP_FLASH/%s.txt", openedFN);
	    sprintf (writingFile, "./ZIP_FLASH/Erased/%s.txt", openedFN);
	   
	    infp2=fopen(openedFNP, "r");
	    outfp2=fopen(writingFile, "w");
	
		fprintf (outfp2, "ZIP_FLASH\n");
		while( fgets(line, 256, infp2)!=NULL ){     /* NULL applies to the end of Pointer, while EOF applies to variables */
		       if ( strncmp(line, "total", 5)==0 )
		          fprintf (outfp2, "total\n");
		       else if ( strncmp(line, "./", 2)==0 )
		          fprintf (outfp2, "%s", line);
		       else if ( strncmp(line, "\n", 1)==0 )
		          fprintf (outfp2, "\n");
		       else{     /* At here, I apply  FIELD(s) Erasing Options */

					/* ***************************************************************** */

					   if (OPTION==1) {
						   copy_a_2_3_4_5_6_7_8_9 (upField, &line[0]);           /* Erasing Field No.1 Only*/
						   fprintf (outfp2, "%s", upField);
						   /* Blanks for "  -rwxrwxr-x" */
					   }
					   if (OPTION==2) {
						   copy_b_1_2_3_and_5_6_7_8_9 (upField, &line[0]);      /* Erasing Field No.4 Only*/
						   fprintf (outfp2, "%s", upField);
						   /* Blanks for "root" (in case of CD) OR else */
					   }
					   if (OPTION==3) {
						   copy_c_1_2_and_5_6_7_8_9 (upField, &line[0]);      /* Erasing Fields, 3+4 */
						   fprintf (outfp2, "%s", upField);
						   /* Blanks for "root  root" OR else */
					   }
					   if (OPTION==4) {
						   copy_d_2_and_5_6_7_8_9 (upField, &line[0]);       /* Erasing Fields, 1, 3+4  */
						   fprintf (outfp2, "%s", upField);
						   /* Blanks for "  -rwxrwxr-x" and   "root  root" OR else */
					   }
					   if (OPTION==5) {
						   copy_e_2_3_4_5_and_9 (upField, &line[0]);      /*  Erasing Fields, 1, 6+7+8  */
						   fprintf (outfp2, "%s", upField);
						   /* Blanks for 1st "-rwxrwxr-x" and 6th_7th_8th "Apr 11 20:23" */
					   }
					   if (OPTION==6) {
						   copy_f_1_and_3_and_6_7_8_9 (upField, &line[0]);     /* Erasing Fields, 2, 4+5   */
						   fprintf (outfp2, "%s", upField);
						   /* Blanks for 2nd "1" and 4th_5th "admin 16384" OR else */
					   }
					/* ***************************************************************** */
                   	       
				}  /* closing Else-loop */
                
		}     /* closing While-loop of ZIP catalog Reading */
		*openedFN=0;   /* Reset, in case */
		*openedFNP=0;
		fclose (infp2);
		fclose (outfp2);
	}   /* closing, one-by-one file opening While-loop */
	fclose (infpZIP_FLASH);
}