URI: 
       files.c - gramscii - A simple editor for ASCII box-and-arrow charts
   DIR Log
   DIR Files
   DIR Refs
   DIR Tags
   DIR README
   DIR LICENSE
       ---
       files.c (2694B)
       ---
            1 #define _POSIX_C_SOURCE 200112L
            2 
            3 #include <stdio.h>
            4 #include <string.h>
            5 #include "gramscii.h"
            6 
            7 
            8 /** extern declarations **/
            9 
           10 extern lineset_t screen; /* what is visualised */
           11 
           12 extern int WIDTH, HEIGHT;
           13 
           14 extern int force_new;
           15 extern char modified; /* set to 1 if screen modified since last save */
           16 extern char fname[256];
           17 
           18 /*** File management ***/
           19 
           20 void write_file(FILE *fc){
           21         FILE *fout;
           22         int i, ne;
           23 
           24         if (!fname[0] || force_new){
           25                 get_string(fc, "Write to: ", fname, 255);
           26                 if ((fout=fopen(fname, "r"))!=NULL){
           27                         if (!is_yes(get_key(fc,"File exists. Overwrite [y/n]?")) ){
           28                                 fclose(fout);
           29                                 return;
           30                         }
           31                         fclose(fout);
           32                 }
           33         }
           34         if((fout=fopen(fname, "w"))==NULL){
           35                 get_key(fc, "Error opening file.");
           36                 return;
           37         }
           38         ne = 0;
           39         for (i=0; i<HEIGHT; i++){
           40                 if (strlen(screen.l[i].s)){/* remove trailing blank lines */
           41                         /* put the empty lines preceeding the current non-empty one */
           42                         while (ne--)
           43                                 fprintf(fout, "\n");
           44                         fprintf(fout, "%s\n", screen.l[i].s);
           45                         ne = 0;
           46                 }
           47                 else ne++;
           48         }
           49         fclose(fout);
           50         modified = 0;
           51         get_key(fc, "File saved.");
           52         redraw();
           53 }
           54 
           55 void check_modified(FILE *fc){
           56 
           57         if (modified){
           58                 if (!is_yes(get_key(fc, "Unsaved changes. Write to file [y/n]?")) ){
           59                         return;
           60                 }
           61                 write_file(fc);
           62         }
           63 }
           64 
           65 void load_file(FILE *fc){
           66 
           67         char newfname[256];
           68         FILE *fin;
           69         int i;
           70 
           71         get_string(fc, "Load file: ", newfname, 255);
           72         if ((fin=fopen(newfname, "r")) != NULL){
           73                 i = 0;
           74                 while((fgets(screen.l[i].s, WIDTH+1, fin)) != NULL && i<HEIGHT){
           75                         screen.l[i].lst = strlen(screen.l[i].s) - 2;
           76                         screen.l[i].n = i;
           77                         screen.l[i].s[strlen(screen.l[i].s)-1]='\0';
           78                         i++;
           79                 }
           80                 for(;i<HEIGHT; i++){
           81                         erase_line(i);
           82                 }
           83                 fclose(fin);
           84         }
           85         strcpy(fname, newfname);
           86         modified=0;
           87         redraw();
           88 }
           89 
           90 void new_file(FILE *fc){
           91         check_modified(fc);
           92         erase_screen();
           93         go_to(HOME);
           94         redraw();
           95         fname[0] = '\0';
           96         modified=0;
           97 }
           98 
           99 void read_file_at(FILE *fc, int xl, int yl){
          100 
          101         char nfname[512], tmp[512], *fptr, *tptr;
          102         FILE *fin;
          103         int i, j;
          104         char ftype;
          105 
          106         get_string(fc, "Read file: ", nfname, 511);
          107         fptr = nfname;
          108         while(*fptr && _isblank(*fptr))
          109                  fptr ++;
          110         if (*fptr == '!'){
          111                 fin = popen(++fptr, "r");
          112                 ftype = FPIPE;
          113         }
          114         else {
          115                 fin = fopen(fptr, "r");
          116                 ftype = FFILE;
          117         }
          118         if (fin != NULL){
          119                 copy_lines_to_ring(0, HEIGHT-1, PRV_STATE);
          120                 i = yl;
          121                 while((fgets(tmp, WIDTH+1, fin)) != NULL && i<HEIGHT){
          122                         j = xl;
          123                         tptr = tmp;
          124                         if (strlen(tmp))
          125                                 tmp[strlen(tmp) - 1] = '\0';
          126                         ensure_line_length(& (screen.l[i]), xl + strlen(tmp) + 1);
          127                         while (*tptr && j < WIDTH){
          128                                 set_xy(j, i, *tptr);
          129                                 j++;
          130                                 tptr ++;
          131                         }
          132                         i++;
          133                 }
          134                 if (ftype == FFILE)
          135                         fclose(fin);
          136                 else
          137                         pclose(fin);
          138                 modified = 1;
          139                 redraw();
          140                 copy_lines_to_ring(yl, i-1, NEW_STATE);
          141         }
          142 }