home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Education Sampler 1992 [NeXTSTEP]
/
Education_1992_Sampler.iso
/
Mathematics
/
form1.1
/
2A5onA4.c
next >
Wrap
C/C++ Source or Header
|
1992-08-27
|
4KB
|
186 lines
Below is the program 2A5onA4.c for the dvips by Rokicki
It is not the prettiest code (it was a quick hack) but it worked for me
(on a NeXT computer)
Good luck
/*
Program combines two A5 pages in a postscript file so that they
are written on a single A4 sheet of paper. It assumes that the
postscript file was generated with the statement
dvips filename -t landscape -o
The A5 pagesize should have been established in the TeX file.
The algorithm is rather simple:
1: look for the line with the statement
%%EndProlog
2: Set page counter on zero
3: For each %%Page at the beginning of a line, raise the
page counter by one.
4: If the page counter becomes odd, add after the next '\n'
the string
"-20 -30 translate\n"
5: After adding the above line, search for the string
"eop\n%%Page"
6: Once found, raise the page counter by one.
Take out the string and all characters to the first
'\n'. Leave the '\n' and add after it:
"clear SI restore 0 421 translate\n"
7: Any line that starts with "%%EOF" terminates the file.
The "%%EOF" should be copied.
The running syntax is:
2A5onA4 infile outfile
Program made by J.A.M.Vermaseren
*/
#include <stdio.h>
#include <string.h>
#define BUFSIZE 1024
FILE *fin, *fout;
char buffer[2*BUFSIZE];
char *from = buffer;
char *last = buffer;
char *hi = buffer;
int pagenum = 0;
char oddpage[] = "\n-20 -30 translate\n";
char eop[] = "eop\n";
char evenpage[] = "\nclear SI restore 0 421 translate\n";
int ReadOne(void);
void PutOne(int);
char lastchar = 0;
int
main(argc,argv)
int argc;
char **argv;
{
int c, cp;
int i, imax;
char *s;
argc--; argv++;
if ( argc != 2 ) {
puts("Proper use is: 2A5onA4 infile outfile\n");
return(-1);
}
if ( ( fin = fopen(*argv,"r") ) == NULL ) {
puts("Cannot open file "); puts(*argv); puts("\n");
return(-1);
}
c = getc(fin);
cp = getc(fin);
if ( c != '%' || cp != '!' ) {
puts("Input isn't a postscript file\n");
fclose(fin);
return(-1);
}
if ( ( fout = fopen(argv[1],"w") ) == NULL ) {
puts("Cannot open file "); puts(argv[1]); puts("\n");
fclose(fin);
return(-1);
}
PutOne(c);
PutOne(cp);
while ( ( c = ReadOne() ) != EOF ) {
PutOne(c);
if ( c == '\n' && strncmp(from,"%%EndProlog",11) == 0 ) break;
}
if ( c == EOF ) {
puts("Irregular end to input file\n");
fclose(fin); fclose(fout); return(-1);
}
for ( i = 0; i < 11; i++ ) { c = ReadOne(); PutOne(c); }
/*
Now we have to start pagehunting
*/
while ( ( c = ReadOne() ) != EOF ) {
PutOne(c);
if ( c == '\n' && strncmp(from,"%%Page:",7) == 0 ) {
while ( ( c = ReadOne() ) != EOF && c != '\n' ) {
PutOne(c);
}
if ( c == EOF ) break;
pagenum++;
s = oddpage;
imax = strlen(s);
for ( i = 0; i < imax; i++ ) PutOne(*s++);
while ( ( c = ReadOne() ) != EOF ) {
if ( c == 'e' && strncmp(from,"op\n%%Page:",10) == 0 ) {
for ( i = 0; i < 10; i++ ) ReadOne();
while ( ( c = ReadOne() ) != EOF && c != '\n' ) {}
if ( c == EOF ) {
s = eop;
imax = strlen(s);
for ( i = 0; i < imax; i++ ) PutOne(*s++);
break;
}
s = evenpage;
imax = strlen(s);
if ( lastchar == '\n' ) {
s++;
for ( i = 1; i < imax; i++ ) PutOne(*s++);
}
else {
for ( i = 0; i < imax; i++ ) PutOne(*s++);
}
break;
}
else PutOne(c);
}
}
}
fclose(fin);
fclose(fout);
return(0);
}
int
ReadOne()
{
char *to;
int num;
if ( from >= hi ) {
to = buffer + BUFSIZE;
while ( last > hi ) *--to = *--last;
from = to;
num = fread(buffer+BUFSIZE,1,BUFSIZE,fin);
if ( num < BUFSIZE ) {
if ( ferror(fin) ) {
puts("Error while reading\n");
fclose(fin);
fclose(fout);
exit(-1);
}
hi = last = buffer + BUFSIZE + num;
*hi = '\0';
}
else {
last = buffer + 2*BUFSIZE;
hi = last - 16;
}
}
if ( *from == '\0' ) return(EOF);
return(*from++);
}
void
PutOne(x)
int x;
{
if ( putc(x,fout) == EOF ) {
fclose(fin);
fclose(fout);
puts("Error while writing\n");
exit(-1);
}
lastchar = x;
}