1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
/*
* ps.c -- Postscript scanning and copying routines.
* Copyright (C) 1992 Timothy O. Theisen
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Author: Tim Theisen Systems Programmer
* Internet: [email protected] Department of Computer Sciences
* UUCP: uwvax!tim University of Wisconsin-Madison
* Phone: (608)262-0438 1210 West Dayton Street
* FAX: (608)262-9777 Madison, WI 53706
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ps.h"
#ifndef SEEK_SET
#define SEEK_SET 0
#endif
#ifndef BUFSIZ
#define BUFSIZ 1024
#endif
/* length calculates string length at compile time */
/* can only be used with character constants */
#define length(a) (sizeof(a)-1)
#define iscomment(a, b) (strncmp(a, b, length(b)) == 0)
#define DSCcomment(a) (a[0] == '%' && a[1] == '%')
/*
* pscopy -- copy lines of Postscript from a section of one file
* to another file.
* Automatically switch to binary copying whenever
* %%BeginBinary/%%EndBinary or %%BeginData/%%EndData
* comments are encountered.
*/
void
pscopy(from, to, begin, end)
FILE *from;
FILE *to;
long begin; /* set negative to avoid initial seek */
long end;
{
char line[PSLINELENGTH]; /* 255 characters + 1 newline + 1 NULL */
char text[PSLINELENGTH]; /* Temporary storage for text */
unsigned int num;
unsigned int i;
char buf[BUFSIZ];
if (begin >= 0) fseek(from, begin, SEEK_SET);
while (ftell(from) < end) {
fgets(line, sizeof line, from);
fputs(line, to);
if (!(DSCcomment(line) && iscomment(line+2, "Begin"))) {
/* Do nothing */
} else if (iscomment(line+7, "Data:")) {
int rc = 0;
text[0] = '\0';
rc = sscanf(line+length("%%BeginData:"), "%d %*s %256s", &num,text);
text[256] = '\0';
if (rc >= 1) {
if (strcmp(text, "Lines") == 0) {
for (i=0; i < num; i++) {
fgets(line, sizeof line, from);
fputs(line, to);
}
} else {
while (num > BUFSIZ) {
fread(buf, sizeof (char), BUFSIZ, from);
fwrite(buf, sizeof (char), BUFSIZ, to);
num -= BUFSIZ;
}
fread(buf, sizeof (char), num, from);
fwrite(buf, sizeof (char), num, to);
}
}
} else if (iscomment(line+7, "Binary:")) {
if(sscanf(line+length("%%BeginBinary:"), "%d", &num) == 1) {
while (num > BUFSIZ) {
fread(buf, sizeof (char), BUFSIZ, from);
fwrite(buf, sizeof (char), BUFSIZ, to);
num -= BUFSIZ;
}
fread(buf, sizeof (char), num, from);
fwrite(buf, sizeof (char), num, to);
}
}
}
}
/*
* pscopyuntil -- copy lines of Postscript from a section of one file
* to another file until a particular comment is reached.
* Automatically switch to binary copying whenever
* %%BeginBinary/%%EndBinary or %%BeginData/%%EndData
* comments are encountered.
*/
char *
pscopyuntil(from, to, begin, end, comment)
FILE *from;
FILE *to;
long begin; /* set negative to avoid initial seek */
long end;
const char *comment;
{
char line[PSLINELENGTH]; /* 255 characters + 1 newline + 1 NULL */
char text[PSLINELENGTH]; /* Temporary storage for text */
unsigned int num;
unsigned int i;
int comment_length;
char buf[BUFSIZ];
char *cp;
comment_length = strlen(comment);
if (begin >= 0) fseek(from, begin, SEEK_SET);
while (ftell(from) < end) {
fgets(line, sizeof line, from);
/* iscomment cannot be used here,
* because comment_length is not known at compile time. */
if (strncmp(line, comment, comment_length) == 0) {
cp = (char *) malloc(strlen(line)+1);
if (cp == NULL) {
fprintf(stderr, "Fatal Error: Dynamic memory exhausted.\n");
exit(-1);
}
strcpy(cp, line);
return cp;
}
fputs(line, to);
if (!(DSCcomment(line) && iscomment(line+2, "Begin"))) {
/* Do nothing */
} else if (iscomment(line+7, "Data:")) {
int rc = 0;
text[0] = '\0';
rc = sscanf(line+length("%%BeginData:"), "%d %*s %256s", &num,text);
text[256] = '\0';
if (rc >= 1) {
if (strcmp(text, "Lines") == 0) {
for (i=0; i < num; i++) {
fgets(line, sizeof line, from);
fputs(line, to);
}
} else {
while (num > BUFSIZ) {
fread(buf, sizeof (char), BUFSIZ, from);
fwrite(buf, sizeof (char), BUFSIZ, to);
num -= BUFSIZ;
}
fread(buf, sizeof (char), num, from);
fwrite(buf, sizeof (char), num, to);
}
}
} else if (iscomment(line+7, "Binary:")) {
if(sscanf(line+length("%%BeginBinary:"), "%d", &num) == 1) {
while (num > BUFSIZ) {
fread(buf, sizeof (char), BUFSIZ, from);
fwrite(buf, sizeof (char), BUFSIZ, to);
num -= BUFSIZ;
}
fread(buf, sizeof (char), num, from);
fwrite(buf, sizeof (char), num, to);
}
}
}
return NULL;
}
|