home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / Mathematics / form1.1 / 2A5onA4.c next >
C/C++ Source or Header  |  1992-08-27  |  4KB  |  186 lines

  1. Below is the program 2A5onA4.c for the dvips by Rokicki
  2. It is not the prettiest code (it was a quick hack) but it worked for me
  3. (on a NeXT computer)
  4. Good luck
  5.  
  6.  
  7. /*
  8.     Program combines two A5 pages in a postscript file so that they
  9.     are written on a single A4 sheet of paper. It assumes that the
  10.     postscript file was generated with the statement
  11.         dvips filename -t landscape -o
  12.     The A5 pagesize should have been established in the TeX file.
  13.  
  14.     The algorithm is rather simple:
  15.  
  16.     1:    look for the line with the statement
  17.         %%EndProlog
  18.     2:    Set page counter on zero
  19.     3:    For each %%Page at the beginning of a line, raise the
  20.         page counter by one.
  21.     4:    If the page counter becomes odd, add after the next '\n'
  22.         the string
  23.         "-20 -30 translate\n"
  24.     5:    After adding the above line, search for the string
  25.         "eop\n%%Page"
  26.     6:    Once found, raise the page counter by one.
  27.         Take out the string and all characters to the first
  28.         '\n'. Leave the '\n' and add after it:
  29.         "clear SI restore 0 421 translate\n"
  30.     7:    Any line that starts with "%%EOF" terminates the file.
  31.         The "%%EOF" should be copied.
  32.  
  33.     The running syntax is:
  34.     2A5onA4 infile outfile
  35.  
  36.     Program made by J.A.M.Vermaseren
  37. */
  38.  
  39. #include <stdio.h>
  40. #include <string.h>
  41.  
  42. #define BUFSIZE 1024
  43.  
  44. FILE *fin, *fout;
  45. char buffer[2*BUFSIZE];
  46. char *from = buffer;
  47. char *last = buffer;
  48. char *hi = buffer;
  49. int pagenum = 0;
  50.  
  51. char oddpage[] = "\n-20 -30 translate\n";
  52. char eop[] = "eop\n";
  53. char evenpage[] = "\nclear SI restore 0 421 translate\n";
  54.  
  55. int ReadOne(void);
  56. void PutOne(int);
  57.  
  58. char lastchar = 0;
  59.            
  60. int
  61. main(argc,argv)
  62. int argc;
  63. char **argv;
  64. {
  65.     int c, cp;
  66.     int i, imax;
  67.     char *s;
  68.     argc--; argv++;
  69.     if ( argc != 2 ) {
  70.         puts("Proper use is: 2A5onA4 infile outfile\n");
  71.         return(-1);
  72.     }
  73.     if ( ( fin = fopen(*argv,"r") ) == NULL ) {
  74.         puts("Cannot open file "); puts(*argv); puts("\n");
  75.         return(-1);
  76.     }
  77.     c = getc(fin);
  78.     cp = getc(fin);
  79.     if ( c != '%' || cp != '!' ) {
  80.         puts("Input isn't a postscript file\n");
  81.         fclose(fin);
  82.         return(-1);
  83.     }
  84.     if ( ( fout = fopen(argv[1],"w") ) == NULL ) {
  85.         puts("Cannot open file "); puts(argv[1]); puts("\n");
  86.         fclose(fin);
  87.         return(-1);
  88.     }
  89.     PutOne(c);
  90.     PutOne(cp);
  91.     while ( ( c = ReadOne() ) != EOF ) {
  92.         PutOne(c);
  93.         if ( c == '\n' && strncmp(from,"%%EndProlog",11) == 0 ) break;
  94.     }
  95.     if ( c == EOF ) {
  96.         puts("Irregular end to input file\n");
  97.         fclose(fin); fclose(fout); return(-1);
  98.     }
  99.     for ( i = 0; i < 11; i++ ) { c = ReadOne(); PutOne(c); }
  100. /*
  101.     Now we have to start pagehunting
  102. */
  103.     while ( ( c = ReadOne() ) != EOF ) {
  104.         PutOne(c);
  105.         if ( c == '\n' && strncmp(from,"%%Page:",7) == 0 ) {
  106.             while ( ( c = ReadOne() ) != EOF && c != '\n' ) {
  107.                 PutOne(c);
  108.             }
  109.             if ( c == EOF ) break;
  110.             pagenum++;
  111.             s = oddpage;
  112.             imax = strlen(s);
  113.             for ( i = 0; i < imax; i++ ) PutOne(*s++);
  114.  
  115.             while ( ( c = ReadOne() ) != EOF ) {
  116.                 if ( c == 'e' && strncmp(from,"op\n%%Page:",10) == 0 ) {
  117.                     for ( i = 0; i < 10; i++ ) ReadOne();
  118.                     while ( ( c = ReadOne() ) != EOF && c != '\n' ) {}
  119.                     if ( c == EOF ) {
  120.                         s = eop;
  121.                         imax = strlen(s);
  122.                         for ( i = 0; i < imax; i++ ) PutOne(*s++);
  123.                         break;
  124.                     }
  125.                     s = evenpage;
  126.                     imax = strlen(s);
  127.                     if ( lastchar == '\n' ) {
  128.                         s++;
  129.                         for ( i = 1; i < imax; i++ ) PutOne(*s++);
  130.                     }
  131.                     else {
  132.                         for ( i = 0; i < imax; i++ ) PutOne(*s++);
  133.                     }
  134.                     break;
  135.                 }
  136.                 else PutOne(c);
  137.             }
  138.         }
  139.     }
  140.     fclose(fin);
  141.     fclose(fout);
  142.     return(0);
  143. }
  144.  
  145. int
  146. ReadOne()
  147. {
  148.     char *to;
  149.     int num;
  150.     if ( from >= hi ) {
  151.         to = buffer + BUFSIZE;
  152.         while ( last > hi ) *--to = *--last;
  153.         from = to;
  154.         num = fread(buffer+BUFSIZE,1,BUFSIZE,fin);
  155.         if ( num < BUFSIZE ) {
  156.             if ( ferror(fin) ) {
  157.                 puts("Error while reading\n");
  158.                 fclose(fin);
  159.                 fclose(fout);
  160.                 exit(-1);
  161.             }
  162.             hi = last = buffer + BUFSIZE + num;
  163.             *hi = '\0';
  164.         }
  165.         else {
  166.             last = buffer + 2*BUFSIZE;
  167.             hi = last - 16;
  168.         }
  169.     }
  170.     if ( *from == '\0' ) return(EOF);
  171.     return(*from++);
  172. }
  173.  
  174. void
  175. PutOne(x)
  176. int x;
  177. {
  178.     if ( putc(x,fout) == EOF ) {
  179.         fclose(fin);
  180.         fclose(fout);
  181.         puts("Error while writing\n");
  182.         exit(-1);
  183.     }
  184.     lastchar = x;
  185. }
  186.