1 /*	hdb.c	1.6	(Berkeley) 84/04/07
2  *
3  * Copyright -C- 1982 Barry S. Roitblat
4  *
5  *      This file contains database routines for the hard copy programs of the
6  * gremlin picture editor.
7  */
8 
9 #include "gprint.h"
10 
11 /* imports from main.c */
12 extern int linenum;		/* current line number in input file */
13 extern char gremlinfile[];	/* name of file currently reading */
14 
15 /* imports from c */
16 
17 extern char *malloc();
18 extern char *strcpy();
19 extern char *sprintf();
20 
21 /* imports from point.c */
22 
23 extern POINT *PTInit();
24 extern POINT *PTMakePoint();
25 
26 ELT *DBInit()
27 /*
28  *      This routine returns a pointer to an initialized database element
29  * which would be the only element in an empty list.
30  */
31 
32 {
33     return(NULL);
34 }  /* end DBInit */
35 
36 ELT *DBCreateElt(type, pointlist, brush, size, text, db)
37 int type, brush, size;
38 POINT *pointlist;
39 char *text;
40 ELT *(*db) ;
41 /*
42  *      This routine creates a new element with the specified attributes and
43  * links it into database.
44  */
45 
46 {
47     register ELT *temp;
48 
49     temp = (ELT *) malloc(sizeof(ELT));
50     temp->nextelt = *db;
51     temp->type = type;
52     temp->ptlist = pointlist;
53     temp->brushf = brush;
54     temp->size = size;
55     temp->textpt = text;
56     *db = temp;
57     return(temp);
58 } /* end CreateElt */
59 
60 ELT *DBRead(file)
61 register FILE *file;
62 /*
63  *      This routine reads the specified file into a database and
64  * returns a pointer to that database.
65  */
66 
67 {
68     register int i;
69     register int done;		/* flag for input exhausted */
70     register int type;		/* element type */
71     register float nx;		/* x holder so x is not set before orienting */
72     ELT *elist;			/* pointer to the file's elements */
73     POINT *plist;		/* pointer for reading in points */
74     char  string[100], *txt;
75     float x, y;			/* x and y are read in point coords */
76     int len, brush, size;
77 
78 
79     elist = DBInit();
80     (void) fscanf(file,"%s",string);
81     if (strcmp(string, "gremlinfile")) {
82         error("%s is not a gremlin file", gremlinfile);
83         return(elist);
84     }
85     (void) fscanf(file, "%d%f%f", &size, &x, &y);
86 			/* ignore orientation and file positioning point */
87     done = FALSE;
88     while (!done) {
89         if (fscanf(file,"%d", &size) == EOF) {
90             error("%s, error in file format", gremlinfile);
91             return(elist);
92         }
93         if ((type = size) < 0) {	/* no more data */
94             done = TRUE;
95             (void) fclose(file);
96         } else {
97             (void) fscanf(file, "%f%f", &x, &y);
98             plist = PTInit();		/* pointlist terminated by -1,-1 */
99 	    if (TEXT(type)) {		/* only one point for text elements */
100 		nx = xorn(x,y);
101                 (void) PTMakePoint(nx, y = yorn(x,y), &plist);
102 		savebounds(nx, y);
103                 do
104 		    (void) fscanf(file, "%f%f", &x, &y);
105 		while ((x >= 0.0) || (y >= 0.0));
106 	    } else {
107 		while ((x >= 0.0) || (y >= 0.0)) {
108 		    nx = xorn(x,y);
109 		    (void) PTMakePoint(nx, y = yorn(x,y), &plist);
110 		    savebounds(nx, y);
111 		    (void) fscanf(file, "%f%f", &x, &y);
112 		}
113 	    }
114             (void) fscanf(file, "%d%d", &brush, &size);
115             (void) fscanf(file, "%d", &len);
116             txt = malloc((unsigned) len + 1);
117             (void) getc(file);        /* throw away space character */
118             for (i=0; i<len; ++i) {
119                 txt[i] = getc(file);
120 	    }
121             txt[len] = '\0';
122             (void) DBCreateElt(type, plist, brush, size, txt, &elist);
123         }  /* end else */
124     }  /* end while not done */;
125     return(elist);
126 } /* end DBRead */
127 
128