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

  1. /*  VERSION 0057  (DATE: 28/04/87)  (TIME: 23:01)  */
  2. /*
  3.     e (qed) screen editor
  4.  
  5.     (C) G. Nigel Gilbert, MICROLOGY, 1981
  6.  
  7.     August-December 1981
  8.  
  9.     Modified: Aug-Dec    1984:    BDS-C 'e'(vers 4.6a) to 'qe' (J.W. Haefner)
  10.               March        1985:    BDS-C 'qe' to DeSmet-C 'qed' (J.W. Haefner)
  11.  
  12.     FILE: qed8
  13.  
  14.     FUNCTIONS: loc,gettext,getline,inject,deltp,puttext,readtext,getc,
  15.             opentext,balloc,error
  16.  
  17.     PURPOSE: get and put text lines into and out of storage
  18.  
  19. */
  20.  
  21. #include "qed.h"
  22.  
  23. loc(line,move)    /*returns line+move, adjusted to be within text*/
  24. int line, move;
  25. {
  26.     int y,nl;
  27.  
  28.     if ( (y=line+move) < 1) y=1;
  29.     if (lastl == UNKNOWN && y > lastread) readtext(y);
  30.     if (y > lastl) return lastl;
  31.     return y;
  32. }
  33.  
  34. gettext(line)    /*makes 'line' the current line*/
  35. int line;
  36. {
  37.     char *getline();
  38.  
  39.     strcpy(text,getline(line));
  40.     cline=line;
  41. }
  42.  
  43. char *getline(line)    /*returns address of text of line 'line' and updates*/
  44. int line;
  45.                         /*page usage*/
  46. {
  47.     int slot;
  48.  
  49.     line=loc(line,0);
  50.     if ( (slot=pageloc[tp[line].page]) <= 0)
  51.         slot=swappin(tp[line].page);
  52.     if (usage[slot] > FREE) usage[slot]=++clock;
  53.     return slotaddr[slot] + tp[line].moffset;
  54. }
  55.  
  56. inject(line,txt)    /* inserts 'txt' after 'line',split big files */
  57. int line;
  58. char *txt;
  59. {
  60.     int l, p, balloc();
  61.     struct addr anaddr;
  62.     char tempn[FILELEN],split;
  63.  
  64.     if ( &tp[lastread+2] >= slotaddr[tppages]) {
  65.         /*need another slot to store tp's in */
  66.          if (tppages+2 >= slotsinmem) {   /*org: "tppages+2"*/
  67.             /*error("Too many lines of text");*/
  68.             if(expert)xprtmess("Too many lines. Split? ");
  69.             else putmess("Too many lines of text. Split file? ");
  70.             if((split=getlow())=='n') return FAIL;
  71.             else {
  72.                 do {
  73.                     if(expert)xprtmess("File: ");
  74.                     else putmess("Split file name: ");
  75.                     scans(name,15);
  76.                   } while ( (!exists(name)) || (fcreat(name,fbuf)==FAIL) );
  77.                   writefile(1,line-2,name,name,NO);
  78.                 /*if(exists(name)) writefile(1,line-2,name,name,NO);*/
  79.                   /*delete lines just saved*/
  80.                 deltp(1,line-2);
  81.                 changed=YES;
  82.                 l=strlen(getline(1));
  83.                   /*recover used tp space*/
  84.                   /*assume two hanging lines fit in one page*/
  85.                 strcpy((slotaddr[1]),getline(1));
  86.                 strcpy((slotaddr[1]+l+1),getline(2));
  87.                   /*loc of the two lines*/
  88.                 tp[1].moffset=0;
  89.                 tp[1].page=1;
  90.                 tp[2].moffset=l+1;
  91.                 tp[2].page=1;
  92.                   /*start of next spot in current page*/
  93.                 allocp=l+strlen(slotaddr[1]+tp[2].moffset)+3;
  94.                 tppages=1;
  95.                 newpage=1;
  96.                 pageloc[0]=0;
  97.                 pageloc[1]=1;
  98.                 npaddr=slotaddr[1];
  99.                 usage[0]=NOTAVAIL;
  100.                 usage[1]=INUSE;
  101.                 for(l=2;l<slotsinmem;l++) usage[l]=FREE;
  102.                 close(pagefd);
  103.                 pagefd=NOFILE;
  104.                 funlink(pagingfile);
  105.                 line=lastread=2;
  106.                 inbuf[inbufp++]=tran[BOFKEY];    /*goto beginning of file*/
  107.                 goto noswap;
  108.             }
  109.         }
  110.         if (usage[tppages] == NOTAVAIL) /*ie page is being alloc'ed into*/
  111.             allocp=PAGESIZE+1; /*force alloc into new page*/
  112.         swapout(tppages);
  113.         usage[tppages++]=NOTAVAIL;
  114.         }
  115. noswap:
  116.     if (++line < ++lastread)
  117.           /*movmem(&tp[line],&tp[line+1],(lastread-line)*sizeof(anaddr));*/
  118.         _move((lastread-line)*sizeof(anaddr),&tp[line],&tp[line+1]);
  119.     tp[line].moffset=p=balloc(1+strlen(txt));
  120.     tp[line].page=newpage;
  121.     strcpy(p+npaddr,txt);
  122.     if (lastl != UNKNOWN)lastl++;
  123.     return line;
  124. }
  125.  
  126. deltp(from,num)    /*deletes 'num' lines from 'from' onwards by shifting pointers*/
  127. int from, num;
  128. {
  129.     struct addr anaddr;
  130.  
  131. /*movmem(&tp[from+num],&tp[from],(lastread+1-(from+num))*sizeof(anaddr));*/
  132.     _move((lastread+1-(from+num))*sizeof(anaddr),&tp[from+num],&tp[from]);
  133.     if (lastl != UNKNOWN) lastl-=num;
  134.     lastread-=num;
  135.     if (lastl < 1)lastl=1;
  136.     if (lastread < 1)lastread=1;
  137. }
  138.  
  139. puttext()    /*replaces cline's text if it has been altered*/
  140. {
  141.     int p, cp, balloc();
  142.  
  143.     if (altered) {
  144.         if (!trail) {
  145.             for (cp=strlen(text)-1; cp >= 0 && isspace(text[cp]);
  146.                  cp--) text[cp]='\0';
  147.             }
  148.         tp[cline].moffset=p=balloc(1+strlen(text));
  149.         tp[cline].page=newpage;
  150.         strcpy(p+npaddr,text);
  151.         altered=NO;
  152.         }
  153. }
  154.  
  155. readtext(line)    /*reads file being edited into virtual memory until 'line'
  156.           is read, or eof.  If eof, sets lastl to number of last line
  157.           read.  File is assumed to be already open*/
  158. int line;
  159. {
  160.     char txt[LLIM], *t;
  161.     int i, c, l;
  162.  
  163.     while (lastread < line) {
  164.         for (i=0, t=&txt[0]; i < LLIM &&
  165. #if CPM
  166.            (
  167.             ((c=egetc(textbuf)) >= ' ' && c != ENDFILE)
  168.              ||
  169.             c != '\n' && c != DFAIL && c != ENDFILE
  170. #endif
  171. #if DOS2            /*28.iv.87: do not stop on ^Z*/
  172.             ((c=egetc(textbuf)) != ENDFILE && c!='\n' && c!=DFAIL
  173. #endif
  174.            ); i++)
  175.                 if (c) *(t++)=c;
  176.                 else i--;
  177.         if (txt[i-1] == '\r') t--;
  178.         *t='\0';
  179.         if ( (l=inject(lastread,txt)) == FAIL) goto toomuch;
  180.         else if ((lastread=l)%100 == 0) putlineno(lastread);
  181.         if ( (goteof= ((c==ENDFILE) || (c==DFAIL))) ) goto eof;
  182.         }
  183.     return;
  184.  
  185. eof: 
  186.     fclose(textbuf);
  187. toomuch: 
  188.     lastl=lastread;
  189. }
  190.  
  191. opentext(name)    /*open the file being edited for reading*/
  192. char *name;
  193. {
  194.     if (fopen(name,textbuf) == FAIL) {
  195.             /* attempt to open with default extnsion added */
  196.         if (fopen(strcat(name,defext),textbuf) == FAIL) {
  197.             error("Can't open file");
  198.             name[0]='\0';
  199.             lastl=1;
  200.             return FAIL;
  201.             }
  202.         }
  203.     return YES;
  204. }
  205.  
  206. balloc(n)    /*allocate and return the offset in newpage of a vector size n*/
  207. unsigned n;
  208. {
  209.     sint slot;
  210.  
  211.     if (allocp  + n >= PAGESIZE) {
  212.         /*no room in current newpage; get another*/
  213.         if (pageloc[newpage] > 0)usage[pageloc[newpage]]=INUSE;
  214.         pageloc[++newpage]=slot=freememslot();
  215.         usage[slot]=NOTAVAIL;
  216.         allocp=0; 
  217.         npaddr=slotaddr[slot];
  218.         }
  219.     allocp+=n;
  220.     return allocp-n;
  221. }
  222.  
  223. error(message)
  224. char *message;
  225. {
  226.     if (errmess != NULL) return;
  227.     gotoxy(32,0);
  228.     putstr(errmess=message);
  229.     resetcursor();
  230.     inbufp=0;
  231. }
  232.  
  233.