home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume8 / xfig2.8 / part10 / read1_3.c < prev   
Encoding:
C/C++ Source or Header  |  1990-07-03  |  10.4 KB  |  488 lines

  1. /* 
  2.  *    FIG : Facility for Interactive Generation of figures
  3.  *
  4.  *    Copyright (c) 1985, 1988 by Supoj Sutanthavibul (supoj@sally.UTEXAS.EDU)
  5.  *    January 1985.
  6.  *    1st revision : March 1988.
  7.  *
  8.  *    %W%    %G%
  9. */
  10.  
  11. /*******************************************************************/
  12. /***************       Read version 1.3 format       ***************/
  13. /*******************************************************************/
  14. #include "fig.h"
  15. #include "alloc.h"
  16. #include "object.h"
  17.  
  18. /*******    Fig 1.3 subtype of objects    *******/
  19. #define            DRAW_ELLIPSE_BY_RAD     1
  20. #define            DRAW_ELLIPSE_BY_DIA     2
  21. #define            DRAW_CIRCLE_BY_RAD     3
  22. #define            DRAW_CIRCLE_BY_DIA     4
  23. #define            DRAW_CIRCULAR_ARC    5
  24. #define            DRAW_POLYLINE        6
  25. #define            DRAW_BOX        7
  26. #define            DRAW_POLYGON        8
  27. #define            DRAW_TEXT        9
  28. #define            DRAW_SPLINE        10
  29. #define            DRAW_CLOSEDSPLINE    11
  30. #define            DRAW_COMPOUND        13
  31.  
  32. extern F_arrow        *forward_arrow(), *backward_arrow();
  33. extern int        errno;
  34. extern char        *sys_errlist[];
  35. extern int        sys_nerr, errno;
  36.  
  37. static F_ellipse    *read_1_3_ellipseobject();
  38. static F_line        *read_1_3_lineobject();
  39. static F_text        *read_1_3_textobject();
  40. static F_spline        *read_1_3_splineobject();
  41. static F_arc        *read_1_3_arcobject();
  42. static F_compound    *read_1_3_compoundobject();
  43.  
  44. extern int        line_no;
  45. extern int        num_object;
  46.  
  47. int
  48. read_1_3_objects(fp, obj)
  49. FILE        *fp;
  50. F_compound    *obj;
  51. {
  52.     F_ellipse    *e, *le = NULL;
  53.     F_line        *l, *ll = NULL;
  54.     F_text        *t, *lt = NULL;
  55.     F_spline    *s, *ls = NULL;
  56.     F_arc        *a, *la = NULL;
  57.     F_compound    *c, *lc = NULL;
  58.     int         n;
  59.     int         object, pixperinch, canvaswid, canvasht, coord_sys;
  60.  
  61.     n = fscanf(fp,"%d%d%d%d\n", &pixperinch, &coord_sys, &canvaswid, &canvasht);
  62.     if (n != 4) {
  63.         put_msg("Incorrect format in the first line in input file");
  64.         return(-1);
  65.         }
  66.     obj->nwcorner.x = pixperinch;
  67.     obj->nwcorner.y = coord_sys;
  68.     while (fscanf(fp, "%d", &object) == 1) {
  69.         switch (object) {
  70.         case O_POLYLINE :
  71.             if ((l = read_1_3_lineobject(fp)) == NULL) return(-1);
  72.             if (ll)
  73.             ll = (ll->next = l);
  74.             else
  75.             ll = obj->lines = l;
  76.             num_object++;
  77.             break;
  78.         case O_SPLINE :
  79.             if ((s = read_1_3_splineobject(fp)) == NULL) return(-1);
  80.             if (ls)
  81.             ls = (ls->next = s);
  82.             else
  83.             ls = obj->splines = s;
  84.             num_object++;
  85.             break;
  86.         case O_ELLIPSE :
  87.             if ((e = read_1_3_ellipseobject(fp)) == NULL) return(-1);
  88.             if (le)
  89.             le = (le->next = e);
  90.             else
  91.             le = obj->ellipses = e;
  92.             num_object++;
  93.             break;
  94.         case O_ARC :
  95.             if ((a = read_1_3_arcobject(fp)) == NULL) return(-1);
  96.             if (la)
  97.             la = (la->next = a);
  98.             else
  99.             la = obj->arcs = a;
  100.             num_object++;
  101.             break;
  102.         case O_TEXT :
  103.             if ((t = read_1_3_textobject(fp)) == NULL) return(-1);
  104.             if (lt)
  105.             lt = (lt->next = t);
  106.             else
  107.             lt = obj->texts = t;
  108.             num_object++;
  109.             break;
  110.         case O_COMPOUND :
  111.             if ((c = read_1_3_compoundobject(fp)) == NULL) return(-1);
  112.             if (lc)
  113.             lc = (lc->next = c);
  114.             else
  115.             lc = obj->compounds = c;
  116.             num_object++;
  117.             break;
  118.         default:
  119.             put_msg("Incorrect object code %d", object);
  120.             return(-1);
  121.         } /*  switch */
  122.         } /*  while */
  123.     if (feof(fp))
  124.         return(0);
  125.     else
  126.         return(errno);
  127.     }
  128.  
  129. static F_arc *
  130. read_1_3_arcobject(fp)
  131. FILE    *fp;
  132. {
  133.     F_arc    *a;
  134.     int    f, b, h, w, n;
  135.  
  136.     if (NULL == (Arc_malloc(a))) {
  137.         put_msg(Err_mem);
  138.         return(NULL);
  139.         }
  140.     a->type = T_3_POINTS_ARC;
  141.     a->color = BLACK;
  142.     a->depth = 0;
  143.     a->pen = 0;
  144.     a->for_arrow = NULL;
  145.     a->back_arrow = NULL;
  146.     a->next = NULL;
  147.     n = fscanf(fp, " %d %d %d %f %d %d %d %d %d %f %f %d %d %d %d %d %d\n",
  148.         &a->type, &a->style, &a->thickness, 
  149.         &a->style_val, &a->direction, &f, &b,
  150.         &h, &w, &a->center.x, &a->center.y, 
  151.         &a->point[0].x, &a->point[0].y, 
  152.         &a->point[1].x, &a->point[1].y, 
  153.         &a->point[2].x, &a->point[2].y);
  154.     if (n != 17) {
  155.         put_msg("incomplete arc data");
  156.         free((char*)a);
  157.         return(NULL);
  158.         }
  159.     if (f) {
  160.         a->for_arrow = forward_arrow();
  161.         a->for_arrow->wid = w;
  162.         a->for_arrow->ht = h;
  163.         }
  164.     if (b) {
  165.         a->back_arrow = backward_arrow();
  166.         a->back_arrow->wid = w;
  167.         a->back_arrow->ht = h;
  168.         }
  169.     return(a);
  170.     }
  171.  
  172. static F_compound *
  173. read_1_3_compoundobject(fp)
  174. FILE    *fp;
  175. {
  176.     F_arc        *a, *la = NULL;
  177.     F_ellipse    *e, *le = NULL;
  178.     F_line        *l, *ll = NULL;
  179.     F_spline    *s, *ls = NULL;
  180.     F_text        *t, *lt = NULL;
  181.     F_compound    *com, *c, *lc = NULL;
  182.     int         n, object;
  183.  
  184.     if (NULL == (Compound_malloc(com))) {
  185.         put_msg(Err_mem);
  186.         return(NULL);
  187.         }
  188.     com->arcs = NULL;
  189.     com->ellipses = NULL;
  190.     com->lines = NULL;
  191.     com->splines = NULL;
  192.     com->texts = NULL;
  193.     com->compounds = NULL;
  194.     com->next = NULL;
  195.     n = fscanf(fp, " %d %d %d %d\n", &com->nwcorner.x, &com->nwcorner.y,
  196.         &com->secorner.x, &com->secorner.y);
  197.     if (n != 4) {
  198.         put_msg("Incorrect compound object format");
  199.         return(NULL);
  200.         }
  201.     while (fscanf(fp, "%d", &object) == 1) {
  202.         switch (object) {
  203.         case O_POLYLINE :
  204.             if ((l = read_1_3_lineobject(fp)) == NULL) { 
  205.             free_line(&l);
  206.             return(NULL);
  207.             }
  208.             if (ll)
  209.             ll = (ll->next = l);
  210.             else
  211.             ll = com->lines = l;
  212.             break;
  213.         case O_SPLINE :
  214.             if ((s = read_1_3_splineobject(fp)) == NULL) { 
  215.             free_spline(&s);
  216.             return(NULL);
  217.             }
  218.             if (ls)
  219.             ls = (ls->next = s);
  220.             else
  221.             ls = com->splines = s;
  222.             break;
  223.         case O_ELLIPSE :
  224.             if ((e = read_1_3_ellipseobject(fp)) == NULL) { 
  225.             free_ellipse(&e);
  226.             return(NULL);
  227.             }
  228.             if (le)
  229.             le = (le->next = e);
  230.             else
  231.             le = com->ellipses = e;
  232.             break;
  233.         case O_ARC :
  234.             if ((a = read_1_3_arcobject(fp)) == NULL) { 
  235.             free_arc(&a);
  236.             return(NULL);
  237.             }
  238.             if (la)
  239.             la = (la->next = a);
  240.             else
  241.             la = com->arcs = a;
  242.             break;
  243.         case O_TEXT :
  244.             if ((t = read_1_3_textobject(fp)) == NULL) { 
  245.             free_text(&t);
  246.             return(NULL);
  247.             }
  248.             if (lt)
  249.             lt = (lt->next = t);
  250.             else
  251.             lt = com->texts = t;
  252.             break;
  253.         case O_COMPOUND :
  254.             if ((c = read_1_3_compoundobject(fp)) == NULL) { 
  255.             free_compound(&c);
  256.             return(NULL);
  257.             }
  258.             if (lc)
  259.             lc = (lc->next = c);
  260.             else
  261.             lc = com->compounds = c;
  262.             break;
  263.         case O_END_COMPOUND :
  264.             return(com);
  265.         } /*  switch */
  266.         }
  267.     if (feof(fp))
  268.         return(com);
  269.     else {
  270.         put_msg("Format error: %s", sys_errlist[errno]);
  271.         return(NULL);
  272.         }
  273.     }
  274.  
  275. static F_ellipse *
  276. read_1_3_ellipseobject(fp)
  277. FILE    *fp;
  278. {
  279.     F_ellipse    *e;
  280.     int        n, t;
  281.  
  282.     if (NULL == (Ellipse_malloc(e))) {
  283.         put_msg(Err_mem);
  284.         return(NULL);
  285.         }
  286.     e->color = BLACK;
  287.     e->angle = 0.0;
  288.     e->depth = 0;
  289.     e->pen = 0;
  290.     e->area_fill = NULL;
  291.     e->next = NULL;
  292.     n = fscanf(fp," %d %d %d %f %d %d %d %d %d %d %d %d %d\n", 
  293.         &t, &e->style,
  294.         &e->thickness, &e->style_val, &e->direction, 
  295.         &e->center.x, &e->center.y, 
  296.         &e->radiuses.x, &e->radiuses.y, 
  297.         &e->start.x, &e->start.y, 
  298.         &e->end.x, &e->end.y);
  299.     if (n != 13) {
  300.         put_msg("incomplete ellipse data");
  301.         free((char*)e);
  302.         return(NULL);
  303.         }
  304.     if (t == DRAW_ELLIPSE_BY_RAD)
  305.         e->type = T_ELLIPSE_BY_RAD;
  306.     else if (t == DRAW_ELLIPSE_BY_DIA)
  307.         e->type = T_ELLIPSE_BY_DIA;
  308.     else if (t == DRAW_CIRCLE_BY_RAD)
  309.         e->type = T_CIRCLE_BY_RAD;
  310.     else
  311.         e->type = T_CIRCLE_BY_DIA;
  312.     return(e);
  313.     }
  314.  
  315. static F_line *
  316. read_1_3_lineobject(fp)
  317. FILE            *fp;
  318. {
  319.     F_line    *l;
  320.     F_point    *p, *q;
  321.     int    f, b, h, w, n, t, x, y;
  322.  
  323.     if (NULL == (Line_malloc(l))) {
  324.         put_msg(Err_mem);
  325.         return(NULL);
  326.         }
  327.     l->color = BLACK;
  328.     l->depth = 0;
  329.     l->pen = 0;
  330.     l->area_fill = NULL;
  331.     l->for_arrow = NULL;
  332.     l->back_arrow = NULL;
  333.     l->next = NULL;
  334.     if (NULL == (Point_malloc(p))) {
  335.         put_msg(Err_mem);
  336.         return(NULL);
  337.         }
  338.     l->points = p;
  339.     n = fscanf(fp, " %d %d %d %f %d %d %d %d %d %d", &t, 
  340.         &l->style, &l->thickness, &l->style_val,
  341.         &f, &b, &h, &w, &p->x, &p->y);
  342.     if (n != 10) {
  343.         put_msg("incomplete line data");
  344.         free((char*)l);
  345.         return(NULL);
  346.         }
  347.     if (t == DRAW_POLYLINE)
  348.         l->type = T_POLYLINE;
  349.     else if (t == DRAW_POLYGON)
  350.         l->type = T_POLYGON;
  351.     else
  352.         l->type = T_BOX;
  353.     if (f) {
  354.         l->for_arrow = forward_arrow();
  355.         l->for_arrow->wid = w;
  356.         l->for_arrow->ht = h;
  357.         }
  358.     if (b) {
  359.         l->back_arrow = backward_arrow();
  360.         l->back_arrow->wid = w;
  361.         l->back_arrow->ht = h;
  362.         }
  363.     for (;;) {
  364.         if (fscanf(fp, " %d %d", &x, &y) != 2) {
  365.         put_msg("incomplete line object");
  366.         free_linestorage(l);
  367.         return(NULL);
  368.         }
  369.         if (x == 9999) break;
  370.         if (NULL == (Point_malloc(q))) {
  371.           put_msg(Err_mem);
  372.           return(NULL);
  373.         }
  374.         q->x = x;
  375.         q->y = y;
  376.         q->next = NULL;
  377.         p->next = q;
  378.         p = q;
  379.         }
  380.     return(l);
  381.     }
  382.  
  383. static F_spline *
  384. read_1_3_splineobject(fp)
  385. FILE    *fp;
  386. {
  387.     F_spline    *s;
  388.     F_point        *p, *q;
  389.     int        f, b, h, w, n, t, x, y;
  390.  
  391.     if (NULL == (Spline_malloc(s))) {
  392.           put_msg(Err_mem);
  393.           return(NULL);
  394.         }
  395.     s->color = BLACK;
  396.     s->depth = 0;
  397.     s->pen = 0;
  398.     s->area_fill = NULL;
  399.     s->for_arrow = NULL;
  400.     s->back_arrow = NULL;
  401.     s->controls = NULL;
  402.     s->next = NULL;
  403.     if (NULL == (Point_malloc(p))) {
  404.           put_msg(Err_mem);
  405.           return(NULL);
  406.         }
  407.     s->points = p;
  408.     n = fscanf(fp, " %d %d %d %f %d %d %d %d %d %d", 
  409.             &t, &s->style, &s->thickness, &s->style_val,
  410.             &f, &b,
  411.             &h, &w, &p->x, &p->y);
  412.     if (n != 10) {
  413.         put_msg("incomplete spline data");
  414.         free((char*)s);
  415.         return(NULL);
  416.         }
  417.     if (t == DRAW_CLOSEDSPLINE)
  418.         s->type = T_CLOSED_NORMAL;
  419.     else
  420.         s->type = T_OPEN_NORMAL;
  421.     if (f) {
  422.         s->for_arrow = forward_arrow();
  423.         s->for_arrow->wid = w;
  424.         s->for_arrow->ht = h;
  425.         }
  426.     if (b) {
  427.         s->back_arrow = backward_arrow();
  428.         s->back_arrow->wid = w;
  429.         s->back_arrow->ht = h;
  430.         }
  431.     for (;;) {
  432.         if (fscanf(fp, " %d %d", &x, &y) != 2) {
  433.         put_msg("incomplete spline object");
  434.         free_splinestorage(s);
  435.         return(NULL);
  436.         };
  437.         if (x == 9999) break;
  438.         if (NULL == (Point_malloc(q))) {
  439.           put_msg(Err_mem);
  440.           return(NULL);
  441.         }
  442.         q->x = x;
  443.         q->y = y;
  444.         q->next = NULL;
  445.         p->next = q;
  446.         p = q;
  447.         }
  448.     return(s);
  449.     }
  450.  
  451. static F_text *
  452. read_1_3_textobject(fp)
  453. FILE    *fp;
  454. {
  455.     F_text    *t;
  456.     int    n;
  457.     char    buf[128];
  458.  
  459.     if (NULL == (Text_malloc(t))) {
  460.           put_msg(Err_mem);
  461.           return(NULL);
  462.         }
  463.     t->type = T_LEFT_JUSTIFIED;
  464.     t->style = PLAIN;
  465.     t->color = BLACK;
  466.     t->depth = 0;
  467.     t->pen = 0;
  468.     t->angle = 0.0;
  469.     t->next = NULL;
  470.     n = fscanf(fp," %d %d %d %d %d %d %d %[^\n]", &t->font, 
  471.         &t->size, &t->style, &t->height, &t->length, 
  472.         &t->base_x, &t->base_y, buf);
  473.     if (n != 8) {
  474.         put_msg("incomplete text data");
  475.         free((char*)t);
  476.         return(NULL);
  477.         }
  478.     t->cstring = (char *) calloc((unsigned)(strlen(buf)+1), sizeof(char));
  479.     if (t->cstring == NULL) {
  480.         put_msg(Err_mem);
  481.         free((char*) t);
  482.         return(NULL);
  483.         }
  484.     strcpy(t->cstring, buf);
  485.     if (t->size == 0) t->size = 18;
  486.     return(t);
  487.     }
  488.