1 /* Last non-groff version: hdb.c 1.8 (Berkeley) 84/10/20
3 * Copyright -C- 1982 Barry S. Roitblat
5 * This file contains database routines for the hard copy programs of the
6 * gremlin picture editor.
22 #define MAXSTRING_S "127"
24 /* imports from main.cpp */
26 extern int linenum; /* current line number in input file */
27 extern char gremlinfile[]; /* name of file currently reading */
28 extern int SUNFILE; /* TRUE if SUN gremlin file */
29 extern int compatibility_flag; /* TRUE if in compatibility mode */
30 extern void savebounds(double x, double y);
32 /* imports from hpoint.cpp */
34 extern POINT *PTInit();
35 extern POINT *PTMakePoint(double x, double y, POINT ** pplist);
38 int DBGetType(register char *s);
42 * This routine returns a pointer to an initialized database element which
43 * would be the only element in an empty list.
48 return ((ELT *) NULL);
53 * This routine creates a new element with the specified attributes and
54 * links it into database.
66 temp = (ELT *) malloc(sizeof(ELT));
69 temp->ptlist = pointlist;
79 * This routine reads the specified file into a database and returns a
80 * pointer to that database.
83 DBRead(register FILE *file)
86 register int done; /* flag for input exhausted */
87 register double nx; /* x holder so x is not set before orienting */
88 int type; /* element type */
89 ELT *elist; /* pointer to the file's elements */
90 POINT *plist; /* pointer for reading in points */
91 char string[MAXSTRING], *txt;
92 double x, y; /* x and y are read in point coords */
98 (void) fscanf(file, "%" MAXSTRING_S "s%*[^\n]\n", string);
99 if (strcmp(string, "gremlinfile")) {
100 if (strcmp(string, "sungremlinfile")) {
101 error("'%1' is not a gremlin file", gremlinfile);
107 (void) fscanf(file, "%d%lf%lf\n", &size, &x, &y);
108 /* ignore orientation and file positioning point */
112 /* if (fscanf(file,"%" MAXSTRING_S "s\n", string) == EOF) */
113 /* I changed the scanf format because the element */
114 /* can have two words (e.g. CURVE SPLINE) */
115 if (fscanf(file, "\n%" MAXSTRING_S "[^\n]%*[^\n]\n", string) == EOF) {
116 error("'%1', error in file format", gremlinfile);
120 type = DBGetType(string); /* interpret element type */
121 if (type < 0) { /* no more data */
125 (void) xscanf(file, &x, &y); /* always one point */
127 (void) fscanf(file, "%lf%lf\n", &x, &y); /* always one point */
128 #endif /* UW_FASTSCAN */
129 plist = PTInit(); /* NULL point list */
132 * Files created on the SUN have point lists terminated by a line
133 * containing only an asterik ('*'). Files created on the AED have
134 * point lists terminated by the coordinate pair (-1.00 -1.00).
136 if (TEXT(type)) { /* read only first point for TEXT elements */
139 (void) PTMakePoint(nx, y, &plist);
143 while (xscanf(file, &x, &y));
147 fgets(string, MAXSTRING, file);
148 if (string[0] == '*') { /* SUN gremlin file */
151 (void) sscanf(string, "%lf%lf", &x, &y);
152 if ((x == -1.00 && y == -1.00) && (!SUNFILE))
155 if (compatibility_flag)
156 savebounds(xorn(x, y), yorn(x, y));
159 } while (!lastpoint);
160 #endif /* UW_FASTSCAN */
161 } else { /* not TEXT element */
166 (void) PTMakePoint(nx, y, &plist);
168 } while (xscanf(file, &x, &y));
174 (void) PTMakePoint(nx, y, &plist);
177 fgets(string, MAXSTRING, file);
178 if (string[0] == '*') { /* SUN gremlin file */
181 (void) sscanf(string, "%lf%lf", &x, &y);
182 if ((x == -1.00 && y == -1.00) && (!SUNFILE))
186 #endif /* UW_FASTSCAN */
188 (void) fscanf(file, "%d%d\n", &brush, &size);
189 (void) fscanf(file, "%d", &len); /* text length */
190 (void) getc(file); /* eat blank */
191 txt = (char *) malloc((unsigned) len + 1);
192 for (i = 0; i < len; ++i) { /* read text */
199 (void) DBCreateElt(type, plist, brush, size, txt, &elist);
201 } /* end while not done */ ;
207 * Interpret element type in string s.
208 * Old file format consisted of integer element types.
209 * New file format has literal names for element types.
212 DBGetType(register char *s)
214 if (isdigit(s[0]) || (s[0] == '-')) /* old element format or EOF */
233 "Warning: Bezier Curves will be printed as B-Splines\n");
247 fatal("unknown element type");
248 // fatal() does not return
259 fatal("unknown element type");
260 // fatal() does not return
271 fatal("unknown element type");
272 // fatal() does not return
275 fatal("unknown element type");
278 return 0; /* never reached */
283 * Optimization hack added by solomon@crys.wisc.edu, 12/2/86.
284 * A huge fraction of the time was spent reading floating point numbers from
285 * the input file, but the numbers always have the format 'ddd.dd'. Thus
286 * the following special-purpose version of fscanf.
288 * xscanf(f,xp,yp) does roughly what fscanf(f,"%f%f",xp,yp) does except:
289 * -the next piece of input must be of the form
290 * <space>* <digit>*'.'<digit>* <space>* <digit>*'.'<digit>*
291 * -xscanf eats the character following the second number
292 * -xscanf returns 0 for "end-of-data" indication, 1 otherwise, where
293 * end-of-data is signalled by a '*' [in which case the rest of the
294 * line is gobbled], or by '-1.00 -1.00' [but only if !SUNFILE].
301 register int c, i, j, m, frac;
302 int iscale = 1, jscale = 1; /* x = i/scale, y=j/jscale */
304 while ((c = getc(f)) == ' ');
306 while ((c = getc(f)) != '\n');
310 while (isdigit(c) || c == '.' || c == '-') {
321 i = 10 * i + c - '0';
327 *xp = (double) i / (double) iscale;
329 while ((c = getc(f)) == ' ');
331 while (isdigit(c) || c == '.' || c == '-') {
342 j = 10 * j + c - '0';
348 *yp = (double) j / (double) jscale;
349 return (SUNFILE || i != -iscale || j != -jscale);
351 #endif /* UW_FASTSCAN */