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.cc */
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 void savebounds(float x, float y);
27 /* imports from hpoint.cc */
29 extern POINT *PTInit();
30 extern POINT *PTMakePoint(float x, float y, POINT ** pplist);
33 int DBGetType(register char *s);
37 * This routine returns a pointer to an initialized database element which
38 * would be the only element in an empty list.
43 return ((ELT *) NULL);
48 * This routine creates a new element with the specified attributes and
49 * links it into database.
61 temp = (ELT *) malloc(sizeof(ELT));
64 temp->ptlist = pointlist;
74 * This routine reads the specified file into a database and returns a
75 * pointer to that database.
78 DBRead(register FILE *file)
81 register int done; /* flag for input exhausted */
82 register float nx; /* x holder so x is not set before orienting */
83 int type; /* element type */
84 ELT *elist; /* pointer to the file's elements */
85 POINT *plist; /* pointer for reading in points */
86 char string[MAXSTRING], *txt;
87 float x, y; /* x and y are read in point coords */
93 (void) fscanf(file, "%" MAXSTRING_S "s%*[^\n]\n", string);
94 if (strcmp(string, "gremlinfile")) {
95 if (strcmp(string, "sungremlinfile")) {
96 error("`%1' is not a gremlin file", gremlinfile);
102 (void) fscanf(file, "%d%f%f\n", &size, &x, &y);
103 /* ignore orientation and file positioning point */
107 /* if (fscanf(file,"%" MAXSTRING_S "s\n", string) == EOF) */
108 /* I changed the scanf format because the element */
109 /* can have two words (e.g. CURVE SPLINE) */
110 if (fscanf(file, "\n%" MAXSTRING_S "[^\n]%*[^\n]\n", string) == EOF) {
111 error("`%1', error in file format", gremlinfile);
115 type = DBGetType(string); /* interpret element type */
116 if (type < 0) { /* no more data */
121 (void) xscanf(file, &x, &y); /* always one point */
123 (void) fscanf(file, "%f%f\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, "%f%f", &x, &y);
148 if ((x == -1.00 && y == -1.00) && (!SUNFILE))
151 } while (!lastpoint);
152 #endif /* UW_FASTSCAN */
153 } else { /* not TEXT element */
158 (void) PTMakePoint(nx, y, &plist);
160 } while (xscanf(file, &x, &y));
166 (void) PTMakePoint(nx, y, &plist);
169 fgets(string, MAXSTRING, file);
170 if (string[0] == '*') { /* SUN gremlin file */
173 (void) sscanf(string, "%f%f", &x, &y);
174 if ((x == -1.00 && y == -1.00) && (!SUNFILE))
178 #endif /* UW_FASTSCAN */
180 (void) fscanf(file, "%d%d\n", &brush, &size);
181 (void) fscanf(file, "%d", &len); /* text length */
182 (void) getc(file); /* eat blank */
183 txt = (char *) malloc((unsigned) len + 1);
184 for (i = 0; i < len; ++i) { /* read text */
188 (void) DBCreateElt(type, plist, brush, size, txt, &elist);
190 } /* end while not done */ ;
196 * Interpret element type in string s.
197 * Old file format consisted of integer element types.
198 * New file format has literal names for element types.
201 DBGetType(register char *s)
203 if (isdigit(s[0]) || (s[0] == '-')) /* old element format or EOF */
222 "Warning: Bezier Curves will be printed as B-Splines\n");
236 fatal("unknown element type");
247 fatal("unknown element type");
258 fatal("unknown element type");
261 fatal("unknown element type");
264 return 0; /* never reached */
269 * Optimization hack added by solomon@crys.wisc.edu, 12/2/86.
270 * A huge fraction of the time was spent reading floating point numbers from
271 * the input file, but the numbers always have the format 'ddd.dd'. Thus
272 * the following special-purpose version of fscanf.
274 * xscanf(f,xp,yp) does roughly what fscanf(f,"%f%f",xp,yp) does except:
275 * -the next piece of input must be of the form
276 * <space>* <digit>*'.'<digit>* <space>* <digit>*'.'<digit>*
277 * -xscanf eats the character following the second number
278 * -xscanf returns 0 for "end-of-data" indication, 1 otherwise, where
279 * end-of-data is signalled by a '*' [in which case the rest of the
280 * line is gobbled], or by '-1.00 -1.00' [but only if !SUNFILE].
287 register int c, i, j, m, frac;
288 int iscale = 1, jscale = 1; /* x = i/scale, y=j/jscale */
290 while ((c = getc(f)) == ' ');
292 while ((c = getc(f)) != '\n');
296 while (isdigit(c) || c == '.' || c == '-') {
307 i = 10 * i + c - '0';
313 *xp = (double) i / (double) iscale;
315 while ((c = getc(f)) == ' ');
317 while (isdigit(c) || c == '.' || c == '-') {
328 j = 10 * j + c - '0';
334 *yp = (double) j / (double) jscale;
335 return (SUNFILE || i != -iscale || j != -jscale);
337 #endif /* UW_FASTSCAN */