/* Pro134D.c */
/* Word Capture in the middle of 'inline' by Memory Location '&inline[]'.  */
/* This is Portable version of 'Pro134D.exe' (DATE-TIME Stamp Eraser)
 *	to share.
 */
/* Read 'HEADER.TXT' file.
 *    Originally, it was Array, the fileNames[] listing.
 *    Two Magic Numbers became a user's input.
 *    One Magic Characger was added as a generic Drive name.
 */
/* SDW is working now, and now for ZIP.  I preferred different
 *   Pointer version (*infpSDW, *infpZIP), I wrote before.
 *   Codes become redundant, but it is easy to read, and I
 *   don't have to worry about if (SDW or ZIP)-> then this way, that way.
 */
/* Regarding char *fileNames [28, OR, 128], I am still confusing,
 *   is this 128 is the max length of string(s), OR the max
 *   number of static array members 28 ?
 */
/* User's input Drive name is introduced, and bypassed in one way,
 *   and strictly recognized in another way.   See deails in comment.
 */


#include <stdio.h>
#include <string.h>
#include <ctype.h>
// #define MgPathSDW 29   /* 33 for WEB-ARCH.01-02;  29 for CORP.01-01 */
// #define MgPathZIP 15   /* With extra CORP.02, could be 23 */

char inline [256];
char jumpThere [128];
char wordColumn1  [128];
char wordColumn10 [128];
char wordColumn16 [128];
char wordColumn22 [128];
char wordColumn29 [128];
char wordColumn39 [128];
char wordColumn45 [128];

char openingFile [64];
char openedFilename  [64];
char openedFNWithPath [128];   /* Full name is openedFileNameWithPath */
char writingFile [64];
// char name1 [64];
char name2 [64];               /* About user's input Drive name + alpha */


extern char *firstwd (char *to, char *from);
extern char *shiftedLine (char *to, char *from);   /* Pro998-2.c */
extern char *firstWD     (char *to, char *from);   /* 1stWd-09.c */
extern char *secondWD    (char *to, char *from);   /* 2ndWd-06.c */
extern char *thirdWD     (char *to, char *from);   /* 3rdWd-06.c */
extern char *fourthWD    (char *to, char *from);   /* 4thWd-07.c */
extern char *fifthWD     (char *to, char *from);   /* 5thWd-10.c */
extern char *sixthWD     (char *to, char *from);   /* 6thWd-06.c */
extern char *seventhWD   (char *to, char *from);   /* 7thWdEnd.c */



void main(void){
   char MgDrive[16], UpperLetterMgDrive[16];
   int i, MgPathSDW, MgPathZIP;

   FILE *infpSDW, *infpZIP;
   infpSDW=fopen ("HEADER.TXT", "r");
   infpZIP=fopen ("HEADER.TXT", "r");
				   /* Right now, both 'SDW(ZIP)-head.TXT' are the same, so this doesn't make sense.   But for convenience of opening another set of file listing. */

   i=MgPathSDW=MgPathZIP=0;

   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("Directory of C:\\TO-CD\\CORP.02\\001-DIRC.TRY  (SDW in Hard disk)\n");
   printf("Directory of E:\\001-DIRC.TRY                  (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 29 charaters for SDW and 15 for ZIP, \n");
   printf("then, they become the same \\001-DIRC.TRY.  Thus, erase the path \n");
   printf("difference by setting,\n");
   printf("     MgPathSDW is 29,\n");
   printf("     MgPathZIP is 15.\n");
   printf("Count carefully by using a cursor.  And come back here to go to \n");
   printf("the next step below.   To exit now, as always [Ctrl]-key + [C]-key.\n\n");
   printf("Type Number of MgPathSDW: ");
   scanf("%d", &MgPathSDW);
   printf("Magic Path for SDW is set to %d.\n\n", MgPathSDW);

   printf("Type Number of MgPathZIP: ");
   scanf("%d", &MgPathZIP);
   printf("Magic Path for ZIP is set to %d.\n\n", MgPathZIP);

   printf("Type Drive Name of Your ZIP Disk: ");
   scanf("%s", &MgDrive);
   UpperLetterMgDrive[0]=toupper(MgDrive[0]);
   UpperLetterMgDrive[1]='\0';           /* Without this, the Drive name was unstable. */
   printf("Drive name for ZIP is set to %s.\n\n", UpperLetterMgDrive );

   printf("One moment.  Then, go to see if DATE-TIME stamps erased\n");
   printf("files are there at \\SDW\\ERASED and \\ZIP\\ERASED.\n");
   printf("If there, we can go to the final step '5Diff.exe'.\n");
   printf("In case directory Path difference are not erased, count \n");
   printf("Magic Path (SDW and ZIP) again, and run this Program again.\n\n");



   while( (fgets(openingFile, 64, infpSDW))!=NULL )
   {
	   FILE *infp1, *outfp1;

	   firstwd (openedFilename, &openingFile[4]);   /* This firstwd() is important to capture the filename */
													/*  without extra blanks and a carrige return.         */
	   sprintf(openedFNWithPath, "SDW\\%s", openedFilename);
	   infp1=fopen ( openedFNWithPath, "r");
	   sprintf(writingFile, "SDW\\ERASED\\%s", openedFilename);
	   outfp1=fopen( writingFile, "w");

	   i++;                     /* Maximum i is the MgFileNo in Array version */
	   while(fgets(inline, 256, infp1)!=NULL){
		  if     (strncmp(inline, " Volume in drive C", 18)==0)
			 fprintf (outfp1, "C\:\n");
		  else if(strncmp(inline, " Volume Serial", 14)==0)
			 ;
		  else if(strncmp(inline, "Directory of C", 14)==0){
			 shiftedLine(jumpThere, &inline[MgPathSDW]);
			 fprintf (outfp1, "%s", jumpThere);
		  }else if(strncmp(inline, "\n", 1)==0)
			 fprintf (outfp1, "\n");
		  else if(strncmp(inline, "      ", 6)==0)
			 fprintf (outfp1, "%s", inline);
		  else if(strncmp(inline, "Total", 5)==0)
			 fprintf (outfp1, "%s", inline);
		  else     /* At here, I need to apply DATE-TIME Stamp Erasing */
		  {
			 firstWD  (wordColumn1,  &inline[0]);
			 secondWD (wordColumn10, &inline[9]);
			 thirdWD  (wordColumn16, &inline[15]);
			 fourthWD (wordColumn22, &inline[21]);
			 fifthWD  (wordColumn29, &inline[28]);
			 sixthWD  (wordColumn39, &inline[38]);
			 seventhWD(wordColumn45, &inline[44]);

			 fprintf (outfp1, "%s", wordColumn1);
			 fprintf (outfp1, "%s", wordColumn10);
			 fprintf (outfp1, "%s", wordColumn16);
			 fprintf (outfp1, "%s", wordColumn22);
	//		 fprintf (outfp1, "%s", wordColumn29);
	//		 fprintf (outfp1, "%s", wordColumn39);
			 fprintf (outfp1, "                ");
			 fprintf (outfp1, "%s", wordColumn45);

			 *wordColumn1 =0;     /* Reset */
			 *wordColumn10=0;
			 *wordColumn16=0;
			 *wordColumn22=0;
			 *wordColumn29=0;
			 *wordColumn39=0;
			 *wordColumn45=0;
		  }
	   }
	   *openedFilename=0;         /* Reset, in case  */
	   *openedFNWithPath=0;

	   fclose (infp1);
	   fclose (outfp1);
   }    /* Close the first Filename changing WHILE-LOOP for SDW */
   MgPathSDW=0;  /* Reset */
   fclose (infpSDW);



   while( (fgets(openingFile, 64, infpZIP))!=NULL )
   {

	   FILE *infp2, *outfp2;

	   firstwd (openedFilename, &openingFile[4]);

	   sprintf(openedFNWithPath, "ZIP\\%s", openedFilename);
	   infp2=fopen ( openedFNWithPath, "r");
	   sprintf(writingFile, "ZIP\\ERASED\\%s", openedFilename);
	   outfp2=fopen( writingFile, "w");

	   /* Appending user's input Drive name to strings */
//	   strcat(name1, " Volume in drive ");
//	   strcat(name1, UpperLetterMgDrive);
	   strcat(name2, "Directory of ");
	   strcat(name2, UpperLetterMgDrive);

	   i++;                     /* Maximum i is the MgFileNo in Array version */
	   while(fgets(inline, 256, infp2)!=NULL){

		  if(strncmp(inline, " Volume in drive", 16)==0){
			 fprintf (outfp2, "%s\:\n", UpperLetterMgDrive);
							   /* Bypassing strict recognition of Drive name */
//		  else if(strncmp(inline, name1, 18)==0)
//			 fprintf (outfp2, "%s\:\n", UpperLetterMgDrive);
							   /* This recognition worked only partially, see comment. */
		  }else if(strncmp(inline, " Volume Serial", 14)==0)
			 ;
		  else if(strncmp(inline, name2, 14)==0){
			 shiftedLine(jumpThere, &inline[MgPathZIP]);
			 fprintf (outfp2, "%s", jumpThere);
							   /* Keeping strict recognition of Drive name */
		  }else if(strncmp(inline, "\n", 1)==0)
			 fprintf (outfp2, "\n");
		  else if(strncmp(inline, "      ", 6)==0)
			 fprintf (outfp2, "%s", inline);
		  else if(strncmp(inline, "Total", 5)==0)
			 fprintf (outfp2, "%s", inline);
		  else     /* At here, I need to apply DATE-TIME Stamp Erasing */
		  {
			 firstWD  (wordColumn1,  &inline[0]);
			 secondWD (wordColumn10, &inline[9]);
			 thirdWD  (wordColumn16, &inline[15]);
			 fourthWD (wordColumn22, &inline[21]);
			 fifthWD  (wordColumn29, &inline[28]);
			 sixthWD  (wordColumn39, &inline[38]);
			 seventhWD(wordColumn45, &inline[44]);

			 fprintf (outfp2, "%s", wordColumn1);
			 fprintf (outfp2, "%s", wordColumn10);
			 fprintf (outfp2, "%s", wordColumn16);
			 fprintf (outfp2, "%s", wordColumn22);
	//		 fprintf (outfp2, "%s", wordColumn29);
	//		 fprintf (outfp2, "%s", wordColumn39);
			 fprintf (outfp2, "                ");
			 fprintf (outfp2, "%s", wordColumn45);

			 *wordColumn1 =0;     /* Rest */
			 *wordColumn10=0;
			 *wordColumn16=0;
			 *wordColumn22=0;
			 *wordColumn29=0;
			 *wordColumn39=0;
			 *wordColumn45=0;
		  }
	   }
	   fclose (infp2);
	   fclose (outfp2);


	}     /* Close the second Filename changing WHILE-LOOP for ZIP */
	MgPathZIP=0;  /* Reset */
//	*name1=0;
	*name2=0;
	fclose (infpZIP);
}