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

  1. /*  VERSION 0001    (DATE: 28.III.85)  (TIME: 9:54 am)                  */
  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: qe8
  11.  
  12.     FUNCTIONS: loc,gettext,getline,inject,deltp,puttext,readtext,getc,
  13.             opentext,balloc,error
  14.  
  15.     PURPOSE: get and put text lines into and out of storage
  16.  
  17. */
  18.  
  19. #include "qe.h"
  20.  
  21. loc(line,move)    /*returns line+move, adjusted to be within text*/
  22. int line, move;
  23. {
  24.     int y;
  25.  
  26.     if ( (y=line+move) < 1) y=1;
  27.     if (lastl == UNKNOWN && y > lastread) readtext(y);
  28.     if (y > lastl) return lastl;
  29.     return y;
  30. }
  31.  
  32. gettext(line)    /*makes 'line' the current line*/
  33. int line;
  34. {
  35.     strcpy(text,getline(line));
  36.     cline=line;
  37. }
  38.  
  39. char *getline(line)    /*returns address of text of line 'line' and updates
  40.                 page usage*/
  41. int line;
  42. {
  43.     int slot;
  44.  
  45.     line=loc(line,0);
  46.     if ( (slot=pageloc[tp[line].page]) <= 0)
  47.         slot=swappin(tp[line].page);
  48.     if (usage[slot] > FREE) usage[slot]=++clock;
  49.     return slotaddr[slot] + tp[line].moffset;
  50. }
  51.  
  52. inject(line,txt)    /* inserts 'txt' after 'line' */
  53. int line;
  54. char *txt;
  55. {
  56.     int l, p, balloc();
  57.     struct addr anaddr;
  58.  
  59.     if ( &tp[lastread+2] >= slotaddr[tppages]) {
  60.         /*need another slot to store tp's in */
  61.         if (tppages+2 == slotsinmem) {
  62.             error("Too many lines of text");
  63.             return FAIL;
  64.             }
  65.         if (usage[tppages] == NOTAVAIL) /*ie page is being alloc'ed into*/
  66.             allocp=PAGESIZE+1; /*force alloc into new page*/
  67.         swapout(tppages);
  68.         usage[tppages++]=NOTAVAIL;
  69.         }
  70.     if (++line < ++lastread)
  71.         movmem(&tp[line],&tp[line+1],(lastread-line)*sizeof(anaddr));
  72.     tp[line].moffset=p=balloc(1+strlen(txt));
  73.     tp[line].page=newpage;
  74.     strcpy(p+npaddr,txt);
  75.     if (lastl != UNKNOWN)lastl++;
  76.     return line;
  77. }
  78.  
  79. deltp(from,num)    /*deletes 'num' lines from 'from' onwards by shifting pointers*/
  80. int from, num;
  81. {
  82.     struct addr anaddr;
  83.  
  84.     movmem(&tp[from+num],&tp[from],(lastread+1-(from+num))*sizeof(anaddr));
  85.     if (lastl != UNKNOWN) lastl-=num;
  86.     lastread-=num;
  87.     if (lastl < 1)lastl=1;
  88.     if (lastread < 1)lastread=1;
  89. }
  90.  
  91. puttext()    /*replaces cline's text if it has been altered*/
  92. {
  93.     int p, cp, balloc();
  94.  
  95.     if (altered) {
  96.         if (!trail) {
  97.             for (cp=strlen(text)-1; cp >= 0 && isspace(text[cp]);
  98.                  cp--) text[cp]='\0';
  99.             }
  100.         tp[cline].moffset=p=balloc(1+strlen(text));
  101.         tp[cline].page=newpage;
  102.         strcpy(p+npaddr,text);
  103.         altered=NO;
  104.         }
  105. }
  106.  
  107. readtext(line)    /*reads file being edited into virtual memory until 'line'
  108.           is read, or eof.  If eof, sets lastl to number of last line
  109.           read.  File is assumed to be already open*/
  110. int line;
  111. {
  112.     char txt[LLIM], *t;
  113.     int i, c, l;
  114.  
  115.     while (lastread < line) {
  116.         for (i=0, t=&txt[0]; i < LLIM && ((c=getc(textbuf)) >= ' ' ||
  117.             c != '\n' && c != FAIL && c != ENDFILE); i++)
  118.                 if (c) *(t++)=c;
  119.             else i--;
  120.         if (txt[i-1] == '\r') t--; 
  121.         *t='\0';
  122.         if ( (l=inject(lastread,txt)) == FAIL) goto toomuch;
  123.         else if ((lastread=l)%100 == 0) putlineno(lastread);
  124.         if ( (goteof= c == ENDFILE || c == FAIL) )  goto eof;
  125.         }
  126.     return;
  127.  
  128. eof: 
  129.     fclose(textbuf);
  130. toomuch: 
  131.     lastl=lastread;
  132. }
  133.  
  134. opentext(name)    /*open the file being edited for reading*/
  135. char *name;
  136. {
  137.     if (fopen(name,textbuf) == FAIL) {
  138.         error("Can't open file");
  139.         name[0]='\0';
  140.         lastl=1;
  141.         return FAIL;
  142.         }
  143.     return YES;
  144. }
  145.  
  146. balloc(n)    /*allocate and return the offset in newpage of a vector size n*/
  147. unsigned n;
  148. {
  149.     sint slot;
  150.  
  151.     if (allocp  + n >= PAGESIZE) {
  152.         /*no room in current newpage; get another*/
  153.         if (pageloc[newpage] > 0)usage[pageloc[newpage]]=INUSE;
  154.         pageloc[++newpage]=slot=freememslot();
  155.         usage[slot]=NOTAVAIL;
  156.         allocp=0; 
  157.         npaddr=slotaddr[slot];
  158.         }
  159.     allocp+=n;
  160.     return allocp-n;
  161. }
  162.  
  163. error(message)
  164. char *message;
  165. {
  166.     if (errmess != NULL) return;
  167.     gotoxy(32,0);
  168.     puts(errmess=message);
  169.     resetcursor();
  170.     inbufp=0;
  171. }
  172.  
  173. ifting pointers*/
  174. int from, num;
  175. {
  176.     str