home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / BBS / CEL141.ZIP / CEL141C.ZIP / TOOLKIT.ZIP / TEXTTOOL.PAS < prev    next >
Pascal/Delphi Source File  |  1992-08-14  |  8KB  |  379 lines

  1. procedure reloadtext (sector:integer; var q:message);
  2. function maketext (var q:message):integer;
  3. procedure trimmessage (var m:message);
  4. procedure TextToFile(m:message;filename:lstr);
  5.  
  6. procedure setbam (sector,val:integer);
  7.   begin
  8.     seek (mapfile,sector);
  9.     write (mapfile,val)
  10.   end;
  11.  
  12.  
  13. procedure reloadtext (sector:integer; var q:message);
  14. var k:char;
  15.     sectorptr,tmp,n:integer;
  16.     buff:buffer;
  17.     x:boolean;
  18.  
  19.   procedure chk;
  20.   begin
  21.     iocode:=ioresult;
  22.     if iocode<>0 then writeln (usr,'(Error ',iocode,' reading message)')
  23.   end;
  24.  
  25. begin  {reloadtext}
  26.   sectorptr:=32767;
  27.   n:=1;
  28.   q.text[1]:='';
  29.   repeat
  30.     if sectorptr>sectorsize then begin
  31.       if sector<0 then exit;
  32.       if (sector>filesize(tfile)) or (sector>filesize(mapfile)) then begin
  33.         writeln('**Seek past end of EOF**');
  34.         exit;
  35.       end;
  36.       seek (tfile,sector); chk;
  37.       read (tfile,buff); chk;
  38.       seek (mapfile,sector); chk;
  39.       read (mapfile,tmp); chk;
  40.       if tmp=-2 then begin
  41.         tmp:=-1;
  42.         seek (mapfile,sector); chk;
  43.         write (mapfile,tmp); chk;
  44.       end;
  45.       sector:=tmp;
  46.       sectorptr:=1
  47.     end;
  48.     k:=buff[sectorptr];
  49.     case k of
  50.       #0,#10:;
  51.       #13:if n>=maxmessagesize
  52.             then k:=#0
  53.             else begin
  54.               n:=n+1;
  55.               q.text[n]:=''
  56.             end
  57.       else q.text[n]:=q.text[n]+k
  58.     end;
  59.     sectorptr:=sectorptr+1
  60.   until k=#0;
  61.   q.numlines:=n;
  62.   chk;
  63. end;
  64.  
  65. procedure deletetext (sector:integer);
  66. var next:integer;
  67. begin
  68.   while sector>=0 do begin
  69.     seek (mapfile,sector);
  70.     read (mapfile,next);
  71.     setbam (sector,-2);
  72.     sector:=next
  73.   end
  74. end;
  75.  
  76. function maketext (var q:message):integer;
  77. var line,pos,sector,prev:integer;
  78.     bufptr:integer;
  79.     curline:anystr;
  80.     k:char;
  81.     buff:buffer;
  82.  
  83.  
  84.   function nextblank (first:integer; linkit:boolean):integer;
  85.   var cnt,i,blank:integer;
  86.   begin
  87.     nextblank:=-1;
  88.     if first<-1 then first:=-1;
  89.     if first>=numsectors then exit;
  90.     seek (mapfile,first+1);
  91.     for cnt:=first+1 to numsectors do begin
  92.       read (mapfile,i);
  93.       if i=-2 then begin
  94.         blank:=cnt;
  95.         if (first>=0) and linkit then setbam (first,blank);
  96.         nextblank:=blank;
  97.         exit
  98.       end
  99.     end
  100.   end;
  101.  
  102.   function firstblank:integer;
  103.   begin
  104.     firstblank:=nextblank (-1,false)
  105.   end;
  106.  
  107.   procedure ensuretfilesize (sector:integer);
  108.   var cnt:integer;
  109.       buff:buffer;
  110.   begin
  111.     if sector<filesize(tfile) then exit;
  112.     if (sector<0) or (sector>numsectors) then exit;
  113.     fillchar (buff,sizeof(buff),'┼');
  114.     seek (tfile,filesize(tfile));
  115.     for cnt:=filesize(tfile) to sector do write (tfile,buff);
  116.     fillchar (buff,sizeof(buff),'═')
  117.   end;
  118.  
  119.   procedure writesector (sector:integer; var q:buffer);
  120.   var n:integer;
  121.   begin
  122.     if (sector<0) or (sector>numsectors) then exit;
  123.     seek (mapfile,sector);
  124.     read (mapfile,n);
  125.     if n<>-2 then begin
  126.       error ('Overwrite error sector=%1!','',strr(sector));
  127.       exit
  128.     end;
  129.     ensuretfilesize (sector);
  130.     seek (tfile,sector);
  131.     write (tfile,q)
  132.   end;
  133.  
  134.   procedure flushbuf;
  135.   begin
  136.     writesector (sector,buff);
  137.     prev:=sector;
  138.     sector:=nextblank(prev,true);
  139.     bufptr:=1;
  140.   end;
  141.  
  142.   procedure outofroom;
  143.   begin
  144.     writeln (^B'Sorry, out of room!');
  145.     maketext:=-1
  146.   end;
  147.  
  148. begin    {Maketext}
  149.   if q.numlines=0 then begin
  150.     writeln (^B'No Data to Save');
  151.     maketext:=-1;
  152.     exit
  153.   end;
  154.   prev:=-1;
  155.   if prev<>-2 then begin
  156.     firstfree:=firstblank;
  157.     sector:=firstfree
  158.   end;
  159.   maketext:=sector;
  160.   if sector=-1 then begin
  161.     outofroom;
  162.     exit
  163.   end;
  164.   bufptr:=1;
  165.   for line:=1 to q.numlines do begin
  166.     curline:=q.text[line]+^M;
  167.     if line=q.numlines then curline:=curline+chr(0);
  168.     for pos:=1 to length(curline) do begin
  169.       k:=curline[pos];
  170.       buff[bufptr]:=k;
  171.       bufptr:=bufptr+1;
  172.       if bufptr>sectorsize then begin
  173.         flushbuf;
  174.         if sector=-1 then begin
  175.           outofroom;
  176.           exit
  177.         end
  178.       end
  179.     end
  180.   end;
  181.   if bufptr>1 then flushbuf;
  182.   setbam (prev,-1);
  183.   firstfree:=nextblank(firstfree,false);
  184.   if firstfree=-1 then firstfree:=firstblank;
  185.   if isauthor then writeln(usr,'[Writing at sector ',sector,']');
  186. end;
  187.  
  188. function copytext (sector:integer):integer;
  189. var me:message;
  190. begin
  191.   reloadtext (sector,me);
  192.   copytext:=maketext (me)
  193. end;
  194.  
  195. procedure trimmessage (var m:message);
  196. var cnt,cnt2:integer;
  197.     s: lstr;
  198. begin
  199.   s:='';
  200.   for cnt:=1 to 80 do s:=s+'╬';
  201.   for cnt:=1 to m.numlines do
  202.     while m.text[cnt][length(m.text[cnt])]=' ' do
  203.       m.text[cnt][0]:=pred(m.text[cnt][0]);
  204.   while (m.numlines>0) and (m.text[m.numlines]='') do
  205.     m.numlines:=m.numlines-1;
  206.   for cnt:=m.numlines+2 to maxmessagesize do
  207.     m.text[cnt]:=s;
  208. end;
  209.  
  210.  
  211.  
  212. function charhit:boolean;
  213. var k:char;
  214. begin
  215.   if modeminlock then while numchars>0 do k:=GrabChar;
  216.   if hungupon or keyhit
  217.     then charhit:=true
  218.     else if online
  219.       then charhit:=(not modeminlock) and (numchars>0)
  220.       else charhit:=false
  221. end;
  222.  
  223.  
  224.   procedure WVT52(t:anystr);
  225.   var cnt:integer;
  226.   begin
  227.   if modemoutlock then exit;
  228.    if t[2]=#234 then delete (t,1,1);
  229.    for cnt:=1 to length(t) do sendchar (t[cnt]);
  230.   end;
  231.  
  232.  
  233. function charready:boolean;
  234. var k:char;
  235. begin
  236.   if modeminlock then while numchars>0 do k:=Grabchar;
  237.   if hungupon or keyhit
  238.     then charready:=true
  239.     else if online
  240.       then charready:=(not modeminlock) and (numchars>0)
  241.         else charready:=false;
  242.   while numchars>0 do k:=Grabchar;
  243. end;
  244.  
  245.  
  246. procedure waitforchar;
  247. var t:integer;
  248.     k:char;
  249. begin
  250.   t:=timer+setup.mintimeout;
  251.   if t>=1440 then t:=t-1440;
  252.   repeat
  253.     if timer>=t then forcehangup:=true
  254.   until (charready or hungupon);
  255.   chainstr:='';
  256.   while numchars>0 do k:=GrabChar;
  257. end;
  258.  
  259.  
  260. procedure PrintLine(line:anystr);
  261. var    i,cnt:byte;
  262.         color:sstr;
  263. begin
  264.   i:=0;
  265.   while i<(length(line)) do begin
  266.         inc(i);
  267.         if line[i]='|' then begin
  268.             color:=line[i+1]+line[i+2];
  269.             ansicolor(valu(color));
  270.             i:=i+2;
  271.             end
  272.         else
  273.             write(line[i]);
  274.   end;
  275. {  if length(line)<80 then }write(#13);
  276. end;
  277.  
  278.  
  279.   function QuoteLine(s:string):boolean;
  280.   var  Posit:integer;
  281.   begin
  282.     QuoteLine:=false;
  283.     Posit:=pos('>',s);
  284.     if (Posit>0) and (Posit<5) then
  285.         QuoteLine:=true;
  286.     if copy (s,1,3)='---' then
  287.       QuoteLine:=true;
  288.     end;
  289.  
  290. procedure ShowMessage(q:message);
  291. var n,m,b,mm,DelayTime:integer;
  292.      p:byte;
  293.      a:sstr;
  294.      s:char;
  295.    quoting:boolean;
  296.  
  297. begin
  298.   LastMessage:=q;
  299.   n:=1;
  300.   Quoting:=false;
  301.   repeat
  302.         if not rawmode then begin
  303.             mm:=0;
  304.       if quoting and not (QuoteLine(q.text[n])) then begin
  305.          quoting:=false;
  306.          ansicolor(urec.sixthcolor);
  307.          end;
  308.  
  309.             Quoting:=QuoteLine(q.text[n]);
  310.  
  311.       if quoting then
  312.                 ansicolor(urec.seventhcolor);
  313.  
  314.             repeat
  315.                 if length(q.text[n])>0 then begin
  316.                     mm:=mm+1;
  317.                     s:=q.text[n,mm];
  318.           write (s);
  319.                   end;  {if q.text[n]>0}
  320.              until mm>=length(q.text[n]);
  321.         end
  322.         else write(q.text[n]);
  323.         writeln;
  324.         inc(n);
  325.   until break or (n>q.numlines) or hungupon;
  326.   ansicolor (urec.regularcolor)
  327. end;
  328.  
  329.  
  330.  
  331. procedure printtext (sector:integer);
  332. var    q:message;
  333. begin
  334.   reloadtext (sector,q);
  335.   ShowMessage(q);
  336. end;
  337.  
  338. procedure TextToFile(m:message;filename:lstr);
  339. var f:text;
  340.     cnt:word;
  341. begin
  342.   {$I-}
  343.   assign(f,filename);
  344.   rewrite(f);
  345.   for cnt:=1 to m.numlines do
  346.     writeln(f,m.text[cnt]);
  347.   textclose(f);
  348.   close(f);
  349.   end;
  350.  
  351.  
  352. begin
  353. end.
  354.  
  355.  
  356.  
  357. (*
  358.    This unit contains the asinine forum-style text storage and retreival
  359.    system.  It is not efficient, it is not simple, and it is not easy to
  360.    use.
  361.  
  362.    Reloadtext will load text from "sector" (the address of the text) into
  363.    a MESSAGE variable.
  364.  
  365.    Maketext will store text to a data file from a message variable, and
  366.    return the address.
  367.  
  368.    CopyText will copy text from one source to another, and return the
  369.    address of the copy.
  370.  
  371.    TrimMessage sets all unused bytes in a message variable to one character,
  372.    as to improve compression on zip and v.42bis.
  373.  
  374.    TexttoFile converts a message variable to a text file.
  375.  
  376.    PrintMessage displays a text file.
  377.  
  378. *)
  379.