/*  dpfilter.c

	This program converts DATAPLOT output in hpgl format to a format
	   readable by an HP-LaserJet III.

        Getting DATAPLOT output:
	   Assign the DATAPLOT DEVICE 2 as follows:
		DEVICE 2 HPGL 7574

        Converting dppl1f.dat:
	Execute DPFILTER:  The program asks the user for the input datafile
	   (the DATAPLOT output file) and the desired name of the output
	   file to be sent to the printer.  Currently, these two files must
	   have different names.

	When DPFILTER finishes, simply print the output file as you would
	   any text file.

	Feel free to copy, distribute, and modify this program.  The author
	   of the program encourages comments and appreciates notification
	   of any improvements made to the program.

	Ken Snyder
	Bldg. 226/ Room B348
	975-4260
*/

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

#define BUFFER_SIZE 10000  

int main(argc,argv)
   int argc;
   char *argv[];
				/* input and output files can be
				   included in the command line */
{
   char *buffer, 	/* holds each line of HGL commands */
	*flag,
	*stump;		/* HGL commands following a new page command.
			   PCL commands must be sent to the printer to
			   eject the page.
			*/
   FILE *infile,*outfile;
   char infile_name[80],outfile_name[80];

   int  i,
	page_count=0,   /* number pages found */
	line_length;

   long line_count=0,   /* number of lines in input file */
        byte_count=0;   /* size of input file */

   /*** ALLOCATE MEMORY FOR buffer ***/
   buffer = (char *)malloc((BUFFER_SIZE+1)*sizeof(char));
   if(buffer == NULL) {
      printf("Unable to allocate memory for buffer\n\n");
      exit(0);
      }
   buffer[BUFFER_SIZE] = '\000';

   /*** OPEN INPUT & OUTPUT FILES ***/
   if(argc < 2) {
      /* GET INPUT FILE NAME FROM CONSOLE */
      printf("Input file : ");scanf("%s",infile_name);
      }
   else
      /* GET INPUT FILE NAME FROM COMMAND LINE */
      strcpy(infile_name,argv[1]);

   /* OPEN INPUT FILE */
   if((infile=fopen(infile_name,"rt")) == NULL) {
      printf("Unable to open %s for input\n\n",infile_name);
      exit(0);
      }

   if(argc < 3) {
      /* GET OUTPUT FILE NAME FROM CONSOLE */
      printf("Output file: ");scanf("%s",outfile_name);
      }
   else
      /* GET OUTPUT FILE NAME FROM COMMAND LINE */
      strcpy(outfile_name,argv[2]);

   /* OPEN OUTPUT FILE */
   if((outfile=fopen(outfile_name,"wt")) == NULL) {
      printf("Unable to open %s for input\n\n",outfile_name);
      exit(0);
      }


   /*** PLACE HEADER ON OUTPUT FILE ***/
   fprintf(outfile,"\033&l1O"); /* Landscape */
   fprintf(outfile,"\033%%1B");	/* HP-GL mode */

   printf("Starting page: %d\n",++page_count);

   /* READ EACH LINE OF HGL COMMANDS INTO buffer AND PARSE */
   while((flag=fgets(buffer,BUFFER_SIZE,infile)) != NULL) {
      line_count ++;
      i = 0;	/* POINTER TO NEXT CHARACTER IN buffer */
      line_length = strlen(buffer);
      byte_count += (long)line_length;
      while( i < line_length ) {
	 if(buffer[i] == 'A') {		/* THE KEY BETWEEN PAGES IS 'AF;' */
	    if(buffer[i+1] == 'F') {
		/*** EJECT PAGE ***/
		buffer[i] = '\n';	   /* TERMINATE HGL COMMAND LINE */
		buffer[i+1] = '\0';
		if(strlen(buffer) > 1)
		   fputs(buffer,outfile); /* SEND SHORTENED COMMAND LINE */
                /*** ENTER PCL MODE ***/
	        fprintf(outfile,"\033%%0A"); /* CONTRARY TO THE HP MANUAL */
                /*** EJECT PAGE ***/
		fprintf(outfile,"\033&l0H");
		/*** ENTER HP-GL MODE ***/
		fprintf(outfile,"\033%%1B");
		/*** PRINT REMAINDER OF BUFFER ***/
		stump = &buffer[i+2];     /* REMAINDER OF HGL COMMANDS */
		if(strlen(stump) > 1)
		   /* MOVE THE REMAINING COMMANDS LEFT TO buffer[0] */
		   strcpy(buffer,stump);
		i = 0;    /* POINT TO BEGINNING OF buffer */
		line_length = strlen(buffer);
		printf("Starting page: %d\n",++page_count);
	    }
	 }
	 i ++;
      }
      fputs(buffer,outfile);  /* COPY HGL COMMANDS TO OUTPUT FILE */
   }

   /*** ENTER PCL MODE ***/
   fprintf(outfile,"\033%%0A");
   /*** EJECT PAGE ***/
   fprintf(outfile,"\033&l0H");
   /*** PORTRAIT ORIENTATION ***/
   fprintf(outfile,"\033&l0O");

   close(infile);
   close(outfile);

   free(buffer);

   printf("line count: %ld\n",line_count);
   printf("byte count: %ld\n",byte_count);
   return 0;
}


