home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 100-199 / ff197.lzh / Stevie / fileio.c < prev    next >
C/C++ Source or Header  |  1989-03-28  |  6KB  |  254 lines

  1. /*
  2.  * STEVIE - Simply Try this Editor for VI Enthusiasts
  3.  *
  4.  * Code Contributions By : Tim Thompson           twitch!tjt
  5.  *                         Tony Andrews           onecom!wldrdg!tony 
  6.  *                         G. R. (Fred) Walter    watmath!watcgl!grwalter 
  7.  */
  8.  
  9. #include "stevie.h"
  10.  
  11. void
  12. filemess(s)
  13.     char           *s;
  14. {
  15.     sprintf(IObuff, "\"%s\" %s", ((Filename == NULL) ? "" : Filename), s);
  16.     msg(IObuff);
  17. }
  18.  
  19. void
  20. renum()
  21. {
  22.     LPtr           *p;
  23.     unsigned long   l = 0;
  24.  
  25.     for (p = Filemem; p != NULL; p = nextline(p), l += LINEINC)
  26.     p->linep->num = l;
  27.  
  28.     Fileend->linep->num = 0xffffffffL;
  29. }
  30.  
  31. #ifdef    MEGAMAX
  32. overlay "fileio"
  33. #endif
  34.  
  35. bool_t
  36. readfile(fname, fromp, nochangename)
  37.     char           *fname;
  38.     LPtr           *fromp;
  39.     bool_t          nochangename;    /* if TRUE, don't change the Filename */
  40. {
  41.     FILE           *f, *fopen();
  42.     LINE           *curr;
  43.     char            buf2[80];
  44.     int             c;
  45.     int             IObuffsize = 0;
  46.     long            nchars = 0;
  47.     int             linecnt = 0;
  48.     bool_t          wasempty = bufempty();
  49.     int             nonascii = 0;    /* count garbage characters */
  50.     int             nulls = 0;    /* count nulls */
  51.     bool_t          incomplete = FALSE;    /* was the last line incomplete? */
  52.     bool_t          toolong = FALSE;    /* a line was too long */
  53.  
  54.     curr = fromp->linep;
  55.  
  56.     if (!nochangename)
  57.     Filename = strsave(fname);
  58.  
  59.     f = fopen(fname, "r");
  60.     if (f == NULL)
  61.     return TRUE;
  62.  
  63.     filemess("");
  64.  
  65.     do {
  66.     c = getc(f);
  67.  
  68.     if (c == EOF) {
  69.         if (IObuffsize == 0)/* normal loop termination */
  70.         break;
  71.  
  72.         /*
  73.          * If we get EOF in the middle of a line, note the fact and
  74.          * complete the line ourselves. 
  75.          */
  76.         incomplete = TRUE;
  77.         c = NL;
  78.     }
  79.     if (c >= 0x80) {
  80.         c -= 0x80;
  81.         nonascii++;
  82.     }
  83.     /*
  84.      * If we reached the end of the line, OR we ran out of space for it,
  85.      * then process the complete line. 
  86.      */
  87.     if (c == NL || IObuffsize == (IOSIZE - 1)) {
  88.         LINE           *lp;
  89.  
  90.         if (c != NL)
  91.         toolong = TRUE;
  92.  
  93.         IObuff[IObuffsize++] = NUL;
  94.         if ((lp = newline(IObuffsize)) == NULL) {
  95.         fprintf(stderr, "not enough memory - should never happen");
  96.         getout(1);
  97.         }
  98.         strcpy(lp->s, IObuff);
  99.  
  100.         curr->next->prev = lp;    /* new line to next one */
  101.         lp->next = curr->next;
  102.  
  103.         curr->next = lp;    /* new line to prior one */
  104.         lp->prev = curr;
  105.  
  106.         curr = lp;        /* new line becomes current */
  107.         IObuffsize = 0;
  108.         linecnt++;
  109.     } else if (c == NUL) {
  110.         nulls++;        /* count and ignore nulls */
  111.     } else {
  112.         IObuff[IObuffsize++] = (char) c;    /* normal character */
  113.     }
  114.  
  115.     nchars++;
  116.     } while (!incomplete && !toolong);
  117.  
  118.     fclose(f);
  119.  
  120.     /*
  121.      * If the buffer was empty when we started, we have to go back and remove
  122.      * the "dummy" line at Filemem and patch up the ptrs. 
  123.      */
  124.     if (wasempty && linecnt != 0) {
  125.     LINE           *dummy = Filemem->linep;    /* dummy line ptr */
  126.  
  127.     free(dummy->s);        /* free string space */
  128.     Filemem->linep = Filemem->linep->next;
  129.     free((char *) dummy);    /* free LINE struct */
  130.     Filemem->linep->prev = Filetop->linep;
  131.     Filetop->linep->next = Filemem->linep;
  132.  
  133.     Curschar->linep = Filemem->linep;
  134.     Topchar->linep = Filemem->linep;
  135.     }
  136.     renum();
  137.  
  138.     if (toolong) {
  139.     sprintf(IObuff, "\"%s\" Line too long", fname);
  140.     msg(IObuff);
  141.     return FALSE;
  142.     }
  143.     sprintf(IObuff, "\"%s\" %s%d line%s, %ld character%s",
  144.         fname,
  145.         incomplete ? "[Incomplete last line] " : "",
  146.         linecnt, (linecnt > 1) ? "s" : "",
  147.         nchars, (nchars > 1) ? "s" : "");
  148.  
  149.     buf2[0] = NUL;
  150.  
  151.     if (nonascii || nulls) {
  152.     if (nonascii) {
  153.         if (nulls)
  154.         sprintf(buf2, " (%d null, %d non-ASCII)",
  155.             nulls, nonascii);
  156.         else
  157.         sprintf(buf2, " (%d non-ASCII)", nonascii);
  158.     } else
  159.         sprintf(buf2, " (%d null)", nulls);
  160.     }
  161.     strcat(IObuff, buf2);
  162.     msg(IObuff);
  163.  
  164.     return FALSE;
  165. }
  166.  
  167. /*
  168.  * writeit - write to file 'fname' lines 'start' through 'end' 
  169.  *
  170.  * If either 'start' or 'end' contain null line pointers, the default is to use
  171.  * the start or end of the file respectively. 
  172.  */
  173. bool_t
  174. writeit(fname, start, end)
  175.     char           *fname;
  176.     LPtr           *start, *end;
  177. {
  178.     FILE           *f, *fopen();
  179.     FILE           *fopenb();    /* open in binary mode, where needed */
  180.     char           *backup, *s;
  181.     long            nchars;
  182.     int             lines;
  183.     LPtr           *p;
  184.  
  185.     sprintf(IObuff, "\"%s\"", fname);
  186.     msg(IObuff);
  187.  
  188.     /*
  189.      * Form the backup file name - change foo.* to foo.bak 
  190.      */
  191.     backup = alloc((unsigned) (strlen(fname) + 5));
  192.     strcpy(backup, fname);
  193.     for (s = backup; *s && *s != '.'; s++);
  194.     *s = NUL;
  195.     strcat(backup, ".bak");
  196.  
  197.     /*
  198.      * Delete any existing backup and move the current version to the backup.
  199.      * For safety, we don't remove the backup until the write has finished
  200.      * successfully. And if the 'backup' option is set, leave it around. 
  201.      */
  202.     rename(fname, backup);
  203.  
  204.     f = P(P_CR) ? fopen(fname, "w") : fopenb(fname, "w");
  205.  
  206.     if (f == NULL) {
  207.     emsg("Can't open file for writing!");
  208.     free((char *) backup);
  209.     return FALSE;
  210.     }
  211.     /*
  212.      * If we were given a bound, start there. Otherwise just start at the
  213.      * beginning of the file. 
  214.      */
  215.     if (start == NULL || start->linep == NULL)
  216.     p = Filemem;
  217.     else
  218.     p = start;
  219.  
  220.     lines = 0;
  221.     nchars = 0;
  222.     do {
  223.     fprintf(f, "%s\n", p->linep->s);
  224.     nchars += strlen(p->linep->s) + 1;
  225.     lines++;
  226.  
  227.     /*
  228.      * If we were given an upper bound, and we just did that line, then
  229.      * bag it now. 
  230.      */
  231.     if (end != NULL && end->linep != NULL) {
  232.         if (end->linep == p->linep)
  233.         break;
  234.     }
  235.     } while ((p = nextline(p)) != NULL);
  236.  
  237.     fclose(f);
  238.     sprintf(IObuff, "\"%s\" %d line%s, %ld character%s", fname,
  239.         lines, (lines > 1) ? "s" : "",
  240.         nchars, (nchars > 1) ? "s" : "");
  241.     msg(IObuff);
  242.     UNCHANGED;
  243.  
  244.     /*
  245.      * Remove the backup unless they want it left around 
  246.      */
  247.     if (!P(P_BK))
  248.     remove(backup);
  249.  
  250.     free((char *) backup);
  251.  
  252.     return TRUE;
  253. }
  254.