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.
19 /* imports from main.cc */
21 extern int linenum
; /* current line number in input file */
22 extern char gremlinfile
[]; /* name of file currently reading */
23 extern int SUNFILE
; /* TRUE if SUN gremlin file */
24 extern void savebounds(float x
, float y
);
26 /* imports from hpoint.cc */
28 extern POINT
*PTInit();
29 extern POINT
*PTMakePoint(float x
, float y
, POINT
** pplist
);
32 int DBGetType(register char *s
);
36 * This routine returns a pointer to an initialized database element which
37 * would be the only element in an empty list.
42 return ((ELT
*) NULL
);
47 * This routine creates a new element with the specified attributes and
48 * links it into database.
60 temp
= (ELT
*) malloc(sizeof(ELT
));
63 temp
->ptlist
= pointlist
;
73 * This routine reads the specified file into a database and returns a
74 * pointer to that database.
77 DBRead(register FILE *file
)
80 register int done
; /* flag for input exhausted */
81 register float nx
; /* x holder so x is not set before orienting */
82 int type
; /* element type */
83 ELT
*elist
; /* pointer to the file's elements */
84 POINT
*plist
; /* pointer for reading in points */
85 char string
[MAXSTRING
], *txt
;
86 float x
, y
; /* x and y are read in point coords */
92 (void) fscanf(file
, "%s\n", string
);
93 if (strcmp(string
, "gremlinfile")) {
94 if (strcmp(string
, "sungremlinfile")) {
95 error("`%1' is not a gremlin file", gremlinfile
);
101 (void) fscanf(file
, "%d%f%f\n", &size
, &x
, &y
);
102 /* ignore orientation and file positioning point */
106 /* if (fscanf(file,"%s\n", string) == EOF) */
107 /* I changed the scanf format because the element */
108 /* can have two words (e.g. CURVE SPLINE) */
109 if (fscanf(file
, "\n%[^\n]\n", string
) == EOF
) {
110 error("`%1', error in file format", gremlinfile
);
114 type
= DBGetType(string
); /* interpret element type */
115 if (type
< 0) { /* no more data */
120 (void) xscanf(file
, &x
, &y
); /* always one point */
122 (void) fscanf(file
, "%f%f\n", &x
, &y
); /* always one point */
123 #endif /* UW_FASTSCAN */
124 plist
= PTInit(); /* NULL point list */
127 * Files created on the SUN have point lists terminated by a line
128 * containing only an asterik ('*'). Files created on the AED have
129 * point lists terminated by the coordinate pair (-1.00 -1.00).
131 if (TEXT(type
)) { /* read only first point for TEXT elements */
134 (void) PTMakePoint(nx
, y
, &plist
);
138 while (xscanf(file
, &x
, &y
));
142 fgets(string
, MAXSTRING
, file
);
143 if (string
[0] == '*') { /* SUN gremlin file */
146 (void) sscanf(string
, "%f%f", &x
, &y
);
147 if ((x
== -1.00 && y
== -1.00) && (!SUNFILE
))
150 } while (!lastpoint
);
151 #endif /* UW_FASTSCAN */
152 } else { /* not TEXT element */
157 (void) PTMakePoint(nx
, y
, &plist
);
159 } while (xscanf(file
, &x
, &y
));
165 (void) PTMakePoint(nx
, y
, &plist
);
168 fgets(string
, MAXSTRING
, file
);
169 if (string
[0] == '*') { /* SUN gremlin file */
172 (void) sscanf(string
, "%f%f", &x
, &y
);
173 if ((x
== -1.00 && y
== -1.00) && (!SUNFILE
))
177 #endif /* UW_FASTSCAN */
179 (void) fscanf(file
, "%d%d\n", &brush
, &size
);
180 (void) fscanf(file
, "%d", &len
); /* text length */
181 (void) getc(file
); /* eat blank */
182 txt
= (char *) malloc((unsigned) len
+ 1);
183 for (i
= 0; i
< len
; ++i
) { /* read text */
187 (void) DBCreateElt(type
, plist
, brush
, size
, txt
, &elist
);
189 } /* end while not done */ ;
195 * Interpret element type in string s.
196 * Old file format consisted of integer element types.
197 * New file format has literal names for element types.
200 DBGetType(register char *s
)
202 if (isdigit(s
[0]) || (s
[0] == '-')) /* old element format or EOF */
223 fatal("unknown element type");
234 fatal("unknown element type");
245 fatal("unknown element type");
248 fatal("unknown element type");
251 return 0; /* never reached */
256 * Optimization hack added by solomon@crys.wisc.edu, 12/2/86.
257 * A huge fraction of the time was spent reading floating point numbers from
258 * the input file, but the numbers always have the format 'ddd.dd'. Thus
259 * the following special-purpose version of fscanf.
261 * xscanf(f,xp,yp) does roughly what fscanf(f,"%f%f",xp,yp) does except:
262 * -the next piece of input must be of the form
263 * <space>* <digit>*'.'<digit>* <space>* <digit>*'.'<digit>*
264 * -xscanf eats the character following the second number
265 * -xscanf returns 0 for "end-of-data" indication, 1 otherwise, where
266 * end-of-data is signalled by a '*' [in which case the rest of the
267 * line is gobbled], or by '-1.00 -1.00' [but only if !SUNFILE].
274 register int c
, i
, j
, m
, frac
;
275 int iscale
= 1, jscale
= 1; /* x = i/scale, y=j/jscale */
277 while ((c
= getc(f
)) == ' ');
279 while ((c
= getc(f
)) != '\n');
283 while (isdigit(c
) || c
== '.' || c
== '-') {
294 i
= 10 * i
+ c
- '0';
300 *xp
= (double) i
/ (double) iscale
;
302 while ((c
= getc(f
)) == ' ');
304 while (isdigit(c
) || c
== '.' || c
== '-') {
315 j
= 10 * j
+ c
- '0';
321 *yp
= (double) j
/ (double) jscale
;
322 return (SUNFILE
|| i
!= -iscale
|| j
!= -jscale
);
324 #endif /* UW_FASTSCAN */