summaryrefslogtreecommitdiffstats
path: root/src/preproc/grn/hdb.cpp
blob: 9ba3eaa80a1a9a346d53929b7119b46f33bbe769 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
 /* Last non-groff version: hdb.c  1.8 (Berkeley) 84/10/20
 *
 * Copyright -C- 1982 Barry S. Roitblat
 *
 * This file contains database routines for the hard copy programs of
 * the gremlin picture editor.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>
#include "gprint.h"
#include <string.h>
#include <ctype.h>

#include "errarg.h"
#include "error.h"

#define MAXSTRING 128
#define MAXSTRING_S "127"

/* imports from main.cpp */

extern char gremlinfile[];	/* name of file currently reading */
extern int SUNFILE;		/* TRUE if SUN gremlin file */
extern int compatibility_flag;	/* TRUE if in compatibility mode */
extern void *grnmalloc(size_t size, const char *what);
extern void savebounds(double x, double y);

/* imports from hpoint.cpp */

extern POINT *PTInit();
extern POINT *PTMakePoint(double x, double y, POINT ** pplist);


int DBGetType(char *s);

static long lineno = 0;		/* line number of gremlin file */

/*
 * This routine returns a pointer to an initialized database element
 * which would be the only element in an empty list.
 */
ELT *
DBInit()
{
  return ((ELT *) NULL);
}				/* end DBInit */


/*
 * This routine creates a new element with the specified attributes and
 * links it into database.
 */
ELT *
DBCreateElt(int type,
	    POINT * pointlist,
	    int brush,
	    int size,
	    char *text,
	    ELT **db)
{
  ELT *temp = 0;

  temp = (ELT *) grnmalloc(sizeof(ELT), "picture element");
  temp->nextelt = *db;
  temp->type = type;
  temp->ptlist = pointlist;
  temp->brushf = brush;
  temp->size = size;
  temp->textpt = text;
  *db = temp;
  return (temp);
}				/* end CreateElt */


/*
 * This routine reads the specified file into a database and returns a
 * pointer to that database.
 */
ELT *
DBRead(FILE *file)
{
  int i;
  int done;		/* flag for input exhausted */
  double nx;		/* x holder so x is not set before orienting */
  int type;			/* element type */
  ELT *elist;			/* pointer to the file's elements */
  POINT *plist;			/* pointer for reading in points */
  char string[MAXSTRING], *txt;
  double x, y;			/* x and y are read in point coords */
  int len, brush, size;
  int lastpoint;

  SUNFILE = FALSE;
  elist = DBInit();
  int nitems = fscanf(file, "%" MAXSTRING_S "s%*[^\n]\n", string);
  if (nitems != 1) {
    error_with_file_and_line(gremlinfile, lineno,
			     "malformed input; giving up on this"
			     " picture");
    return (elist);
  }
  lineno++;
  if (strcmp(string, "gremlinfile")) {
    if (strcmp(string, "sungremlinfile")) {
      error_with_file_and_line(gremlinfile, lineno,
			       "not a gremlin file; giving up on this"
			       " picture");
      return (elist);
    }
    SUNFILE = TRUE;
  }

  nitems = fscanf(file, "%d%lf%lf\n", &size, &x, &y);
  if (nitems != 3) {
    error_with_file_and_line(gremlinfile, lineno,
			     "malformed input; giving up on this"
			     " picture");
    return (elist);
  }
  lineno++;
  /* ignore orientation and file positioning point */

  done = FALSE;
  while (!done) {
    /* if (fscanf(file,"%" MAXSTRING_S "s\n", string) == EOF) */
    /* I changed the scanf format because the element */
    /* can have two words (e.g. CURVE SPLINE)         */
    if (fscanf(file, "\n%"
		      MAXSTRING_S
		     "[^\n]%*[^\n]\n", string) == EOF) {
      lineno++;
      error_with_file_and_line(gremlinfile, lineno, "error in format;"
			       " giving up on this picture");
      return (elist);
    }
    lineno++;

    type = DBGetType(string);	/* interpret element type */
    if (type < 0) {		/* no more data */
      done = TRUE;
    } else {
      /* always one point */
#ifdef UW_FASTSCAN
      (void) xscanf(file, &x, &y);
#else
      nitems = fscanf(file, "%lf%lf\n", &x, &y);
      if (nitems != 2) {
	error_with_file_and_line(gremlinfile, lineno,
				 "malformed input; giving up on this"
				 " picture");
	return (elist);
      }
      lineno++;
#endif	/* UW_FASTSCAN */
      plist = PTInit();		/* NULL point list */

      /*
       * Files created on the SUN have point lists terminated by a line
       * containing only an asterisk ('*').  Files created on the AED
       * have point lists terminated by the coordinate pair (-1.00
       * -1.00).
       */
      if (TEXT(type)) {	/* read only first point for TEXT elements */
	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 {
	  char *cp = fgets(string, MAXSTRING, file);
	  if (0 /* nullptr */ == cp) {
	    error_with_file_and_line(gremlinfile, lineno,
				     "premature end-of-file or error"
				     " reading input; giving up on this"
				     " picture");
	    return(elist);
	  }
	  lineno++;
	  if (string[0] == '*') {	/* SUN gremlin file */
	    lastpoint = TRUE;
	  } else {
	    if (!sscanf(string, "%lf%lf", &x, &y)) {
	      error_with_file_and_line(gremlinfile, lineno,
				       "expected coordinate pair, got"
				       " '%1'; giving up on this"
				       " picture", string);
	      return(elist);
	    }
	    if ((x == -1.00 && y == -1.00) && (!SUNFILE))
	      lastpoint = TRUE;
	    else {
	      if (compatibility_flag)
		savebounds(xorn(x, y), yorn(x, y));
	    }
	  }
	} while (!lastpoint);
#endif	/* UW_FASTSCAN */
      } else {			/* not TEXT element */
#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);

	  char *cp = fgets(string, MAXSTRING, file);
	  if (0 /* nullptr */ == cp) {
	    error_with_file_and_line(gremlinfile, lineno,
				     "premature end-of-file or error"
				     " reading input; giving up on this"
				     " picture");
	    return(elist);
	  }
	  lineno++;
	  if (string[0] == '*') {	/* SUN gremlin file */
	    lastpoint = TRUE;
	  } else {
	    (void) sscanf(string, "%lf%lf", &x, &y);
	    if ((x == -1.00 && y == -1.00) && (!SUNFILE))
	      lastpoint = TRUE;
	  }
	}
#endif	/* UW_FASTSCAN */
      }
      nitems = fscanf(file, "%d%d\n", &brush, &size);
      if (nitems != 2) {
	error_with_file_and_line(gremlinfile, lineno,
				 "malformed input; giving up on this"
				 " picture");
	return (elist);
      }
      lineno++;
      nitems = fscanf(file, "%d", &len);	/* text length */
      if (nitems != 1) {
	error_with_file_and_line(gremlinfile, lineno,
				 "malformed input; giving up on this"
				 " picture");
	return (elist);
      }
      (void) getc(file);		/* eat blank */
      lineno++;				/* advance line counter early */
      if (len < 0) {
	error_with_file_and_line(gremlinfile, lineno,
				 "length claimed for text is nonsense:"
				 " '%1'; giving up on this picture",
				 len);
	return (elist);
      }
      txt = (char *) grnmalloc((unsigned) len + 1, "element text");
      for (i = 0; i < len; ++i) {	/* read text */
        int c = getc(file);
        if (c == EOF)
          break;
	txt[i] = c;
      }
      if (feof(file)) {
	error_with_file_and_line(gremlinfile, lineno,
				 "end of file while reading text of"
				 " length %1; giving up on this"
				 " picture", len);
	return (elist);
      }
      txt[len] = '\0';
      (void) DBCreateElt(type, plist, brush, size, txt, &elist);
    }				/* end else */
  } /* end while not done */ ;
  return (elist);
}				/* end DBRead */


/*
 * Interpret element type in string s.
 * Old file format consisted of integer element types.
 * New file format has literal names for element types.
 */
int
DBGetType(char *s)
{
  if (isdigit(s[0]) || (s[0] == '-'))	/* old element format or EOF */
    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':
	warning_with_file_and_line(gremlinfile, lineno,
				   "using B-spline for Bezier curve");
	return(BSPLINE);
      default:
	return(CURVE);
      }
    }
    switch (s[4]) {
    case 'L':
      return (CENTLEFT);
    case 'C':
      return (CENTCENT);
    case 'R':
      return (CENTRIGHT);
    default:
      error_with_file_and_line(gremlinfile, lineno,
			       "unknown element type '%1'", s);
      return -1;
    }
  case 'B':
    switch (s[3]) {
    case 'L':
      return (BOTLEFT);
    case 'C':
      return (BOTCENT);
    case 'R':
      return (BOTRIGHT);
    default:
      error_with_file_and_line(gremlinfile, lineno,
			       "unknown element type '%1'", s);
      return -1;
    }
  case 'T':
    switch (s[3]) {
    case 'L':
      return (TOPLEFT);
    case 'C':
      return (TOPCENT);
    case 'R':
      return (TOPRIGHT);
    default:
      error_with_file_and_line(gremlinfile, lineno,
			       "unknown element type '%1'", s);
      return -1;
    }
  default:
    error_with_file_and_line(gremlinfile, lineno,
			     "unknown element type '%1'", s);
    return -1;
  }
}

#ifdef UW_FASTSCAN
/*
 * Optimization hack added by solomon@crys.wisc.edu, 12/2/86.
 * A huge fraction of the time was spent reading floating point numbers
 * from the input file, but the numbers always have the format 'ddd.dd'.
 * Thus the following special-purpose version of fscanf.
 *
 * xscanf(f,xp,yp) does roughly what fscanf(f,"%f%f",xp,yp) does except:
 *   -the next piece of input must be of the form
 *      <space>* <digit>*'.'<digit>* <space>* <digit>*'.'<digit>*
 *   -xscanf eats the character following the second number
 *   -xscanf returns 0 for "end-of-data" indication, 1 otherwise, where
 *    end-of-data is signalled by a '*' [in which case the rest of the
 *    line is gobbled], or by '-1.00 -1.00' [but only if !SUNFILE].
 */
int
xscanf(FILE *f,
       double *xp,
       double *yp)
{
  int c, i, j, m, frac;
  int iscale = 1, jscale = 1;	/* x = i/scale, y=j/jscale */

  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	/* UW_FASTSCAN */

// Local Variables:
// fill-column: 72
// mode: C++
// End:
// vim: set cindent noexpandtab shiftwidth=2 textwidth=72: