
/*
  Name:               postsplit.c
  Processor:          Amiga 68000
  Compiler directive: lc -L xxx.c
  Creation Date:      4 Sept 92
  Version:            1.02
  Author:             I Parker

  Description Splits pagestream colour postscript output into separate files
  anl part working

  Known bugs:- At 'End Page' key words, two cr's are placed

*/

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

#define UNLOCK 0
#define LOCK 1

FILE *ifp, *ofp, *fopen();
void Rout1(), Rout2(), Rout3(), Rout4(), Output(), Stop();
char inline[300];
char outline[300];
int index,a,b;


main (argc, argv)
int argc;
char *argv[];
{
	if(argc != 3)
	{
	printf("\033[33mpostsplit by I Parker V1.02\n");
			 /* \033[33m = prt blue */
	printf("Usage:- postsplit file.ps ([cyan|magenta|yellow|black|spotcolour]\n");
        printf("[anl|fnt])\033[31m\n"); 
			 /* \033[31m = reset */
	}

	if(argv[2][0] == 'a' && argv[2][1] == 'n' && argv[2][2] == 'l')
	{
	Rout1(argv);
	}
   	if(argv[2][0] == 'f' && argv[2][1] == 'n' && argv[2][2] == 't')
	{
	Rout3(argv);
	}
   	if(argv[2][0] == 'n' && argv[2][1] == 'u' && argv[2][2] == 'l')
   {
   Rout4();
   }
	if(argc == 3)
	{
        Rout2(argv);
	}
} /* end of main */

void Rout1(anl) /* routine to analyse the file */
char *anl[];
{
char name[40];

   if((ifp = fopen(anl[1],"r"))==NULL)
	 {
	   fprintf(stderr, "Error : Cannot open input file %s\n", strupr(anl[1]));
	   exit(-1);
    }
	  strcpy(name,anl[1]);
	  strcat(name,".anl");

	  if((ofp = fopen(name, "wb"))==NULL)
	    {
	    fprintf(stderr, "Error: Unable to open output file %s\n",name);
	    exit(1);
	    }
		  printf("postscript analysis routine\n");
		  while(fgets(inline,300,ifp)!=NULL) /* to end of file */
		   {
		   index = 0;
			a = 0;
                      for (index=0; inline[index] != '\0'; index++)
switch(inline[index])
{
  case '%': { a=4; } break;

  case '!':  {  if(a==4)  {  a=30;  Output(inline[index]);  }  }  break;

  case '(':   {   a=2;   }   break;

  case '"':  {  if(a==2)  {  a=10;  Output(inline[index]);  }  }  break;

  default: if(a > 5) { Output(inline[index]); }

} /* end of switch */
 } /* end of while - null */

      Stop();
} /* end of rout1 */

void Rout2(spl) /* routine to split the file */
char *spl[];
{
char name[40]; /* this is so that the o/p file name can be generated with */
               /* a suffix attached */
int index;
int flag = UNLOCK;
int countbig = 1; /* count beginings */
int countend = 1; /* count endings */
int preq = 0; /* page required */
int pmax = 0; /* maximum page in file */

{
	if(spl[2][0] == 'a' && spl[2][1] == 'n' && spl[2][2] == 'l')
        return;
	if(spl[2][0] == 'n' && spl[2][1] == 'u' && spl[2][2] == 'l')
        return;
	if(spl[2][0] == 'f' && spl[2][1] == 'n' && spl[2][2] == 't')
        return;

	if((ifp = fopen(spl[1],"r"))==NULL) {
	fprintf(stderr, "Error : Cannot open input file %s\n", strupr(spl[1]));
		exit(-1);
		}
	strcpy(name,spl[1]); /* create o/p file name from i/p names */
	strcat(name,".");
	strcat(name,spl[2]);

	if((ofp = fopen(name, "wb"))==NULL) {
	fprintf(stderr, "Error : Unable to open output file %s\n",name);
	exit(1);
	}
   printf("Enter page number required \n");
   scanf("%d",&preq);
   preq = preq+1;
   printf("Enter maximum page number in file \n");
   scanf("%d",&pmax);
   pmax = pmax+1;
	printf("Splitting postscript file generating %s fragment\n",name);	
	while(fgets(inline,300,ifp)!=NULL)
	 {
		   index = 0;
			a = 0;
         b = 0;
                      for (index=0; inline[index] != '\0'; index++)
{
   if(countbig < 2)
   {
   Output(inline[index]);
   }
   if(preq == countbig)
   {
   Output(inline[index]);
	}
   if(preq == countend)
   {
   countbig=100;
   }
	if(pmax == countend)
	{
	Output(inline[index]);
	}
switch(inline[index])  /* fine phrase % Begin P*/
{
 case '%': { a=1; } break;

 case ' ': { if(a==1) a=2; if(a==7) a=8; } break;

 case 'B': { if(a==2) a=3;else a=0; } break;

 case 'e': { if(a==3) a=4;else a=0; } break;

 case 'g': { if(a==4) a=5;else a=0; } break;

 case 'i': { if(a==5) a=6;else a=0; } break;

 case 'n': { if(a==6) a=7;else a=0; } break;

 case 'P': { if(a==8) {flag = LOCK; a=9;countbig++;}else a=0;} break;
 
 default: break;

} /* end of switch */
switch(inline[index])  /* fine phrase % End P*/
{
case '%': { b=11;} break;

case ' ': { if(b==11) b=12; if(b==15) b=16; } break;

case 'E': { if(b==12) b=13;else b=0; } break;

case 'n': { if(b==13) b=14;else b=0; } break;

case 'd': { if(b==14) b=15;else b=0; } break;

case 'P': { if(b==16) {b=17;}else b=0; } break;

case 'a': { if(b==17) {b=18;}else b=0;}break;

case 'g': { if(b==18) {b=19;}else b=0;}break;

case 'e': { if(b==19) {countend++;}else b=0;}break;

default: break;

} /* end of else */
} /* end of for */
	}
}
	Stop();
} /* end of rout2 */

void Rout3(blc)
char *blc[];
{
char name[40]; /* this is so that the o/p file name can be generated with */
               /* a suffix attached */
int index;
int countfnt = 0;
{
	if((ifp = fopen(blc[1],"r"))==NULL) {
	fprintf(stderr, "Error : Cannot open input file %s\n", strupr(blc[1]));
		exit(-1);
		}
	strcpy(name,blc[1]); /* create o/p file name from i/p names */
	strcat(name,".fnt");

	if((ofp = fopen(name, "wb"))==NULL) {
	fprintf(stderr, "Error : Unable to open output file %s\n",name);
	exit(1);
	}
   printf("Extracting adobe font data (other fonts must be done manualy)\n");
	while(fgets(inline,300,ifp)!=NULL)
	 {
		   index = 0;
			a = 0;
                      for (index=0; inline[index] != '\0'; index++)
{
   if (a==2 && countfnt == 2)
   {
   a=0;
   Output('%');
   Output('!');
   }
	if( countfnt > 1)
	{
	Output(inline[index]);
	}

switch(inline[index])  /* fine phrase % Begin P*/
{
 case '%': { a=1; } break;

 case '!': { if(a==1) {a=2;countfnt++;}} break;

 default: {a=0;break;}
} /* end of switch */

} /* end of for */
	}
}
	Stop();
} /* end of rout3 */

void Rout4()
{
printf("rout4 not programmed\n");
}

void Output(ili)
char ili;
{
putc(ili,ofp);

}

void Stop()
{
fclose(ifp);
fclose(ofp);
exit();
}