#include "gprint.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "errarg.h"
#include "error.h"
#define MAXSTRING 128
#define MAXSTRING_S "127"
extern int linenum;
extern char gremlinfile[];
extern int SUNFILE;
extern int compatibility_flag;
extern void savebounds(double x, double y);
extern POINT *PTInit();
extern POINT *PTMakePoint(double x, double y, POINT ** pplist);
int DBGetType(register char *s);
ELT *
DBInit()
{
return ((ELT *) NULL);
}
ELT *
DBCreateElt(int type,
POINT * pointlist,
int brush,
int size,
char *text,
ELT **db)
{
register ELT *temp;
temp = (ELT *) malloc(sizeof(ELT));
temp->nextelt = *db;
temp->type = type;
temp->ptlist = pointlist;
temp->brushf = brush;
temp->size = size;
temp->textpt = text;
*db = temp;
return (temp);
}
ELT *
DBRead(register FILE *file)
{
register int i;
register int done;
register double nx;
int type;
ELT *elist;
POINT *plist;
char string[MAXSTRING], *txt;
double x, y;
int len, brush, size;
int lastpoint;
SUNFILE = FALSE;
elist = DBInit();
(void) fscanf(file, "%" MAXSTRING_S "s%*[^\n]\n", string);
if (strcmp(string, "gremlinfile")) {
if (strcmp(string, "sungremlinfile")) {
error("`%1' is not a gremlin file", gremlinfile);
return (elist);
}
SUNFILE = TRUE;
}
(void) fscanf(file, "%d%lf%lf\n", &size, &x, &y);
done = FALSE;
while (!done) {
if (fscanf(file, "\n%" MAXSTRING_S "[^\n]%*[^\n]\n", string) == EOF) {
error("`%1', error in file format", gremlinfile);
return (elist);
}
type = DBGetType(string);
if (type < 0) {
done = TRUE;
} else {
#ifdef UW_FASTSCAN
(void) xscanf(file, &x, &y);
#else
(void) fscanf(file, "%lf%lf\n", &x, &y);
#endif
plist = PTInit();
if (TEXT(type)) {
nx = xorn(x, y);
y = yorn(x, y);
(void) PTMakePoint(nx, y, &plist);
savebounds(nx, y);
#ifdef UW_FASTSCAN
while (xscanf(file, &x, &y));
#else
lastpoint = FALSE;
do {
fgets(string, MAXSTRING, file);
if (string[0] == '*') {
lastpoint = TRUE;
} else {
(void) sscanf(string, "%lf%lf", &x, &y);
if ((x == -1.00 && y == -1.00) && (!SUNFILE))
lastpoint = TRUE;
else {
if (compatibility_flag)
savebounds(xorn(x, y), yorn(x, y));
}
}
} while (!lastpoint);
#endif
} else {
#ifdef UW_FASTSCAN
do {
nx = xorn(x, y);
y = yorn(x, y);
(void) PTMakePoint(nx, y, &plist);
savebounds(nx, y);
} while (xscanf(file, &x, &y));
#else
lastpoint = FALSE;
while (!lastpoint) {
nx = xorn(x, y);
y = yorn(x, y);
(void) PTMakePoint(nx, y, &plist);
savebounds(nx, y);
fgets(string, MAXSTRING, file);
if (string[0] == '*') {
lastpoint = TRUE;
} else {
(void) sscanf(string, "%lf%lf", &x, &y);
if ((x == -1.00 && y == -1.00) && (!SUNFILE))
lastpoint = TRUE;
}
}
#endif
}
(void) fscanf(file, "%d%d\n", &brush, &size);
(void) fscanf(file, "%d", &len);
(void) getc(file);
txt = (char *) malloc((unsigned) len + 1);
for (i = 0; i < len; ++i) {
int c = getc(file);
if (c == EOF)
break;
txt[i] = c;
}
txt[len] = '\0';
(void) DBCreateElt(type, plist, brush, size, txt, &elist);
}
} ;
return (elist);
}
int
DBGetType(register char *s)
{
if (isdigit(s[0]) || (s[0] == '-'))
return (atoi(s));
switch (s[0]) {
case 'P':
return (POLYGON);
case 'V':
return (VECTOR);
case 'A':
return (ARC);
case 'C':
if (s[1] == 'U') {
if (s[5] == '\n')
return (CURVE);
switch (s[7]) {
case 'S':
return(BSPLINE);
case 'E':
fprintf(stderr,
"Warning: Bezier Curves will be printed as B-Splines\n");
return(BSPLINE);
default:
return(CURVE);
}
}
switch (s[4]) {
case 'L':
return (CENTLEFT);
case 'C':
return (CENTCENT);
case 'R':
return (CENTRIGHT);
default:
fatal("unknown element type");
}
case 'B':
switch (s[3]) {
case 'L':
return (BOTLEFT);
case 'C':
return (BOTCENT);
case 'R':
return (BOTRIGHT);
default:
fatal("unknown element type");
}
case 'T':
switch (s[3]) {
case 'L':
return (TOPLEFT);
case 'C':
return (TOPCENT);
case 'R':
return (TOPRIGHT);
default:
fatal("unknown element type");
}
default:
fatal("unknown element type");
}
return 0;
}
#ifdef UW_FASTSCAN
int
xscanf(FILE *f,
double *xp,
double *yp)
{
register int c, i, j, m, frac;
int iscale = 1, jscale = 1;
while ((c = getc(f)) == ' ');
if (c == '*') {
while ((c = getc(f)) != '\n');
return 0;
}
i = m = frac = 0;
while (isdigit(c) || c == '.' || c == '-') {
if (c == '-') {
m++;
c = getc(f);
continue;
}
if (c == '.')
frac = 1;
else {
if (frac)
iscale *= 10;
i = 10 * i + c - '0';
}
c = getc(f);
}
if (m)
i = -i;
*xp = (double) i / (double) iscale;
while ((c = getc(f)) == ' ');
j = m = frac = 0;
while (isdigit(c) || c == '.' || c == '-') {
if (c == '-') {
m++;
c = getc(f);
continue;
}
if (c == '.')
frac = 1;
else {
if (frac)
jscale *= 10;
j = 10 * j + c - '0';
}
c = getc(f);
}
if (m)
j = -j;
*yp = (double) j / (double) jscale;
return (SUNFILE || i != -iscale || j != -jscale);
}
#endif