3 /* Last non-groff version: hdb.c 1.8 (Berkeley) 84/10/20
5 * Copyright -C- 1982 Barry S. Roitblat
7 * This file contains database routines for the hard copy programs of the
8 * gremlin picture editor.
20 #define MAXSTRING_S "127"
22 /* imports from main.cpp */
24 extern int linenum
; /* current line number in input file */
25 extern char gremlinfile
[]; /* name of file currently reading */
26 extern int SUNFILE
; /* TRUE if SUN gremlin file */
27 extern int compatibility_flag
; /* TRUE if in compatibility mode */
28 extern void savebounds(double x
, double y
);
30 /* imports from hpoint.cpp */
32 extern POINT
*PTInit();
33 extern POINT
*PTMakePoint(double x
, double y
, POINT
** pplist
);
36 int DBGetType(register char *s
);
40 * This routine returns a pointer to an initialized database element which
41 * would be the only element in an empty list.
46 return ((ELT
*) NULL
);
51 * This routine creates a new element with the specified attributes and
52 * links it into database.
64 temp
= (ELT
*) malloc(sizeof(ELT
));
67 temp
->ptlist
= pointlist
;
77 * This routine reads the specified file into a database and returns a
78 * pointer to that database.
81 DBRead(register FILE *file
)
84 register int done
; /* flag for input exhausted */
85 register double nx
; /* x holder so x is not set before orienting */
86 int type
; /* element type */
87 ELT
*elist
; /* pointer to the file's elements */
88 POINT
*plist
; /* pointer for reading in points */
89 char string
[MAXSTRING
], *txt
;
90 double x
, y
; /* x and y are read in point coords */
96 (void) fscanf(file
, "%" MAXSTRING_S
"s%*[^\n]\n", string
);
97 if (strcmp(string
, "gremlinfile")) {
98 if (strcmp(string
, "sungremlinfile")) {
99 error("`%1' is not a gremlin file", gremlinfile
);
105 (void) fscanf(file
, "%d%lf%lf\n", &size
, &x
, &y
);
106 /* ignore orientation and file positioning point */
110 /* if (fscanf(file,"%" MAXSTRING_S "s\n", string) == EOF) */
111 /* I changed the scanf format because the element */
112 /* can have two words (e.g. CURVE SPLINE) */
113 if (fscanf(file
, "\n%" MAXSTRING_S
"[^\n]%*[^\n]\n", string
) == EOF
) {
114 error("`%1', error in file format", gremlinfile
);
118 type
= DBGetType(string
); /* interpret element type */
119 if (type
< 0) { /* no more data */
123 (void) xscanf(file
, &x
, &y
); /* always one point */
125 (void) fscanf(file
, "%lf%lf\n", &x
, &y
); /* always one point */
126 #endif /* UW_FASTSCAN */
127 plist
= PTInit(); /* NULL point list */
130 * Files created on the SUN have point lists terminated by a line
131 * containing only an asterik ('*'). Files created on the AED have
132 * point lists terminated by the coordinate pair (-1.00 -1.00).
134 if (TEXT(type
)) { /* read only first point for TEXT elements */
137 (void) PTMakePoint(nx
, y
, &plist
);
141 while (xscanf(file
, &x
, &y
));
145 fgets(string
, MAXSTRING
, file
);
146 if (string
[0] == '*') { /* SUN gremlin file */
149 (void) sscanf(string
, "%lf%lf", &x
, &y
);
150 if ((x
== -1.00 && y
== -1.00) && (!SUNFILE
))
153 if (compatibility_flag
)
154 savebounds(xorn(x
, y
), yorn(x
, y
));
157 } while (!lastpoint
);
158 #endif /* UW_FASTSCAN */
159 } else { /* not TEXT element */
164 (void) PTMakePoint(nx
, y
, &plist
);
166 } while (xscanf(file
, &x
, &y
));
172 (void) PTMakePoint(nx
, y
, &plist
);
175 fgets(string
, MAXSTRING
, file
);
176 if (string
[0] == '*') { /* SUN gremlin file */
179 (void) sscanf(string
, "%lf%lf", &x
, &y
);
180 if ((x
== -1.00 && y
== -1.00) && (!SUNFILE
))
184 #endif /* UW_FASTSCAN */
186 (void) fscanf(file
, "%d%d\n", &brush
, &size
);
187 (void) fscanf(file
, "%d", &len
); /* text length */
188 (void) getc(file
); /* eat blank */
189 txt
= (char *) malloc((unsigned) len
+ 1);
190 for (i
= 0; i
< len
; ++i
) { /* read text */
197 (void) DBCreateElt(type
, plist
, brush
, size
, txt
, &elist
);
199 } /* end while not done */ ;
205 * Interpret element type in string s.
206 * Old file format consisted of integer element types.
207 * New file format has literal names for element types.
210 DBGetType(register char *s
)
212 if (isdigit(s
[0]) || (s
[0] == '-')) /* old element format or EOF */
231 "Warning: Bezier Curves will be printed as B-Splines\n");
245 fatal("unknown element type");
256 fatal("unknown element type");
267 fatal("unknown element type");
270 fatal("unknown element type");
273 return 0; /* never reached */
278 * Optimization hack added by solomon@crys.wisc.edu, 12/2/86.
279 * A huge fraction of the time was spent reading floating point numbers from
280 * the input file, but the numbers always have the format 'ddd.dd'. Thus
281 * the following special-purpose version of fscanf.
283 * xscanf(f,xp,yp) does roughly what fscanf(f,"%f%f",xp,yp) does except:
284 * -the next piece of input must be of the form
285 * <space>* <digit>*'.'<digit>* <space>* <digit>*'.'<digit>*
286 * -xscanf eats the character following the second number
287 * -xscanf returns 0 for "end-of-data" indication, 1 otherwise, where
288 * end-of-data is signalled by a '*' [in which case the rest of the
289 * line is gobbled], or by '-1.00 -1.00' [but only if !SUNFILE].
296 register int c
, i
, j
, m
, frac
;
297 int iscale
= 1, jscale
= 1; /* x = i/scale, y=j/jscale */
299 while ((c
= getc(f
)) == ' ');
301 while ((c
= getc(f
)) != '\n');
305 while (isdigit(c
) || c
== '.' || c
== '-') {
316 i
= 10 * i
+ c
- '0';
322 *xp
= (double) i
/ (double) iscale
;
324 while ((c
= getc(f
)) == ' ');
326 while (isdigit(c
) || c
== '.' || c
== '-') {
337 j
= 10 * j
+ c
- '0';
343 *yp
= (double) j
/ (double) jscale
;
344 return (SUNFILE
|| i
!= -iscale
|| j
!= -jscale
);
346 #endif /* UW_FASTSCAN */