home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume19 / xfig / part05 / f_save.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-27  |  6.8 KB  |  269 lines

  1. /*
  2.  * FIG : Facility for Interactive Generation of figures
  3.  * Copyright (c) 1985 by Supoj Sutanthavibul
  4.  *
  5.  * "Permission to use, copy, modify, distribute, and sell this software and its
  6.  * documentation for any purpose is hereby granted without fee, provided that
  7.  * the above copyright notice appear in all copies and that both the copyright
  8.  * notice and this permission notice appear in supporting documentation. 
  9.  * No representations are made about the suitability of this software for 
  10.  * any purpose.  It is provided "as is" without express or implied warranty."
  11.  */
  12.  
  13. #include "fig.h"
  14. #include "resources.h"
  15. #include "mode.h"
  16. #include "object.h"
  17. #include "w_setup.h"
  18.  
  19. extern int    num_object;
  20.  
  21. write_file(file_name)
  22.     char       *file_name;
  23. {
  24.     FILE       *fp;
  25.  
  26.     if (!ok_to_write(file_name, "SAVE"))
  27.     return (-1);
  28.  
  29.     if ((fp = fopen(file_name, "w")) == NULL) {
  30.     put_msg("Couldn't open file %s, %s", file_name, sys_errlist[errno]);
  31.     return (-1);
  32.     }
  33.     num_object = 0;
  34.     if (write_objects(fp)) {
  35.     put_msg("Error writing file %s, %s", file_name, sys_errlist[errno]);
  36.     return (-1);
  37.     }
  38.     put_msg("%d object(s) saved in \"%s\"", num_object, file_name);
  39.     return (0);
  40. }
  41.  
  42. int
  43. write_objects(fp)
  44.     FILE       *fp;
  45. {
  46.     extern char        file_header[];
  47.     F_arc       *a;
  48.     F_compound       *c;
  49.     F_ellipse       *e;
  50.     F_line       *l;
  51.     F_spline       *s;
  52.     F_text       *t;
  53.  
  54.     /*
  55.      * Number 2 means that the origin (0,0) is at the upper left corner of
  56.      * the screen (2nd quadrant)
  57.      */
  58.  
  59.     put_msg("Writing . . .");
  60.     fprintf(fp, "%s\n", file_header);
  61.     fprintf(fp, "%d %d\n", PIX_PER_INCH, 2);
  62.     for (a = objects.arcs; a != NULL; a = a->next) {
  63.     num_object++;
  64.     write_arc(fp, a);
  65.     }
  66.     for (c = objects.compounds; c != NULL; c = c->next) {
  67.     num_object++;
  68.     write_compound(fp, c);
  69.     }
  70.     for (e = objects.ellipses; e != NULL; e = e->next) {
  71.     num_object++;
  72.     write_ellipse(fp, e);
  73.     }
  74.     for (l = objects.lines; l != NULL; l = l->next) {
  75.     num_object++;
  76.     write_line(fp, l);
  77.     }
  78.     for (s = objects.splines; s != NULL; s = s->next) {
  79.     num_object++;
  80.     write_spline(fp, s);
  81.     }
  82.     for (t = objects.texts; t != NULL; t = t->next) {
  83.     num_object++;
  84.     write_text(fp, t);
  85.     }
  86.     if (ferror(fp)) {
  87.     fclose(fp);
  88.     return (-1);
  89.     }
  90.     if (fclose(fp) == EOF)
  91.     return (-1);
  92.     return (0);
  93. }
  94.  
  95. write_arc(fp, a)
  96.     FILE       *fp;
  97.     F_arc       *a;
  98. {
  99.     F_arrow       *f, *b;
  100.  
  101.     fprintf(fp, "%d %d %d %d %d %d %d %d %.3f %d %d %d %.3f %.3f %d %d %d %d %d %d\n",
  102.         O_ARC, a->type, a->style, a->thickness,
  103.         a->color, a->depth, a->pen, a->fill_style,
  104.         a->style_val, a->direction,
  105.         ((f = a->for_arrow) ? 1 : 0), ((b = a->back_arrow) ? 1 : 0),
  106.         a->center.x, a->center.y,
  107.         a->point[0].x, a->point[0].y,
  108.         a->point[1].x, a->point[1].y,
  109.         a->point[2].x, a->point[2].y);
  110.     if (f)
  111.     fprintf(fp, "\t%d %d %.3f %.3f %.3f\n", f->type, f->style,
  112.         f->thickness, f->wid, f->ht);
  113.     if (b)
  114.     fprintf(fp, "\t%d %d %.3f %.3f %.3f\n", b->type, b->style,
  115.         b->thickness, b->wid, b->ht);
  116. }
  117.  
  118. write_compound(fp, com)
  119.     FILE       *fp;
  120.     F_compound       *com;
  121. {
  122.     F_arc       *a;
  123.     F_compound       *c;
  124.     F_ellipse       *e;
  125.     F_line       *l;
  126.     F_spline       *s;
  127.     F_text       *t;
  128.  
  129.     fprintf(fp, "%d %d %d %d %d\n", O_COMPOUND, com->nwcorner.x,
  130.         com->nwcorner.y, com->secorner.x, com->secorner.y);
  131.     for (a = com->arcs; a != NULL; a = a->next)
  132.     write_arc(fp, a);
  133.     for (c = com->compounds; c != NULL; c = c->next)
  134.     write_compound(fp, c);
  135.     for (e = com->ellipses; e != NULL; e = e->next)
  136.     write_ellipse(fp, e);
  137.     for (l = com->lines; l != NULL; l = l->next)
  138.     write_line(fp, l);
  139.     for (s = com->splines; s != NULL; s = s->next)
  140.     write_spline(fp, s);
  141.     for (t = com->texts; t != NULL; t = t->next)
  142.     write_text(fp, t);
  143.     fprintf(fp, "%d\n", O_END_COMPOUND);
  144. }
  145.  
  146. write_ellipse(fp, e)
  147.     FILE       *fp;
  148.     F_ellipse       *e;
  149. {
  150.     if (e->radiuses.x == 0 || e->radiuses.y == 0)
  151.     return;
  152.  
  153.     fprintf(fp, "%d %d %d %d %d %d %d %d %.5f %d %.3f %d %d %d %d %d %d %d %d\n",
  154.         O_ELLIPSE, e->type, e->style, e->thickness,
  155.         e->color, e->depth, e->pen, e->fill_style,
  156.         e->style_val, e->direction, e->angle,
  157.         e->center.x, e->center.y,
  158.         e->radiuses.x, e->radiuses.y,
  159.         e->start.x, e->start.y,
  160.         e->end.x, e->end.y);
  161. }
  162.  
  163. write_line(fp, l)
  164.     FILE       *fp;
  165.     F_line       *l;
  166. {
  167.     F_point       *p;
  168.     F_arrow       *f, *b;
  169.     int           npts;
  170.  
  171.     if (l->points == NULL)
  172.     return;
  173.     fprintf(fp, "%d %d %d %d %d %d %d %d %.3f %d %d %d\n",
  174.         O_POLYLINE, l->type, l->style, l->thickness,
  175.      l->color, l->depth, l->pen, l->fill_style, l->style_val, l->radius,
  176.         ((f = l->for_arrow) ? 1 : 0), ((b = l->back_arrow) ? 1 : 0));
  177.     if (f)
  178.     fprintf(fp, "\t%d %d %.3f %.3f %.3f\n", f->type, f->style,
  179.         f->thickness, f->wid, f->ht);
  180.     if (b)
  181.     fprintf(fp, "\t%d %d %.3f %.3f %.3f\n", b->type, b->style,
  182.         b->thickness, b->wid, b->ht);
  183.     if (l->type == T_EPS_BOX)
  184.     fprintf(fp, "\t%d %s\n", l->eps->flipped, l->eps->file);
  185.  
  186.     fprintf(fp, "\t");
  187.     npts=0;
  188.     for (p = l->points; p != NULL; p = p->next) {
  189.     fprintf(fp, " %d %d", p->x, p->y);
  190.     if (++npts >= 8 && p->next != NULL)
  191.         {
  192.         fprintf(fp,"\n\t");
  193.         npts=0;
  194.         }
  195.     };
  196.     fprintf(fp, " 9999 9999\n");
  197. }
  198.  
  199. write_spline(fp, s)
  200.     FILE       *fp;
  201.     F_spline       *s;
  202. {
  203.     F_control       *cp;
  204.     F_point       *p;
  205.     F_arrow       *f, *b;
  206.     int           npts;
  207.  
  208.     if (s->points == NULL)
  209.     return;
  210.     fprintf(fp, "%d %d %d %d %d %d %d %d %.3f %d %d\n",
  211.         O_SPLINE, s->type, s->style, s->thickness,
  212.         s->color, s->depth, s->pen, s->fill_style, s->style_val,
  213.         ((f = s->for_arrow) ? 1 : 0), ((b = s->back_arrow) ? 1 : 0));
  214.     if (f)
  215.     fprintf(fp, "\t%d %d %.3f %.3f %.3f\n", f->type, f->style,
  216.         f->thickness, f->wid, f->ht);
  217.     if (b)
  218.     fprintf(fp, "\t%d %d %.3f %.3f %.3f\n", b->type, b->style,
  219.         b->thickness, b->wid, b->ht);
  220.     fprintf(fp, "\t");
  221.     for (p = s->points; p != NULL; p = p->next) {
  222.     fprintf(fp, " %d %d", p->x, p->y);
  223.     };
  224.     fprintf(fp, " 9999 9999\n");/* terminating code  */
  225.  
  226.     if (s->controls == NULL)
  227.     return;
  228.     fprintf(fp, "\t");
  229.     npts=0;
  230.     for (cp = s->controls; cp != NULL; cp = cp->next) {
  231.     fprintf(fp, " %.3f %.3f %.3f %.3f",
  232.         cp->lx, cp->ly, cp->rx, cp->ry);
  233.     if (++npts >= 2 && cp->next != NULL)
  234.         {
  235.         fprintf(fp,"\n\t");
  236.         npts=0;
  237.         }
  238.     };
  239.     fprintf(fp, "\n");
  240. }
  241.  
  242. write_text(fp, t)
  243.     FILE       *fp;
  244.     F_text       *t;
  245. {
  246.     if (t->length == 0)
  247.     return;
  248.     fprintf(fp, "%d %d %d %d %d %d %d %.5f %d %d %d %d %d %s\1\n",
  249.         O_TEXT, t->type, t->font, t->size, t->pen,
  250.         t->color, t->depth, t->angle,
  251.         t->flags, t->height, t->length,
  252.         t->base_x, t->base_y, t->cstring);
  253. }
  254.  
  255. emergency_save(file_name)
  256.     char       *file_name;
  257. {
  258.     FILE       *fp;
  259.  
  260.     if ((fp = fopen(file_name, "w")) == NULL)
  261.     return (-1);
  262.     num_object = 0;
  263.     if (write_objects(fp))
  264.     return (-1);
  265.     (void) fprintf(stderr, "xfig: %d object(s) saved in \"%s\"\n",
  266.            num_object, file_name);
  267.     return (0);
  268. }
  269.