home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 133_01 / e5 < prev    next >
Text File  |  1985-03-10  |  4KB  |  207 lines

  1. /*
  2.     e screen editor
  3.  
  4.     (C) G. Nigel Gilbert, MICROLOGY, 1981
  5.  
  6.     August-December 1981
  7.  
  8.     FILE: e5
  9.  
  10.     FUNCTIONS: readfile, writefile, exists, scans, retag, format
  11.  
  12.     PURPOSE: read and write files
  13.  
  14. */
  15.  
  16. #include "e.h"
  17.  
  18. readfile(name)    /* read file 'name' and insert it after current line*/
  19. char *name;
  20. {
  21.     int c, i, line;
  22.  
  23.     if (fopen(name,fbuf) == FAIL) {
  24.         error("Can't find file");
  25.         name[0]='\0'; 
  26.         return(FAIL);
  27.         }
  28.     puttext();
  29.     line=cline;
  30.     do {
  31.         for (i=0; i < LLIM && (c=getc(fbuf)) != FAIL && c != '\n'
  32.             && c != ENDFILE; ) text[i++]=c;
  33.         if (text[i-1] == '\r') i--;
  34.         text[i]='\0';
  35.         if (line%10 == 0) putlineno(line);
  36.         } while (inject(line++,text) != FAIL && c != FAIL && c != ENDFILE);
  37.     fclose(fbuf);
  38.     gettext(cline);
  39. }
  40.  
  41. writefile(from,to,name,nametoprint,exiting)
  42. int from, to, exiting;
  43. char *name, *nametoprint;
  44. {
  45.     int l, copying, copysecstart,copybytestart;
  46.     char c, *t, *getline();
  47.  
  48.     puttext();
  49.     if (name[0] <= ' ') {
  50.         error("Bad name");
  51.         return FAIL;
  52.         }
  53.     if (fcreat(name,fbuf) == FAIL) {
  54.         error("Can't write file - directory full?");
  55.         return(FAIL);
  56.         }
  57.     putmess("Saving "); 
  58.     puts(nametoprint);
  59.     copying=NO;
  60.     if (exiting) to=lastread;
  61.     else if (to == lastl) to=loc(lastl,0);
  62.     for (l=from; l <= to; ) {
  63.         t=getline(l++);
  64.         while (*t) if (putc(*t++,fbuf) == FAIL) goto diskfull;
  65.         if (l <= to ) {
  66.             if (putc('\r',fbuf) == FAIL) goto diskfull;
  67.             if (putc('\n',fbuf) == FAIL) goto diskfull;
  68.             }
  69.         }
  70.     if (exiting && (lastl == UNKNOWN) || !goteof) {
  71.         copysecstart=tell(textbuf->_fd) -
  72.         (textbuf->_nleft + SECSIZ)/SECSIZ;
  73.         copybytestart=SECSIZ-(textbuf->_nleft + SECSIZ)%SECSIZ;
  74.         if (putc('\r',fbuf) == FAIL) goto diskfull;
  75.         if (putc('\n',fbuf) == FAIL) goto diskfull;
  76.         copying=YES;
  77.         while ( (c=getc(textbuf)) != ENDFILE && c != FAIL)
  78.             if (putc(c,fbuf) == FAIL) goto diskfull;
  79.         }
  80.     if (putc(ENDFILE,fbuf) == FAIL)goto diskfull;
  81.     if (fflush(fbuf) == FAIL) goto diskfull;
  82.     if (fclose(fbuf) == FAIL) {
  83.         error("Can't close file");
  84.         goto reposition;
  85.         }
  86.     if (copying) fclose(textbuf);
  87.     return YES;
  88. diskfull:
  89.     error("Disk full");
  90. reposition:
  91.     if (copying) {
  92.         fclose(fbuf); 
  93.         funlink(name);
  94.         seek(textbuf->_fd,copysecstart,0);
  95.         textbuf->_nleft=0;
  96.         while (copybytestart--) getc(textbuf);
  97.         }
  98.     return FAIL;
  99. }
  100.  
  101. exists(name)
  102. char *name;
  103. {
  104.     char c;
  105.  
  106.     c='y';
  107.     if (checkexists(name)) {
  108.         putmess("OK to replace "); 
  109.         puts(name); 
  110.         puts(" ? ");
  111.         putch((c=getlow()));
  112.         putret(); 
  113.         }
  114.     return c == 'y';
  115. }
  116.  
  117. checkexists(name)    /*return YES if file 'name' exists, else NO */
  118. char *name;
  119. {
  120.     int fd;
  121.     
  122.     if (dskcheck(setjmp(dskerr)) != 0 || (fd=open(name,0)) == FAIL) return NO;
  123.     close(fd);
  124.     return YES;
  125. }
  126.  
  127. scans(answer,maxlen)
  128. char *answer;
  129. int maxlen;
  130. {
  131.     char c, getscankey();
  132.     int n, i;
  133.  
  134.     maxlen--;
  135.     n=0;
  136.     while (n < maxlen) {
  137.         switch((c=getscankey())) {
  138.         case LEFTKEY    :
  139.         case DELLEFT    :
  140.             if (n) {
  141.                 putch(BACKSP); 
  142.                 putch(' ');
  143.                 putch(BACKSP);
  144.                 n--; 
  145.                 answer--;
  146.                 }
  147.             break;
  148.         case CR        :
  149.         case ESCKEY    :
  150.             n=maxlen; 
  151.             break;
  152.         case RETRIEVE    :
  153.             if (n == 0) {
  154.                 while (*answer) {
  155.                     dispch(*answer++);
  156.                     n++;
  157.                     }
  158.                 break;
  159.                 }
  160.         default        :   
  161.             dispch(c); 
  162.             *answer++=c; 
  163.             n++;
  164.             break;
  165.             }
  166.         }
  167.     *answer='\0';
  168.     putret();
  169.     return c;
  170. }
  171.  
  172. retag(name,tag)    /*puts a new suffix on a file name*/
  173. char *name, *tag;
  174. {
  175.     for (; *name && *name != '.'; name++);
  176.     if (!*name)*name='.';
  177.     for (++name; (*name=*tag); name++, tag++);
  178. }
  179.  
  180. format(name)    /*puts filename 'name' into a standard format, adding
  181.         disk drive if not supplied.  Accepts user area numbers
  182.         Output format is UU/D:FFFFFFFF.xxx   */
  183. char *name;
  184. {
  185.     char tempname[FILELEN], *cn, *ct;
  186.     int n, t;
  187.     
  188.     if (!*name) return;
  189.     for (n=t=0; isdigit(name[n]); n++) tempname[n]=name[n];    /*copy user area*/
  190.     if (n && name[n] == '/') {
  191.         tempname[n]='/';
  192.         t=++n;
  193.         }
  194.     if (name[n+1] != ':') {                /*insert drive */
  195.         tempname[t++]= curdsk+'A';
  196.         tempname[t++]=':';
  197.         }
  198.     for (; t < FILELEN && (tempname[t]=name[n]); t++, n++); /*copy rest*/
  199.  
  200.     for (cn=name,ct=tempname; (*cn=toupper(*ct++)); cn++) /*upcase all but extn */
  201.         if (*cn == '.') break;
  202.     while (*cn++) *cn=tolower(*ct++);
  203. }
  204. start,0);
  205.         textbuf->_nleft=0;
  206.         while (copybytestart--) getc(textbuf);
  207.         }