1 #ifndef lint
2 static char *sccsid = "idsort.c	(CWI)	1.1	85/03/01";
3 #endif
4 #include "idfilt.h"
5 
6 FILE *infile;
7 FILE *tempfile;
8 char *filename;
9 int lineno = 0;
10 
11 float maxx, maxy;
12 float minx, miny;
13 float width = 4.0;
14 float colwid = 6.0;
15 float xscale;
16 
17 boolean maxxset, maxyset;
18 boolean minxset, minyset;
19 boolean widset, colset;
20 boolean boundset;
21 
22 boolean veryfirst = TRUE;
23 
24 main (argc, argv)
25 int argc;
26 char *argv[];
27 {
28 	while (argc > 1 && argv[1][0] == '-') {
29 		fprintf (stderr, "ideal filter: unknown flag %c\n", argv[1][1]);
30 		argc--;
31 		argv++;
32 	}
33 	if (argc < 2) {
34 		infile = stdin;
35 		lineno = 0;
36 		interpret (infile);
37 	} else {
38 		while (argc-- > 1) {
39 			if (!(infile = fopen (*++argv, "r"))) {
40 				fprintf (stderr, "ideal sorter: can't open %s\n", *argv);
41 				exit (1);
42 			}
43 			filename = *argv;
44 			lineno = 0;
45 			interpret (infile);
46 			fclose (infile);
47 		}
48 	}
49 	exit (0);
50 }
51 
52 interpret (infile)
53 register FILE *infile;
54 {
55 	char buf[250];
56 
57 	int numitems;
58 	char cmd[10];
59 	int i[10];
60 	float f[30];
61 	char *string;
62 	boolean indots;
63 
64 
65 	indots = FALSE;
66 	while (fgets (buf, sizeof buf, infile)) {
67 		if (!indots) {
68 			fputs (buf, stdout);
69 			if (strncmp(buf,".IS",3) == 0) {
70 				indots = TRUE;
71 				maxxset = minxset = maxyset = minyset = FALSE;
72 				colset = boundset = widset = FALSE;
73 				boundset = FALSE;
74 				tempfile = fopen ("jUnKfOo", "w");
75 			}
76 		} else {
77 			if (strncmp(buf,"...line",7)) {
78 				if (!strncmp(buf,".IE",3) || !strncmp(buf,".IF",3)) {
79 					idendE ();
80 					indots = FALSE;
81 				}
82 				fputs (buf, stdout);
83 				if (!boundset) {
84 					if (strcmp (cmd, "...maxx") == 0) {
85 						sscanf (buf, "%s %f", cmd, &f[0]);
86 						idmaxx (f[0]);
87 					} else if (strcmp (cmd, "...maxy") == 0) {
88 						sscanf (buf, "%s %f", cmd, &f[0]);
89 						idmaxy (f[0]);
90 					} else if (strcmp (cmd, "...minx") == 0) {
91 						sscanf (buf, "%s %f", cmd, &f[0]);
92 						idminx (f[0]);
93 					} else if (strcmp (cmd, "...miny") == 0) {
94 						sscanf (buf, "%s %f", cmd, &f[0]);
95 						idminy (f[0]);
96 					} else if (strcmp (cmd, "...width") == 0) {
97 						sscanf (buf, "%s %f", cmd, &f[0]);
98 						idwidth (f[0]);
99 					} else if (strcmp (cmd, "...colwid") == 0) {
100 						sscanf (buf, "%s %f", cmd, &f[0]);
101 						idcolwid (f[0]);
102 					} else if (strcmp (cmd, "...obbox") == 0) {
103 						if (!veryfirst) {
104 							maxxset = maxyset = TRUE;
105 							minxset = minyset = TRUE;
106 							boundset = TRUE;
107 						}
108 					} else {
109 						idendbound ();
110 						veryfirst = FALSE;
111 					}
112 				}
113 			} else {
114 				if (!boundset)
115 					idendbound();
116 				sscanf (buf, "%s %f %f %f %f", cmd, &f[0], &f[1], &f[2], &f[3]);
117 				idline (f[0], f[1], f[2], f[3]);
118 			}
119 		}
120 	}
121 }
122 
123 void idmaxx (x)
124 float x;
125 {
126 	if (!maxxset) {
127 		maxx = x;
128 		maxxset = TRUE;
129 	}
130 }
131 
132 void idmaxy (y)
133 float y;
134 {
135 	if (!maxyset) {
136 		maxy = y;
137 		maxyset = TRUE;
138 	}
139 }
140 
141 void idminx (x)
142 float x;
143 {
144 	if (!minxset) {
145 		minx = x;
146 		minxset = TRUE;
147 	}
148 }
149 
150 void idminy (y)
151 float y;
152 {
153 	if (!minyset) {
154 		miny = y;
155 		minyset = TRUE;
156 	}
157 }
158 
159 void idwidth (wid)
160 float wid;
161 {
162 	if (!widset) {
163 		width = wid;
164 		widset = TRUE;
165 	}
166 }
167 
168 void idcolwid (wid)
169 float wid;
170 {
171 	if (!colset) {
172 		colwid = wid;
173 		colset = TRUE;
174 	}
175 }
176 
177 void idendbound ()
178 {
179 	if (boundset)
180 		return;
181 	idminx (-6.0);
182 	idmaxy (6.0);
183 	idmaxx (6.0);
184 	idminy (-6.0);
185 	if (maxx - minx < 0.2) {
186 		maxx += 1;
187 		minx -= 1;
188 	}
189 	if (maxy - miny < 0.2) {
190 		maxy += 1;
191 		miny -= 1;
192 	}
193 	xscale = width*972.0/(maxx - minx);
194 	boundset = TRUE;
195 }
196 
197 void idendE ()
198 {
199 	char c;
200 	fclose (tempfile);
201 	system ("sort +2 -r -o jUnKfOo jUnKfOo");
202 	tempfile = fopen ("jUnKfOo", "r");
203 	while ((c = getc(tempfile)) != EOF)
204 		putchar(c);
205 	fclose (tempfile);
206 	system ("rm jUnKfOo");
207 }
208 
209 
210 void idline (x1, y1, x2, y2)
211 float x1, y1, x2, y2;
212 {
213 	double t;
214 	int numsegs, i;
215 	if (y1 < y2 ) {
216 		t = x1;
217 		x1 = x2;
218 		x2 = t;
219 		t = y1;
220 		y1 = y2;
221 		y2 = t;
222 	}
223 	numsegs = xscale*abs(y2-y1)/250;
224 	if (numsegs <= 0) numsegs = 1;
225 	for (i = 0; i < numsegs; i ++)
226 		fprintf (tempfile, "...line %f %f %f %f\n",
227 			x1 + i*(x2-x1)/numsegs,
228 			y1 + i*(y2-y1)/numsegs,
229 			x1 + (i+1)*(x2-x1)/numsegs,
230 			y1 + (i+1)*(y2-y1)/numsegs
231 		);
232 }
233