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.
18 #define MAXSTRING_S "127"
20 /* imports from main.cpp */
22 extern int linenum; /* current line number in input file */
23 extern char gremlinfile[]; /* name of file currently reading */
24 extern int SUNFILE; /* TRUE if SUN gremlin file */
25 extern int compatibility_flag; /* TRUE if in compatibility mode */
26 extern void savebounds(double x, double y);
28 /* imports from hpoint.cpp */
30 extern POINT *PTInit();
31 extern POINT *PTMakePoint(double x, double y, POINT ** pplist);
34 int DBGetType(register char *s);
38 * This routine returns a pointer to an initialized database element which
39 * would be the only element in an empty list.
44 return ((ELT *) NULL);
49 * This routine creates a new element with the specified attributes and
50 * links it into database.
62 temp = (ELT *) malloc(sizeof(ELT));
65 temp->ptlist = pointlist;
75 * This routine reads the specified file into a database and returns a
76 * pointer to that database.
79 DBRead(register FILE *file)
82 register int done; /* flag for input exhausted */
83 register double nx; /* x holder so x is not set before orienting */
84 int type; /* element type */
85 ELT *elist; /* pointer to the file's elements */
86 POINT *plist; /* pointer for reading in points */
87 char string[MAXSTRING], *txt;
88 double x, y; /* x and y are read in point coords */
94 (void) fscanf(file, "%" MAXSTRING_S "s%*[^\n]\n", string);
95 if (strcmp(string, "gremlinfile")) {
96 if (strcmp(string, "sungremlinfile")) {
97 error("`%1' is not a gremlin file", gremlinfile);
103 (void) fscanf(file, "%d%lf%lf\n", &size, &x, &y);
104 /* ignore orientation and file positioning point */
108 /* if (fscanf(file,"%" MAXSTRING_S "s\n", string) == EOF) */
109 /* I changed the scanf format because the element */
110 /* can have two words (e.g. CURVE SPLINE) */
111 if (fscanf(file, "\n%" MAXSTRING_S "[^\n]%*[^\n]\n", string) == EOF) {
112 error("`%1', error in file format", gremlinfile);
116 type = DBGetType(string); /* interpret element type */
117 if (type < 0) { /* no more data */
121 (void) xscanf(file, &x, &y); /* always one point */
123 (void) fscanf(file, "%lf%lf\n", &x, &y); /* always one point */
124 #endif /* UW_FASTSCAN */
125 plist = PTInit(); /* NULL point list */
128 * Files created on the SUN have point lists terminated by a line
129 * containing only an asterik ('*'). Files created on the AED have
130 * point lists terminated by the coordinate pair (-1.00 -1.00).
132 if (TEXT(type)) { /* read only first point for TEXT elements */
135 (void) PTMakePoint(nx, y, &plist);
139 while (xscanf(file, &x, &y));
143 fgets(string, MAXSTRING, file);
144 if (string[0] == '*') { /* SUN gremlin file */
147 (void) sscanf(string, "%lf%lf", &x, &y);
148 if ((x == -1.00 && y == -1.00) && (!SUNFILE))
151 if (compatibility_flag)
152 savebounds(xorn(x, y), yorn(x, y));
155 } while (!lastpoint);
156 #endif /* UW_FASTSCAN */
157 } else { /* not TEXT element */
162 (void) PTMakePoint(nx, y, &plist);
164 } while (xscanf(file, &x, &y));
170 (void) PTMakePoint(nx, y, &plist);
173 fgets(string, MAXSTRING, file);
174 if (string[0] == '*') { /* SUN gremlin file */
177 (void) sscanf(string, "%lf%lf", &x, &y);
178 if ((x == -1.00 && y == -1.00) && (!SUNFILE))
182 #endif /* UW_FASTSCAN */
184 (void) fscanf(file, "%d%d\n", &brush, &size);
185 (void) fscanf(file, "%d", &len); /* text length */
186 (void) getc(file); /* eat blank */
187 txt = (char *) malloc((unsigned) len + 1);
188 for (i = 0; i < len; ++i) { /* read text */
195 (void) DBCreateElt(type, plist, brush, size, txt, &elist);
197 } /* end while not done */ ;
203 * Interpret element type in string s.
204 * Old file format consisted of integer element types.
205 * New file format has literal names for element types.
208 DBGetType(register char *s)
210 if (isdigit(s[0]) || (s[0] == '-')) /* old element format or EOF */
229 "Warning: Bezier Curves will be printed as B-Splines\n");
243 fatal("unknown element type");
254 fatal("unknown element type");
265 fatal("unknown element type");
268 fatal("unknown element type");
271 return 0; /* never reached */
276 * Optimization hack added by solomon@crys.wisc.edu, 12/2/86.
277 * A huge fraction of the time was spent reading floating point numbers from
278 * the input file, but the numbers always have the format 'ddd.dd'. Thus
279 * the following special-purpose version of fscanf.
281 * xscanf(f,xp,yp) does roughly what fscanf(f,"%f%f",xp,yp) does except:
282 * -the next piece of input must be of the form
283 * <space>* <digit>*'.'<digit>* <space>* <digit>*'.'<digit>*
284 * -xscanf eats the character following the second number
285 * -xscanf returns 0 for "end-of-data" indication, 1 otherwise, where
286 * end-of-data is signalled by a '*' [in which case the rest of the
287 * line is gobbled], or by '-1.00 -1.00' [but only if !SUNFILE].
294 register int c, i, j, m, frac;
295 int iscale = 1, jscale = 1; /* x = i/scale, y=j/jscale */
297 while ((c = getc(f)) == ' ');
299 while ((c = getc(f)) != '\n');
303 while (isdigit(c) || c == '.' || c == '-') {
314 i = 10 * i + c - '0';
320 *xp = (double) i / (double) iscale;
322 while ((c = getc(f)) == ' ');
324 while (isdigit(c) || c == '.' || c == '-') {
335 j = 10 * j + c - '0';
341 *yp = (double) j / (double) jscale;
342 return (SUNFILE || i != -iscale || j != -jscale);
344 #endif /* UW_FASTSCAN */