home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / SYSPC22.ZIP / TEXTRET.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1986-12-31  |  4.7 KB  |  218 lines

  1. overlay procedure reloadtext (sector:integer; var q:message);
  2. var k:char;
  3.     sectorptr,tmp,n:integer;
  4.     buff:buffer;
  5.     x:boolean;
  6.  
  7.   procedure setbam (sector,val:integer);
  8.   begin
  9.     seek (mapfile,sector);
  10.     write (mapfile,val)
  11.   end;
  12.  
  13.   procedure chk;
  14.   begin
  15.     iocode:=ioresult;
  16.     if iocode<>0 then writeln (usr,'(Error ',iocode,' reading message)')
  17.   end;
  18.  
  19. begin
  20.   sectorptr:=32767;
  21.   n:=1;
  22.   q.text[1]:='';
  23.   repeat
  24.     if sectorptr>sectorsize then begin
  25.       if sector<0 then exit;
  26.       seek (tfile,sector); chk;
  27.       read (tfile,buff); chk;
  28.       seek (mapfile,sector); chk;
  29.       read (mapfile,tmp); chk;
  30.       if tmp=-2 then begin
  31.         tmp:=-1;
  32.         seek (mapfile,sector); chk;
  33.         write (mapfile,tmp); chk;
  34.       end;
  35.       sector:=tmp;
  36.       sectorptr:=1
  37.     end;
  38.     k:=buff[sectorptr];
  39.     case k of
  40.       #0,#10:;
  41.       #13:if n>=maxmessagesize
  42.             then k:=#0
  43.             else begin
  44.               n:=n+1;
  45.               q.text[n]:=''
  46.             end
  47.       else q.text[n]:=q.text[n]+k
  48.     end;
  49.     sectorptr:=sectorptr+1
  50.   until k=#0;
  51.   q.numlines:=n;
  52.   chk
  53. end;
  54.  
  55. overlay procedure deletetext (sector:integer);
  56. var next:integer;
  57.  
  58.   procedure setbam (sector,val:integer);
  59.   begin
  60.     seek (mapfile,sector);
  61.     write (mapfile,val)
  62.   end;
  63.  
  64. begin
  65.   while sector>=0 do begin
  66.     seek (mapfile,sector);
  67.     read (mapfile,next);
  68.     setbam (sector,-2);
  69.     sector:=next
  70.   end
  71. end;
  72.  
  73. overlay function maketext (var q:message):integer;
  74. var line,pos,sector,prev:integer;
  75.     bufptr:integer;
  76.     curline:anystr;
  77.     k:char;
  78.     buff:buffer;
  79.  
  80.   procedure setbam (sector,val:integer);
  81.   begin
  82.     seek (mapfile,sector);
  83.     write (mapfile,val)
  84.   end;
  85.  
  86.   function nextblank (first:integer; linkit:boolean):integer;
  87.   var cnt,i,blank:integer;
  88.   begin
  89.     nextblank:=-1;
  90.     if first<-1 then first:=-1;
  91.     if first>=numsectors then exit;
  92.     seek (mapfile,first+1);
  93.     for cnt:=first+1 to numsectors do begin
  94.       read (mapfile,i);
  95.       if i=-2 then begin
  96.         blank:=cnt;
  97.         if (first>=0) and linkit then setbam (first,blank);
  98.         nextblank:=blank;
  99.         exit
  100.       end
  101.     end
  102.   end;
  103.  
  104.   function firstblank:integer;
  105.   begin
  106.     firstblank:=nextblank (-1,false)
  107.   end;
  108.  
  109.   procedure ensuretfilesize (sector:integer);
  110.   var cnt:integer;
  111.       buff:buffer;
  112.   begin
  113.     if sector<filesize(tfile) then exit;
  114.     if (sector<0) or (sector>numsectors) then exit;
  115.     fillchar (buff,sizeof(buff),'*');
  116.     seek (tfile,filesize(tfile));
  117.     for cnt:=filesize(tfile) to sector do write (tfile,buff);
  118.     fillchar (buff,sizeof(buff),'!')
  119.   end;
  120.  
  121.   procedure writesector (sector:integer; var q:buffer);
  122.   var n:integer;
  123.   begin
  124.     if (sector<0) or (sector>numsectors) then exit;
  125.     seek (mapfile,sector);
  126.     read (mapfile,n);
  127.     if n<>-2 then begin
  128.       error ('Overwrite error sector=%1!','',strr(sector));
  129.       exit
  130.     end;
  131.     ensuretfilesize (sector);
  132.     seek (tfile,sector);
  133.     write (tfile,q)
  134.   end;
  135.  
  136.   procedure flushbuf;
  137.   begin
  138.     writesector (sector,buff);
  139.     prev:=sector;
  140.     sector:=nextblank(prev,true);
  141.     bufptr:=1;
  142.   end;
  143.  
  144.   procedure outofroom;
  145.   begin
  146.     writestr ('Sorry, out of room!');
  147.     maketext:=-1
  148.   end;
  149.  
  150. begin
  151.   if q.numlines=0 then begin
  152.     writestr ('Message blank!');
  153.     maketext:=-1;
  154.     exit
  155.   end;
  156.   if firstfree>=0 then begin
  157.     sector:=firstfree;
  158.     seek (mapfile,sector);
  159.     read (mapfile,prev)
  160.   end else prev:=-1;
  161.   if prev<>-2 then begin
  162.     firstfree:=firstblank;
  163.     sector:=firstfree
  164.   end;
  165.   maketext:=sector;
  166.   if sector=-1 then begin
  167.     outofroom;
  168.     exit
  169.   end;
  170.   bufptr:=1;
  171.   for line:=1 to q.numlines do begin
  172.     curline:=q.text[line]+^M;
  173.     if line=q.numlines then curline:=curline+chr(0);
  174.     for pos:=1 to length(curline) do begin
  175.       k:=curline[pos];
  176.       buff[bufptr]:=k;
  177.       bufptr:=bufptr+1;
  178.       if bufptr>sectorsize then begin
  179.         flushbuf;
  180.         if sector=-1 then begin
  181.           outofroom;
  182.           exit
  183.         end
  184.       end
  185.     end
  186.   end;
  187.   if bufptr>1 then flushbuf;
  188.   setbam (prev,-1);
  189.   firstfree:=nextblank(firstfree,false);
  190.   if firstfree=-1 then firstfree:=firstblank
  191. end;
  192.  
  193. const q:byte=0;  { Separate overlay modules }
  194.  
  195. overlay function copytext (sector:integer):integer;
  196. var me:message;
  197. begin
  198.   reloadtext (sector,me);
  199.   copytext:=maketext (me)
  200. end;
  201.  
  202. overlay procedure printtext (sector:integer);
  203. var q:message;
  204.     x,b:boolean;
  205.     n:integer;
  206. begin
  207.   reloadtext (sector,q);
  208.   writeln (^B);
  209.   n:=1;
  210.   repeat
  211.     writeln (q.text[n]);
  212.     n:=n+1
  213.   until break or (n>q.numlines) or hungupon;
  214.   x:=xpressed; b:=break;
  215.   writeln (^B^M);
  216.   xpressed:=x; break:=b
  217. end;
  218.