home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / t / tcsel003.zip / READTEXT.PAS < prev    next >
Pascal/Delphi Source File  |  1992-10-16  |  7KB  |  273 lines

  1. {$A+,B-,D-,E+,F-,I+,L-,N-,O-,R+,S-,V-}
  2. {$M 4048,65536,655360}
  3.  
  4. program Readtext;
  5.  
  6. { Author Trevor J Carlsen - released into the public domain 1991         }
  7. {        PO Box 568                                                      }
  8. {        Port Hedland                                                    }
  9. {        Western Australia 6721                                          }
  10. {        Voice +61 91 73 2026  Data +61 91 73 2569                       }
  11. {        FidoNet 3:690/644                                               }
  12.  
  13. { This example programs displays a text file using simple word wrap. The }
  14. { cursor keys are used to page forward or backwards by page or by line.  }
  15. { The program makes some important assumptions.  The main one is that no }
  16. { line in the file will ever exceed 255 characters in length.  To get    }
  17. { around this restriction the ReadTxtLine function would need to be      }
  18. { rewritten.                                                             }
  19.  
  20. { The other major restriction is that files exceeding a size able to be  }
  21. { totally placed in RAM cannot be viewed.                                }
  22.  
  23. {$DEFINE TurboPower (Remove the period if you have Turbo Power's TPro)  }
  24.  
  25. uses
  26.   {$IFDEF TurboPower }
  27.   tpcrt,
  28.   colordef;
  29.   {$ELSE}
  30.   crt;
  31.   {$ENDIF}
  32.  
  33. const
  34.   {$IFNDEF TurboPower }
  35.   BlackOnLtGray = $70;      LtGrayOnBlue = $17;
  36.   {$ENDIF}
  37.   LineLength    = 79;       MaxLines     = 6000;
  38.   ScreenLines   = 22;       escape       = $011b;
  39.   Home          = $4700;    _end         = $4f00;
  40.   upArrow       = $4800;    downArrow    = $5000;
  41.   PageUp        = $4900;    PageDown     = $5100;
  42.  
  43. type
  44.   LineStr    = string[Linelength];
  45.   StrPtr     = ^LineStr;
  46.  
  47. var
  48.   TxtFile    : text;
  49.   Lines      : array[1..MaxLines] of StrPtr;
  50.   NumberLines: 1..MaxLines+1;
  51.   CurrentLine: 1..MaxLines+1-ScreenLines;
  52.   st         : string;
  53.   finished   : boolean;
  54.   OldExitProc: pointer;
  55.   TxtBuffer  : array[0..16383] of byte;
  56.   OldAttr    : byte;
  57.  
  58. function LastPos(ch : char; S : string): byte;
  59.   { Returns the last position of ch in S or zero if ch not in S }
  60.   var
  61.     x   : word;
  62.     len : byte absolute S;
  63.   begin
  64.     x := succ(len);
  65.     repeat
  66.       dec(x);
  67.     until (x = 0) or (S[x] = ch);
  68.     LastPos := x;
  69.   end;  { LastPos }
  70.  
  71. function Wrap(var S,CarryOver: string): string;
  72.   { Returns a string of maximum length Linelength from S. Any additional }
  73.   { characters remaining are placed into CarryOver.                      }
  74.   const
  75.     space = #32;
  76.   var
  77.     temp      : string;
  78.     LastSpace : byte;
  79.     len       : byte absolute S;
  80.   begin
  81.     FillChar(temp,sizeof(temp),32);
  82.     temp := S; CarryOver := ''; wrap := temp;
  83.     if length(temp) > LineLength then begin
  84.       LastSpace := LastPos(space,copy(temp,1,LineLength+1));
  85.       if LastSpace <> 0 then begin
  86.         Wrap[0]   := chr(LastSpace - 1);
  87.         CarryOver := copy(temp,LastSpace + 1, 255)
  88.       end  { if LastSpace... }
  89.       else begin
  90.         Wrap[0]   := chr(len);
  91.         CarryOver := copy(temp,len,255);
  92.       end; { else }
  93.     end; { if (length(S))...}
  94.   end;  { Wrap }
  95.  
  96. function ReadTxtLine(var f: text; L: byte): string;
  97.   var
  98.     temp : string;
  99.     len  : byte absolute temp;
  100.     done : boolean;
  101.   begin
  102.     len := 0; done := false;
  103.     {$I-}
  104.     while not eoln(f) do begin
  105.       read(f,temp);
  106.       if IOResult <> 0 then begin
  107.         writeln('Error reading file - aborted');
  108.         halt;
  109.       end;
  110.     end; { while }
  111.     if eoln(f) then readln(f);
  112.     ReadTxtLine := st + Wrap(temp,st);
  113.     finished := eof(f);
  114.   end;  { ReadTxtLine }
  115.  
  116. procedure ReadTxtFile(var f: text);
  117.   var
  118.     x : word;
  119.   begin
  120.     st          := '';
  121.     NumberLines := 1;
  122.     repeat
  123.       if NumberLines > MaxLines then begin
  124.         writeln('File too big');
  125.         halt;
  126.       end;
  127.       if (MaxAvail >= Sizeof(LineStr)) then
  128.         new(Lines[NumberLines])
  129.       else begin
  130.         writeln('Insufficient memory');
  131.         halt;
  132.       end;
  133.       FillChar(Lines[NumberLines]^,LineLength+1,32);
  134.       if length(st) > LineLength then
  135.         Lines[NumberLines]^  := wrap(st,st)
  136.       else if length(st) <> 0 then begin
  137.         Lines[NumberLines]^  := st;
  138.         st := '';
  139.       end else
  140.         Lines[NumberLines]^  := ReadTxtLine(f,LineLength+1);
  141.       Lines[NumberLines]^[0] := chr(LineLength);
  142.       if not finished then
  143.         inc(NumberLines);
  144.     until finished;
  145.   end;  { ReadTxtFile }
  146.  
  147. procedure DisplayScreen(line: word);
  148.   var
  149.     x : byte;
  150.   begin
  151.     GotoXY(1,1);
  152.     for x := 1 to ScreenLines - 1 do
  153.       writeln(Lines[x-1+line]^);
  154.     write(Lines[x+line]^)
  155.   end;
  156.  
  157. procedure PreviousPage;
  158.   begin
  159.     if CurrentLine > ScreenLines then
  160.       dec(CurrentLine,ScreenLines-1)
  161.     else
  162.       CurrentLine := 1;
  163.   end;  { PreviousPage }
  164.  
  165. procedure NextPage;
  166.   begin
  167.     if CurrentLine < (succ(NumberLines) - ScreenLines * 2) then
  168.       inc(CurrentLine,ScreenLines-1)
  169.     else
  170.       CurrentLine := succ(NumberLines) - ScreenLines;
  171.   end;   { NextPage }
  172.  
  173. procedure PreviousLine;
  174.   begin
  175.     if CurrentLine > 1 then
  176.       dec(CurrentLine)
  177.     else
  178.       CurrentLine := 1;
  179.   end;  { PreviousLine }
  180.  
  181. procedure NextLine;
  182.   begin
  183.     if CurrentLine < (succ(NumberLines) - ScreenLines) then
  184.       inc(CurrentLine)
  185.     else
  186.       CurrentLine := succ(NumberLines) - ScreenLines;
  187.   end; { NextLine }
  188.  
  189. procedure StartOfFile;
  190.   begin
  191.     CurrentLine := 1;
  192.   end; { StartOfFile }
  193.  
  194. procedure EndOfFile;
  195.   begin
  196.     CurrentLine := succ(NumberLines) - ScreenLines;
  197.   end;  { EndOfFile }
  198.  
  199. procedure DisplayFile;
  200.  
  201.   function KeyWord : word; assembler;
  202.     asm
  203.       mov  ah,0
  204.       int  16h
  205.     end;
  206.  
  207.   begin
  208.     DisplayScreen(CurrentLine);
  209.     repeat
  210.       case KeyWord of
  211.         PageUp    : PreviousPage;
  212.         PageDown  : NextPage;
  213.         UpArrow   : PreviousLine;
  214.         DownArrow : NextLine;
  215.         Home      : StartOfFile;
  216.         _End      : EndOfFile;
  217.         Escape    : halt;
  218.       end; { case }
  219.       DisplayScreen(CurrentLine);
  220.     until false;
  221.   end; { DisplayFile }
  222.  
  223. procedure NewExitProc;far;
  224.   begin
  225.     ExitProc := OldExitProc;
  226.     {$IFDEF TurboPower}
  227.     NormalCursor;
  228.     {$ENDIF}
  229.     window(1,1,80,25);
  230.     TextAttr := OldAttr;
  231.     Clrscr;
  232.   end;
  233.  
  234. procedure Initialise;
  235.   begin
  236.     CurrentLine := 1;
  237.     if ParamCount <> 1 then begin
  238.       writeln('No file name parameter');
  239.       halt;
  240.     end;
  241.     OldAttr := TextAttr;
  242.     assign(TxtFile,Paramstr(1));
  243.     {$I-}  reset(TxtFile);
  244.     if IOResult <> 0 then begin
  245.       writeln('Unable to open ',Paramstr(1));
  246.       halt;
  247.     end;
  248.     SetTextBuf(TxtFile,TxtBuffer);
  249.     Window(1,23,80,25);
  250.     TextAttr := BlackOnCyan;
  251.     clrscr;
  252.     writeln('              Next Page = [PageDown]     Previous Page = [PageUp]');
  253.     writeln('              Next Line = [DownArrow]    Previous Line = [UpArrow]');
  254.     write('         Start of File = [Home]   End of File = [End]   Quit = [Escape]');
  255.     Window(1,1,80,22);
  256.     TextAttr := LtGrayOnBlue;
  257.     clrscr;
  258.     {$IFDEF TurboPower}
  259.     HiddenCursor;
  260.     {$ENDIF}
  261.     OldExitProc := ExitProc;
  262.     ExitProc    := @NewExitProc;
  263.   end;
  264.  
  265. begin
  266.   Initialise;
  267.   ReadTxtFile(TxtFile);
  268.   DisplayFile;
  269. end.
  270.  
  271.  
  272.  
  273.