home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume8 / xfig2.8 / part06 / save.c < prev    next >
C/C++ Source or Header  |  1990-07-02  |  7KB  |  267 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 : August 1985.
  7.  *    2nd revision : March 1988.
  8.  *
  9.  *    %W%    %G%
  10. */
  11. #include "fig.h"
  12. #include "resources.h"
  13. #include "func.h"
  14. #include "object.h"
  15. #include "const.h"
  16.  
  17. extern F_compound    objects;
  18.  
  19. extern int        figure_modified;
  20. extern int        errno;
  21.  
  22. extern             null_proc();
  23.  
  24. extern char        *sys_errlist[];
  25. extern int        sys_nerr, errno;
  26. extern int        num_object;
  27.  
  28. write_file(file_name, prompt)
  29. char    *file_name;
  30. int    prompt;
  31. {
  32.     FILE        *fp;
  33.     struct stat    file_status;
  34.     char        string[180];
  35.  
  36.     if (*file_name == 0) {
  37.         put_msg("No file.  Abort save operation.");
  38.         return(-1);
  39.         }
  40.     if (stat(file_name, &file_status) == 0) { /* file exists */
  41.         if (file_status.st_mode & S_IFDIR) {
  42.         put_msg("\"%s\" is a directory", file_name);
  43.         return(-1);
  44.         }
  45.         if (file_status.st_mode & S_IWRITE) { /* writing is permitted */
  46.         if (file_status.st_uid != geteuid()) {
  47.             put_msg("\"%s\" permission is denied", file_name);
  48.             return(-1);
  49.             }
  50.         else if (prompt) {
  51.             sprintf(string, "\"%s\" File exists. Please click the LEFT button to COMFIRM overwrite. To cancel, click the MIDDLE or RIGHT button.", file_name);
  52.             if (wmgr_confirm(canvas_win, string) != -1) {
  53.             put_msg("Cancel save");
  54.             return(-1);
  55.             }
  56.             }
  57.         }
  58.         else {
  59.         put_msg("\"%s\" File is read only", file_name);
  60.         return(-1);
  61.         }
  62.         }
  63.     else if (errno != ENOENT)
  64.         return(-1);  /* file does exist but stat fails */
  65.  
  66.     if ((fp = fopen(file_name, "w")) == NULL) {
  67.         blink_msg();
  68.         put_msg("Couldn't open file %s, %s", file_name, sys_errlist[errno]);
  69.         return(-1);
  70.         }
  71.     else {
  72.         figure_modified = 0;
  73.         num_object = 0;
  74.         write_objects(fp);
  75.         put_msg("%d objects saved in \"%s\"", num_object, file_name);
  76.         return(0);
  77.         }
  78.     }
  79.  
  80. write_objects(fp)
  81. FILE    *fp;
  82. {
  83.     extern char    file_header[];
  84.     F_arc        *a;
  85.     F_compound    *c;
  86.     F_ellipse    *e;
  87.     F_line        *l;
  88.     F_spline    *s;
  89.     F_text        *t;
  90.  
  91.     /*  Number 2 means that the origin (0,0) is at the upper
  92.     left corner of the screen (2nd quadrant)    */
  93.  
  94.     put_msg("Writing . . .");
  95.     fprintf(fp, "%s\n", file_header);
  96.     fprintf(fp, "%d %d\n", PIX_PER_INCH, 2);
  97.     for (a = objects.arcs; a != NULL; a = a-> next) {
  98.         num_object++;
  99.         write_arc(fp, a);
  100.         }
  101.     for (c = objects.compounds; c != NULL; c = c-> next) {
  102.         num_object++;
  103.         write_compound(fp, c);
  104.         }
  105.     for (e = objects.ellipses; e != NULL; e = e-> next) {
  106.         num_object++;
  107.         write_ellipse(fp, e);
  108.         }
  109.     for (l = objects.lines; l != NULL; l = l-> next) {
  110.         num_object++;
  111.         write_line(fp, l);
  112.         }
  113.     for (s = objects.splines; s != NULL; s = s-> next) {
  114.         num_object++;
  115.         write_spline(fp, s);
  116.         }
  117.     for (t = objects.texts; t != NULL; t = t-> next) {
  118.         num_object++;
  119.         write_text(fp, t);
  120.         }
  121.     fclose(fp);
  122.     }
  123.  
  124. write_arc(fp, a)
  125. FILE    *fp;
  126. F_arc    *a;
  127. {
  128.     F_arrow    *f, *b;
  129.  
  130.     fprintf(fp, "%d %d %d %d %d %d %d %d %.3f %d %d %d %.3f %.3f %d %d %d %d %d %d\n",
  131.         O_ARC, a->type, a->style, a->thickness, 
  132.         a->color, a->depth, a->pen, a->area_fill,
  133.         a->style_val, a->direction,
  134.         ((f = a->for_arrow) ? 1 : 0), ((b = a->back_arrow) ? 1 : 0),
  135.         a->center.x, a->center.y, 
  136.         a->point[0].x, a->point[0].y, 
  137.         a->point[1].x, a->point[1].y, 
  138.         a->point[2].x, a->point[2].y);
  139.     if (f)
  140.         fprintf(fp, "\t%d %d %.3f %.3f %.3f\n", f->type, f->style,
  141.             f->thickness, f->wid, f->ht);
  142.     if (b)
  143.         fprintf(fp, "\t%d %d %.3f %.3f %.3f\n", b->type, b->style,
  144.             b->thickness, b->wid, b->ht);
  145.     }
  146.  
  147. write_compound(fp, com)
  148. FILE        *fp;
  149. F_compound    *com;
  150. {
  151.     F_arc        *a;
  152.     F_compound    *c;
  153.     F_ellipse    *e;
  154.     F_line        *l;
  155.     F_spline    *s;
  156.     F_text        *t;
  157.  
  158.     fprintf(fp, "%d %d %d %d %d\n", O_COMPOUND, com->nwcorner.x,
  159.         com->nwcorner.y, com->secorner.x, com->secorner.y);
  160.     for (a = com->arcs; a != NULL; a = a-> next) write_arc(fp, a);
  161.     for (c = com->compounds; c != NULL; c = c-> next) write_compound(fp, c);
  162.     for (e = com->ellipses; e != NULL; e = e-> next) write_ellipse(fp, e);
  163.     for (l = com->lines; l != NULL; l = l-> next) write_line(fp, l);
  164.     for (s = com->splines; s != NULL; s = s-> next) write_spline(fp, s);
  165.     for (t = com->texts; t != NULL; t = t-> next) write_text(fp, t);
  166.     fprintf(fp, "%d\n", O_END_COMPOUND);
  167.     }
  168.  
  169. write_ellipse(fp, e)
  170. FILE        *fp;
  171. F_ellipse    *e;
  172. {
  173.     if( e->radiuses.x == 0 || e->radiuses.y == 0 )
  174.         return;
  175.     
  176.     fprintf(fp, "%d %d %d %d %d %d %d %d %.3f %d %.3f %d %d %d %d %d %d %d %d\n",
  177.         O_ELLIPSE, e->type, e->style, e->thickness, 
  178.         e->color, e->depth, e->pen, e->area_fill,
  179.         e->style_val, e->direction, e->angle,
  180.         e->center.x, e->center.y, 
  181.         e->radiuses.x, e->radiuses.y, 
  182.         e->start.x, e->start.y, 
  183.         e->end.x, e->end.y);
  184.     }
  185.  
  186. write_line(fp, l)
  187. FILE    *fp;
  188. F_line    *l;
  189. {
  190.     F_point    *p;
  191.     F_arrow    *f, *b;
  192.  
  193.     if( l->points == NULL )
  194.         return;
  195. #ifndef TFX
  196.     if (l->type == T_ARC_BOX)
  197.         fprintf(fp, "%d %d %d %d %d %d %d %d %.3f %d %d %d\n",
  198.         O_POLYLINE, l->type, l->style, l->thickness,
  199.         l->color, l->depth, l->pen, l->area_fill, l->style_val, l->radius,
  200.         ((f = l->for_arrow) ? 1 : 0), ((b = l->back_arrow) ? 1 : 0));
  201.     else
  202. #endif TFX
  203.         fprintf(fp, "%d %d %d %d %d %d %d %d %.3f %d %d\n",
  204.         O_POLYLINE, l->type, l->style, l->thickness,
  205.         l->color, l->depth, l->pen, l->area_fill, l->style_val,
  206.         ((f = l->for_arrow) ? 1 : 0), ((b = l->back_arrow) ? 1 : 0));
  207.     if (f)
  208.         fprintf(fp, "\t%d %d %.3f %.3f %.3f\n", f->type, f->style,
  209.             f->thickness, f->wid, f->ht);
  210.     if (b)
  211.         fprintf(fp, "\t%d %d %.3f %.3f %.3f\n", b->type, b->style,
  212.             b->thickness, b->wid, b->ht);
  213.     fprintf(fp, "\t");
  214.     for (p = l->points; p!= NULL; p = p->next) {
  215.         fprintf(fp, " %d %d", p->x, p->y);
  216.         };
  217.     fprintf(fp, " 9999 9999\n");
  218.     }
  219.  
  220. write_spline(fp, s)
  221. FILE        *fp;
  222. F_spline    *s;
  223. {
  224.     F_control    *cp;
  225.     F_point        *p;
  226.     F_arrow        *f, *b;
  227.  
  228.     if( s->points == NULL )
  229.         return;
  230.     fprintf(fp, "%d %d %d %d %d %d %d %d %.3f %d %d\n",
  231.         O_SPLINE, s->type, s->style, s->thickness,
  232.         s->color, s->depth, s->pen, s->area_fill, s->style_val,
  233.         ((f = s->for_arrow) ? 1 : 0), ((b = s->back_arrow) ? 1 : 0));
  234.     if (f)
  235.         fprintf(fp, "\t%d %d %.3f %.3f %.3f\n", f->type, f->style,
  236.             f->thickness, f->wid, f->ht);
  237.     if (b)
  238.         fprintf(fp, "\t%d %d %.3f %.3f %.3f\n", b->type, b->style,
  239.             b->thickness, b->wid, b->ht);
  240.     fprintf(fp, "\t");
  241.     for (p = s->points; p != NULL; p = p->next) {
  242.         fprintf(fp, " %d %d", p->x, p->y);
  243.         };
  244.     fprintf(fp, " 9999 9999\n");  /* terminating code  */
  245.  
  246.     if (s->controls == NULL) return;
  247.     fprintf(fp, "\t");
  248.     for (cp = s->controls; cp != NULL; cp = cp->next) {
  249.         fprintf(fp, " %.3f %.3f %.3f %.3f",
  250.             cp->lx, cp->ly, cp->rx, cp->ry);
  251.         };
  252.     fprintf(fp, "\n");
  253.     }
  254.  
  255. write_text(fp, t)
  256. FILE    *fp;
  257. F_text    *t;
  258. {
  259.     if( t->length == 0 )
  260.         return;
  261.     fprintf(fp, "%d %d %d %d %d %d %d %.3f %d %d %d %d %d %s\1\n", 
  262.         O_TEXT, t->type, t->font, t->size, t->pen,
  263.         t->color, t->depth, t->angle,
  264.         t->style, t->height, t->length, 
  265.         t->base_x, t->base_y, t->cstring);
  266.     }
  267.