In storage_traverse(), don't access cllnode directly after the hook.
[grace.git] / grconvert / readbin.c
blob49ca0c3bee0224ab612f78bd9ef73e2d0d3c5a92
1 #include <config.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 /*
7 * for XDR
8 */
9 #ifndef VMS
10 # include <rpc/rpc.h>
11 #else
12 # ifndef __ALPHA
13 # define _XOPEN_SOURCE_EXTENDED 1
14 # endif
15 # ifdef MULTINET
16 # include <types.h>
17 # define DONT_DECLARE_MALLOC
18 # include "multinet_root:[multinet.include.rpc]rpc.h"
19 # else
20 # include <ucx$rpcxdr.h>
21 # endif
22 #endif
24 #include "grconvert.h"
26 static XDR xdrs;
28 #define BADMAGIC 999999
29 #define BADVERSION 999998
32 * Function prototypes
34 int read_char(char *d, int n, FILE * fout);
35 int read_int(int *d, int n, FILE * fout);
36 int read_double(double *d, int n, FILE * fin);
38 int read_charstr(char *d, FILE * fout);
39 int read_float(float *d, int n, FILE * fout);
40 int read_short(short *d, int n, FILE * fout);
41 static int read_velocityp(velocityp * d, FILE * fin);
42 static int read_world(world * d, FILE * fin);
43 static int read_view(view * d, FILE * fin);
44 static int read_world_stack(world_stack * d, FILE * fin);
45 static int read_labels(labels * d, FILE * fin);
46 static int read_plotarr(plotarr * d, FILE * fin);
47 static int read_tickmarks(tickmarks * d, FILE * fin);
48 static int read_legend(legend * d, FILE * fin);
49 static int read_framep(framep * d, FILE * fin);
50 static int read_BoxPlot(BoxPlot * d, FILE * fin);
52 int replace_xdr_short( short *i );
56 * Read type boxtype
58 int read_boxtype(boxtype * d, FILE * fin)
60 int err = 0;
61 short magic, version, testmagic = 20, testversion = 0;
62 if ((err = read_short(&magic, 1, fin)))
63 return err;
64 if (magic != testmagic)
65 return BADMAGIC;
66 if ((err = read_short(&version, 1, fin)))
67 return err;
68 if (version != testversion)
69 return BADVERSION;
70 if ((err = read_int(&(d->active), 1, fin)))
71 return err;
72 if ((err = read_int(&(d->loctype), 1, fin)))
73 return err;
74 if ((err = read_int(&(d->gno), 1, fin)))
75 return err;
76 if ((err = read_double(&(d->x1), 1, fin)))
77 return err;
78 if ((err = read_double(&(d->y1), 1, fin)))
79 return err;
80 if ((err = read_double(&(d->x2), 1, fin)))
81 return err;
82 if ((err = read_double(&(d->y2), 1, fin)))
83 return err;
84 if ((err = read_int(&(d->lines), 1, fin)))
85 return err;
86 if ((err = read_int(&(d->linew), 1, fin)))
87 return err;
88 if ((err = read_int(&(d->color), 1, fin)))
89 return err;
90 if ((err = read_int(&(d->fill), 1, fin)))
91 return err;
92 if ((err = read_int(&(d->fillcolor), 1, fin)))
93 return err;
94 if ((err = read_int(&(d->fillpattern), 1, fin)))
95 return err;
96 return err;
100 * Read type ellipsetype
102 int read_ellipsetype(ellipsetype * d, FILE * fin)
104 int err = 0;
105 short magic, version, testmagic = 41, testversion = 0;
106 if ((err = read_short(&magic, 1, fin)))
107 return err;
108 if (magic != testmagic) {
109 /* try to recover so that older saves will be compatible */
110 replace_xdr_short( &magic );
111 return BADMAGIC;
113 if ((err = read_short(&version, 1, fin)))
114 return err;
115 if (version != testversion)
116 return BADVERSION;
117 if ((err = read_int(&(d->active), 1, fin)))
118 return err;
119 if ((err = read_int(&(d->loctype), 1, fin)))
120 return err;
121 if ((err = read_int(&(d->gno), 1, fin)))
122 return err;
123 if ((err = read_double(&(d->x1), 1, fin)))
124 return err;
125 if ((err = read_double(&(d->y1), 1, fin)))
126 return err;
127 if ((err = read_double(&(d->x2), 1, fin)))
128 return err;
129 if ((err = read_double(&(d->y2), 1, fin)))
130 return err;
131 if ((err = read_int(&(d->lines), 1, fin)))
132 return err;
133 if ((err = read_int(&(d->linew), 1, fin)))
134 return err;
135 if ((err = read_int(&(d->color), 1, fin)))
136 return err;
137 if ((err = read_int(&(d->fill), 1, fin)))
138 return err;
139 if ((err = read_int(&(d->fillcolor), 1, fin)))
140 return err;
141 if ((err = read_int(&(d->fillpattern), 1, fin)))
142 return err;
143 return err;
147 * Read type linetype
149 int read_linetype(linetype * d, FILE * fin)
151 int err = 0;
152 short magic, version, testmagic = 21, testversion = 0;
153 if ((err = read_short(&magic, 1, fin)))
154 return err;
155 if (magic != testmagic)
156 return BADMAGIC;
157 if ((err = read_short(&version, 1, fin)))
158 return err;
159 if (version != testversion)
160 return BADVERSION;
161 if ((err = read_int(&(d->active), 1, fin)))
162 return err;
163 if ((err = read_int(&(d->loctype), 1, fin)))
164 return err;
165 if ((err = read_int(&(d->gno), 1, fin)))
166 return err;
167 if ((err = read_double(&(d->x1), 1, fin)))
168 return err;
169 if ((err = read_double(&(d->y1), 1, fin)))
170 return err;
171 if ((err = read_double(&(d->x2), 1, fin)))
172 return err;
173 if ((err = read_double(&(d->y2), 1, fin)))
174 return err;
175 if ((err = read_int(&(d->lines), 1, fin)))
176 return err;
177 if ((err = read_int(&(d->linew), 1, fin)))
178 return err;
179 if ((err = read_int(&(d->color), 1, fin)))
180 return err;
181 if ((err = read_int(&(d->arrow), 1, fin)))
182 return err;
183 if ((err = read_int(&(d->atype), 1, fin)))
184 return err;
185 if ((err = read_double(&(d->asize), 1, fin)))
186 return err;
187 return err;
191 * Read type plotstr
193 int read_plotstr(plotstr * d, FILE * fin)
195 int i, err = 0;
196 short magic, version, testmagic = 22, testversion = 0;
197 if ((err = read_short(&magic, 1, fin)))
198 return 1;
199 if (magic != testmagic)
200 return BADMAGIC;
201 if ((err = read_short(&version, 1, fin)))
202 return 2;
203 if (version != testversion)
204 return BADVERSION;
205 if ((err = read_int(&(d->active), 1, fin)))
206 return 3;
207 if ((err = read_int(&(d->loctype), 1, fin)))
208 return 4;
209 if ((err = read_int(&(d->gno), 1, fin)))
210 return 5;
211 if ((err = read_double(&(d->x), 1, fin)))
212 return 6;
213 if ((err = read_double(&(d->y), 1, fin)))
214 return 7;
215 if ((err = read_int(&(d->lines), 1, fin)))
216 return 8;
217 if ((err = read_int(&(d->linew), 1, fin)))
218 return 9;
219 if ((err = read_int(&(d->color), 1, fin)))
220 return 10;
221 if ((err = read_int(&(d->rot), 1, fin)))
222 return 11;
223 if ((err = read_int(&(d->font), 1, fin)))
224 return 12;
225 if ((err = read_int(&(d->just), 1, fin)))
226 return 13;
227 if ((err = read_double(&(d->charsize), 1, fin)))
228 return 14;
229 if ((err = read_int(&i, 1, fin)))
230 return 15;
231 if (i <= 0) {
232 err = -1;
233 return 0;
236 if (d->s != NULL) {
237 free(d->s);
238 } else {
239 d->s = (char *) malloc(i * sizeof(char));
242 d->s = (char *) malloc(i * sizeof(char));
243 if ((err = read_char(d->s, i, fin)))
244 return 16;
245 return err;
249 * Read type velocityp
251 static int read_velocityp(velocityp * d, FILE * fin)
253 int err = 0;
254 short magic, version, testmagic = 25, testversion = 0;
255 if ((err = read_short(&magic, 1, fin)))
256 return err;
257 if (magic != testmagic)
258 return BADMAGIC;
259 if ((err = read_short(&version, 1, fin)))
260 return err;
261 if (version != testversion)
262 return BADVERSION;
263 if ((err = read_int(&(d->active), 1, fin)))
264 return err;
265 if ((err = read_int(&(d->type), 1, fin)))
266 return err;
267 if ((err = read_int(&(d->color), 1, fin)))
268 return err;
269 if ((err = read_int(&(d->lines), 1, fin)))
270 return err;
271 if ((err = read_int(&(d->linew), 1, fin)))
272 return err;
273 if ((err = read_int(&(d->arrowtype), 1, fin)))
274 return err;
275 if ((err = read_int(&(d->loctype), 1, fin)))
276 return err;
277 if ((err = read_double(&(d->velx), 1, fin)))
278 return err;
279 if ((err = read_double(&(d->vely), 1, fin)))
280 return err;
281 if ((err = read_double(&(d->vscale), 1, fin)))
282 return err;
283 if ((err = read_int(&(d->units), 1, fin)))
284 return err;
285 if ((err = read_double(&(d->userlength), 1, fin)))
286 return err;
287 if ((err = read_plotstr(&(d->vstr), fin)))
288 return err;
289 return err;
293 * Read type world
295 static int read_world(world * d, FILE * fin)
297 int err = 0;
298 short magic, version, testmagic = 26, testversion = 0;
299 if ((err = read_short(&magic, 1, fin)))
300 return err;
301 if (magic != testmagic)
302 return BADMAGIC;
303 if ((err = read_short(&version, 1, fin)))
304 return err;
305 if (version != testversion)
306 return BADVERSION;
307 if ((err = read_double(&(d->xg1), 1, fin)))
308 return err;
309 if ((err = read_double(&(d->xg2), 1, fin)))
310 return err;
311 if ((err = read_double(&(d->yg1), 1, fin)))
312 return err;
313 if ((err = read_double(&(d->yg2), 1, fin)))
314 return err;
315 return err;
319 * Read type view
321 static int read_view(view * d, FILE * fin)
323 int err = 0;
324 short magic, version, testmagic = 27, testversion = 0;
325 if ((err = read_short(&magic, 1, fin)))
326 return err;
327 if (magic != testmagic)
328 return BADMAGIC;
329 if ((err = read_short(&version, 1, fin)))
330 return err;
331 if (version != testversion)
332 return BADVERSION;
333 if ((err = read_double(&(d->xv1), 1, fin)))
334 return err;
335 if ((err = read_double(&(d->xv2), 1, fin)))
336 return err;
337 if ((err = read_double(&(d->yv1), 1, fin)))
338 return err;
339 if ((err = read_double(&(d->yv2), 1, fin)))
340 return err;
341 return err;
345 * Read type world_stack
347 static int read_world_stack(world_stack * d, FILE * fin)
349 int i, err = 0;
350 short magic, version, testmagic = 28, testversion = 0;
351 if ((err = read_short(&magic, 1, fin)))
352 return err;
353 if (magic != testmagic)
354 return BADMAGIC;
355 if ((err = read_short(&version, 1, fin)))
356 return err;
357 if (version != testversion)
358 return BADVERSION;
359 if ((err = read_world(&(d->w), fin)))
360 return err;
361 for (i = 0; i < 3; i++) {
362 if ((err = read_world(&(d->t[i]), fin)))
363 return err;
365 return err;
369 * Read type labels
371 static int read_labels(labels * d, FILE * fin)
373 int err = 0;
374 short magic, version, testmagic = 29, testversion = 0;
375 if ((err = read_short(&magic, 1, fin)))
376 return err;
377 if (magic != testmagic)
378 return BADMAGIC;
379 if ((err = read_short(&version, 1, fin)))
380 return err;
381 if (version != testversion)
382 return BADVERSION;
383 if ((err = read_plotstr(&(d->title), fin)))
384 return err;
385 if ((err = read_plotstr(&(d->stitle), fin)))
386 return err;
387 return err;
391 * Read type plotarr
393 static int read_plotarr(plotarr * d, FILE * fin)
395 char buf[512];
396 int i, cnt, ind, err = 0;
397 short magic, version, testmagic = 33, testversion = 0;
398 if ((err = read_short(&magic, 1, fin)))
399 return 1;
400 if (magic != testmagic)
401 return BADMAGIC;
402 if ((err = read_short(&version, 1, fin)))
403 return 2;
404 if (version != testversion)
405 return BADVERSION;
406 if ((err = read_int(&(d->active), 1, fin)))
407 return 3;
408 if ((err = read_int(&(d->type), 1, fin)))
409 return 4;
410 if ((err = read_int(&(d->deact), 1, fin)))
411 return 5;
412 if ((err = read_int(&(d->len), 1, fin)))
413 return 6;
414 if ((err = read_double(&(d->missing), 1, fin)))
415 return 7;
416 if ((err = read_int(&cnt, 1, fin)))
417 return 8;
418 for (i = 0; i < cnt; i++) {
419 if ((err = read_int(&ind, 1, fin)))
420 return 9;
421 d->ex[ind] = (double *) malloc(d->len * sizeof(double));
422 if ((err = read_double(d->ex[ind], d->len, fin)))
423 return 10;
425 if (d->type == XYSTRING) {
426 d->s = (char **) malloc(d->len * sizeof(char *));
427 if (d->s == NULL) {
428 return 101;
430 for (i=0;i<d->len;i++) {
431 if ((err = read_charstr(buf, fin)))
432 return 102;
433 d->s[i] = (char *) malloc((strlen(buf) + 1) * sizeof(char));
434 if (d->s[i] == NULL) {
435 return 101;
437 strcpy(d->s[i], buf);
440 if ((err = read_double(&(d->xmin), 1, fin)))
441 return 11;
442 if ((err = read_double(&(d->xmax), 1, fin)))
443 return 12;
444 if ((err = read_double(&(d->ymin), 1, fin)))
445 return 13;
446 if ((err = read_double(&(d->ymax), 1, fin)))
447 return 14;
448 if ((err = read_int(&(d->sym), 1, fin)))
449 return 15;
450 if ((err = read_char(&(d->symchar), 1, fin)))
451 return 16;
452 if ((err = read_int(&(d->symskip), 1, fin)))
453 return 17;
454 if ((err = read_int(&(d->symfill), 1, fin)))
455 return 18;
456 if ((err = read_int(&(d->symdot), 1, fin)))
457 return 19;
458 if ((err = read_int(&(d->symlines), 1, fin)))
459 return 20;
460 if ((err = read_int(&(d->symlinew), 1, fin)))
461 return 21;
462 if ((err = read_int(&(d->symcolor), 1, fin)))
463 return 22;
464 if ((err = read_double(&(d->symsize), 1, fin)))
465 return 23;
466 if ((err = read_int(&(d->avgflag), 1, fin)))
467 return 24;
468 if ((err = read_int(&(d->avgstdflag), 1, fin)))
469 return 25;
470 if ((err = read_int(&(d->avg2stdflag), 1, fin)))
471 return 26;
472 if ((err = read_int(&(d->avg3stdflag), 1, fin)))
473 return 27;
474 if ((err = read_int(&(d->avgallflag), 1, fin)))
475 return 28;
476 if ((err = read_int(&(d->avgvalflag), 1, fin)))
477 return 29;
478 if ((err = read_int(&(d->harmonicflag), 1, fin)))
479 return 30;
480 if ((err = read_int(&(d->geometricflag), 1, fin)))
481 return 31;
482 if ((err = read_int(&(d->font), 1, fin)))
483 return 32;
484 if ((err = read_int(&(d->format), 1, fin)))
485 return 33;
486 if ((err = read_int(&(d->prec), 1, fin)))
487 return 34;
488 if ((err = read_int(&(d->just), 1, fin)))
489 return 35;
490 if ((err = read_int(&(d->where), 1, fin)))
491 return 36;
492 if ((err = read_double(&(d->valsize), 1, fin)))
493 return 37;
494 if ((err = read_int(&(d->lines), 1, fin)))
495 return 38;
496 if ((err = read_int(&(d->linew), 1, fin)))
497 return 39;
498 if ((err = read_int(&(d->color), 1, fin)))
499 return 40;
500 if ((err = read_int(&(d->lineskip), 1, fin)))
501 return 41;
502 if ((err = read_int(&(d->fill), 1, fin)))
503 return 42;
504 if ((err = read_int(&(d->fillusing), 1, fin)))
505 return 43;
506 if ((err = read_int(&(d->fillcolor), 1, fin)))
507 return 44;
508 if ((err = read_int(&(d->fillpattern), 1, fin)))
509 return 45;
510 if ((err = read_int(&(d->errbar), 1, fin)))
511 return 46;
512 if ((err = read_int(&(d->errbarxy), 1, fin)))
513 return 47;
514 if ((err = read_int(&(d->errbar_linew), 1, fin)))
515 return 48;
516 if ((err = read_int(&(d->errbar_lines), 1, fin)))
517 return 49;
518 if ((err = read_int(&(d->errbar_riser), 1, fin)))
519 return 50;
520 if ((err = read_int(&(d->errbar_riser_linew), 1, fin)))
521 return 51;
522 if ((err = read_int(&(d->errbar_riser_lines), 1, fin)))
523 return 52;
524 if ((err = read_double(&(d->errbarper), 1, fin)))
525 return 53;
526 if ((err = read_double(&(d->hilowper), 1, fin)))
527 return 54;
528 if ((err = read_int(&(d->density_plot), 1, fin)))
529 return 55;
530 if ((err = read_double(&(d->zmin), 1, fin)))
531 return 56;
532 if ((err = read_double(&(d->zmax), 1, fin)))
533 return 57;
534 if ((err = read_charstr(d->comments, fin)))
535 return 58;
536 if ((err = read_charstr(d->lstr, fin)))
537 return 58;
538 if ((err = read_int(&(d->hotlink), 1, fin)))
539 return 59;
540 if ((err = read_int(&(d->hotsrc), 1, fin)))
541 return 60;
542 if ((err = read_charstr(d->hotfile, fin)))
543 return 61;
544 if ((err = read_double(d->emin, 6, fin)))
545 return 62;
546 if ((err = read_double(d->emax, 6, fin)))
547 return 63;
548 if ((err = read_int(d->imin, 6, fin)))
549 return 64;
550 if ((err = read_int(d->imax, 6, fin)))
551 return 65;
552 /* TODO for (i = 0; i < 0; i++) {
553 if ((err = read_Regression(d->r, fin))) return err;
555 TODO for (i = 0; i < 0; i++) {
556 if ((err = read_Spline(d->spl, fin))) return err;
557 } */
558 return err;
562 * Read type tickmarks
564 static int read_tickmarks(tickmarks * d, FILE * fin)
566 int i, cnt, err = 0;
567 short magic, version, testmagic = 34, testversion = 0;
568 if ((err = read_short(&magic, 1, fin)))
569 return err;
570 if (magic != testmagic)
571 return BADMAGIC;
572 if ((err = read_short(&version, 1, fin)))
573 return err;
574 if (version != testversion)
575 return BADVERSION;
576 if ((err = read_int(&(d->axis), 1, fin)))
577 return err;
578 if ((err = read_int(&(d->active), 1, fin)))
579 return err;
580 if ((err = read_int(&(d->alt), 1, fin)))
581 return err;
582 if ((err = read_double(&(d->tmin), 1, fin)))
583 return err;
584 if ((err = read_double(&(d->tmax), 1, fin)))
585 return err;
586 if ((err = read_double(&(d->tmajor), 1, fin)))
587 return err;
588 if ((err = read_double(&(d->tminor), 1, fin)))
589 return err;
590 if ((err = read_double(&(d->offsx), 1, fin)))
591 return err;
592 if ((err = read_double(&(d->offsy), 1, fin)))
593 return err;
594 if ((err = read_plotstr(&(d->label), fin)))
595 return err;
596 if ((err = read_int(&(d->label_layout), 1, fin)))
597 return err;
598 if ((err = read_int(&(d->label_place), 1, fin)))
599 return err;
600 if ((err = read_int(&(d->tl_flag), 1, fin)))
601 return err;
602 if ((err = read_int(&(d->tl_type), 1, fin)))
603 return err;
604 if ((err = read_int(&(d->tl_layout), 1, fin)))
605 return err;
606 if ((err = read_int(&(d->tl_angle), 1, fin)))
607 return err;
608 if ((err = read_int(&(d->tl_sign), 1, fin)))
609 return err;
610 if ((err = read_int(&(d->tl_just), 1, fin)))
611 return err;
612 if ((err = read_int(&(d->tl_prec), 1, fin)))
613 return err;
614 if ((err = read_int(&(d->tl_format), 1, fin)))
615 return err;
616 if ((err = read_int(&(d->tl_skip), 1, fin)))
617 return err;
618 if ((err = read_int(&(d->tl_staggered), 1, fin)))
619 return err;
620 if ((err = read_int(&(d->tl_starttype), 1, fin)))
621 return err;
622 if ((err = read_int(&(d->tl_stoptype), 1, fin)))
623 return err;
624 if ((err = read_double(&(d->tl_start), 1, fin)))
625 return err;
626 if ((err = read_double(&(d->tl_stop), 1, fin)))
627 return err;
628 if ((err = read_int(&(d->tl_op), 1, fin)))
629 return err;
630 if ((err = read_double(&(d->tl_vgap), 1, fin)))
631 return err;
632 if ((err = read_double(&(d->tl_hgap), 1, fin)))
633 return err;
634 if ((err = read_int(&(d->tl_font), 1, fin)))
635 return err;
636 if ((err = read_double(&(d->tl_charsize), 1, fin)))
637 return err;
638 if ((err = read_int(&(d->tl_color), 1, fin)))
639 return err;
640 if ((err = read_int(&(d->tl_linew), 1, fin)))
641 return err;
642 if ((err = read_charstr(d->tl_appstr, fin)))
643 return err;
644 if ((err = read_charstr(d->tl_prestr, fin)))
645 return err;
646 if ((err = read_int(&(d->t_type), 1, fin)))
647 return err;
648 if ((err = read_int(&(d->t_flag), 1, fin)))
649 return err;
650 if ((err = read_int(&(d->t_mflag), 1, fin)))
651 return err;
652 if ((err = read_int(&(d->t_integer), 1, fin)))
653 return err;
654 if ((err = read_int(&(d->t_num), 1, fin)))
655 return err;
656 if ((err = read_int(&(d->t_inout), 1, fin)))
657 return err;
658 if ((err = read_int(&(d->t_log), 1, fin)))
659 return err;
660 if ((err = read_int(&(d->t_op), 1, fin)))
661 return err;
662 if ((err = read_int(&(d->t_color), 1, fin)))
663 return err;
664 if ((err = read_int(&(d->t_lines), 1, fin)))
665 return err;
666 if ((err = read_int(&(d->t_linew), 1, fin)))
667 return err;
668 if ((err = read_int(&(d->t_mcolor), 1, fin)))
669 return err;
670 if ((err = read_int(&(d->t_mlines), 1, fin)))
671 return err;
672 if ((err = read_int(&(d->t_mlinew), 1, fin)))
673 return err;
674 if ((err = read_double(&(d->t_size), 1, fin)))
675 return err;
676 if ((err = read_double(&(d->t_msize), 1, fin)))
677 return err;
678 if ((err = read_int(&(d->t_drawbar), 1, fin)))
679 return err;
680 if ((err = read_int(&(d->t_drawbarcolor), 1, fin)))
681 return err;
682 if ((err = read_int(&(d->t_drawbarlines), 1, fin)))
683 return err;
684 if ((err = read_int(&(d->t_drawbarlinew), 1, fin)))
685 return err;
686 if ((err = read_int(&(d->t_gridflag), 1, fin)))
687 return err;
688 if ((err = read_int(&(d->t_mgridflag), 1, fin)))
689 return err;
690 if ((err = read_int(&(d->t_spec), 1, fin)))
691 return err;
692 if ((err = read_int(&cnt, 1, fin)))
693 return err;
694 if ((err = read_double(d->t_specloc, cnt, fin)))
695 return err;
696 for (i = 0; i < cnt; i++) {
697 if ((err = read_plotstr(&(d->t_speclab[i]), fin)))
698 return err;
700 if ((err = read_int(&(d->spec_font), 1, fin)))
701 return err;
702 if ((err = read_double(&(d->spec_charsize), 1, fin)))
703 return err;
704 if ((err = read_int(&(d->spec_color), 1, fin)))
705 return err;
706 if ((err = read_int(&(d->spec_linew), 1, fin)))
707 return err;
708 return err;
712 * Read type legend
714 static int read_legend(legend * d, FILE * fin)
716 int err = 0;
717 short magic, version, testmagic = 36, testversion = 0;
718 if ((err = read_short(&magic, 1, fin)))
719 return 1;
720 if (magic != testmagic)
721 return BADMAGIC;
722 if ((err = read_short(&version, 1, fin)))
723 return 2;
724 if (version != testversion)
725 return BADVERSION;
726 if ((err = read_int(&(d->active), 1, fin)))
727 return 3;
728 if ((err = read_int(&(d->loctype), 1, fin)))
729 return 4;
730 if ((err = read_int(&(d->layout), 1, fin)))
731 return 5;
732 if ((err = read_int(&(d->vgap), 1, fin)))
733 return 6;
734 if ((err = read_int(&(d->hgap), 1, fin)))
735 return err;
736 if ((err = read_int(&(d->len), 1, fin)))
737 return 7;
738 if ((err = read_int(&(d->box), 1, fin)))
739 return 8;
740 if ((err = read_double(&(d->legx), 1, fin)))
741 return 9;
742 if ((err = read_double(&(d->legy), 1, fin)))
743 return 10;
744 if ((err = read_int(&(d->font), 1, fin)))
745 return 11;
746 if ((err = read_double(&(d->charsize), 1, fin)))
747 return 12;
748 if ((err = read_int(&(d->color), 1, fin)))
749 return 13;
750 if ((err = read_int(&(d->linew), 1, fin)))
751 return 14;
752 if ((err = read_int(&(d->lines), 1, fin)))
753 return 15;
754 if ((err = read_int(&(d->boxfill), 1, fin)))
755 return 16;
756 if ((err = read_int(&(d->boxfillusing), 1, fin)))
757 return 17;
758 if ((err = read_int(&(d->boxfillcolor), 1, fin)))
759 return 18;
760 if ((err = read_int(&(d->boxfillpat), 1, fin)))
761 return 19;
762 if ((err = read_int(&(d->boxlcolor), 1, fin)))
763 return 20;
764 if ((err = read_int(&(d->boxlinew), 1, fin)))
765 return 21;
766 if ((err = read_int(&(d->boxlines), 1, fin)))
767 return 22;
768 return err;
772 * Read type framep
774 static int read_framep(framep * d, FILE * fin)
776 int err = 0;
777 short magic, version, testmagic = 38, testversion = 0;
778 if ((err = read_short(&magic, 1, fin)))
779 return err;
780 if (magic != testmagic)
781 return BADMAGIC;
782 if ((err = read_short(&version, 1, fin)))
783 return err;
784 if (version != testversion)
785 return BADVERSION;
786 if ((err = read_int(&(d->active), 1, fin)))
787 return err;
788 if ((err = read_int(&(d->type), 1, fin)))
789 return err;
790 if ((err = read_int(&(d->color), 1, fin)))
791 return err;
792 if ((err = read_int(&(d->lines), 1, fin)))
793 return err;
794 if ((err = read_int(&(d->linew), 1, fin)))
795 return err;
796 if ((err = read_int(&(d->fillbg), 1, fin)))
797 return err;
798 if ((err = read_int(&(d->bgcolor), 1, fin)))
799 return err;
800 return err;
804 * Read type BoxPlot
806 static int read_BoxPlot(BoxPlot * d, FILE * fin)
808 int err = 0;
809 short magic, version, testmagic = 39, testversion = 0;
810 if ((err = read_short(&magic, 1, fin)))
811 return err;
812 if (magic != testmagic)
813 return BADMAGIC;
814 if ((err = read_short(&version, 1, fin)))
815 return err;
816 if (version != testversion)
817 return BADVERSION;
818 if ((err = read_double(&(d->il), 1, fin)))
819 return err;
820 if ((err = read_double(&(d->iu), 1, fin)))
821 return err;
822 if ((err = read_double(&(d->ol), 1, fin)))
823 return err;
824 if ((err = read_double(&(d->ou), 1, fin)))
825 return err;
826 if ((err = read_int(&(d->nthresh), 1, fin)))
827 return err;
828 if ((err = read_int(&(d->outliers), 1, fin)))
829 return err;
830 if ((err = read_int(&(d->wtype), 1, fin)))
831 return err;
832 if ((err = read_double(&(d->boxwid), 1, fin)))
833 return err;
834 return err;
838 * Read type graph
840 int read_graph(graph * d, FILE * fin)
842 int i, cnt, ind, err = 0;
843 short magic, version, testmagic = 40, testversion = 0;
844 if ((err = read_short(&magic, 1, fin)))
845 return err;
846 if (magic != testmagic)
847 return BADMAGIC;
848 if ((err = read_short(&version, 1, fin)))
849 return err;
850 if (version != testversion)
851 return BADVERSION;
852 if ((err = read_int(&(d->active), 1, fin)))
853 return err;
854 if ((err = read_int(&(d->hidden), 1, fin)))
855 return err;
856 if ((err = read_int(&(d->label), 1, fin)))
857 return err;
858 if ((err = read_int(&(d->type), 1, fin)))
859 return err;
860 if ((err = read_int(&(d->noauto_world), 1, fin)))
861 return err;
862 if ((err = read_int(&(d->noauto_tics), 1, fin)))
863 return err;
864 if ((err = read_int(&(d->auto_type), 1, fin)))
865 return err;
866 if ((err = read_int(&(d->parmsread), 1, fin)))
867 return err;
869 /* parmsread must be assumed to be true when read from a binary file */
870 /* (we don't erase the above 2 lines for the sake of compatibility) */
871 d->parmsread = TRUE;
873 if ((err = read_int(&(d->revx), 1, fin)))
874 return err;
875 if ((err = read_int(&(d->revy), 1, fin)))
876 return err;
877 if ((err = read_int(&(d->maxplot), 1, fin)))
878 return err;
880 if ((err = read_int(&cnt, 1, fin)))
881 return err;
882 for (i = 0; i < cnt; i++) {
883 if ((err = read_int(&ind, 1, fin)))
884 return err;
885 if ((err = read_plotarr(&(d->p[ind]), fin))) {
886 return err;
890 if ((err = read_legend(&(d->l), fin))) {
891 return err;
893 if ((err = read_world(&(d->w), fin)))
894 return err;
895 if ((err = read_view(&(d->v), fin)))
896 return err;
897 if ((err = read_world(&(d->rt), fin)))
898 return err;
899 if ((err = read_labels(&(d->labs), fin)))
900 return err;
901 for (i = 0; i < 4; i++) {
902 if ((err = read_tickmarks(&(d->t[i]), fin)))
903 return err;
905 if ((err = read_framep(&(d->f), fin)))
906 return err;
907 if ((err = read_int(&(d->pointset), 1, fin)))
908 return err;
909 if ((err = read_int(&(d->pt_type), 1, fin)))
910 return err;
911 if ((err = read_double(&(d->dsx), 1, fin)))
912 return err;
913 if ((err = read_double(&(d->dsy), 1, fin)))
914 return err;
915 if ((err = read_int(&(d->fx), 1, fin)))
916 return err;
917 if ((err = read_int(&(d->fy), 1, fin)))
918 return err;
919 if ((err = read_int(&(d->px), 1, fin)))
920 return err;
921 if ((err = read_int(&(d->py), 1, fin)))
922 return err;
923 if ((err = read_int(&(d->ws_top), 1, fin)))
924 return err;
925 for (i = 0; i < d->ws_top; i++) {
926 if ((err = read_world_stack(&(d->ws[i]), fin)))
927 return err;
930 /* add this for compatability issues */
931 if( d->ws_top == 0 )
932 d->ws_top = 1;
934 if ((err = read_int(&(d->curw), 1, fin)))
935 return err;
936 if ((err = read_velocityp(&(d->vp), fin)))
937 return err;
938 if ((err = read_BoxPlot(&(d->bp), fin)))
939 return err;
940 return err;
943 void open_xdr(FILE *fp, int rw) /* rw write = 0, read = 1 */
945 if (rw == 0) {
946 xdrstdio_create(&xdrs, fp, XDR_ENCODE);
947 } else {
948 xdrstdio_create(&xdrs, fp, XDR_DECODE);
952 void close_xdr(void)
955 xdr_destroy(&xdrs);
959 int read_double(double *d, int n, FILE * fp)
961 int err;
962 if (n <= 0) return 0;
963 err = xdr_vector(&xdrs, (char *) d, n, sizeof(double), (xdrproc_t) xdr_double);
964 return err ? 0 : err;
968 int read_int(int *d, int n, FILE * fp)
970 int err;
971 if (n <= 0) return 0;
972 err = xdr_vector(&xdrs, (char *) d, n, sizeof(int), (xdrproc_t) xdr_int);
973 return err ? 0 : err;
976 int read_charstr(char *d, FILE * fp)
978 int err, n;
979 n = strlen(d) + 1;
980 xdr_int(&xdrs, &n);
981 err = xdr_vector(&xdrs, (char *) d, n, sizeof(char), (xdrproc_t) xdr_char);
982 return err ? 0 : err;
985 int read_char(char *d, int n, FILE * fp)
987 int err;
988 if (n <= 0) return 0;
989 err = xdr_vector(&xdrs, (char *) d, n, sizeof(char), (xdrproc_t) xdr_char);
990 return err ? 0 : err;
993 int read_short(short *d, int n, FILE * fp)
995 int err;
996 if (n <= 0) return 0;
997 err = xdr_vector(&xdrs, (char *) d, n, sizeof(short), (xdrproc_t) xdr_short);
998 return err ? 0 : err;
1001 int read_float(float *d, int n, FILE * fp)
1003 int err;
1004 if (n <= 0) return 0;
1005 err = xdr_vector(&xdrs, (char *) d, n, sizeof(float), (xdrproc_t) xdr_float);
1006 return err ? 0 : err;
1009 int replace_xdr_int( int *i )
1011 int err;
1012 xdr_setpos( &xdrs, xdr_getpos( &xdrs )-4 );
1013 err = xdr_vector(&xdrs, (char *) i, 1, sizeof(int), (xdrproc_t) xdr_int);
1014 xdr_setpos( &xdrs, xdr_getpos( &xdrs )-4 );
1015 return err ? 0: err;
1018 int replace_xdr_short( short *i )
1020 int err;
1021 xdr_setpos( &xdrs, xdr_getpos( &xdrs )-4 );
1022 err = xdr_vector(&xdrs, (char *) i, 1, sizeof(short), (xdrproc_t) xdr_short);
1023 xdr_setpos( &xdrs, xdr_getpos( &xdrs )-4 );
1024 return err ? 0: err;
1030 * write state information
1032 #define PARMS_MAGIC 1002003
1033 #define HEADERLENGTH 64
1036 int is_state_save(char *fname)
1038 int magic, clen, slen, ilen, flen, dlen;
1039 char buf[256];
1040 char ver[64], ord[64], math[64], name[64];
1041 FILE *fp;
1042 if ((fp = fopen(fname, "r")) == NULL) {
1043 return 0;
1045 open_xdr(fp, 1); /* open XDR stream */
1046 read_char(buf, HEADERLENGTH, fp);
1047 buf[HEADERLENGTH - 1] = 0;
1048 sscanf(buf, "%d %s %s %s %s %d %d %d %d %d",
1049 &magic, ver, ord, math, name, &clen, &slen, &ilen, &flen, &dlen);
1050 close_xdr();
1051 fclose(fp);
1052 return (magic == PARMS_MAGIC) ? 1 : 0;
1056 * getbinary - read in binary project file
1058 * return: 0 - o.k.
1059 * 1 - nothing read in
1060 * 2 - partial read
1062 int getbinary(int gno, char *fname, int imbed)
1064 int ind, i, k, cnt, ng, magic;
1065 int clen, slen, ilen, flen, dlen, nblocks;
1066 char buf[256];
1067 char ver[64], ord[64], math[64], name[64];
1068 FILE *pp;
1069 if ((pp = fopen(fname, "rb")) == NULL) {
1070 sprintf(buf, "Can't open project file %s", fname);
1071 errmsg(buf);
1072 return 1;
1074 strcpy( docname, fname );
1075 open_xdr(pp, 1); /* open XDR stream */
1076 read_char(buf, HEADERLENGTH, pp);
1077 buf[HEADERLENGTH - 1] = 0;
1078 sscanf(buf, "%d %s %s %s %s %d %d %d %d %d",
1079 &magic, ver, ord, math, name, &clen, &slen, &ilen, &flen, &dlen);
1080 if (magic != PARMS_MAGIC) {
1081 errmsg("Bad magic in project file");
1082 fclose(pp);
1083 return 1;
1085 read_int(&cnt, 1, pp);
1086 for (k = 0; k < cnt; k++) {
1087 read_int(&ng, 1, pp);
1088 if (read_graph(&g[ng], pp)) {
1089 errmsg("Error reading project file (graphs), cancelled");
1090 fclose(pp);
1091 return 2;
1094 read_int(&cnt, 1, pp);
1095 for (k = 0; k < cnt; k++) {
1096 read_int(&ind, 1, pp);
1097 if (read_linetype(&lines[ind], pp)) {
1098 errmsg("Error reading project file (lines), cancelled");
1099 fclose(pp);
1100 return 2;
1102 lines[ind].active = ON;
1104 read_int(&cnt, 1, pp);
1105 for (k = 0; k < cnt; k++) {
1106 read_int(&ind, 1, pp);
1107 if (read_boxtype(&boxes[ind], pp)) {
1108 errmsg("Error reading project file (boxes), cancelled");
1109 fclose(pp);
1110 return 2;
1112 boxes[ind].active = ON;
1114 read_int(&cnt, 1, pp);
1115 for (k = 0; k < cnt; k++) {
1116 read_int(&ind, 1, pp);
1117 switch(read_ellipsetype(&ellip[ind], pp)) {
1118 case 0:
1119 ellip[ind].active = ON;
1120 break;
1121 case BADMAGIC:
1122 /* assume error from reading pre-ellipse file so put things
1123 back onto stream */
1124 if( replace_xdr_int( &ind ) || replace_xdr_int( &cnt ) ) {
1125 errmsg("Error reading project file (ellipses), cancelled");
1126 fclose(pp);
1127 return 2;
1128 } else
1129 k = cnt; /* end loop */
1130 break;
1131 default:
1132 errmsg("Error reading project file (plotstr), cancelled");
1133 fclose(pp);
1134 return 2;
1137 read_int(&cnt, 1, pp);
1138 for (k = 0; k < cnt; k++) {
1139 read_int(&ind, 1, pp);
1140 if (read_plotstr(&pstr[ind], pp)) {
1141 errmsg("Error reading project file (plotstr), cancelled");
1142 fclose(pp);
1143 return 2;
1146 /* read block data */
1147 read_int(&nblocks, 1, pp);
1148 if (nblocks != 0) {
1149 read_int(&blocklen, 1, pp);
1150 read_int(&blockncols, 1, pp);
1151 for (i = 0; i < blockncols; i++) {
1152 read_int(&ind, 1, pp); /* TODO need to fix this malloc business */
1153 blockdata[ind] = (double *) malloc(sizeof(double) * blocklen);
1154 read_double(blockdata[ind], blocklen, pp);
1157 /* read description */
1158 if( read_charstr( description, pp ) )
1159 strcpy( description, "Just a typical project I assume" );
1160 if( read_int(&page_layout, 1, pp))
1161 page_layout=FREE;
1162 close_xdr();
1163 fclose(pp);
1164 return 0;