home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / pascal / swag / textfile.swg / 0008_READTEXT.PAS.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1993-05-28  |  7.2 KB  |  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.