home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 158_01 / qe5 < prev    next >
Text File  |  1987-10-12  |  5KB  |  231 lines

  1. /*  VERSION 0002    (DATE: 23.VII.85)  (TIME: 3:35PM)                   */
  2. /*
  3.     e (qe) screen editor
  4.  
  5.     (C) G. Nigel Gilbert, MICROLOGY, 1981
  6.  
  7.     August-December 1981
  8.     Modified: To QE from e (ver 4.6a) by J.W. Haefner -- Oct 84-Mar 85
  9.  
  10.     FILE: qe5
  11.  
  12.     FUNCTIONS: readfile, writefile, exists, scans, retag, format
  13.  
  14.     PURPOSE: read and write files
  15.  
  16. */
  17.  
  18. #include "qe.h"
  19. #define ESC    0x1b
  20. #define GS    0x1d
  21.  
  22. readfile(name)    /* read file 'name' and insert it after cursor*/
  23. char *name;
  24. {
  25.     int c, i, j, k, line, first, res;
  26.     char textb[LLIM];
  27.  
  28.     if (fopen(name,fbuf) == FAIL) {
  29.         error("Can't find file");
  30.         name[0]='\0'; 
  31.         return(FAIL);
  32.         }
  33.     first=YES;
  34.     i=charn; 
  35.     line=cline;
  36.     for (j=charn, k=0; (textb[k]=text[j]); j++, k++);
  37.     do {
  38.         while (i < LLIM && (c=getc(fbuf)) != FAIL && c != '\n'
  39.         && c != ENDFILE)
  40.          text[i++]=c;
  41.         if (text[i-1] == '\r') i--;
  42.         text[i]='\0';
  43.         if (c == ENDFILE) for (k=0; (text[i]=textb[k]) && i < LLIM; i++, k++);
  44.         if (first) {
  45.             altered=YES;
  46.             puttext();
  47.             first=NO;
  48.             res=YES;
  49.             }
  50.         else {
  51.             res=inject(line,text);
  52.             line++;
  53.             }
  54.         i=0;
  55.         } while (c != FAIL && res != FAIL && c != ENDFILE);
  56.     fclose(fbuf);
  57.     gettext(cline);
  58. }
  59.  
  60. writefile(from,to,name,nametoprint,exiting)
  61. int from, to, exiting;
  62. char *name, *nametoprint;
  63. {
  64.     int l, copying, copysecstart,copybytestart;
  65.     char c, *t, *getline();
  66.  
  67.     puttext();
  68.     if (name[0] <= ' ') {
  69.         error("Bad name");
  70.         return FAIL;
  71.         }
  72.     if (fcreat(name,fbuf) == FAIL) {
  73.         error("Can't write file - directory full?");
  74.         return(FAIL);
  75.         }
  76.     if (expert) xprtmess("Saving ");
  77.     else putmess("Saving ");
  78.     puts(nametoprint);
  79.     copying=NO;
  80.     if (exiting) to=lastread;
  81.     else if (to == lastl) to=loc(lastl,0);
  82.     for (l=from; l <= to; ) {
  83.         t=getline(l++);
  84.         while (*t) if(putc(*t++,fbuf) == FAIL) goto diskfull;
  85.         if (l <= to ) {
  86.             if (putc('\r',fbuf) == FAIL) goto diskfull;
  87.             if (putc('\n',fbuf) == FAIL) goto diskfull;
  88.             }
  89.         }
  90.     if (exiting && (lastl == UNKNOWN) || !goteof) {
  91.         copysecstart=tell(textbuf->_fd) -
  92.         (textbuf->_nleft + SECSIZ)/SECSIZ;
  93.         copybytestart=SECSIZ-(textbuf->_nleft + SECSIZ)%SECSIZ;
  94.         if (putc('\r',fbuf) == FAIL) goto diskfull;
  95.         if (putc('\n',fbuf) == FAIL) goto diskfull;
  96.         copying=YES;
  97.         while ( (c=getc(textbuf)) != ENDFILE && c != FAIL)
  98.             if (putc(c,fbuf) == FAIL) goto diskfull;
  99.         }
  100.     if (putc(ENDFILE,fbuf) == FAIL)goto diskfull;
  101.     if (fflush(fbuf) == FAIL) goto diskfull;
  102.     if (fclose(fbuf) == FAIL) {
  103.         error("Can't close file");
  104.         goto reposition;
  105.         }
  106.     if (copying) fclose(textbuf);
  107.     return YES;
  108. diskfull:
  109.     error("Disk full");
  110. reposition:
  111.     if (copying) {
  112.         fclose(fbuf);
  113.         funlink(name);
  114.         textbuf->_nleft=0;
  115.         while (copybytestart--) getc(textbuf);
  116.         }
  117.     return FAIL;
  118. }
  119.  
  120. exists(name)
  121. char *name;
  122. {
  123.     char c;
  124.  
  125.     c='y';
  126.     if (checkexists(name)) {
  127.         if (expert) xprtmess("Replace ");
  128.         else putmess("OK to replace ");
  129.         puts(name); 
  130.         puts(" ? ");
  131.         putch((c=getlow()));
  132.         putret(); 
  133.         }
  134.     return c == 'y';
  135. }
  136.  
  137. checkexists(name)    /*return YES if file 'name' exists, else NO */
  138. char *name;
  139. {
  140.     int fd;
  141.     
  142.     if (dskcheck(setjmp(dskerr)) != 0 || (fd=open(name,0)) == FAIL) return NO;
  143.     close(fd);
  144.     return YES;
  145. }
  146.  
  147. scans(answer,maxlen)
  148. char *answer;
  149. int maxlen;
  150. {
  151.     char c;
  152.     int n, i;
  153.  
  154.     maxlen--;
  155.     n=0;
  156.     while (n < maxlen) {
  157.         switch((c=getscankey())) {
  158.         case BACKSP        :
  159.         case DELLEFT_P    :
  160.             if (n) {
  161.                 putch(BACKSP); 
  162.                 putch(' ');
  163.                 putch(BACKSP);
  164.                 n--; 
  165.                 answer--;
  166.                 }
  167.             break;
  168.         case ESCKEY_P    :
  169.         case CR_P        :
  170.             n=maxlen; 
  171.             break;
  172.         case LEFTKEY_P    :
  173.             c = tran[LEFTKEY];
  174.             goto passthru;
  175.             break;
  176.         case RETRIEVE_P    :
  177.             /*if (n == 0) {*/
  178.             if (*answer) {
  179.                 while (*answer) {
  180.                     dispch(*answer++);
  181.                     n++;
  182.                     }
  183.                 break;
  184.                 }
  185.             else {
  186.                 c = tran[RETRIEVE];
  187.                 goto passthru;
  188.                 }
  189.         case GS    :                /* kludge to permit ESC in find/alter */
  190.             c=ESC;
  191. passthru:    default        :
  192.             dispch(c);
  193.             *answer++=c; 
  194.             n++;
  195.             break;
  196.             }
  197.         }
  198.     *answer='\0';
  199.     putret();
  200.     return (c & ~PARBIT);        /* maintain compatibility with other calls*/
  201. }
  202.  
  203. retag(name,tag)    /*puts a new suffix on a file name*/
  204. char *name, *tag;
  205. {
  206.     for (; *name && *name != '.'; name++);
  207.     if (!*name)*name='.';
  208.     for (++name; (*name=*tag); name++, tag++);
  209. }
  210.  
  211. format(name)
  212. char *name;
  213. {
  214.     char tempname[FILELEN], *n, *tn;
  215.  
  216.     if (!*name) return;
  217.     if (*(name+1) == ':') strcpy(tempname,name);
  218.     else {
  219.         tempname[0]=curdsk+'A';
  220.         tempname[1]=':';
  221.         name[12]='\0';
  222.         strcpy(&tempname[2],name);
  223.         }
  224.     for (n=name,tn=tempname; (*n=toupper(*tn++)); n++) if (*n == '.') break;
  225.     while (*n++) *n=tolower(*tn++);
  226. }
  227. 
  228.     if (copying) {
  229.         fclose(fbuf);
  230.         funlink(name);
  231.         textb